
#include<iostream>
#include<math.h>
#include<algorithm>
#include<stdint.h>
#include<vector>
#include<deque>
#include<stack>
#include<functional>
#include<string>
#include<cstring>
#include<time.h>
#include<array>
#include<iomanip>
#include<list>
#include<set>
#include<map>
#include<random>
#include<unordered_map>
#include<unordered_set>
#include<bitset>
#include <queue>


using namespace std;
using ll = long long;
using ull = unsigned long long;
using uint = unsigned int;
using ldb = long double;


//BDD,ZDD,kdtree, bdtree,bicalc, bolonoy, doloney, tree, chinesemod,segmenttree,daikusutora, saidairyuu, 2bugurahu, heirokenshutu, topologicalsort, kyourenketuseibun

#define REP(i,a,b) for(ll i = a; i < b; ++i)
#define PRI(s) cout << s << endl
#define PRIF(v, n) printf("%."#n"f\n", v)
#define PRIY PRI("Yes")
#define PRIN PRI("No")
#define MINS(a,b) a = min(a,(decltype(a))(b))
#define MAXS(a,b) a = max(a,(decltype(a))(b))


using arr = array<ll, 3>;
int main() {
	auto proc = [](string str) {
		int bra = 0;
		int cket = 0;
		for (char c : str) {
			if (c == '(') {
				++bra;
			}
			else {
				if (bra > 0) --bra;
				else ++cket;
			}
		}
		return arr{cket, bra, bra - cket};
	};
	auto cmp = [](arr a, arr b) {
		if (a[0] == b[0])return a[1] > b[1];
		return a[0] < b[0];
	};
	auto cmp2 = [](arr a, arr b) {
		if (a[0] == b[0])return a[1] > b[1];
		return a[0] > b[0];
	};
	ll N;
	cin >> N;
	vector<arr> plus,minus;
	REP(i, 0, N) {
		string str;
		cin >> str;
		auto a = proc(str);
		if (a[2] >= 0) plus.push_back(a);
		else minus.push_back(a);
	}
	sort(plus.begin(), plus.end(), cmp);
	if (plus.size() == 0 || plus[0][0] > 0) {
		PRIN;
		return 0;
	}
	sort(minus.begin(), minus.end(), cmp2);
	int last = -1;
	REP(i, 0, minus.size()) {
		if (minus[i][1] == 0) {
			last = i;
			break;
		}
	}
	if (last == -1) {
		if (minus.size() != 0)PRIN;
		else {
			for (auto p : plus) {
				if (p[0] != 0 || p[1] != 0) {
					PRIN;
					return 0;
				}
			}
			PRIY;
		}
		return 0;
	}
	int cur = 0;
	for (auto p : plus) {
		cur -= p[0];
		if (cur < 0) {
			PRIN;
			return 0;
		}
		cur += p[1];
	}
	REP(i, 0, minus.size()) {
		if (i == last)continue;
		auto p = minus[i];
		cur -= p[0];
		if (cur < 0) {
			PRIN;
			return 0;
		}
		cur += p[1];
	}
	cur -= minus[last][0];
	if (cur == 0)PRIY;
	else PRIN;
	return 0;
}
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <iomanip>
using ll = long long;
using uint = unsigned int;
using graph = std::vector<std::vector<ll>>;
using wGraph = std::vector<std::vector<std::pair<ll,ll>>>;
#define rep(i,n) for (int i=0; i < int(n); i++)

using namespace std;
ll MOD7 = 1000000007;
ll MOD9 = 1000000009;
vector<ll> InputVec(ll N){
    vector<ll> A(N);
    for (int i = 0; i < N; ++i) {
        cin >> A[i];
    }
    return A;
}
void OutputVec(vector<ll> A){
    for (int i = 0; i < A.size(); ++i) {
        cout << A[i] << ",";
    }
    cout << endl;
}
vector<vector<ll>> InputVec2d(ll H,ll W){
    vector<vector<ll>> A(H);
    for (int yi = 0; yi < H; ++yi) {
        A[yi] = vector<ll>(W);
    }
    for (int yi = 0; yi < H; ++yi) {
        for (int xi = 0; xi < W; ++xi) {
            cin >> A[yi][xi];
        }
    }
    return A;
}
vector<vector<char>> InputVec2dStr(ll H,ll W){
    vector<vector<char>> A(H);
    for (int yi = 0; yi < H; ++yi) {
        A[yi] = vector<char>(W);
    }
    for (int yi = 0; yi < H; ++yi) {
        string S;
        cin >> S;
        for (int xi = 0; xi < W; ++xi) {
            A[yi][xi] = S[xi];
        }
    }
    return A;
}
void OutputVec2d(vector<vector<ll>> A){
    for (int yi = 0; yi < A.size(); ++yi) {
        for (int xi = 0; xi < A[yi].size(); ++xi) {
            cout << A[yi][xi] << ",";
        }
        cout << endl;
    }
}
graph InputGraph(ll N,ll M){
    graph G(N);
    for (int i = 0; i < M; ++i) {
        ll a,b;
        cin >> a >> b;
        a--;b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    return G;
}
graph InputDGraph(ll N,ll M){
    graph G(N);
    for (int i = 0; i < M; ++i) {
        ll a,b;
        cin >> a >> b;
        a--;b--;
        G[a].push_back(b);
    }
    return G;
}
wGraph InputWGraph(ll N,ll M){
    wGraph G(N);
    for (int i = 0; i < M; ++i) {
        ll a,b,c;
        cin >> a >> b >> c;
        a--;b--;
        G[a].push_back(make_pair(b,c));
        G[b].push_back(make_pair(a,c));
    }
    return G;
}
wGraph InputDWGraph(ll N, ll M){
    wGraph G(N);
    for (int i = 0; i < M; ++i) {
        ll a,b,c;
        cin >> a >> b >> c;
        a--;b--;
        G[a].push_back(make_pair(b,c));
    }
    return G;
}
void OutputGraph(graph G){
    for (int i = 0; i < G.size(); ++i) {
        cout << i << ":";
        for (int j = 0; j < G[i].size(); ++j) {
            cout << G[i][j]<< ",";
        }
        cout << endl;
    }
}

void OutputGraph(wGraph G){
    for (int i = 0; i < G.size(); ++i) {
        cout << i << ":";
        for (int j = 0; j < G[i].size(); ++j) {
            cout << G[i][j].first <<"/" << G[i][j].second << ",";
        }
        cout << endl;
    }
}

void OutputAnswerBool(bool b,bool isUpperCase = false){
    if(b){
        if(isUpperCase){
            cout << "YES" << endl;
        }else{
            cout << "Yes" << endl;
        }
    }else{
        if(isUpperCase){
            cout << "NO" << endl;
        }else{
            cout << "No" << endl;
        }
    }
}

int main() {
    string N;
    cin >> N;
    ll digit = N.size();
    vector<vector<ll>>memo(2);
    for (int yi = 0; yi < 2; ++yi) {
        memo[yi] = vector<ll>(digit);
        for (int xi = 0; xi < digit; ++xi) {
            memo[yi][xi] = (1ll << 50);
        }
    }
    ll first = N[0] - '0';
    memo[0][0] = min(11 - first,first);
    memo[1][0] = min(11 - first - 1,first + 1);

    for (int i = 1; i < digit; ++i) {
        ll num = N[i] - '0';
        memo[0][i] = min(memo[0][i-1] + num,memo[1][i-1] + (10 - num));
        memo[1][i] = min(memo[0][i-1] + num + 1,memo[1][i-1] + (10 - num - 1));
    }
    cout << (memo[0][digit-1]) << endl;
}
