#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int N = 2 * 1e5;

ll bpow(ll a, ll b) {
	long long res = 1;
	while (b > 0) {
		if (b & 1)
			res = res * a;
		a = a * a;
		b >>= 1;
	}
	return res;
}

int n;
map<int, int> compressed, cnt;
bitset<N> isHalf, halfCon;
set<int> halves;

vi conn[N];
int depth[N];
int ans = 0;

void count(int v, int m) {
	if (isHalf[v] && m) return;
	if (halfCon[v] && m) return;
	if (!cnt[v]) return;

	for (int u : conn[v]) {
		if (isHalf[u] || !cnt[u]) continue;
		if (halfCon[u] && m) return;
		int lim = min(cnt[u], cnt[v]);
		cnt[u] -= lim, cnt[v] -= lim;
		ans += lim;
	}

	if (isHalf[v]) {
		ans += cnt[v] / 2;
		cnt[v] %= 2;
	}
}

struct Comp
{
    // Compare 2 Player objects using name
    bool operator ()(const int& a, const int& b)
    {
		if (halfCon[a]) return false;
		if (halfCon[b]) return true;
        return depth[a] <= depth[b];
    }
};

int main() {
	cin.tie(0)->sync_with_stdio(0);
	cin >> n;
	vi a(n);
	rep(i, 0, n) {
		cin >> a[i];
		compressed[a[i]];
	}

	int pos = 0;
	for (auto& x : compressed) {
		if (!(bpow(2, ceil(log2(x.first))) - x.first)) {
			isHalf[pos] = 1;
			halves.insert(pos);
		}
		x.second = pos++;
	}

	rep(i, 0, n) {
		a[i] = compressed[a[i]];
		++cnt[a[i]];
	}

	for (pii i : compressed) {
		ll nxt = bpow(2, ceil(log2(i.first))) - i.first;
		if (nxt != i.first && compressed.count(nxt)) {
			conn[i.second].push_back(compressed[nxt]);
			conn[compressed[nxt]].push_back(i.second);
		}
	}

	queue<int> q, rq;
	vector<bool> vis(pos, false);

	rep(i, 0, pos) {
		if (sz(conn[i]) == 1) {
			q.push(i);
			vis[i] = true;
		}
	}

	while (!q.empty()) {
		int v = q.front();
		rq.push(v);
		q.pop();
		for (auto u : conn[v]) {
			if (!vis[u]) {
				q.push(u);
				depth[u] = depth[v] + 1;
				vis[u] = true;
			}
		}
	}

	halfCon.reset();
	set<int> semiHalves;
	for (int i : halves) {
		for (int u : conn[i]) {
			halfCon[u] = true;
			semiHalves.insert(u);
		}
	}

	rep(i, 0, n) {
		sort(all(conn[i]), Comp());
	}

	while (!rq.empty()) {
		int v = rq.front();
		rq.pop();
		count(v, 1);
	}

	for (int i : semiHalves) {
		count(i, 0);
	}

	for (int i : halves) {
		count(i, 0);
	}
	cout << ans;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0, i##_len = (n); i < i##_len; ++i)
#define rep2(i, x, n) for(int i = x, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
using vd = vector<double>;
vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1};

int main() {
  int n;
  cin >> n;
  multiset<ll> st;
  rep(i, n) {
    ll a;
    cin >> a;
    st.insert(a);
  }
  ll ans = 0;
  ll p2 = pow(2, 34);
  while(!st.empty()) {
    auto it = st.end();
    it--;
    ll p = p2;
    while(p > *it) {
      p /= 2;
      auto jt = st.lower_bound(p - *it);
      if(jt == it || jt == st.end() || *jt != p - *it) continue;
      st.erase(jt);
      ans++;
      break;
    }
    st.erase(it);
  }
  cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;

int main() {
  int R, G;
  cin >> R >> G;
  if(0 <= R){
    if(4500 >= G){
      cout << (G - R) * 2 + R << endl;
    }else{
    cout << "Please input number (goal rating) under 4500." << endl;
    }
  }else{
  cout << "Please input number (rating) over 0." << endl;
  }
 }

#include<bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++)

int main(){
 int R,G; cin>>R>>G;
 cout<<(G+G-R)<<endl;

 return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
int main(){
      int tmp;
      int dp[100];
      cin>> tmp;
      do{
            fill(dp,dp+100,0);
            dp[0] = 1;
            for (int i = 0; i<tmp;i++)
            {
                  for(int j = 1;j < 4;j++)
                        dp[i+j] += dp[i];
            }
            cout << dp[tmp]/3650 + 1<<endl;
            cin >> tmp;
      }while(tmp);

}

#include <bits/stdc++.h>
#define int long long
#define double long double
#define INF 1e18
using namespace std;
signed main() {
  int N;
  while (cin>>N, N!=0) {
    if (N < 3) {
      cout << 1 << endl;
      continue;
    }

    int dp[N];
    dp[0] = 1;
    dp[1] = dp[0]+1;
    dp[2] = dp[1]+dp[0]+1;
    for (int i = 3; i < N; i++) {
      dp[i] = dp[i-3] + dp[i-2] + dp[i-1];
    }
    cout << ceil((dp[N-1]/10.0)/365.0) << endl;
  }
}


#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cassert>
#include <stack>
#include <limits>
#include <array>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <memory>
#include <numeric>
#include <tuple>

constexpr long long mod = static_cast<long long>(1e9 + 7);

void dfs1(int i, const std::vector<std::vector<int>>& edges, std::vector<std::pair<int, int>>& memo, int& cnt) {
    memo[i] = {i, 0};
    for(auto v: edges[i]) {
        if(memo[v].second < 0) dfs1(v, edges, memo, cnt);
    }
    memo[i].second = cnt++;
}

void dfs2(int i, const std::vector<std::vector<int>>& edges, std::vector<int>& memo, int cnt) {
    memo[i] = cnt;
    for(auto v: edges[i]) {
        if(memo[v] < 0) dfs2(v, edges, memo, cnt);
    }
}

int main() {
    int V, E;
    std::cin >> V >> E;
    std::vector<std::vector<int>> edges(V);
    std::vector<std::vector<int>> redges(V);
    while(E--) {
        int s, t;
        std::cin >> s >> t;
        edges[s].push_back(t);
        redges[t].push_back(s);
    }
    int cnt = 0;
    std::vector<std::pair<int, int>> memo(V, {-1, -1});
    for(int i = 0; i < V; ++i) {
        if(memo[i].second < 0) dfs1(i, edges, memo, cnt);
    }
    std::sort(begin(memo), end(memo), [](auto x, auto y){return x.second > y.second;});
    std::vector<int> ans(V, -1);
    cnt = 0;
    for(auto val: memo) {
        if(ans[val.first] < 0) {
            dfs2(val.first, redges, ans, cnt++);
        }
    }

    int Q;
    std::cin >> Q;
    while(Q--) {
        int u, v;
        std::cin >> u >> v;
        std::cout << (ans[u] == ans[v] ? 1 : 0) << "\n";
    }
    std::cout << std::flush;

    return 0;
}

#include <climits>
#include <iostream>
#include <stack>
#include <vector>
using namespace std;

void dfs(int u, vector<vector<int> >& G, int& t, vector<int>& ord, vector<int>& low, int& k, vector<int>& cmp, stack<int>& s) {
	ord[u] = low[u] = t++;
	s.push(u);
	for (int v: G[u]) {
		if (ord[v] == -1) {
			dfs(v, G, t, ord, low, k, cmp, s);
			low[u] = min(low[u], low[v]);
		}
		else if (cmp[v] == -1)
			low[u] = min(low[u], ord[v]);
	}
	if (ord[u] == low[u]) {
		for (;;) {
			int v = s.top(); s.pop();
			cmp[v] = k;
			if (v == u) break;
		}
		k++;
	}
}

vector<int> scc(vector<vector<int> >& G) {
	int N = G.size(), t = 0, k = 0;
	vector<int> ord(N, -1), low(N), cmp(N, -1);
	stack<int> s;
	for (int u = 0; u < N; u++)
		if (ord[u] == -1)
			dfs(u, G, t, ord, low, k, cmp, s);
	return cmp;
}

int main() {
	int N; cin >> N;
	vector<vector<int> > G(N);
	int M; cin >> M;
	while (M--) {
		int u, v; scanf("%d%d", &u, &v);
		G[u].push_back(v);
	}
	vector<int> cmp = scc(G);
	int Q; cin >> Q;
	while (Q--) {
		int u, v; scanf("%d%d", &u, &v);
		printf("%d\n", cmp[u] == cmp[v]);
	}
}


/*
  Motto - try hard to become an expert
*/
#include <bits/stdc++.h>
#include <map>
#include <algorithm>
#include <vector>
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <chrono>
#include <thread>
#include <iomanip>

//define
#define rep(i,n)    for(int i=0;i<n;i++)
#define repu(i,k,n) for(int i=k;i<=n;i++)
#define repd(i,k,n) for(int i=k;i>=n;i--)
#define se second
#define fi first
#define pb push_back
#define mp make_pair
#define INF (int)INT_MAX
#define all(v)  v.begin(), v.end()
#define endl "\n"

//typedef
typedef long long int ll;
typedef long double ld;
typedef std::pair<int, int> pii;
typedef std::vector<int> vi;
typedef std::vector< pii > vii;
typedef std::vector< ll > vll;
typedef std::vector< std::pair < pii, int > > vpp;

const int N = 3*1e5 +5 ;
const long long MOD = 1e9 + 7;
const long long inf = 1e18;

using namespace std;

ll modpow(ll x,ll n){
    ll res=1;
    while(n>0){
        if(n&1) res=res*x%MOD;
        x=x*x%MOD;
        n>>=1;
    }
    return res;
}

ll modFact(ll n, ll p)
{
    if (n >= p)
        return 0;

    ll result = 1;
    for (ll i = 1; i <= n; i++)
        result = (result * i) % p;

    return result;
}

bool isPrime(int x)
{
    if(x%2==0)
        return 0;
    for(int i=3;i<=sqrt(x);i+=2)
    {
        if(x%i==0)
            return 0;
    }
    return 1;
}

ll binomialCoeff(ll n, ll k)
{
    ll C[n + 1][k + 1];
    ll i, j;

    for (i = 0; i <= n; i++)
    {
        for (j = 0; j <= min(i, k); j++)
        {

            if (j == 0 || j == i)
                C[i][j] = 1;

            else
                C[i][j] = (C[i - 1][j - 1] +
                                        C[i - 1][j])%MOD;
        }
    }

    return C[n][k];
}
bool compare(const pair<int,int> &a , const pair<int,int>&b){
    //set<pair<int,int> , decltype(&compare)>s(&compare) ;
    if(a.fi!=b.fi)return a.se<b.se ;
    return a.fi<b.fi;
}

// struct compare{
//     // for priority_queue
//     bool operator()(const int &a , const int&b){
//         return a<b ;
//     }
// };

void solve()
{
    int n , m ;
    cin>>n>>m;
    vector<int>L(m) , R(m) ;
    rep(i , m){
        cin>>L[i]>>R[i] ;
    }
    int mal = INT_MIN ;
    int mir = INT_MAX ;
    rep(i , m){
        mal = max(L[i] , mal) ;
        mir = min(mir , R[i]) ;
    }
    int ans= 0 ;
    repu(i , 1 , n){
        if(mal<=i && mir>=i){
            ans++;
        }
    }
    cout<<ans<<endl;
}

int main() {
    #ifndef ONLINE_JUDGE
        // for getting input from input.txt
        freopen("input.txt", "r", stdin);
        // for writing output to output.txt
        freopen("output.txt", "w", stdout);
    #endif

    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    int t = 1;
    //cin>>t;
    while(t--)
    {
        solve();
    }
    cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
}

#include <iostream>
#include <queue>
#include <vector>

using namespace std;

typedef long long ll;
typedef vector<vector<char>> G;

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

long long bfs(int sx, int sy, vector<vector<bool>> &vis, G &g) {
  int h = g.size();
  int w = g[0].size();

  queue<pair<int, int>> que;
  que.push(make_pair(sx, sy));
  vis[sy][sx] = true;

  long long cnt_w = 0, cnt_b = 0;

  while (!que.empty()) {
    auto cur = que.front();
    que.pop();

    int cur_x = cur.first;
    int cur_y = cur.second;

    if (g[cur_y][cur_x] == '#') {
      cnt_b++;
    } else {
      cnt_w++;
    }

    for (int i = 0; i < 4; i++) {
      int nx = cur_x + dx[i];
      int ny = cur_y + dy[i];

      if (nx < 0 || ny < 0 || nx >= w || ny >= h) {
        continue;
      }
      if (vis[ny][nx]) {
        continue;
      }
      if (g[cur_y][cur_x] == g[ny][nx]) {
        continue;
      }
      vis[ny][nx] = true;
      que.push(make_pair(nx, ny));
    }
  }
  return cnt_b * cnt_w;
}

int main() {
  int h, w;
  cin >> h >> w;
  vector<vector<char>> grid(h, vector<char>(w));

  for (int y = 0; y < h; y++) {
    string tmp_s;
    cin >> tmp_s;
    for (int x = 0; x < w; x++) {
      grid[y][x] = tmp_s[x];
    }
  }

  long long ans = 0;
  vector<vector<bool>> is_visited(h, vector<bool>(w, false));
  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      if (!is_visited[y][x]) {
        ans += bfs(x, y, is_visited, grid);
      }
    }
  }
  cout << ans << "\n";

  return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n, k, dig = 0;

  cin >> n >> k;

  int d;
  for (int i = 0; i < k; i++) {
    cin >> d;
    dig |= 1 << d;
  }

  for (int i = n; ; i++) {
    string num = to_string(i);
    bool bad = false;
    for (auto c : num)
      if (dig & (1 << (c - '0'))) {
        bad = true;
        break;
      }

    if (!bad) {
      cout << num << endl;
      break;
    }
  }

  return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(ll i = 0;i<((ll)(n));i++)
#define reg(i,a,b) for(ll i = ((ll)(a));i<=((ll)(b));i++)
#define irep(i,n) for(ll i = ((ll)(n)-1);i>=0;i--)
#define ireg(i,a,b) for(ll i = ((ll)(b));i>=((ll)(a));i--)
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V< V<T> >;
template<class T = int> void print(vector<T> v){for(auto a:v)cout<<a<<" ";cout<<endl;}

/*
*/

ll n,m;

int main(void){
	cin>>n>>m;
	if(n%2==1){
		rep(i,m)cout<<i+1<<" "<<n-i<<endl;
	}else{
		rep(i,min(m,n/4))cout<<i+1<<" "<<n/2-i<<endl;
		m-=n/4;
		if(m>0){
			rep(i,m)cout<<i+n/2+2<<" "<<n-i<<endl;
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define irep(i, n) for (int i = (n); i >= 0; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1 << 25;
const int MOD = 1e9+7;
int x[20][20], y[20][20],a[20];
int main() {
	int n;
	cin >> n;
	rep(i,n) {
		cin >> a[i];
		rep(j,a[i]) {
			cin >> x[i][j] >> y[i][j];
			x[i][j]--;
		}
	}

	int ans = 0;
	rep(bit, (1<<n)) {
		bool ok = true;
		rep(i,n) {
			if (!((bit >> i) & 1)) continue;
			rep(j,a[i]) {
				if (((bit>>x[i][j]) & 1) ^ y[i][j]) ok = false;
			}
		}
		if (ok) ans = max(ans, __builtin_popcount(bit));
	}
	cout << ans << endl;
	return 0;
}

#include <cstdio>
#include <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <utility>
#include <algorithm>
#include <numeric>
#include <functional>

#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#define ALL(a) (a).begin(),(a).end()

using namespace std;
typedef long long ll;

int N, A[110], cnt;

int main(int argc, char *argv[])
{
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin>> N;
  REP(i,N) cin >> A[i];
  bool flag = true;
  while (flag) {
    flag = false;
    RFOR(j,1,N){
      if (A[j] < A[j-1]) {
        swap(A[j], A[j-1]);
        flag = true;
        ++cnt;
      }
    }
  }

  REP(i,N){
    cout << A[i];
    if (i != N-1) {
      cout << " ";
    } else {
      cout << "\n";
    }
  }
  cout << cnt << endl;

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i,m,n) for(int i=(int)(m); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define RREP(i,m,n) for(int i=(int)(m); i>=(int)(n); i--)
#define rrep(i,n) RREP(i,n-1,0)
#define all(v) v.begin(), v.end()
const int inf = 1e9+7;
const ll longinf = 1LL<<60;
const ll mod = 1e9+7;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, a, b;
  cin >> n;
  if(n == 1) cout << "Hello World" << "\n";
  else cin >> a >> b, cout << a + b << "\n";
  return 0;
}

#include <iostream>
using namespace std;

int main() {
	int N;
	cin >> N;
	long long ans = 0, before;
	cin >> before;
	bool HasNoAns = (before > 0);
	for (int i = 1; i < N; i++) {
		int now;
		cin >> now;
		if (now == 0) {
			before = now;
			continue;
		}
		if (now == before + 1) ans++;
		else {
			if (now <= before) ans += now;
			else HasNoAns = true;
		}
		before = now;
	}
	cout << (HasNoAns ? -1 : ans) << endl;
}


#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
using ll = long long;
using p = pair<int,int>;
using Graph = vector<vector<int>>;
int main(int argc, const char * argv[]) {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int a,b,c,d;
    cin>>a>>b>>c>>d;
    if(a+b==c+d)cout<<"Balanced"<<endl;
    else if(a+b>c+d)cout<<"Left"<<endl;
    else cout<<"Right"<<endl;

    return 0;
}

#include<bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef long long ll;

int main(){
    int h,w,a,b;
    cin >> h >> w >> a >> b;
    rep(i,h){
        bool rev_fl = i<b;
        rep(j,w){
            if(j<a){
                if(rev_fl) cout << 1;
                else cout << 0;
            }else {
                if(rev_fl) cout << 0;
                else cout << 1;
            }
        }
        cout << endl;
    }
    return 0;
}


#include<iostream>
#include<vector>
#include<queue>

using namespace std;

#define NOT -100000*2100
#define VMAX 100
#define EMAX 9900

int main(void){

	int V,E;
	int s,t,d;
	long long v[VMAX][VMAX];
	int cnt;
	vector<int> ed[VMAX];
	vector<int> edco[VMAX];
	queue<int> no;

	cin>>V>>E;

	for(int i=0;i<V;i++){
		for(int j=0;j<V;j++){
		v[i][j]=NOT;
		}
	}
;
	for(int i=0;i<E;i++){
		cin>>s>>t>>d;
		ed[s].push_back(t);
		edco[s].push_back(d);
	}

	for(int r=0;r<V;r++){
		no.push(r);
		v[r][r]=0;
		cnt=0;
		while(!no.empty()){
			if(cnt>EMAX*10){
				cout<<"NEGATIVE CYCLE"<<endl;
				return 0;
			}
			for(int i=0;i<ed[no.front()].size();i++){
				if(v[r][no.front()]+edco[no.front()][i]<v[r][ed[no.front()][i]]||v[r][ed[no.front()][i]]==NOT){
					v[r][ed[no.front()][i]]=v[r][no.front()]+edco[no.front()][i];
					cnt++;
					no.push(ed[no.front()][i]);
				}
			}
			no.pop();
		}
	}
	for(int i=0;i<V;i++){
		for(int j=0;j<V;j++){
			if(v[i][j]!=NOT) cout<<v[i][j];
			else cout<<"INF";
			if(j==V-1) cout<<endl;
			else cout<<" ";
		}
	}
}

#include <bits/stdc++.h>

#ifndef LOCAL_
#define fprintf if( false ) fprintf
#endif // LOCAL_
// #define dump() fprintf(stderr, "#%s.%d\n", __func__, __LINE__);
#define dumpl(x1) fprintf(stderr, "#%s.%d (%s) = (%ld)\n", __func__, __LINE__, #x1, x1);
#define dumpll(x1, x2) fprintf(stderr, "#%s.%d (%s, %s) = (%ld, %ld)\n", __func__, __LINE__, #x1, #x2, x1, x2);
#define dumplll(x1, x2, x3) fprintf(stderr, "#%s.%d (%s, %s, %s) = (%ld, %ld, %ld)\n", __func__, __LINE__, #x1, #x2, #x3, x1, x2, x3);
#define dumpd(x1) fprintf(stderr, "#%s.%d (%s) = (%lf)\n", __func__, __LINE__, #x1, x1);
#define dumpdd(x1, x2) fprintf(stderr, "#%s.%d (%s, %s) = (%lf, %lf)\n", __func__, __LINE__, #x1, #x2, x1, x2);
#define loop for(;;)

template<typename T> void scan1(T& x) { fprintf(stderr, "unknown type\n"); }
template<> void scan1(long& x) { if( scanf("%ld", &x) < 0 ) exit(0); }
void scan() {}
template<typename Head, typename... Tail>
void scan(Head& x, Tail&... xs) {
  scan1(x); scan(xs...);
}

template<typename W>
struct N003 {
   typedef std::vector<long> LI;
   typedef std::vector<W>    LW;
   long n, e;
   LI ss, ds;
   LW ws;
   std::vector<LI> iss;
   N003(long n_, const LI& ss_, const LI& ds_, const LW& ws_)
      : n(n_), e(ss_.size()), ss(ss_), ds(ds_), ws(ws_) {
      iss.resize(n+1);
      for(long i = 0; i < e; ++i) {
         iss[ss[i]].push_back(i);
      }
   }
};

template<typename W>
struct N006 {
   std::vector<std::vector<W>> xss;
   bool negativeCycle;
   N006(const N003<W>& g, W inf) : negativeCycle(false) {
      xss.resize(g.n+1);
      for(auto& xs : xss) xs.resize(g.n+1, inf);
      for(long i = 0; i < g.n+1; ++i) xss[i][i] = 0;
      for(long i = 0; i < g.e; ++i) {
         xss[g.ss[i]][g.ds[i]] = g.ws[i];
      }
      for(long z = 0; z < g.n+1; ++z) {
         for(long x = 0; x < g.n+1; ++x) {
            for(long y = 0; y < g.n+1; ++y) {
               W d = xss[x][y];
               W nd = xss[x][z] + xss[z][y];
               xss[x][y] = std::min(d, nd);
            }
         }
      }
      for(long x = 0; x < g.n+1; ++x) {
         for(long y = 0; y < g.n+1; ++y) {
            if( xss[x][y] >= inf / 2 ) {
               xss[x][y] = inf;
            }
         }
         if( xss[x][x] < 0 ) {
            negativeCycle = true;
         }
      }
   }
};

struct Solver {
   Solver() { fprintf(stderr, "--------Solver begin--------\n"); }
   ~Solver() { fprintf(stderr, "--------Solver end--------\n"); }
   void solve() {
      typedef std::vector<long> LI;
      long v, e;
      scan(v, e);
      LI ss(e+1), ds(e+1);
      LI ws(e+1);
      for(long i = 0; i < e; ++i) {
         scan(ss[i], ds[i], ws[i]);
      }
      N003<long> g(v, ss, ds, ws);
      N006<long> warshallfloyd(g, (1LL << 60));
      if( warshallfloyd.negativeCycle ) {
         puts("NEGATIVE CYCLE");
      }
      else {
         for(long i = 0; i < v; ++i) {
            for(long k = 0; k < v; ++k) {
               if( warshallfloyd.xss[i][k] == (1LL << 60) ) {
                  printf("INF");
               }
               else {
                  printf("%ld", warshallfloyd.xss[i][k]);
               }
               putchar( k == v - 1 ? '\n' : ' ');
            }
         }
      }
   }
};

int main() {
  loop std::unique_ptr<Solver>(new Solver())->solve();
}


#include<bits/stdc++.h>
using namespace std;

bool isEven(int x){return x%2==0;}
bool solve(vector<int> a){
    int n=a.size();
    int oneCnt=count(a.begin(),a.end(),1);
    int evenCnt=count_if(a.begin(),a.end(),isEven);
    if(oneCnt || evenCnt<n-1){
        return !isEven(evenCnt);
    }
    else{
        int g=a[0];
        for(int i=0;i<n;i++) g=__gcd(a[i]/2,g);
        for(int i=0;i<n;i++) a[i]/=(g*2);
        return !solve(a);
    }
}
void output(bool x){
    cout<<(x ? "First" : "Second")<<endl;
}
int main(){
    int n;
    cin>>n;
    vector<int> a(n);
    for(int i=0;i<n;i++) cin>>a[i];
    if(n==1){
        output(a[0]!=1);
    }
    else if(n==2){
        output(isEven(a[0]) || isEven(a[1]));
    }
    else{
        output(solve(a));
    }

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define fraction() cout.unsetf(ios::floatfield); cout.precision(10); cout.setf(ios::fixed,ios::floatfield);
#define file() freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define faster() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const long long int infLL = 9000000000000000000;
#define mem(a,b) memset(a, b, sizeof(a) )
#define all(a) (a).begin(),(a).end()
#define mx_int_prime 999999937
const int inf = 2000000000;
const double PI = acos(-1);
const double eps = 1e-9;
typedef long double ld;
#define MOD 1000000007
typedef long long ll;
const int mx=2e5+125;
ll p[mx],a[mx],b[mx];
#define PB push_back
#define endl '\n'
#define S second
#define F first
int main()
{
    faster();
    ll n;
    cin>>n;
    set<ll>s;
    for(int i=0; i<n; i++)
    {
        ll x;
        cin>>x;
        s.insert(x);
    }
    if(s.size()==n)
        cout<<"YES"<<endl;
    else
        cout<<"NO"<<endl;
    return  0;
}


#include<iostream>
#include<string>
using namespace std;
int main() {
	string a;
	getline(cin, a);
	int len = a.length();
		for (int i = len-1; i >=0; i--) {
		cout << a[i];
	}
	cout << endl;
	return 0;
}

#include<bits/stdc++.h>
#define rep(i,n) for(int i=0; i<n; i++)
using namespace std;
using ll = long long;
using pii = pair<int,int>;
ll MOD=1000000007;

ll ReturnDigit(ll n){
  int keta=1;
  while(n/10!=0){
    n /= 10;
    keta++;
  }
  return keta;
}

int main(){
  ll n; cin>>n;
  ll ans = 100000000000;
  for (ll i=1; i*i<=n; i++){
    if (n%i==0){
      ll A = i;
      ll B = n/i;
      //cout << "A" << A << endl;
      //cout << "B" << B << endl;
      ans = min(ans, max(ReturnDigit(A),ReturnDigit(B)));
    }
  }
  cout << ans << endl;
  return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cstring>
#include <complex>
#include <stack>
#include <queue>
#include <unordered_map>
#include <map>

//#define INF 100000000
//#define MOD (int) (1e9+7)
#define rep(i, a) for (int i = 0; i < (a); i++)
using namespace std;

const int MAX = 1000000;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        //そのまま計算すると負の値になるのでMODを足す
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

// 二項係数計算
long long COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main(){
    long long int n, m;
    cin >> n >> m;

    if(m == 1){
        //全部1の1通りしかありえない
        cout << 1 << endl;
        return 0;
    }

    long long int i;
    map<long long int, long long int> prime;

    //素因数分解
    long long int tmp = m;
    for(i = 2; i * i <= m; i++){
        long long int count = 0;
        while(tmp%i == 0){
            tmp /= i;
            count++;
        }
        prime[i] = count;
    }
    if(tmp*tmp > m) prime[tmp] += 1;

    COMinit();
    long long int ans = 1;
    for(auto itr = prime.begin(); itr != prime.end(); itr++){
        long long int val = itr->second;
        ans = (ans * COM(n+val-1, val))%MOD;
    }

    cout << ans << endl;
}

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vb=vector<bool>;
using vvb=vector<vb>;
using vd=vector<double>;
using vvd=vector<vd>;
using vi=vector<int>;
using vvi=vector<vi>;
using vl=vector<ll>;
using vvl=vector<vl>;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
using tll=tuple<ll,ll>;
using tlll=tuple<ll,ll,ll>;
using vs=vector<string>;

#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define rep(i,n) range(i,0,n)
#define rrep(i,n) for(ll i=(n)-1;i>=0;i--)
#define range(i,a,n) for(ll i=(a);i<(n);i++)

#define LINF ((ll)1ll<<60)
#define INF ((int)1<<30)
#define EPS (1e-9)
#define MOD (1000000007ll)
#define fcout(a) cout<<setprecision(a)<<fixed
#define fs first
#define sc second
#define PI 3.1415926535897932384

int dx[]={1,0,-1,0,1,-1,-1,1},dy[]={0,1,0,-1,1,1,-1,-1};

template<class T>bool chmax(T&a,T b){if(a<b){a=b; return true;}return false;}
template<class T>bool chmin(T&a,T b){if(a>b){a=b; return true;}return false;}
template<class S>S acm(vector<S>&a){return accumulate(all(a),S());}
template<class S>S max(vector<S>&a){return *max_element(all(a));}
template<class S>S min(vector<S>&a){return *min_element(all(a));}
void YN(bool b){cout<<(b?"YES":"NO")<<"\n";}
void Yn(bool b){cout<<(b?"Yes":"No")<<"\n";}
void yn(bool b){cout<<(b?"yes":"no")<<"\n";}
int sgn(const double&r){return (r>EPS)-(r<-EPS);} // a>0  : sgn(a)>0
int sgn(const double&a,const double&b){return sgn(a-b);} // b<=c : sgn(b,c)<=0

ll max(int a,ll b){return max((ll)a,b);} ll max(ll a,int b){return max(a,(ll)b);}
template<class T>void puta(T&&t){cout<<t<<"\n";}
template<class H,class...T>void puta(H&&h,T&&...t){cout<<h<<' ';puta(t...);}
template<class S,class T>ostream&operator<<(ostream&os,pair<S,T>p){os<<"["<<p.first<<", "<<p.second<<"]";return os;};
template<class S>auto&operator<<(ostream&os,vector<S>t){bool a=1; for(auto s:t){os<<(a?"":" ")<<s;a=0;} return os;}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	ll n,m;
	cin>>n>>m;
	rep(i,m){
		if(n%2==0){
			puta(i+1,(i+1>((m+1)/2)?n-i-1:n-i));
		}
		else{
			puta(i+1,n-i-1);
		}
	}
}


#include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x ,n) for(int i = x; i < n; i++)
#define repr(i, n) for(int i = n; i >= 0; i--)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
int main(){
    string s;
    int ans=INF;
    cin >> s;

    rep(i,s.size()-2){
        int now = (s[i]-'0')*100 + (s[i+1]-'0')*10 + (s[i+2]-'0')*1;
        ans = min(ans,abs(753-now));
    }

    cout << ans;

    return 0;
}

#include <bits/stdc++.h>

#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl "\n"

using namespace std;
using ll = long long;

const ll MOD = 1000000007LL; // = 10^9 + 7
const double PI = 3.14159265358979;

void solve()
{
  ll s;
  cin >> s;
  ll ans = s;
  while(s >= 100)
  {
    ans = min(ans, abs((s % 1000) - 753));
    s /= 10;
  }
  cout << ans;

}

int main()
{
  fastio;
  solve();

  return 0;
}


#include<bits/stdc++.h>
#include<string>
#define FIO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define ll long long
#define mod (ll)(1e9+7)
#define nax (ll)(1e5)
#define INFL LLONG_MAX
#define NINFL LLONG_MIN
#define INF INT_MAX
#define disp(a,n,arr) for(int i=a;i<(ll)n;i++) cout << arr[i] << " "; cout << "\n";
#define check cout << "Working\n";
#define pi pair<int,int>
#define pl pair<ll,ll>
#define vi vector<int>
#define vl vector<ll>
#define vpl vector<pl>
#define vpi vector<pi>
#define vb vector<bool>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(a) a.begin(),a.end()
using namespace std;

int main() {
	FIO;
	int t = 1;
	//cin >> t;
	while (t--) {
		ll n,m;
		cin >> n >> m;
		vl v(n,0);
		for(int i=0;i<m;i++){
			ll l,r;
			cin >> l >> r;
			l--;
			v[l]++;
			if(r < n) v[r]--;
		}
		ll ans = 0;
		if(v[0] == m) ans++;
		for(int i=1;i<n;i++){
			v[i] += v[i-1];
			if(v[i] == m) ans++;
		}
		cout << ans << "\n";
	}

	return 0;
}

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;
using ll = long long;

int main() {
  int n, m;
  cin >> n >> m;
  int l = 1, r = n;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    if (l < a) {
      l = a;
    }
    if (b < r) {
      r = b;
    }
  }
  if (r < l) {
    cout << 0 << endl;
  } else {
    cout << r - l + 1 << endl;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll          long long
#define vl          vector<ll>
#define vi          vector<int>
#define pi          pair<int,int>
#define pl          pair<ll,ll>
#define all(a)      a.begin(),a.end()
#define mem(a,x)    memset(a,x,sizeof(a))
#define pb          push_back
#define mp          make_pair
#define F           first
#define S           second
#define endl 		"\n"
#define rep(i,a,b)  for(int i=a;i<b;i++)
#define fast_io     std::ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define trace(...) ZZ(#__VA_ARGS__, __VA_ARGS__);
template <typename Arg1> void ZZ(const char* name, Arg1&& arg1) {std::cerr << name << " = " << arg1 << endl;}
template <typename Arg1, typename... Args>void ZZ(const char* names, Arg1&& arg1, Args&&... args)
{
	const char* comma = strchr(names + 1, ',');
	std::cerr.write(names, comma - names) << " : " << arg1;
	ZZ(comma, args...);
}

void solve() {
	string a, b, c;
	cin >> a >> b >> c;
	if (a[a.size() - 1] == b[0] && b[b.size() - 1] == c[0]) {
		cout << "YES";
	}
	else cout << "NO";

}
int main() {
	fast_io;
	int t = 1;
	// cin>>t;
	while (t--) {
		solve();
	}
	// cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
	return 0;
}

#include <iostream>
#include <bitset>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <stack>
#include <cmath>
#include <regex>
#include <iomanip>
#include <climits>
#include <utility>
#include <queue>
using namespace std;
using ll = long long int;
using dd = long double;
const ll MOD = 1e9 + 7;
const dd PI = 3.1415926435;

class UnionFind
{
public:
    UnionFind(int N) : par(N)
    {
        for (ll i = 0; i < N; i++)
        {
            par[i] = i;
        }
    }

    int root(int x)
    {
        if (par[x] == x)
            return x;
        return par[x] = root(par[x]);
    }

    void unite(int x, int y)
    {
        int rx = root(x);
        int ry = root(y);
        if (rx == ry)
            return;
        par[rx] = ry;
    }

    bool same(int x, int y)
    {
        int rx = root(x);
        int ry = root(y);
        return rx == ry;
    }

private:
    vector<int> par;
};

ll gcd(ll a, ll b)
{
    if (a % b == 0)
    {
        return b;
    }
    return gcd(b, a % b);
}

ll fact(ll n)
{
    if (n == 0)
    {
        return 1;
    }
    if (n == 1)
    {
        return 1;
    }
    return n * fact(n - 1) % MOD;
}

ll pow_fast(ll n, ll k)
{
    if (k == 0)
    {
        return 1;
    }
    if (k == 1)
    {
        return n;
    }

    if (k % 2 == 0)
    {
        ll tmp = pow_fast(n, k / 2LL);
        return tmp * tmp % MOD;
    }
    else
    {
        ll tmp = pow_fast(n, k / 2LL);
        tmp *= tmp;
        tmp %= MOD;
        return tmp * n % MOD;
    }
}

map<ll, ll> sosuu(ll n)
{
    map<ll, ll> res;
    for (ll i = 2; i * i <= n; i++)
    {
        while (n % i == 0)
        {
            res[i] += 1;
            n /= i;
        }
    }
    if (n != 1)
    {
        res[n] += 1;
    }
    return res;
}

int main()
{
    string S;
    cin >> S;
    bool ok = false;
    bool ok2 = false;
    bool ok3 = true;

    if (S[0] == 'A') {
        ok = true;
    }

    for (ll i = 2; i < S.size() - 1; i++)
    {
        if (ok2 && S[i] == 'C') {
            ok2 = false;
            break;
        }
        if (S[i] == 'C') {
            ok2 = true;
        }
    }

    for (ll i = 0; i < S.size(); i++)
    {
        if (i == 0 || S[i] == 'C') {
            continue;
        }
        if ('a' > S[i] || S[i] > 'z') {
            ok3 = false;
        }
    }
    if (ok && ok2 && ok3) {
        cout << "AC" << endl;
    } else {
        cout << "WA" << endl;
    }

}


#include <bits/stdc++.h>

using namespace std;

//const int MOD=1e9+7;

const int MOD=998244353;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin>>n;

    if(!(n%3)){
        vector<string> ans(n,string(n,'.'));

        for(int i=0; i<n; i+=3){
            ans[i][i]='a';
            ans[i][i+1]='a';
            ans[i+1][i+2]='a';
            ans[i+2][i+2]='a';
        }
        for(auto s:ans){
            cout<<s<<'\n';
        }
        return 0;
    }
    if(!(n%4)){
        vector<string> vs={"aacd","bbcd","cdaa","cdbb"};
        for(int i=0; i<n; i++){
            for(int j=0; j<n; j+=4){
                cout<<vs[i%4];
            }
            cout<<'\n';
        }
        return 0;
    }
    if(!(n%5)){
        vector<string> vs={"xyycc","xaa.w","i..aw","i..au","ssttu"};
        for(int i=0; i<n; i++){
            for(int j=0; j<n; j+=5){
                cout<<vs[i%5];
            }
            cout<<'\n';
        }
        return 0;
    }
    int a=-1,b=-1;

    for(int i=0; i<1000 && a==-1; i++){
        for(int j=0; j<1000; j++){
            if(5*i+4*j==n){
                a=i;
                b=j;
                break;
            }
        }
    }

    if(a!=-1){
        //cout<<a<<' ';
        vector<string> vs4={"aacd","bbcd","cdaa","cdbb"};
        vector<string> vs5={"xyycc","xaa.w","i..aw","i..au","ssttu"};
        vector<string> ans(n,string(n,'.'));
        int pos=0;

        while(pos<b*4){
            for(int i=0; i<4; i++){
                for(int j=0; j<4; j++){
                    ans[pos+i][pos+j]=vs4[i%4][j];
                }
            }
            pos+=4;
        }
        while(pos<b*4+a*5){
            for(int i=0; i<5; i++){
                for(int j=0; j<5; j++){
                    ans[pos+i][pos+j]=vs5[i%5][j];
                }
            }
            pos+=5;
        }

        for(auto s:ans){
            cout<<s<<'\n';
        }
        return 0;

    }
    for(int i=0; i<1000 && a==-1; i++){
        for(int j=0; j<1000; j++){
            if(7*i+4*j==n){
                a=i;
                b=j;
                break;
            }
        }
    }

    if(a!=-1){
        //cout<<a<<' ';
        vector<string> vs7={"abb...x","a.c...x","ddc...y","...abby","...a.cz","...ddcz","xxyyzz."};
        vector<string> vs4={"aacd","bbcd","cdaa","cdbb"};
        vector<string> ans(n,string(n,'.'));
        int pos=0;

        while(pos<b*4){
            for(int i=0; i<4; i++){
                for(int j=0; j<4; j++){
                    ans[pos+i][pos+j]=vs4[i%4][j];
                }
            }
            pos+=4;
        }
        while(pos<b*4+a*7){
            for(int i=0; i<7; i++){
                for(int j=0; j<7; j++){
                    ans[pos+i][pos+j]=vs7[i%7][j];
                }
            }
            pos+=7;
        }

        for(auto s:ans){
            cout<<s<<'\n';
        }
        return 0;

    }

    cout<<-1;

    return 0;
}

#include<stdio.h>
#include<algorithm>
#include<vector>
#define fst first
#define snd second

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;

char three[3][4] = {
    "xx.",
    "..y",
    "..y"
};

char four[4][5] = {
    "aacd",
    "bbcd",
    "cdaa",
    "cdbb"
};

char five[5][6] = {
    "aabbc",
    "dx..c",
    "dx..b",
    "a.yyb",
    "accdd"
};

char six[6][7] = {
    "aacd..",
    "bbcd..",
    "..aacd",
    "..bbcd",
    "cd..aa",
    "cd..bb"
};

char seven[7][8] = {
    "aabbcc.",
    "xx.xx.a",
    "..y..ya",
    "..y..yb",
    "xx.xx.b",
    "..y..yc",
    "..y..yc"
};

char ans[1000][1000 + 1];

int main() {
    int n;
    scanf("%d", &n);
    if (n == 2) return !puts("-1");
    if (n == 3) {
        for(int i = 0; i < 3; i++)
            puts(three[i]);
    }
    else {
        for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) ans[i][j] = '.';

        for(int st = 0; st < n;) {
            if (n - st == 5) {
                for (int i = 0; i < 5; i++) for (int j =0; j < 5; j++) {
                    ans[st + i][st + j] = five[i][j];
                }
                st += 5;
            }
            else if (n - st == 6) {
                for (int i = 0; i < 6; i++) for (int j = 0; j < 6; j++) {
                    ans[st + i][st + j] = six[i][j];
                }
                st += 6;
            }
            else if (n - st == 7) {
                for (int i = 0; i < 7; i++) for (int j = 0; j < 7; j++) {
                    ans[st + i][st + j] = seven[i][j];
                }
                st += 7;
            }
            else {
                for (int i = 0; i < 4; i++) for (int j = 0; j < 4; j++) {
                    ans[st + i][st + j] = four[i][j];
                }
                st += 4;
            }
        }

        for (int i = 0; i < n; i++)
            puts(ans[i]);
    }
    return 0;
}


#include <iostream>
using namespace std;

static const int MOD = 1000000007;

int main() {
    int N;
    cin >> N;
    long long sum, A[N];
    for (int i = 0; i < N; i++) {
        cin >> A[i];
        sum = (sum + A[i]) % MOD;
    }

    // sum = A1 + A2 + ... + An
    // A1(A2+A3+...+An) + A2(A3+A4+...+An) + ... + An-2(An-1+An) + An-1An
    long long ans = 0;
    for (int i = 0; i < N - 1; i++) {
        sum -= A[i];
        if (sum < 0) sum += MOD;

        ans += A[i] * sum;
        ans %= MOD;
    }
    cout << ans << endl;
    return 0;
}

    #include <bits/stdc++.h>
    #include <ext/pb_ds/assoc_container.hpp>
    #include <ext/pb_ds/tree_policy.hpp>
    #include <ext/pb_ds/detail/standard_policies.hpp>

    using namespace std;
    using namespace __gnu_pbds;

    #define endl "\n"
    #define fi first
    #define se second
    #define pb push_back
    #define int long long
    #define ll long long
    #define ld long double
    #define pii pair<ll,ll>

    #define read(t) ll t;cin>>t;
    #define all(x) x.begin(),x.end()
    #define f(a,b,c) for(ll a=b;a<c;a++)
    #define For(i,n) for(ll i=0;i<n;i++)

    #define what(x) cerr << #x << " is " << x << endl;
    #define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) ;

    typedef tree<pair<ll,ll>, null_type,less<pair<ll,ll>>, rb_tree_tag,tree_order_statistics_node_update> omultiset;
    typedef tree<long long,null_type,less<long long>,rb_tree_tag,tree_order_statistics_node_update> oset;
    typedef tree<long long,null_type,greater<long long>,rb_tree_tag,tree_order_statistics_node_update> oset1;

/*.....................................................................................................................*/

    const ll mod=(ll)1e9+ 7;

    ll add(ll x,ll y) {return (x+y)%mod;}
    ll mult(ll x, ll y){return ((x%mod )* (y%mod)) % mod;}

    long long binpow(long long x, long long p)
    {
        if (p == 0) {return 1;}
    	if (p == 2 || (p & 1)) {return mult(x, binpow(x, p - 1));}
    	return binpow(binpow(x, p / 2), 2);
    }
    ll modinv(ll a) {return binpow(a,mod-2);}

    //A.erase(unique(A.begin(),A.end()),A.end());
    double pi=3.141592653589793238462641;
    ll dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};
    const ll  man  =   (int)2e6+2 ,  inf   =   1e18;

/*......................................................................................................................*/

signed main()
{
    fast;
    int n,ans=0;
    cin>>n;
    vector<long double>a(n);
    for(int i=0;i<n;i++)cin>>a[i];
    map<pair<int,int>,int>cnt;
    for(int i=0;i<n;i++)
    {
        long double y=a[i];
        y*=1000000000;
        ll x=round(y);
        pair<int,int>p;
        while(x%2==0)
        {
            x/=2;
            p.first++;
        }
        while(x%5==0)
        {
            x/=5;
            p.second++;
        }
        cnt[p]++;
    }
    for(auto x:cnt)
    {
        for(auto y:cnt)
        {
            if((x.first.first+y.first.first>=18))
            {
                if(x.first.second+y.first.second>=18)
                {
                    if(x<y)
                    {
                        ans+=(x.second*y.second);
                    }
                    else if(x==y)
                    {
                        ans+=(x.second*(x.second-1))/2;
                    }
                }
            }
        }
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using VI = vector<ll>;
using VV = vector<VI>;
using VS = vector<string>;
using PII = pair<ll, ll>;

// tourist set
template <typename A, typename B>
string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string& s) {
  return '"' + s + '"';
}

string to_string(const char* s) {
  return to_string((string) s);
}

string to_string(bool b) {
  return (b ? "true" : "false");
}

string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}

template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cerr << '\n'; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
// tourist set end

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define rep(i,b) FOR(i, 0, b)
#define ALL(v) (v).begin(), (v).end()
#define p(s) cout<<(s)<<'\n'
#define p2(s, t) cout << (s) << " " << (t) << '\n'
#define br() p("")
#define pn(s) cout << (#s) << " " << (s) << '\n'
#define p_yes() p("YES")
#define p_no() p("NO")
#define SZ(x) ((int)(x).size())
#define SORT(A) sort(ALL(A))
#define RSORT(A) sort(ALL(A), greater<ll>())
#define MP make_pair

void no(){p_no(); exit(0);}
void yes(){p_yes(); exit(0);}

const ll mod = 1e9 + 7;
const ll inf = 1e18;
const double PI = acos(-1);

char rev(char c){
  if(c=='B'){
    return 'W';
  }else{
    return 'B';
  }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    // input
    ll N;
    cin >> N;

    string s;cin>>s;
    ll L = s.size();

    rep(i, L){
      if(i%2==0){
        s[i] = rev(s[i]);
      }
    }
    debug(s);

    ll bl = 0;
    ll wh = 0;
    for(char c : s){
      if(c=='B'){
        bl++;
      }else{
        wh++;
      }
    }

    if(bl!=wh){
      debug("1");
      p(0);
      return 0;
    }

    ll ans = 1;
    ll cnt = 0;
    for(char c : s){
      if(c=='W'){
        cnt++;
      }else{
        ans *= cnt;
        ans %= mod;
        cnt--;
        if(cnt<0){
          debug("2");
          p(0);
          return 0;
        }
      }
    }
    // N!
    FOR(i, 1, N+1){
      ans *= i;
      ans %= mod;
    }
    p(ans);

    return 0;
}


#include<cstdio>
#include<algorithm>
#define fo(i,a,b) for(i=a;i<=b;i++)
using namespace std;
typedef long long ll;
const int maxn=100000+10;
int fa[maxn],co[maxn];
bool bz[maxn],pd[maxn];
int i,j,k,l,r,t,n,m,cnt;
ll ans;
int getfa(int x){
    if (!fa[x]) return x;
    int t=getfa(fa[x]);
    co[x]^=co[fa[x]];
    return fa[x]=t;
}
int main(){
    scanf("%d%d",&n,&m);
    fo(i,1,m){
        scanf("%d%d",&j,&k);
        bz[j]=1;bz[k]=1;
        l=getfa(j);r=getfa(k);
        if (l<r){
            swap(l,r);
            swap(j,k);
        }
        if (l==r&&co[j]==co[k]) pd[l]=1;
        if (l==r) continue;
        pd[r]|=pd[l];
        fa[l]=r;
        co[l]=co[j]^co[k]^1;
    }
    fo(i,1,n)
        if (!bz[i]) cnt++;
    ans=(ll)cnt*(n-cnt)*2+(ll)cnt*cnt;
    cnt=0;
    fo(i,1,n)
        if (bz[i]&&!fa[i]) cnt++;
    ans+=(ll)cnt*cnt;
    cnt=0;
    fo(i,1,n)
        if (bz[i]&&!fa[i]&&!pd[i]) cnt++;
    fo(i,1,n)
        if (bz[i]&&!fa[i]&&!pd[i]) ans+=(ll)cnt;
    printf("%lld\n",ans);
}

#include <bits/stdc++.h>
#include <algorithm>
#include <time.h>
#define l(x) x<<1
#define r(x) ((x<<1) | 1)
#define x first
#define y second
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
const int inf = 2*(1e9) + 10;
const long long MOD =998244353;
const int NMAX = 1000005;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

vector<int> G[NMAX];
int viz[NMAX], color[NMAX];
bool bip;

int dfs(int v, int p){
    for(auto el: G[v]){
        if(el == p){
            continue;
        }
        if(viz[el]){
            if(color[el] != 1-color[v]){
                bip = 0;
            }
        } else {
            color[el] = 1 - color[v];
            viz[el] = 1;
            dfs(el, v);
        }
    }
}

int main(){
    int n, m;
    cin>>n>>m;

    for(int i = 1; i<=m; ++i){
        int a, b;
        cin>>a>>b;
        G[a].push_back(b);
        G[b].push_back(a);
    }

    LL ans = 0;
    LL sum = 0;
    LL ons = 0;
    LL bp = 0;
    LL bm = 0;
    for(int i=1; i<=n; ++i){

        if(G[i].size() == 0){
            ons++;
            continue;
        }
        if(viz[i]){ continue;}
        bip = 1;
        viz[i] = 1;
        dfs(i, 0);
        if(bip){
            bp++;
        } else bm++;
    }
    cout<<2*ons*n - ons*ons + bm*bm + 2*bm*bp + 2*bp*bp<<"\n";

    return 0;
}


#include <bits/stdc++.h>
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define rrep(i,a,b) for(int i=int(a);i>int(b);--i)
using namespace std;
using ll = long long;

int p[2][200010];
map<int, int> mp[200010];

void init(int t, int n){
  rep(i,n) p[t][i]=i;
}

int root(int t, int x){
  if(p[t][x]==x) return x;
  else return p[t][x]=root(t,p[t][x]);
}

bool same(int t, int x, int y){
  return root(t,x)==root(t,y);
}

void unite(int t, int x, int y){
  x = root(t,x);
  y = root(t,y);
  if(x==y) return;

  p[t][x] = y;
}

int main()
{
  int N,K,L;
  cin >> N >> K >> L;

  init(0,N);
  init(1,N);

  int p,q;
  rep(i,K){
    cin >> p >> q;
    unite(0,p-1,q-1);
  }
  rep(i,L){
    cin >> p >> q;
    unite(1,p-1,q-1);
  }

  rep(i,N) mp[root(0,i)][root(1,i)]++;
  rep(i,N) cout << mp[root(0,i)][root(1,i)] << ((i==N-1)?"\n":" ");
}

#include<bits/stdc++.h>
using namespace std;

bool check(vector<string> board)
{
	for (int i=0;i<8;i++)
	{
		if (count(board[i].begin(),board[i].end(),'Q')!=1) return false;
		int cnt=0,cnt1=0,cnt2=0,cnt3=0,cnt4=0;
		for (int j=0;j<8;j++)
		{
			if (board[i][j]=='Q') cnt++;
		}
		if (cnt!=1) return false;
		for (int j=0;j<i+1;j++)
		{
			if (board[i-j][j]=='Q') cnt1++;
			if (board[i-j][7-j]=='Q') cnt2++;
		}
		if (cnt1>1 || cnt2>1) return false;
		for (int j=0;j<8-i;j++)
		{
			if (board[i+j][j]=='Q') cnt3++;
			if (board[i+j][7-j]=='Q') cnt4++;
		}
		if (cnt3>1 || cnt4>1) return false;
	}
	return true;
}

int main()
{
	int K;cin >> K;
	vector<pair<int,int>> state(K);
	int x,y;
	for (int i=0;i<K;i++)
	{
		cin >> x >>y;
		state[i]=make_pair(x,y);
	}
	vector<int> num={0,1,2,3,4,5,6,7};
	do
	{
		bool can=true;
		for (int i=0;i<K;i++)
		{
			x=state[i].first;
			y=state[i].second;
			if (num[x]!=y) can=false;;
		}
		if (!can) continue;
		vector<string> board(8,"........");
		for (int i=0;i<8;i++) board[i][num[i]]='Q';
		if (check(board))
		{
			for (int i=0;i<8;i++)
			{
				cout << board[i] << endl;
			}
			break;
		}
	} while (next_permutation(num.begin(),num.end()));

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <set>
#include <vector>

using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)

int main() {
	int n, m;
	vector<vector<int> > f;
	for (;;) {
		scanf("%d %d", &n, &m);
		if (!n && !m) break;
		f = vector<vector<int> >(n + 1);
		rep(i, m) {
			int a, b;
			scanf("%d %d", &a, &b);
			f[a].push_back(b);
			f[b].push_back(a);
		}

		set<int> friends, friends_2;
		friends.insert(1);
		friends_2.insert(1);
		rep(i, f[1].size()) friends.insert(f[1][i]);

		for (auto ff : friends) {
			rep(i, f[ff].size()) friends_2.insert(f[ff][i]);
		}

		printf("%d\n", friends_2.size() - 1);
	}

	return 0;
}

#include <iostream>
#include <stdio.h>
using namespace std;

int main(){
    int n,m,a[100];
    cin >> n >> m;
    while ((n!=0)||(m!=0)){
        for (int i=0;i<100;i++)
            a[i]=0;
        for (int i=1;i<=n;i++){
            int b,c;
            cin >> b;
            for (int j=1;j<=b;j++){
                cin >> c;
                a[c]++;
            }
        }
        int max=0;
        for (int i=1;i<100;i++){
            if ((a[i]>a[max])&&(a[i]>=m))
                max=i;
        }
        cout << max << endl;
        cin >> n >> m;
    }
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {
    int N;
    cin >> N;
    int M = 0;
    cin >> M;
    vector<int> s(M,0);
    vector<int> c(M,0);
    if(M == 0) {
        cout << (int)pow(10, N - 1)*(N != 1) << endl;
        return 0;
    }
    for(int i = 0; i < M; i++) {
        cin >> s.at(i);
        cin >> c.at(i);
    }
    for(int i = pow(10, N - 1)*(N != 1); i < pow(10, N); i++) {
        int check = 1;
        for(int j = 0; j < M; j++) {
            if((i/(int)pow(10, N - s.at(j)))%10 == c.at(j)) {
                continue;
            }
            check = 0;
        }
        if(check) {
            cout << i << endl;
            return 0;
        }
    }

    cout << -1 << endl;

    return 0;
}

#include <bits/stdc++.h>
#define REP(i,n) for (int i = 0; i <(n); ++i)
#define REP2(i,x,n) for (int i = x; i <(n); ++i)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
using namespace std;
using ll = long long;
using P = pair<int,int>;
static const double PI = acos(-1);
static const int INF = 1e9+7;
#define REP(i,n) for (int i = 0; i <(n); ++i)
//debug
#ifdef _DEBUG
#define debug(var)  do{cout << #var << " :";view(var);}while(0)
#else
#define debug(...)
#endif
template<typename T> void view(T e){cout << e << endl;}
template<typename T> void view(const vector<T>& v){for(const auto& e : v){ cout << e << " "; } cout << endl;}
template<typename T> void view(const vector<vector<T> >& vv){ for(const auto& v : vv){ view(v); } }

int main(){
  //input
  int n, m;
  cin >> n >> m;
  vector<int> s(m), c(m);
  REP(i,m) {
    int sx, cx;
    cin >> sx >> cx;
    sx--;
    s[i] = sx;
    c[i] = cx;
  }
  debug(s);
  debug(c);

  vector<int> a(n, -1);
  debug(a);

  REP(i,m){
    // if((s[i] == 0) && (c[i] == 0)){
    //     cout << -1 << endl;
    //     return 0;
    // }
    if((a[s[i]] != -1) && (a[s[i]] != c[i])){
      cout << -1 << endl;
      return 0;
    }
    a[s[i]] = c[i];
    debug(a);
  }

  if(n == 1){
    if(a[0] == -1) a[0] = 0;
    cout << a[0] << endl;
    return 0;
  }

  if(a[0] == 0){
    cout << -1 << endl;
    return 0;
  }

  REP(i,n){
    if(a[i] == -1){
      if(i == 0) a[i] = 1;
      else a[i] = 0;
    }
  }

  int ans = 0;
  REP(i,n){
    ans *= 10;
    ans += a[i];
  }

  cout << ans << endl;

  return 0;
}


#include"bits/stdc++.h"

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = long long;
using pi = pair<int, int>;
const ll INF = 1LL << 60;

int main() {
	string s;
	cin >> s;
	s.replace(s.begin(), s.begin() + 4, "2018");
	cout << s << endl;
	return 0;
}

//#include "stdafx.h"
#include<bits/stdc++.h>
#include<iostream>
#include<queue>
#include<string>
using namespace std;
string s;
int  ans[26],f;
int main()
{
	cin>>s;
	for(int i=0;i<(int)s.size();i++){
		ans[s[i]-'a']++;
	}

	for(int i=0;i<26;i++){
		if(ans[i]==0){
			cout<<char('a'+i)<<"\n";
			f=1;
			break;
		}
	}
	if(f==0)
		cout<<"None\n";

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<long long> a(n);
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    string s;
    cin >> s;
    vector<long long> b;
    int ans = 0;
    for (int i = n - 1; i >= 0; i--) {
      long long cur = a[i];
      for (auto& x : b) {
        cur = min(cur, cur ^ x);
      }
      if (cur > 0) {
        if (s[i] == '1') {
          ans = 1;
          break;
        } else {
          b.push_back(cur);
        }
      }
    }
    cout << ans << '\n';
  }
  return 0;
}

/*
  Creator :  bizarre
*/
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
#define bizarre  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define file freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
#define ll long long
#define ld long double
#define pi acos(-1)
#define point complex<double>
const int N = 3e5 + 9, inf = 1e18, mod = 1e9 + 7;
vector<int> prime_Numbers;
bool primes[1000005];
double dot(point a, point b)
{
    return (conj(a) * b).real();
}
double cross(point a, point b)
{
    return (conj(a) * b).imag();
}
/*
void prime(int n)
{
    primes[1]=primes[0]=0;
    for(int i=2; i<=n; i++)
        primes[i]=true;
    for(int i=2; i<=n; i++)
    {
        if(primes[i]==true)
        {
            prime_Numbers.push_back(i);
            for(int j=i+i; j<=n; j+=i)
                primes[j]=false;
        }
    }
}
void move1step(long long& a, long long& b, long long q)
{
    long long c= a-q*b;
    a=b;
    b=c;
}
long long egcd(long long r0,long long r1,long long& x0,long long& y0)
{
    long long x1=0,y1=1;
    x0=1;
    y0=0;
    while(r1)
    {
        long long q=r0/r1;
        move1step(r0,r1,q);
        move1step(x0,x1,q);
        move1step(y0,y1,q);
    }
    return r0;
}
bool  solve (long long a, long long b, long long c,long long&x, long long& y)
{
    long long g=egcd(a,b,x,y);
    int m=c/g;
    x*=m;
    y*=m;
    return c%g==0;
}
int gcd(int a, int b)
{
    if(b==0)
        return a;
    return gcd(b,a%b);
}
void PrimeFactorization(int n)
{
    for(int i=2; i*i<=n; i++)
    {
        int c=0;
        if(n%i==0)
        {
            while(n%i==0)
            {
                n/=i;
            }

        }
    }

}
long long ncrtane(long long n, long long r)
{
    long long ans=1,cntr=1;
    for(int i=n; i>n-r; i--)
    {
        ans*=i;
        ans/=cntr;
        cntr++;
    }
    return ans;
}
int ncr(long long n, long long r)
{
    return (((fact[n] * inv[n - r]) % mod) * inv[r]) % mod;
}

int npr(long long n, long long r)
{
    return (fact[n] * inv[n - r]) % mod;
}
long long fact[N], inv[N];
long long fp(long long base, long long exp)
{
    if (exp == 0)
        return 1;
    long long ans = fp(base, exp / 2);
    ans = (ans * ans) % mod;
    if (exp % 2 != 0)
        ans = (ans * (base % mod)) % mod;
    return ans;
}

void pre(long long n)
{
    fact[0] = 1;
    inv[0] = 1;
    for (long long i = 1; i <= n; i++)
    {
        fact[i] = (i * fact[i - 1]) % mod;
        inv[i] = fp(fact[i], mod - 2);
    }
}
*/
/*
ll lfts,lftw;
ll p,f,cnts,cntw,s,w;
ll ss(ll cnts, ll cntw, ll mxmp)
{
     for(int i = 0;i<=cnts;i++)
        {
            ll var = i*s,s = 0,e = cntw,mid,ans;
            if(var>p)break;
            while(s<=e)
            {
                mid = (s+e)/2;
                if((mid*w)+var<=p)ans = mid,s = mid+1;
                else e = mid-1;
            }
            if(mxmp<i+mid)mxmp = i+mid,lfts-=i,lftw-=mid;
        }
        return mxmp;
}
ll ww(ll cnts, ll cntw, ll mxmp)
{
    for(int i = 0;i<=cnts;i++)
        {
            ll var = i*s,s = 0,e = cntw,mid,ans;
            if(var>f)break;
            while(s<=e)
            {
                mid = (s+e)/2;
                if((mid*w)+var<=f)ans = mid,s = mid+1;
                else e = mid-1;
            }

            if(mxmp<i+mid)mxmp = i+mid;
        }
        return mxmp;
}
*/
int main()
{
    bizarre
    int t = 1;
    //cin >> t;
    while(t--)
    {
     int n;
     cin >> n;
     int mnm = 1e9;
     for(int i = 0;i<n;i++)
     {
        ll x;
        cin >> x;
        int cntr = 0;
        while(x%2==0)
        {
            x/=2;
            cntr++;
        }
        mnm = min(mnm,cntr);
     }
     cout << mnm;
    }
    return 0;
}


#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
typedef long long ll;
#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;
#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define FORR(i,a,b) for(int i=(int)(b)-1;i>=(int)(a);i--)
#define CHMIN(a,b) (a) = min((a),(b))
#define CHMAX(a,b) (a) = max((a),(b))

int main()
{
    int n,p;
    while(1==1)
    {
        scanf("%d %d\n",&n,&p);
        if(n==0&&p==0)return 0;
        int ko[55]={};
        int zan=p;
        int poi=0;
        int judge=0;
        while(judge==0)
        {
            for(int i=0;i<n;i++)ko[i]+=zan/n;
            zan-=(zan/n)*n;
            while(zan>0)
            {
                ko[poi]++;
                if(ko[poi]==p)
                {
                    printf("%d\n",poi);
                    judge=1;
                    break;
                }
                zan--;
                poi++;
                if(poi>=n)poi-=n;
            }
            if(judge==1)break;
            while(zan==0)
            {
                if(ko[poi]>0)
                {
                    zan=ko[poi];
                    ko[poi]=0;
                }
                poi++;
                if(poi>=n)poi-=n;
            }
        }
    }
    return 0;
}

#include <iostream>
#include <vector>
using namespace std;

int main(){
	cin.tie(0);
    ios::sync_with_stdio(false);
	vector<int> ansvec;

	for(;;){
		int n, p;
		cin >> n >> p;
		if (n == 0) break;

		int bowlp = p;
		int turn = 0;
		int mayors[50]{0};
		for(;;){
			if (bowlp == 1 && mayors[turn] == p - 1) break;
			else if (bowlp == 0){
				bowlp += mayors[turn];
				mayors[turn] = 0;
			}
			else{
				mayors[turn]++;
				bowlp--;
			}

			if (turn == n - 1) turn = 0;
			else turn++;
		}
		ansvec.push_back(turn);
	}

	for (int ans : ansvec){
		cout << ans << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int,int>;
#define chmin(x,y) x = min(x,y)

const int INF = 1e9 + 5;
int dp[1000005][2];

int main(){
    string s;
    cin >> s;
    reverse(s.begin(), s.end());
    s += '0';
    int n = s.size();
    rep(i,n+1)rep(j,2) dp[i][j] = INF;
    dp[0][0] = 0;
    rep(i,n)rep(j,2){
        int x = s[i] - '0';
        x += j;
        if(x < 10) chmin(dp[i+1][0], dp[i][j] + x);
        if(x > 0) chmin(dp[i+1][1], dp[i][j] + (10 - x));
    }
    int ans = dp[n][0];
    cout << ans << endl;
}

#include <bits/stdc++.h>
using namespace std;
#define int long long
using vec_int = vector<int>;
using P = pair<int,int>;
using T = tuple<int,int,int>;
using ll = long long;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)

int charToInt(char c){
    char zero_num = '0';
    return (int)c - (int)zero_num;
}

signed main(){
    string A; cin>>A;

    //ナイーブに考えれば答えはnC2通り
    // ただし、選択した部分が回文になっていればそれは同じ結果になるのと、
    // 両端の文字が同じならばその両端を除いた部分をひっくり返すのと同じ結果になる
    // したがって
    // 回文の数
    // (各文字に関して同じペアの数 C 2)の和 - そのうち回文の数
    // をnC2から差っ引けばよい
    // と思ったんだけれど、これって回文の数がキャンセルするから、a-zの数を数えればOKなもんだいじゃね?

    char alphabet[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};

    map<char, int> char_num;
    for(int i=0;i<26;i++){
        char_num[alphabet[i]] = 0;
    }
    int length= A.size();
    rep(i, length){
        char_num[A[i]] +=1;
    }

    int answer = length*(length-1)/2;

    for(int i=0;i<26;i++){
        answer -= char_num[alphabet[i]]*(char_num[alphabet[i]]-1)/2;
    }
    cout<<answer+1<<endl;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pi 3.141592653589793238
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
ll MOD=1e9+7;

ll f(pair<string,ll>&x ,pair<string,ll>&y)
{
	if(x.first == y.first)
	{
		return x.second>y.second;
	}
	else
	{
		return x.first<y.first;
	}
}

void solve()
{
	ll a,b;
	cin>>a>>b;
	cout<<max(a+b,2*max(a,b)-1);
	return;
}

int main()
{
        fast;
        ll q;
        q=1;
        // cin>>q;
        while(q--)
        	solve();

        return 0;
}

#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <cmath>
#include <map>
#include <climits>
#include <cassert>
#include <algorithm>
#include <utility>

using namespace std;
using Int = long long int;
using UInt = unsigned long long int;
using VI = vector<long long int>;
using VVI = vector<vector<long long int> >;

int main(void) {
    Int r;
    cin >> r;
    cout << fixed << setprecision(10) << 2 * r * M_PI << endl;
    return 0;
}


#include <bits/stdc++.h>
#define rep(i,n)for(long long i=0;i<(n);i++)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int INF=1e9;
const int MOD=1e9+7;
const double pi=acos(-1);

int main(){
  int n;
  cin >> n;
  vector<ll> s(n+1);
  s[0]=0;
  rep(i,n){
    ll a;
    cin >> a;
    s[i+1]=s[i]+a;
  }

  ll ans=1e18;
  ll l=1,r=3;
  for(int i=2;i<n-1;i++){
    if(r<=i)r=i+1;
    while(l+1<i && abs(s[i]-2*s[l+1])<=abs(s[i]-2*s[l])){
      l++;
    }
    while(r+1<n && abs(s[r+1]-s[i]-(s[n]-s[r+1]))<=abs(s[r]-s[i]-(s[n]-s[r]))){
      r++;
    }
    ll M = max({s[l],s[i]-s[l],s[r]-s[i],s[n]-s[r]});
    ll m = min({s[l],s[i]-s[l],s[r]-s[i],s[n]-s[r]});
    ans = min(ans,M-m);
  }
  cout << ans << endl;
}

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define pii pair<int,int>
#define pll pair<ll, ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define ppi pair<pii,int>
#define pip pair<int,pii>
#define pdd pair<double, double>
#define f first
#define s second
#define MOD 1000000007
#define mkp make_pair
#define M_PI 3.14159265358979323846
#define FOR(i,l,r) for (int i=l;i<=r;i++)
#define LOR(i,l,r) for (ll i=l;i<=r;i++)
#define FORD(i,r,l) for (int i=r;i>=l;i--)
#define LORD(i,r,l) for (ll i=r;i>=l;i--)
#define INF 1000000000
#define CL(x) memset(x,0,sizeof(x))
#define DEB(x) cout << #x << " : " << x << '\n'
#define ALL(x) x.begin(), x.end()
#define SZ(x) x.size()
#define UI(x) (int)(x-'A')
#define LI(x) (int)(x-'a')
typedef long long ll;

#define MXN 500005
int N,A[MXN];
map<pii,ll> DP;
//for x|x specially
ll psum[MXN];
map<int,ll> lsta;
map<int,int> lstp;
//===
ll Ans;
int xb;

ll modexp(ll x, ll y)
{
    ll Pow = x%MOD, rtn = 1;
    while(y)
    {
        if(y&1)rtn=(rtn*Pow)%MOD;
        y>>=1;
        Pow=(Pow*Pow)%MOD;
    }
    return rtn;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin>>N;
    FOR(i,1,N)
    {
        cin>>A[i];
        xb^=A[i];
    }
    int xf=0;
    Ans=1;
    FOR(i,1,N-1)
    {
        xf^=A[i];
        xb^=A[i];
        //0|0 case
        psum[i]=psum[i-1]+( xf==0 && xb==0 );

        ll za[4];
        za[1]=DP[mkp(xb?xb:xf,1)];
        za[2]=DP[mkp(xb?xb:xf,2)];
        za[3]=DP[mkp(xb,3)];

        //(even x)|x case
        if( xf==0 && xb!=0 )
        {
            Ans=(Ans+za[2])%MOD;
            DP[mkp(xb,1)]=(za[1]+za[2])%MOD;
        }
        //(odd x)|0 case
        if( xf!=0 && xb==0 )
        {
            DP[mkp(xf,2)]=(za[1]+za[2]+1)%MOD;
        }
        //(odd x)|x case
        if( xf==xb && xf )
        {
            if( lstp.find(xf)!=lstp.end() )
            {
                int lp = lstp[xf];
                ll la = lsta[xf];
                ll na = (la+za[3]*(psum[i-1]-psum[lp]))%MOD;
                Ans=(Ans+na)%MOD;

                lsta[xf]=na;
                DP[mkp(xf,3)]=(za[3]+na)%MOD;
            }
            else
            {
                DP[mkp(xf,3)]=lsta[xf]=1;
                Ans=(Ans+1)%MOD;
            }
            lstp[xf]=i;
        }
    }
    if(psum[N-1])
    {
        ll Zans=modexp(2,psum[N-1]);
        Ans=(Ans+Zans-1)%MOD;
    }
    cout<<Ans<<'\n';

    return 0;
}

/*
3
1 2 3

5
0 0 0 0 0
*/


#include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i=0; i<n; i++)
#define rep1(i, n) for(int i=1; i<=n; i++)
#define repr(i, n) for(int i=n-1; i>=0; i--)
#define repr1(i, n) for(int i=n; i>=1; i--)
#define all(v) v.begin(),v.end()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const int INF = 1e9;
const ll LLINF = 1e18;
const ll MOD = 1e9+7;
const double EPS = 1e-10;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

int main() {
    int n, k;
    cin >> n >> k;
    cout << n - k + 1 << endl;
}

/**
*    created: 23.06.2020 23:17:01
**/
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
#define pcnt(bit) __builtin_popcount(bit)
#define mod 1000000007
#define pi acos(-1.0)
using namespace std;

signed main() {

    cin.tie(0);
    ios_base::sync_with_stdio(false);

    int n;
    cin >> n;
    if (n == 1) cout << "Hello World" << endl;
    else {
        int a, b;
        cin >> a >> b;
        cout << a + b << endl;
    }
    return 0;
}


//#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cstring>
#include <deque>
#include <map>
using namespace std;

#define ii pair <int, int>
#define lli long long
#define pb push_back
#define X first
#define Y second

const int Mod = 1E9 + 7;
const int Inf = 1E9 + 7;
const int N = 80 + 10;
const int M = 80 * 160 + 1;

int n, m, a[N][N], b[N][N];
bool F[N][N][M][2];

void Input()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) cin >> a[i][j];
    for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) cin >> b[i][j];
}

void Solve()
{
    memset(F, 0, sizeof(F));
    int Sum = a[1][1] - b[1][1];
    Sum = abs(Sum);
    F[1][1][Sum][1] = F[1][1][Sum][0] = true;

    for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
    for (int k = 0; k < M; k++)
    for (int l = 0; l <= 1; l++){
        if (F[i][j][k][l] == true) {
            int sign = 1;
            if (l == 0) sign = -1;
            int k1 = k * sign + a[i + 1][j] - b[i + 1][j];
            int k2 = k * sign + a[i][j + 1] - b[i][j + 1];

            if (k1 >= 0) F[i + 1][j][k1][1] = true;
            else F[i + 1][j][-k1][0] = true;

            if (k2 >= 0) F[i][j + 1][k2][1] = true;
            else F[i][j + 1][-k2][0] = true;
            ///
            k1 = k * sign - a[i + 1][j] + b[i + 1][j];
            k2 = k * sign - a[i][j + 1] + b[i][j + 1];

            if (k1 >= 0) F[i + 1][j][k1][1] = true;
            else F[i + 1][j][-k1][0] = true;

            if (k2 >= 0) F[i][j + 1][k2][1] = true;
            else F[i][j + 1][-k2][0] = true;

        }
    }

    for (int k = 0; k < M; k++) if (F[n][m][k][0] == true || F[n][m][k][1] == true) {
        cout << k << " ";
        return ;
    }
}

#define task "test"
int main()
{
    if (fopen(task ".inp", "r")) {
        freopen(task ".inp", "r", stdin);
        freopen(task ".out", "w", stdout);
    }
    int testcase = 1;
    /// cin >> testcase;
    while (testcase--) {
        Input();
        Solve();
    }
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;

#define fi first
#define se second
#define m_p make_pair
#define p_b push_back
#define e_b emplace_back
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())

#define REP(i,m,n) for(int i=(int)(m);i<(int)(n);i++)
#define rep(i,n) REP(i,0,n)

#ifdef LOCAL//compile with -DLOCAL
#define debug(x) cerr<<"LINE"<<__LINE__<<" : "<<#x<<" = "<<(x)<<endl
#define debug_vec(x) cerr<<"LINE"<<__LINE__<<" : "<<#x<<" = ";\
  rep(i,sz(x)){cerr<<x[i]<<" ";}cerr<<endl
#define debug_mat(x) cerr<<"LINE"<<__LINE__<<" : "<<#x<<" = "<<endl;\
  rep(i,sz(x)){rep(j,sz(x[i])){cerr<<x[i][j]<<" ";}cerr<<endl;}cerr<<endl
#else
#define debug(x) void(0)
#define debug_vec(x) void(0)
#define debug_mat(x) void(0)
#endif

template<class T> bool chmax(T &a,T b){if(a<b){a=b;return true;}return false;}
template<class T> bool chmin(T &a,T b){if(a>b){a=b;return true;}return false;}

int main(){
  ios_base::sync_with_stdio(false);cin.tie(0);
  int H,W;
  cin >> H >> W;
  vector<vector<int>> A(H,vector<int>(W));
  rep(i,H){
    rep(j,W)  cin >> A.at(i).at(j);
  }
  vector<vector<int>> B(H,vector<int>(W));
  rep(i,H){
    rep(j,W)  cin >> B.at(i).at(j);
  }

  int base=100*(H+W);
  vector<vector<vector<bool>>> dp(H,vector<vector<bool>>(W,vector<bool>(2*base,false)));
  dp.at(0).at(0).at(base+A.at(0).at(0)-B.at(0).at(0))=true;
  dp.at(0).at(0).at(base-A.at(0).at(0)+B.at(0).at(0))=true;
  rep(i,H){
    rep(j,W){
      int tmp=A.at(i).at(j)-B.at(i).at(j);
      rep(k,2*base){
        if(i-1>=0 && dp.at(i-1).at(j).at(k)){
          if(k+tmp <2*base) dp.at(i).at(j).at(k+tmp)=true;
          if(k-tmp >=0) dp.at(i).at(j).at(k-tmp)=true;
        }
        if(j-1>=0 && dp.at(i).at(j-1).at(k)){
          if(k+tmp <2*base) dp.at(i).at(j).at(k+tmp)=true;
          if(k-tmp >=0) dp.at(i).at(j).at(k-tmp)=true;
        }
      }
    }
  }
  int ans=1e9;
  rep(i,2*base){
    if(!dp.at(H-1).at(W-1).at(i))  continue;
    chmin(ans,abs(i-base));
  }
  cout << ans << endl;

  return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
    int n,k,l[52],i,j,sum=0;
    cin>>n>>k;
    for(i=0;i<n;i++){
        cin>>l[i];
    }
    sort(l,l+n);
    for(j=n-1;j>=n-k;j--){
        sum+=l[j];
    }
    cout<<sum;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using PLL = pair<ll, ll>;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define repn(i,n) for(int i = 0; i <= (int)(n); i++)
#define srep(i,l,n) for(int i = l; i < (int)(n); i++)
#define srepn(i,l,n) for(int i = l; i <= (int)(n); i++)
#define drep(i,n) for(int i = (int)(n-1); i >= 0; i--)
#define drepn(i,n) for(int i = (int)(n); i >= 0; i--)
#define size(s) (int)s.size()
#define debug(var)  do{std::cout << #var << " : ";view(var);}while(0)
template<typename T> void view(T e){std::cout << e << std::endl;}
template<typename T> void view(const std::vector<T>& v){for(const auto& e : v){ std::cout << e << " "; } std::cout << std::endl;}
template<typename T> void view(const std::vector<std::vector<T> >& vv){ for(const auto& v : vv){ view(v); } }
template<typename T> inline istream& operator>>(istream &i, vector<T> &v) {rep(j, size(v)) i >> v[j]; return i;}
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
template<class T> T gcd(T a, T b) {if(b==0)return a; else return gcd(b,a%b);}
template<class T> T lcm(T a, T b) {return a/gcd(a,b)*b;}
template<class T = int> using V = vector<T>;
template<class T = int> using VV = vector<V<T>>;
bool isIn(int i, int j, int h, int w) {return i >= 0 && i < h && j >= 0 && j < w;}
void Yes(){cout << "Yes" << endl;}
void No(){cout << "No" << endl;}
void YES(){cout << "YES" << endl;}
void NO(){cout << "NO" << endl;}
void err() {cout << -1 << endl;}
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
#define ep emplace_back

const int MOD = 1000000007;
const int INF = 1e9;

#define PI acos(-1);
int dx[4] = {1,-1,0,0};
int dy[4] = {0,0,1,-1};
int ddx[8] = {1,1,1,-1,-1,-1,0,0};
int ddy[8] = {0,1,-1,0,1,-1,1,-1};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, k;
    cin >> n >> k;
    V<> a(n);
    cin >> a;
    sort(rall(a));
    int ans = accumulate(a.begin(), a.begin() + k, 0);
    cout << ans << endl;
}


#include <stdio.h>
#include <string.h>

int main() {
	char str1[12], str2[12], str3[12];

	scanf("%s%s%s", str1, str2, str3);

	int len1 = strlen(str1), len2 = strlen(str2), len3 = strlen(str3);
	if (str1[len1 - 1] == str2[0] && str2[len2 - 1] == str3[0]) {
			puts("YES");
		}
		else {
			puts("NO");
		}

	return 0;
}


#define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <list>
#include <utility>
#include <numeric>

using namespace std;
typedef long long llong;
typedef unsigned long long ullong;

const ullong MOD = 1000000000 + 7;

int main()
{
	string s;
	for (; cin >> s;)
	{
		vector<int> a(14);

		stringstream ss;
		ss << s;

		for (int i = 0; i < 5; i++)
		{
			int x;
			ss >> x;

			a.at(x - 1)++;
			if (x == 1)
			{
				a.at(13)++;
			}

			ss.ignore();
		}

		int maxn = 0;
		int cnt = 0;
		int len = 0;
		bool str = false;
		for (int i = 0; i < 14; i++)
		{
			if (a.at(i) > 0)
			{
				maxn = max(maxn, a.at(i));
				if (i < 13)
				{
					cnt++;
				}
				len++;
				if (len >= 5)
				{
					str = true;
				}
			}
			else
			{
				len = 0;
			}

		}

		if (maxn == 4)
		{
			cout << "four card" << endl;
		}
		else if (maxn == 3)
		{
			if (cnt == 2)
			{
				cout << "full house" << endl;
			}
			else
			{
				cout << "three card" << endl;
			}
		}
		else if (maxn == 2)
		{
			if (cnt == 3)
			{
				cout << "two pair" << endl;
			}
			else
			{
				cout << "one pair" << endl;
			}
		}
		else
		{
			if (str)
			{
				cout << "straight" << endl;
			}
			else
			{
				cout << "null" << endl;
			}
		}
	}
}

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<string>
using namespace std;

int a[5];

string S;

string poker(int p, int q, int r, int s, int t)
{
	if (p == q && q == r && r == s)
	{
		return "four card";
	}
	if (s == t && q == r && r == s)
	{
		return "four card";
	}
	if (p == q && q == r && s == t)
	{
		return "full house";
	}
	if (p == q && r == s && s == t)
	{
		return "full house";
	}
	if (p == q - 1 && q == r - 1 && r == s - 1 && s == t - 1)
	{
		return "straight";
	}
	if (p == 1 && q == 10 && r == 11 && s == 12 && t == 13)
	{
		return "straight";
	}
	if (p == q && q == r)
	{
		return "three card";
	}
	if (r == s && q == r)
	{
		return "three card";
	}
	if (r == s && s == t)
	{
		return "three card";
	}
	if (p == q && r == s)
	{
		return "two pair";
	}
	if (p == q && s == t)
	{
		return "two pair";
	}
	if (q == r && s == t)
	{
		return "two pair";
	}
	if (p == q)
	{
		return "one pair";
	}
	if (q == r)
	{
		return "one pair";
	}
	if (r == s)
	{
		return "one pair";
	}
	if (s == t)
	{
		return "one pair";
	}
	return "null";
}

int main() {
	while (cin >> S)
	{
		int cnt = 0;
		string U = "";
		S += ',';
		for (int i = 0; i < S.size(); i++)
		{
			if (S[i] == ',')
			{
				a[cnt] = stoi(U);
				U = "";
				cnt++;
			}
			else
			{
				U += S[i];
			}
		}
		sort(a, a + 5);
		cout << poker(a[0], a[1], a[2], a[3], a[4]) << endl;
	}
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#define INF 1000000
void cWarshall_Floyd();
void tWarshall_Floyd();
int glid_cost[101][101];
int glid_tim[101][101];
int n,m,a,b,tim,cost,qs,p,q,r;
using namespace std;
void cWarshall_Floyd()
{
	int i,j,k;
	for(k=0;k<m;k++) {
		for(i=0;i<m;i++) {
			for(j=0;j<m;j++) {
				glid_cost[i][j]=min(glid_cost[i][j],glid_cost[i][k]+glid_cost[k][j]);
			}
		}
	}
}
void tWarshall_Floyd()
{
	int i,j,k;
	for(k=0;k<m;k++) {
		for(i=0;i<m;i++) {
			for(j=0;j<m;j++) {
				glid_tim[i][j]=min(glid_tim[i][j],glid_tim[i][k]+glid_tim[k][j]);
			}
		}
	}
}
int main(void)
{
	ios::sync_with_stdio(false);
	int i,j,k;
	while(1) {
		cin>>n>>m;
		if(n==0&&m==0) break;
		for(i=0;i<m;i++) {
			for(j=0;j<m;j++) {
				glid_cost[i][j]=INF;
				glid_tim[i][j]=INF;
			}
		}
		for(i=0;i<m;i++) {
			glid_cost[i][i]=0;
			glid_tim[i][j]=0;
		}
		for(i=0;i<n;i++) {
			cin>>a>>b>>cost>>tim;
			a--;b--;
			glid_cost[a][b]=cost;
			glid_cost[b][a]=cost;
			glid_tim[a][b]=tim;
			glid_tim[b][a]=tim;
		}
		cWarshall_Floyd();
		tWarshall_Floyd();
		cin>>qs;
		for(i=0;i<qs;i++) {
			cin>>p>>q>>r;
			p--;q--;
			if(r==0) cout<<glid_cost[p][q]<<endl;
			else cout<<glid_tim[p][q]<<endl;
		}
	}
	return 0;
}

#include <iostream>
using namespace std;

int main(){
	int n,q;
	bool x[50][100];
	while(cin >> n >> q,n||q){
		for(int i = 0; i < n; i++){
			for(int j = 0; j < 100; j++) x[i][j] = false;
		}
		for(int i = 0; i < n; i++){
			int m;
			cin >> m;
			for(int j = 0; j < m; j++){
				int date;
				cin >> date;
				x[i][date] = true;
			}
		}
		bool ok = true;
		int sum = -1, ans;
		for(int i = 1; i < 100; i++){
			int cnt = 0;
			for(int j = 0; j < n; j++){
				if(x[j][i]) cnt++;
			}
			if(cnt > sum){
				sum = cnt;
				ans = i;
			}
		}
		if(sum >= q) cout << ans << endl;
		else cout << 0 << endl;
	}
}


#include <bits/stdc++.h>
using namespace std;

int main() {

  int n;
  cin >> n;
  long total = 0;

  for (int i = 1; i <= n; i++){
    if(i % 5 != 0 && i % 3 != 0){
      total = total + i;
    }
  }

  cout << total << endl;

}

#include<bits/stdc++.h>

#define ll long long
#define fornum(A,B,C) for(A=B;A<C;++A)
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll,ll>

using namespace std;

/////////////////////////////////////////////////////
ll N;
ll i, j, k;
char S[1010][1010];

const char mk[10][20][20]={
    {},{},{},{},
    {
        "aacd",
        "bbcd",
        "cdaa",
        "cdbb"
    },
    {
        "aabba",
        "b..ca",
        "b..cb",
        "acc.b",
        "abbaa"
    },
    {
        "aabbaa",
        "c.c.c.",
        "c.c.c.",
        ".c.c.c",
        ".c.c.c",
        "aabbaa"
    },
    {
        ".aabbaa",
        "a.cc.cc",
        "ac..c..",
        "bc..c..",
        "b.cc.cc",
        "ac..c..",
        "ac..c.."
    },
};

void crS(ll a,ll p){
    ll i,j;
    fornum(i,0,a){
        fornum(j,0,a){
            S[p + i][p + j] = mk[a][i][j];
        }
    }
}
int main(){
    scanf("%lld", &N);
    if(N==2){
        printf("-1");
        return 0;
    }
    if(N==3){
        printf("aa.\n");
        printf("..a\n");
        printf("..a\n");
        return 0;
    }
    i = N % 4 + 4;
    crS(i, 0);
    while(i<N){
        crS(4, i);
        i+= 4;
    }
    fornum(i,0,N){
        fornum(j,0,N){
            printf("%c", S[i][j] ? S[i][j] : '.');
        }
        printf("\n");
    }

    return 0;
}


#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <complex>
#include <ctime>
#include <cstdlib>

using namespace std;

inline int to_int(string s) {int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string to_str(T x) {ostringstream sout; sout << x; return sout.str();}

typedef long long ll;

int main()
{
	string s;
	map<char, int> ex;
	ex['I'] = 1; ex['V'] = 5; ex['X'] = 10; ex['L'] = 50;
	ex['C'] = 100; ex['D'] = 500; ex['M'] = 1000;

	while(cin >> s)
	{
		int sum = 0;
		for(int i = 0; i < s.size()-1; i++)
		{
			char now = s[i], next = s[i+1];

			if(ex[now] < ex[next])
			{
				sum -= ex[now];
			}
			else
			{
				sum += ex[now];
			}
		}
		sum += ex[s[s.size()-1]];
		cout << sum << endl;
	}
}


#include <bits/stdc++.h>
using namespace std;

//repetition
#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define rep(i, n) for(ll i = 0; i < (ll)(n); i++)

//container util
#define all(x) (x).begin(),(x).end()

//typedef
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VLL;
typedef vector<VLL> VVLL;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;

//const value
//const ll MOD = 1e9 + 7;
//const int dx[] = {0,1,0,-1};//{0,0,1,1,1,-1,-1,-1};
//const int dy[] = {1,0,-1,0};//{1,-1,0,1,-1,0,1,-1};

//conversion
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
inline ll toLL(string s) {ll v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

int n,m;
const int maxM = 202020;
const int INF = 1e9;
vector<PII> edge[3*maxM];
map<PII, int> sid;
int lastId = 0;

// 頂点と色を引数に持って新しく追加する頂点との間にエッジを張る
// 今まで出てきた端点と同じものがあったら同じグループに追加する
int getid(int v, int c){
  PII p(v,c);
  if( sid.find(p) !=  sid.end()) return sid[p];
  return sid[p] = lastId++;
}

void addEdge(int a, int b, int c){
  // 同じ辺の要素はコスト0の辺を貼ったのでそれぞれの端点で独立に考える
  int v = getid(a,c), u = getid(b,c);
  edge[v].emplace_back(u,0);
  edge[u].emplace_back(v,0);
}

void dijkstra(){
  int dist[n + lastId];
  rep(i,n+lastId) dist[i] = INF;
  dist[lastId] = 0;
  priority_queue< PII, vector<PII>, greater<PII>> que;
  que.push({0,lastId});
  while(que.empty() == false){
    PII p = que.top(); que.pop();
    int cdist = p.first, current = p.second;

    if(dist[current] < cdist) continue;

    for(PII e: edge[current]){
      if(dist[e.first] > cdist + e.second){
          que.push({cdist + e.second,e.first});
          dist[e.first] = e.second + cdist;
      }
    }
  }

  if(dist[lastId+n-1] == INF){
    cout << -1 << endl;
    return;
  }else cout << dist[lastId+n-1]/2 << endl;

}

int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;

  int a[m],b[m],c[m];

  rep(i,m){
    cin >> a[i] >> b[i] >> c[i];
    a[i]--,b[i]--;
    addEdge(a[i],b[i],c[i]);
  }

  // 作成した頂点と入力の辺の端点からコスト1の辺を張る
  rep(i,m){
    edge[getid(a[i], c[i])].emplace_back(lastId+a[i], 1);
    edge[getid(b[i], c[i])].emplace_back(lastId+b[i], 1);
    edge[lastId+a[i]].emplace_back(getid(a[i], c[i]), 1);
    edge[lastId+b[i]].emplace_back(getid(b[i], c[i]), 1);
  }

  dijkstra();
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)

int main() {
  int n, q;
  cin >> n >> q;
  vector<tuple<int, int, int>> t;
  rep(i, n) {
    int l, r, x;
    cin >> l >> r >> x;
    l -= x;
    r -= x;
    t.push_back(make_tuple(l, 1, x));
    t.push_back(make_tuple(r, 0, x));
  }
  rep(i, q) {
    int d;
    cin >> d;
    t.push_back(make_tuple(d, 2, 0));
  }
  sort(all(t));
  set<int> st;
  for(auto v : t) {
    int l, b, x;
    tie(l, b, x) = v;
    if(b == 2)
      cout << (st.empty() ? -1 : *st.begin()) << endl;
    else if(b == 1)
      st.insert(x);
    else
      st.erase(x);
  }
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<ll, ll> Pll;
typedef pair<ll, int> Pli;

#define fi first
#define se second

const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll MOD3 = 1812447359;
const ll INF = 1ll << 62;
const double PI = 2 * asin(1);

void yes() {printf("yes\n");}
void no() {printf("no\n");}
void Yes() {printf("Yes\n");}
void No() {printf("No\n");}
void YES() {printf("YES\n");}
void NO() {printf("NO\n");}

int N, A[int(2e5+5)], now[int(2e5+5)];
vector <int> ans;

int main(){
  cin >> N;
  for (int i = 1; i <= N; i++) cin >> A[i];

  for (int i = N; i >= 1; i--){
    if (now[i] % 2 == A[i]) continue;
    ans.push_back(i);
    for (int j = 1; j * j <= i; j++){
      if (i % j != 0) continue;
      now[j]++;
      if (j * j != i) now[i/j]++;
    }
  }

  if (now[1] % 2 != A[1]){
    cout << -1 << endl;
    return 0;
  }

  int M = ans.size();
  cout << M << endl;
  if (M > 0){
    cout << ans[0];
    for (int i = 1; i < M; i++) cout << " " << ans[i];
    cout << endl;
  }

  return 0;
}

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define Sort(Array) sort(Array.begin(), Array.end())
#define Reverse(a) reverse(a.begin(), a.end())
#define out(ans) cout << ans << endl;
const int MOD = 1000000007;
const int INF = 2147483647;
const ld PI = 3.14159265358979323846;

//------------↓------------- M -------------- E ---------------- M --------------- O ---------------↓--------------//
// コンパイル
// g++ -std=c++1z
//
// -------型変換--------
// int を string に変換
// string s = to_string(n);
// string を int に変換
// int n = stoi(s);
//
// -------二分探索---------
// k以上の値が最初に現れる時のイテレータ
// lower_bound(data.begin(), data.end(), k)
// kより大きい値が最初の現れる時のイテレータ O(logN)
// upper_bound(data.begin(), data.end(), k)
// kがdataに存在するかをtrue or falseで返す O(logN)
// binary_search(data.begin(), data.end(), k)
//
//
//
//
//
//
//
//------------↑------------- M -------------- E ---------------- M --------------- O ---------------↑--------------//

int main() {

    int N; cin >> N;
    int a[N+1];
    for (int i = 1; i <= N; ++i) cin >> a[i];

    int b[N+1]; rep(i,N+1) b[i] = -1;
    vector<int> ans;
    for (int i = N; i >= 1; --i) {
        int j = 2;
        int sum = 0;
        while (i*j <= N) {
            sum += b[i*j];
            ++j;
        }
        b[i] = (a[i] + sum) % 2;
        if (b[i] == 1) ans.push_back(i);
    }

    cout << ans.size() << endl;
    rep(i,ans.size()) cout << ans[i] << " ";
    cout << endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(ll i=0;i<(ll)n;i++)

int main(){
    string s; cin >> s;
    rep(i,3){
        if(s[i] == '7'){
            cout << "Yes" << endl;
            return 0;
        }
    }
    cout << "No" << endl;
}

#include<bits/stdc++.h>
using namespace std;
///Welcome to Nasif's Code
#define bug printf("bug\n");
#define bug2(var) cout<<#var<<" "<<var<<endl;
#define co(q) cout<<q<<endl;
#define all(q) (q).begin(),(q).end()
typedef long long int ll;
typedef unsigned long long int ull;
const int MOD = (int)1e9+7;
const int MAX = 1e6;
#define pi acos(-1)
#define inf 1000000000000000LL
#define FastRead    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int main()
{
    FastRead
    //freopen("output.txt", "w", stdout);
    int ans=INT_MAX;
    string s;
    cin>>s;
    for(int i=0; i<=s.length()-3; i++)
    {
        int val=0;
        for(int j=0; j<3; j++)
        {
            val*=10;
            val+=(s[j+i]-'0');
        }
        ans=min(ans,abs(753-val));
    }
    cout<<ans<<endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main(){
  int N,M,X,Y;
  cin>>N>>M>>X>>Y;
  int k=X,l=Y;
  for(int i=0;i<N;i++){
    int j=0;
    cin>>j;
    k=max(k,j);
  }
  for(int i=0;i<M;i++){
    int j=0;
    cin>>j;
    l=min(l,j);
  }
  if(k<l)
    cout<<"No War"<<endl;
  else
    cout<<"War"<<endl;
}

#include <bits/stdc++.h>

#define f(i,a,b) for( ll i = a; i < b ; i++ )
#define af(i,a,b) for( ll i = a; i >= b ; i--)
#define rep(i,a,b,k) for(ll i = a; i < b ; i+= k )
#define arep(i,a,b,k) for( ll i = a; i >= b ; i-= k)
#define ones(x) (ll) __builtin_popcount(x)
#define fs first
#define sc second
#define pb push_back
#define po pop_back
#define mp make_pair
#define sz(a) (ll) a.size()
#define all(a) a.begin(), a.end()
#define sor(a) sort( a.begin(), a.end() )
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL)
#define ller ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr)
#define yes cout<<"Yes\n"
#define no cout<<"No\n"

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> ii ;
typedef vector<ll>  vi ;
typedef vector<ii> vii ;

const ll MAX = 10000000;
const ll inf = 5e18-1;

int main() {
	fastio;
	ll h,w,a,b;
	cin >> h >> w >> a >> b;
	f(i,0,h){
		f(j,0,w){
			ll x = ((i < b)^(j < a));
			cout << x;
		}
		cout << '\n';
	}
	return 0;
	}


#include <cstdio>
using ll = long long;
inline ll gcd(ll x, ll y)
{
	while (y)
	{
		auto r = x % y;
		x = y;
		y = r;
	}
	return x;
}
inline bool chk(int n)
{
	for (int i = 2; i * i <= n; i++)
	{
		if (!(n % i))
			return false;
	}
	return true;
}
inline ll lcm(ll x, ll y) { return x && y ? x / gcd(x, y) * y : x + y; }
ll pri[1005], arr[505][505];
int main()
{
	// freopen("AGC027-D.in", "r", stdin);
	int n, cur = 0, len = 0;
	scanf("%d", &n);
	bool even = n & 1 ^ 1;
	n += even;
	for (int i = 2; len <= n * 2; i++)
	{
		if (chk(i))
			pri[len++] = i;
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
			arr[i][j] = 1;
	}
	for (int i = 0; i < n; i += 2, cur++)
	{
		int x = i, y = 0;
		while (x < n && y < n)
			arr[x++][y++] *= pri[cur];
	}
	for (int i = 2; i < n; i += 2, cur++)
	{
		int x = 0, y = i;
		while (x < n && y < n)
			arr[x++][y++] *= pri[cur];
	}
	cur = len - 1;
	for (int i = 0; i < n; i += 2, cur--)
	{
		int x = i, y = 0;
		while (x >= 0 && y < n)
			arr[x--][y++] *= pri[cur];
	}
	for (int i = 2; i < n; i += 2, cur--)
	{
		int x = n - 1, y = i;
		while (x >= 0 && y < n)
			arr[x--][y++] *= pri[cur];
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = i & 1 ^ 1; j < n; j += 2)
			arr[i][j] = lcm(lcm(i ? arr[i - 1][j] : 0, j ? arr[i][j - 1] : 0),
							lcm(arr[i + 1][j], arr[i][j + 1])) + 1;
	}
	n -= even;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
			printf("%lld%c", arr[i][j], " \n"[j + 1 == n]);
	}
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
using pdd = pair<double, double>;

int main() {
    int N; cin >> N;
    vector<pdd> points(N);
    for (int i = 0; i < N; i++) {
        double x, y; cin >> x >> y;
        points[i] = make_pair(x, y);
    }
    vector<vector<double>> dist(N, vector<double>(N));
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            dist[i][j] = sqrt(pow(points[i].first - points[j].first, 2) + pow(points[i].second - points[j].second, 2));
        }
    }
    vector<int> perm(N);
    for (int i = 0; i < N; i++) perm[i] = i;
    double ans = 0;
    double n_fact = 1.0;
    for (int i = 1; i <= N; i++) n_fact *= i;
    do {
        double pathlen = 0;
        for (int i = 0; i < N - 1; i++) pathlen += dist[perm[i]][perm[i+1]];
        ans += pathlen / n_fact;
    } while (next_permutation(perm.begin(), perm.end()));
    cout << fixed << setprecision(10) << ans << endl;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
    int n;
    cin >> n;
    vector<long> v;
    for (int i = 0; i < n; ++i) {
        long x;
        cin >> x;
        v.push_back(x);
    }
    cout << *min_element(v.begin(), v.end()) << " ";
    cout << *max_element(v.begin(), v.end()) << " ";
    cout << accumulate(v.begin(), v.end(), 0L) << "\n";
}

#include <iostream>
using namespace std;
int main(void){
    long long n,a,min=1000000,max=-1000000,s=0;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>a;
        if(a>max)max=a;
        if(a<min)min=a;
        s=s+a;
    }
    cout<<min<<" "<<max<<" "<<s<<endl;
}


/*
    飲んだ魔剤で家が建つ。
    created at: 2020-04-19 14:53:28
                2020-04-19 16:16:46 余計な部分を削った版
*/

#include <bits/stdc++.h>
using namespace std;

#define all(x) (x).begin(),(x).end()
#define INF 1000000000

bool compare_by_b(pair<int, int> a, pair<int, int> b)
{
    if (a.second != b.second) {
        return a.second < b.second;
    } else {
        return a.first < b.first;
    }
}

int main()
{
    int n;
    cin >> n;
    vector<pair<int, int>> red(n), blue(n);
    for (int i = 0; i < n; i++)
        cin >> red[i].first >> red[i].second;
    for (int i = 0; i < n; i++)
        cin >> blue[i].first >> blue[i].second;

    sort(all(red), compare_by_b);
    reverse(all(red));

    sort(all(blue));

    int ans = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            pair<int, int> r = red[j];
            pair<int, int> b = blue[i];

            if (r.first < b.first && r.second < b.second) {
                // cout << r.first << "," << r.second << " " << b.first << "," << b.second << endl;
                ans++;
                red[j].second = INF;
                break;
            }
        }
    }

    cout << ans << endl;

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
#define INF 10000000
#define rep(i,a,b) for(ll (i)=a;(i)<(ll)(b);(i)++)
using ipair = pair<ll,ll>;

//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_7_A&lang=jp

vector<vector<ll>> capacity;
vector<vector<ll>> flow;
vector<ll> pred;

//増加させることができる経路を探す
//可能経路は，predに保存される ex. pred[v] = u ... u->vの経路
bool findPath(ll s, ll t){
    ll N = capacity.size();
    pred.assign(N,-1);

    //幅優先探索
    queue<ll> que; que.push(s);
    while(!que.empty()){
        ll u = que.front(); que.pop();

        //Flowネットワークを調べる
        for(ll v=0;v<N;v++){
            //「経路がある」「先の点が未探索」「増加可能」
            if(capacity[u][v]>0 && pred[v]==-1 && capacity[u][v]>flow[u][v]){
                pred[v] = u;
                if (v== t) return true;
                que.push(v);
            }
        }
        //残余ネットワークを調べる
        for(ll v=0;v<N;v++){
            if(capacity[v][u]>0 && pred[v]==-1 && flow[v][u]>0){
                pred[v] = u;
                que.push(v);
            }
        }
    }
    return false;
}

//ネットワークを更新する．
void update(ll s, ll t){
    ll tmp,u,v,delta = INF;//増加分

    //最小の増加量をtargetから遡って探す
    v = t;
    while(v!=s){
        u = pred[v];
        if(capacity[u][v]>0) tmp = capacity[u][v] -flow[u][v];
        else if (capacity[v][u]>0) tmp = flow[v][u];
        delta = min(delta,tmp);
        v = u;
    }
    //求まった増加量を実際のネットワークに反映させる
    v = t;
    while(v!=s){
        u = pred[v];
        if(capacity[u][v]>0) flow[u][v] += delta;
        else if (capacity[v][u]>0) flow[v][u] -= delta;
        v = u;
    }
}

ll edmonds_karp(ll s, ll t){
    //増加経路を探索し，見つかったら更新する
    while(1){
        if(findPath(s,t)==false) break;
        else update(s,t);
    }
    ll ans = 0;
    for(ll v=0;v<capacity.size();v++) ans += flow[s][v];
    return ans;
}

int main(){
    ll N;
    cin >> N;
    ll nodeNum = N+N+2;
    capacity.assign((nodeNum),vector<ll>((nodeNum),0));
    flow.assign((nodeNum),vector<ll>((nodeNum),0));

    vector<ipair> red(N),blue(N);
    rep(i,0,N) cin >> red[i].first >> red[i].second;
    rep(i,0,N) cin >> blue[i].first >> blue[i].second;
    sort(red.begin(),red.end());
    sort(blue.begin(),blue.end());

    //条件を満たすノードを追加
    rep(i,0,N){//blue
        rep(j,0,N){//red
            if(blue[i].first<=red[j].first) break;
            else if(blue[i].second>red[j].second) capacity[i][N+j]=1;
        }
    }
    //start[2N] -> blue -> red -> end[2N+1]
    rep(i,0,N){
        capacity[nodeNum-2][i] = 1;
        capacity[N+i][nodeNum-1] = 1;
    }

    cout << edmonds_karp(nodeNum-2,nodeNum-1) << endl;
}