#include <bits/stdc++.h>
using namespace std;
long long INF = 1e18;
int main(){
    long long d, g;
    cin >> d >> g;
    vector<long long> p(d), c(d), score(d);
    for(int i = 0; i < d; i++) {
        cin >> p[i] >> c[i];
        score[i] = (i + 1) * 100;
    }
    long long ans = INF;
    for(int bit = 0; bit < (1 << d); bit++){
        long long G = g;
        long long res = 0;
        vector<int> flag(d, 1);
        for(int i = 0; i < d; i++){
            if(bit & (1 << i)) {
                G -= p[i]*score[i] + c[i];
                flag[i] = 0;
                res += p[i];
            }
        }
        int t = -1;
        for(int j = 0; j < d; j++){
            if(flag[j] == 1) t = j;
        }
        if(G > 0 && G <= p[t] * score[t] + c[t]){
            res += min(p[t], (G + score[t] - 1) / score[t]);
            ans = min(res, ans);
        }
        else if(G <= 0) ans = min(res, ans);
    }
    cout << ans << endl;
}

/**
*    author:  souzai32
*    created: 01.08.2020 23:22:01
**/

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

int main() {

    int n,m;
    cin >> n >> m;
    vector<tuple<int,int>> con(m);
    int s,c;
    rep(i,m){
        cin >> s >> c;
        con.at(i)=make_tuple(s,c);
    }
    sort(con.begin(),con.end());

    vector<int> num(4,-1);
    bool a=true;
    int ans=0;

    rep(i,m){
        if(num.at(get<0>(con.at(i)))!=-1 && num.at(get<0>(con.at(i)))!=get<1>(con.at(i))) a=false;
        if(get<0>(con.at(i))==1&&get<1>(con.at(i))==0&&n!=1) a=false;
        num.at(get<0>(con.at(i)))=get<1>(con.at(i));
    }

    int t=1;
    rep(i,n) t*=10;

    if(a){
        for(int i=1; i<=n; i++){
            t/=10;
            if(num.at(i)==-1){
                if(i==1 && n!=1) num.at(i)=1;
                else num.at(i)=0;
            }
            ans+=num.at(i)*t;
        }
        cout << ans << endl;
    }
    else cout << -1 << endl;

    return 0;
}


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

const int N = 3000000;
const int MOD = 998244353;

int fact[N];
int ifact[N];

int C(int n, int k) {
  if (k < 0 || k > n) return 0;
  return 1LL * fact[n] * ifact[k] % MOD * ifact[n-k] % MOD;
}

int M(int n, int k) {
  return C(n + k - 1, k - 1);
}

int n, m;

int main() {
  fact[0] = 1;
  for (int i = 1; i < N; ++i) fact[i] = 1LL * fact[i-1] * i % MOD;
  ifact[N-1] = 347057526;
  for (int i = N-2; i >= 0; --i) ifact[i] = 1LL * ifact[i+1] * (i+1) % MOD;

  scanf("%d %d", &n, &m);

  long long ans = 0;
  for (int odd = 0; odd <= m; ++odd) {
    if (m*3 - odd & 1) continue;

    int nn = m*3 - odd >> 1;

    long long res1 = 1LL * M(nn, n) - 1LL * M(nn - m, n) * n % MOD;
    long long res2 = 1LL * M(nn - m, n - 1) * n % MOD;

    ans = ans + res1 * C(n, odd) % MOD + res2 * C(n-1, odd) % MOD;
  }

  ans %= MOD;
  if (ans < 0) ans += MOD;

  printf("%lld\n", ans);
  return 0;
}

#include <bits/stdc++.h>

#define debug(...) fprintf(stderr, __VA_ARGS__)

#ifndef AT_HOME
#define getchar() IO::myGetchar()
#define putchar(x) IO::myPutchar(x)
#endif

namespace IO {
	static const int IN_BUF = 1 << 23, OUT_BUF = 1 << 23;

	inline char myGetchar() {
		static char buf[IN_BUF], *ps = buf, *pt = buf;
		if (ps == pt) {
			ps = buf, pt = buf + fread(buf, 1, IN_BUF, stdin);
		}
		return ps == pt ? EOF : *ps++;
	}

	template<typename T>
	inline bool read(T &x) {
		bool op = 0;
		char ch = getchar();
		x = 0;
		for (; !isdigit(ch) && ch != EOF; ch = getchar()) {
			op ^= (ch == '-');
		}
		if (ch == EOF) {
			return false;
		}
		for (; isdigit(ch); ch = getchar()) {
			x = x * 10 + (ch ^ '0');
		}
		if (op) {
			x = -x;
		}
		return true;
	}

	inline int readStr(char *s) {
		int n = 0;
		char ch = getchar();
		for (; isspace(ch) && ch != EOF; ch = getchar())
			;
		for (; !isspace(ch) && ch != EOF; ch = getchar()) {
			s[n++] = ch;
		}
		s[n] = '\0';
		return n;
	}

	inline void myPutchar(char x) {
		static char pbuf[OUT_BUF], *pp = pbuf;
		struct _flusher {
			~_flusher() {
				fwrite(pbuf, 1, pp - pbuf, stdout);
			}
		};
		static _flusher outputFlusher;
		if (pp == pbuf + OUT_BUF) {
			fwrite(pbuf, 1, OUT_BUF, stdout);
			pp = pbuf;
		}
		*pp++ = x;
	}

	template<typename T>
	inline void print_(T x) {
		if (x == 0) {
			putchar('0');
			return;
		}
		static int num[40];
		if (x < 0) {
			putchar('-');
			x = -x;
		}
		for (*num = 0; x; x /= 10) {
			num[++*num] = x % 10;
		}
		while (*num){
			putchar(num[*num] ^ '0');
			--*num;
		}
	}

	template<typename T>
	inline void print(T x, char ch = '\n') {
		print_(x);
		putchar(ch);
	}

	inline void printStr_(const char *s, int n = -1) {
		if (n == -1) {
			n = strlen(s);
		}
		for (int i = 0; i < n; ++i) {
			putchar(s[i]);
		}
	}

	inline void printStr(const char *s, int n = -1, char ch = '\n') {
		printStr_(s, n);
		putchar(ch);
	}
}
using namespace IO;

const int N = 2500005, P = 998244353;

int n, m;
int fac[N], inv[N];

int qpow(int a, int b) {
	int s = 1;
	for (; b; b >>= 1) {
		if (b & 1) {
			s = 1ll * s * a % P;
		}
		a = 1ll * a * a % P;
	}
	return s;
}

void init(int n) {
	fac[0] = 1;
	for (int i = 1; i <= n; ++i) {
		fac[i] = 1ll * fac[i - 1] * i % P;
	}
	inv[n] = qpow(fac[n], P - 2);
	for (int i = n; i; --i) {
		inv[i - 1] = 1ll * inv[i] * i % P;
	}
}

int C(int n, int m) {
	if (m < 0 || m > n) {
		return 0;
	}
	return 1ll * fac[n] * inv[m] % P * inv[n - m] % P;
}

int main() {
	read(n), read(m);
	init(n + 3 * m);
	int ans = 0;
	for (int i = m & 1; i <= n && i <= m; i += 2) {
		int S = (3 * m - i) >> 1, s = 0;
		for (int j = 0; j <= i && m * j <= S; ++j) {
			for (int k = 0; k <= n - i && m * j + (m + 1) * k <= S; ++k) {
				int t = 1ll * C(i, j) * C(n - i, k) % P * C(S - m * j - (m + 1) * k + n - 1, n - 1) % P;
				if ((j + k) & 1) {
					s = (s + P - t) % P;
				} else {
					s = (s + t) % P;
				}
			}
		}
		ans = (ans + 1ll * s * C(n, i)) % P;
	}
	print(ans);
}


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

#include <iostream>
#include <string>
#include <map>
using namespace std;

long long A[100002] = {0};
long long  S[100002] = {0};
map<long long, long long> m;

int main(int argc, char* argv[]){
	int N;
	long long M; //N 1.. 100000 M 2..1000000000

	cin >> N >> M ;

	A[0] = 0, S[0] = 0;
	for(int i=1; i<=N; i++){
		cin >> A[i];
		S[i] = ( S[i-1]+A[i] ) % M;
		if(m.count(S[i])==0){
			m[S[i]] = 1;
		}
		else{
			long long r = m[S[i]];
			m[S[i]] = r+1;
		}
	}

	unsigned long long ans = 0;
	for(auto it=m.begin(); it!=m.end(); ++it){
		long long k, v;
		k = it->first;
		v = it->second;
		if(k==0){
			ans = ans + v + (v*(v-1))/2; //重複OK
		}
		else{
			ans = ans + (v*(v-1))/2;
		}
	}
	printf("%llu\n", ans);
	return 0;
}

#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main(){
  int N,M;
  cin>>N>>M;
  map<int,int> mp;
  mp[0]=1;
  int n=0;
  while(N--){
    int a;
    cin>>a;
    n=(n+a)%M;
    mp[n]++;
  }
  int ans=0;
  for(pair<int,int> p:mp)ans+=p.second*(p.second-1)/2;
  cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll,ll> pll;
const ll mod=1e9+7;
//const ll mod=998244353;
const ll inf=1LL<<61;

int main() {
  ll n;
  cin >> n;
  vec x(n),y(n);
  for(ll i=0;i<n;i++) cin >> x[i] >> y[i];
  for(ll i=0;i<n;i++) {
    vector<double> d;
    for(ll j=0;j<n;j++) {
      if(i==j) continue;
      d.push_back(atan2(y[j]-y[i],x[j]-x[i]));
    }
    sort(d.begin(),d.end());
    double r=0;
    ll k=d.size();
    for(ll j=0;j<k;j++) {
      d.push_back(d[j]+2*M_PI);
    }
    for(ll j=0;j<k;j++) {
      r=max(r,M_PI-(d[j+k]-d[j+1]));
    }
    cout << fixed << setprecision(10) << r/(2*M_PI) << endl;
  }
}

#include<bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long LL;
typedef pair<LL,LL> P;
const LL mod=1000000007;
const LL LINF=1LL<<60;
const int INF=1<<30;

using Real = double;
using Point = complex< Real >;
const Real EPS = 1e-8, PI = acos(-1);
using Polygon = vector< Point >;

namespace std {
  bool operator<(const Point &a, const Point &b) {
    return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag();
  }
}

inline bool eq(Real a, Real b) { return fabs(b - a) < EPS; }
Real cross(const Point &a, const Point &b) {
  return real(a) * imag(b) - imag(a) * real(b);
}

Real dot(const Point &a, const Point &b) {
  return real(a) * real(b) + imag(a) * imag(b);
}

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C
// 点の回転方向
int ccw(const Point &a, Point b, Point c) {
  b = b - a, c = c - a;
  if(cross(b, c) > EPS) return +1;  // "COUNTER_CLOCKWISE"
  if(cross(b, c) < -EPS) return -1; // "CLOCKWISE"
  if(dot(b, c) < 0) return +2;      // "ONLINE_BACK"
  if(norm(b) < norm(c)) return -2;  // "ONLINE_FRONT"
  return 0;                         // "ON_SEGMENT"
}

Point operator*(const Point &p, const Real &d) {
  return Point(real(p) * d, imag(p) * d);
}

istream &operator>>(istream &is, Point &p) {
  Real a, b;
  is >> a >> b;
  p = Point(a, b);
  return is;
}

ostream &operator<<(ostream &os, Point &p) {
  os << fixed << setprecision(10) << p.real() << " " << p.imag();
}

Polygon convex_hull(Polygon &p) {
  int n = (int) p.size(), k = 0;
  if(n <= 2) return p;
  sort(p.begin(), p.end());
  vector< Point > ch(2 * n);
  for(int i = 0; i < n; ch[k++] = p[i++]) {
    while(k >= 2 && cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < EPS) --k;
  }
  for(int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--]) {
    while(k >= t && cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < EPS) --k;
  }
  ch.resize(k - 1);
  return ch;
}

int main(){
    int n;
    cin >> n;
    Polygon v(n);
    map<Point,int> ma;
    for (int i = 0; i < n; i++) {
        cin >> v[i];
        ma[v[i]] = i;
    }
    auto p = convex_hull(v);
    vector<double> ans(n,0.0);
    if(p.size()==2){
        for (int i = 0; i < p.size(); i++) {
            ans[ma[p[i]]] = 1./2;
        }
    }
    else{
        for (int i = 0; i < p.size(); i++) {
            Point a = p[(i+1)%p.size()] - p[i];
            Point b = p[(i-1+p.size())%p.size()] - p[i];
            double theta = acos(dot(a,b) / abs(a) / abs(b));
            ans[ma[p[i]]] = (PI - theta) / 2 / PI;
        }
    }
    for (int i = 0; i < n; i++) {
        cout << setprecision(30) << ans[i] << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll =long long;
typedef pair<int,int> P;
#define SORT(a) sort((a).begin(),(a).end())
#define rSORT(a) reverse((a).begin(),(a).end())
#define For(i, a, b)    for(int i = (a) ; i < (b) ; ++i)
#define rep(i, n)       For(i, 0, n)
#define debug(x)  cerr << #x << " = " << (x) << endl;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
//Write From this Line

int main()
{
	string s;
	cin >> s;
	cout << s.substr(0,(s.size() - 8)) << endl;
}

#include <stdio.h>
int main (){
	long long int a;
	long long int hasil=0;
	scanf("%lld",&a);
	for (int i=1 ; i<=a ; i++){

	if (i%3==0 && i%5==0){
//		puts("Fizzbuzz");
	}
	else if (i%3==0){
//		puts ("Fizz");
	}
	else if (i%5==0){
//		puts("Buzz");
	}
	else{

	hasil=(hasil+i);

	}

	}
	printf("%lld",hasil);
	return 0;
}


/*
DISCLAIMER: I don't expect this to AC first submit, but it'd be pretty great if it did...
 -> I'm rusty af
*/

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp> //gp_hash_table

#define all(a) a.begin(), a.end()
#define pb push_back
#define eb emplace_back
#define sz(a) (int) a.size()
#define bitcount(a) __builtin_popcount(a)
#define bitcountll(a) __builtin_popcountll(a)

using namespace std;
// using namespace __gnu_pbds;

typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;

void solve() {
    const ll oo = 1e18;
    int n; cin >> n;
    vector<pii> a(n);
    for(auto& p : a) cin >> p.first >> p.second;
    sort(all(a), [](const pii& x, const pii& y) {
        int l = x.first - y.second;
        int r = y.first - x.second;
        return l < r;
    });
    a.insert(a.begin(), {-1, -1});
    vector<vector<ll>> dp(n + 1, vector<ll>(n + 1, oo));
    int ans = 0;
    dp[0][0] = 0;
    for(int i = 1; i <= n; ++i) {
        dp[i][0] = 0;
        for(int x = 1; x <= i; ++x) {
            dp[i][x] = dp[i - 1][x];
            if(dp[i - 1][x - 1] <= a[i].first) {
                dp[i][x] = min(dp[i][x], dp[i - 1][x - 1] + a[i].second);
            }
            if(dp[i][x] < oo) {
                ans = max(ans, x);
            }
        }
    }
    cout << ans << endl;
}

int main() {
    // freopen("input.in", "r", stdin);
    // freopen("input.out", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.precision(20);
    cout << fixed;
    // int t; cin >> t; while(t--)
    solve();
    cout.flush();
    return 0;
}


#include <bits/stdc++.h>

#define F first
#define S second
#define PB push_back

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
typedef priority_queue<int> HEAP;
typedef priority_queue<int, vector<int>, greater<int> > RHEAP;

const int N = 100010, M = 1010;

int n;
int h[N], e[N], ne[N], idx;
int match[N];
bool st[N];
PII p[N];

void add(int a, int b)
{
	e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
}

bool find(int u)
{
	for (int i = h[u]; ~i; i = ne[i])
	{
		int j = e[i];
		if (!st[j])
		{
			st[j] = true;
			if (match[j] == 0 || find(match[j]))
			{
				match[j] = u;
				return true;
			}
		}
	}
	return false;
}

int main()
{
	memset(h, -1, sizeof h);
	scanf("%d", &n);
	for (int i = 1; i <= n; i ++ ) scanf("%d%d", &p[i].F, &p[i].S);
	for (int i = n + 1; i <= 2 * n; i ++ ) scanf("%d%d", &p[i].F, &p[i].S);

	for (int i = 1; i <= n; i ++ )
		for (int j = n + 1; j <= 2 * n; j ++ )
			if (p[i].F < p[j].F && p[i].S < p[j].S)
				add(i, j);

	int res = 0;
	for (int i = 1; i <= n; i ++ )
	{
		memset(st, 0, sizeof st);
		if (find(i)) res ++ ;
	}
	printf("%d\n", res);
    return 0;
}

#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<cstring>
#include<vector>
#include<list>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define rep(i, n) for(int i=0;i<n;++i)
#define rrep(i, n) for(int i=n;i>=0;--i)
const int inf=1e9+7;
const ll mod=1e9+7;
const ll mod1=998244353;
const ll big=1e18;
const double PI=2*asin(1);

int main() {
  int M;
  cin>>M;
  ll d, c;
  ll ans = 0;
  ll sum = 0;
  for(int i=0;i<M;++i) {
    cin>>d>>c;
    ans += c;
    sum += d*c;
  }
  ans--;
  if(sum%9==0) ans += sum/9-1;
  else ans += sum/9;
  cout<<ans<<endl;
}


#include <iostream>
#include <string.h>
#include <vector>
#include <algorithm>
#include <set>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main() {
 ios::sync_with_stdio(false);
    cin.tie(0);

    int n, k;
    cin >> n >> k;
    set<int> s;
    for (int i = 0; i < k; i++) {
        int x;
        cin >> x;
        s.insert(x);
    }
    int a = n;
    while (1) {
        int c = a;
        bool ok = 1;
        while (c > 0) {
            int di = c % 10;
            if (s.count(di)) {
                ok = 0;
                break;
            }
            c /= 10;
        }
        if (ok) {
            cout << a << endl;
            break;
        }
        a++;
    }
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define vlli vector<ll>
#define vi vector<int>
#define vs vector<string>
#define vplli vector< pair< ll,ll> >
#define plli pair< ll,ll >
#define vps vector< pair< string, string> >
#define vpi vector< pair< int, int> >
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define forn(i,a,n) for(ll i=a;i<n;i++)
#define forr(i,n,a) for(ll i=n-1;i>=a;i--)
#define scan(arr,a,n) for(int i=(a);i<(n);i++)cin>>(arr)[i];
#define print(arr,a,n) for(int i=(a);i<(n);i++)cout<<(arr)[i]<<' ';

const ll inf = 1e18;

ll add(ll x, ll y) {ll res = x + y; return (res >= mod ? res - mod : res);}
ll mul(ll x, ll y) {ll res = x * y; return (res >= mod ? res % mod : res);}
ll sub(ll x, ll y) {ll res = x - y; return (res < 0 ? res + mod : res);}
ll power(ll x, ll y) {ll res = 1; x %= mod; while (y) {if (y & 1)res = mul(res, x); y >>= 1; x = mul(x, x);} return res;}
ll mod_inv(ll x) {return power(x, mod - 2);}

int main(){
	fast;
	ll n, k;
	cin>>n>>k;
	ll d;
	map<ll, ll> m;
	forn(i, 0, k){
		cin>>d;
		m[d]++;
	}
	ll ans;
	forn(i, n, n+100000){
		ll x = i, f = 1;
		while(x){
			if(m[x%10]>0){
				f = 0;
				break;
			}
			x/=10;
		}
		if(f == 1){
			ans = i;
			break;
		}
	}
	cout<<ans<<endl;
}


#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
#include <cctype>

#define mp make_pair
#define pb push_back
#define REP(i,a,n) for(int i = a;i < (n);i++)
#define rep(i,n) for(int i = 0;i < (n);i++)
#define all(s) s.begin(), s.end()
#define rall(s) s.rbegin(), s.rend()
#define range(x,min,max) ((min) <= (x) && (x) <= (max))
#define xyrange(x, minX, maxX, y, minY, maxY) (range(x, minX, maxX) && range(y, minY, maxY))

using namespace std;

typedef long long LL;
typedef vector<int> VI;
typedef vector<vector<int> > VVI;
typedef vector<string> VS;
typedef vector<bool> VB;
typedef vector<vector<bool>> VVB;
typedef pair<int,int> PII;
typedef pair<char,int> PCI;

const int DX[]={1,0,-1,0},DY[]={0,-1,0,1};

#define process first
#define movecnt second

int N, M;
VVB area;
vector<PCI> ms;
int posx, posy;

bool check () {
	rep (i, area.size()) {
		rep (j, area[i].size()) {
			if (area[i][j]) return false;
		}
	}
	return true;
}

void move (int movex, int movey, int cnt) {
	rep (i, cnt) {
		if (xyrange(posx, 0, 20, posy, 0, 20)) {
			posx += movex;
			posy += movey;
			area[posy][posx] = false;
		}
	}
}

bool solve () {
	posx = posy = 10;
	area[posy][posx] = false;
	for (auto &m : ms) {
		switch (m.process) {
			case 'N':
				move(0, 1, m.movecnt);
				break;
			case 'S':
				move(0, -1, m.movecnt);
				break;
			case 'E':
				move(1, 0, m.movecnt);
				break;
			case 'W':
				move(-1, 0, m.movecnt);
				break;

		}
	}
	return check();
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	while (cin >> N, N) {
		area.clear();
		area.resize(21, VB(21, false));
		ms.clear();
		rep (i, N) {
			int x, y;
			cin >> x >> y;
			area[y][x] = true;
		}
		cin >> M;
		rep (i, M) {
			PCI m;
			cin >> m.process >> m.movecnt;
			ms.pb(m);
		}

		cout << (solve()? "Yes" : "No") << endl;
	}
	return 0;
}


// ALDS1_12_A.cpp
// Graph II - Minimum Spanning Tree
#include <iostream>
using namespace std;

const int maxN = 100;
const int INF = 2001;
int w[maxN][maxN];
int color[maxN];
int n;
static const int WHITE = 0;
static const int BLACK = 1;

int prim(int x)
{
    int res = 0;
    int cnt = 1;
    color[x] = BLACK;
    while (cnt++ != n) {
        int minCost = INF;
        int pos = 0; // ??°?????°?????????
        for (int i = 0; i < n; i++) {
            if (color[i] != BLACK) continue;
            for (int j = 0; j < n; j++) {
                if (color[j] != WHITE) continue;
                if (w[i][j] < minCost) {
                    pos = j;
                    minCost = w[i][j];
                }
            }
        }
        res += minCost;
        color[pos] = BLACK;
    }
    return res;
}

int main()
{
    cin >> n;
    int num;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> num;
            if (num == -1) w[i][j] = INF;
            else w[i][j] = num;
        }
    }
    cout << prim(0) << endl;
    return 0;
}

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

int P[10010];
void init(int N){
  for (int i=0; i<N; i++) P[i] = i;
}
int root (int a){
  if (P[a] == a) return a;
  else {
    P[a] = root(P[a]);
    return P[a];
  }
}
bool is_same_set(int a, int b){
  return root(a) == root(b);
}
void unite(int a, int b){
  P[root(a)] = root(b);
}

int main(){
  int N;
  cin >> N;
  pair <int, int> edge[10010];
  for (int i=0; i<N; i++){
    for (int j=0; j<N; j++){
      cin >> edge[i*N+j].first;
      edge[i*N+j].second = i*N+j;
    }
  }
  sort(edge, edge+N*N);
  init(N);
  int ans = 0;
  int b = 0;
  for (int k=0; k<N*N; k++){
    while(edge[k].first < 0) k++;
    int i = edge[k].second / N;
    int j = edge[k].second % N;
    if (is_same_set(i,j)){}
    else{
      unite(i,j);
      ans += edge[k].first;
    }
  }
  cout << ans << endl;
}


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

int main()
{
  int n,ans,mn;
  ans=1;
  cin>>n;
  vector<int> a(n);
  for(int i=0;i<n;i++)
  {
    cin>>a.at(i);
  }
  mn=a.at(0);
  for(int i=1;i<n;i++)
  {
    if(mn>a.at(i))
      ans++;
    mn=min(mn,a.at(i));
  }
  cout<<ans<<endl;
}

#include <iostream>
#include <string>

using namespace std;

int main(void){
  string str;
  int n;
  int i=0;
  int ocnt=0, bcnt=0;
  int score=0;
  cin >> n;
  while(i < n){
	cin >> str;
	if(str == "OUT"){
	  ocnt++;
	}else if(str == "HIT"){
	  if(bcnt < 3){
		bcnt++;
	  }else{
		score++;
	  }
	}else if(str == "HOMERUN"){
	  score += bcnt+1;
	  bcnt = 0;
	}
	if(ocnt == 3){
	  cout << score << endl;
	  ocnt = 0;
	  bcnt = 0;
	  score = 0;
	  i++;
	}
  }
}


#include "bits/stdc++.h"

#define rep(i,n) for(int i = 0;i<n;i++)
#define cint(a) int a; cin >> a;
#define cstr(str) string str; cin >> str;
#define vv(Type, n,m,d)vector<vector<Type>> vv(n, vector<Type>(m, d));

using namespace std;
using ll = long long;

const ll mod = 1e9 + 7;

int main(void) {

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

	int n, r; cin >> n >> r;

	if (n >= 10) cout << r << endl;
	else cout << r + 100 * (10 - n) << endl;

	return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const long long INF = 1LL<<60;

ll GCD(ll x,ll y){
    if(y == 0) return x;
    else return GCD(y,x%y);
}

ll LCM(ll a,ll b){
    return a / GCD(a,b) * b;
}

const int MOD = 1000000007;

int main() {
    int a,b; cin >> a >> b;
    if(abs(a-b) <= 1){
        cout << "Yay!";
    }else{
        cout << ":(";
    }
}


# include <bits/stdc++.h>

using namespace std;

#define pii pair<int, int>
#define pll pair<ll, ll>

#define pb push_back
#define eb emplace_back
#define mp make_pair

#define fi first
#define se second

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

const int inf = (int)1e9 + 7;
const int maxn = (int)6e5 + 7;
const int lmaxn = (int)6e6 + 7;
const ll linf = (ll)1e16 + 7;

const ld eps = ld(1e-11);

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

ll gcd(ll a, ll b){
    while(b) a %= b, swap(a, b);
    return a | b;
}

ll lcm(ll a, ll b){
    return a * b / gcd(a, b);
}

string a;

bool ok(string x){
    vector < ll > cnt(27, 0);
    for(ll i : x)
        cnt[i - 'a']++;
    for(ll i : x){
        if(cnt[i - 'a'] > (ll)x.size() / 2)
            return 1;
    }
    return 0;
}

int main(){
  	ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> a;
    for(ll len = 2; len <= min((ll)a.size(), 5ll); ++len){
        for(ll i = 0; i + len - 1 < (ll)a.size(); ++i){
            if(ok(a.substr(i, len))){
                cout << i + 1 << " " << i + len;
                return 0;
            }
        }
    }
    cout << -1 << " " << -1;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,o,n) for(long long i = o;i<n;i++)
#define oneforall ios::sync_with_stdio(false);cin.tie(0);
#define all(v) (v).begin(),(v).end()
#define ini(...) int __VA_ARGS__; in(__VA_ARGS__)
#define inl(...) long long __VA_ARGS__; in(__VA_ARGS__)
#define ins(...) string __VA_ARGS__; in(__VA_ARGS__)
#define int long long
const long long inf=1e18;
void in(){} template <typename T,class... U> void in(T &t,U &...u){ cin >> t; in(u...);}
void out(){cout << "\n";} template <typename T,class... U> void out(const T &t,const U &...u){ cout << t; if(sizeof...(u)) cout << " "; out(u...);}
typedef vector<int> vi;
typedef vector<long long> vl;
typedef long long ll;
typedef vector<pair<long, long > > vpll;
typedef vector<pair<int, int > > vpii;
#define FORR(x,arr) for(auto& x:arr)
#define ZERO(a) memset(a,0,sizeof(a))

//3111111111111111111111111111111

	void solve(){

		ins(morty);
		int tmp = -1;
		int tmp1 = -1;
		FOR(i,0,morty.size()-1){
			if(morty[i] == morty[i+1]){tmp = i+1;tmp1 = i+2;break;}
		}
		FOR(i,0,morty.size()-2){
			if(morty[i] == morty[i+2]){tmp = i+1;tmp1 = i+3;break;}
		}
		out(tmp,tmp1);

	}

int32_t main() {
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall

	solve();

	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<bitset>
#include<string>
#include<stack>
#include<set>
#include<unordered_set>
#include<map>
#include<unordered_map>
#include<cstring>
#include<complex>
#include<cmath>
#include<iomanip>
#include<numeric>
#include<algorithm>
#include<list>
#include<functional>
#include<cassert>
#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define y0 y12
#define y1 y22
#define INF 1987654321
#define PI 3.141592653589793238462643383279502884
#define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
#define fdn(i,a,b,c) for(int (i)=(a);(i)>=(b);(i)-=(c))
#define MEM0(a) memset((a),0,sizeof(a));
#define MEM_1(a) memset((a),-1,sizeof(a));
#define ALL(a) a.begin(),a.end()
#define SYNC ios_base::sync_with_stdio(false);cin.tie(0)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> Pi;
typedef pair<ll, ll> Pll;
typedef pair<double, double> Pd;
typedef vector<int> Vi;
typedef vector<ll> Vll;
typedef vector<double> Vd;
typedef vector<Pi> VPi;
typedef vector<Pll> VPll;
typedef vector<Pd> VPd;
typedef tuple<int, int, int> iii;
typedef tuple<int,int,int,int> iiii;
typedef tuple<ll, ll, ll> LLL;
typedef vector<iii> Viii;
typedef vector<LLL> VLLL;
typedef complex<double> base;
const ll MOD = 998244353;
ll POW(ll a, ll b, ll MMM = MOD) {ll ret=1; for(;b;b>>=1,a=(a*a)%MMM)if(b&1)ret=(ret*a)% MMM; return ret; }
ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
ll lcm(ll a, ll b) { if (a == 0 || b == 0)return a + b; return a*(b / gcd(a, b)); }
int dx[] = { 0,1,0,-1,1,1,-1,-1 }, dy[] = { 1,0,-1,0,1,-1,1,-1 };

char s[2000][2002];
int v[2002][2002],ex[2002][2002],ey[2002][2002];
int main() {
	int n,m,q;
	scanf("%d%d%d",&n,&m,&q);
	fup(i,0,n-1,1)scanf("%s",s[i]);
	fup(i,0,n-1,1)
		fup(j,0,m-1,1)
		v[i+1][j+1]=v[i][j+1]+v[i+1][j]-v[i][j]+(s[i][j]=='1');
	fup(i,0,n-2,1)
		fup(j,0,m-1,1)
		ex[i+1][j+1]=ex[i][j+1]+ex[i+1][j]-ex[i][j]+(s[i][j]=='1' && s[i+1][j]=='1');
	fup(i,0,n-1,1)
		fup(j,0,m-2,1)
		ey[i+1][j+1]=ey[i][j+1]+ey[i+1][j]-ey[i][j]+(s[i][j]=='1' && s[i][j+1]=='1');
	while(q--)
	{
		int x1,y1,x2,y2;
		scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
		int t1=v[x2][y2]-v[x2][y1-1]-v[x1-1][y2]+v[x1-1][y1-1];
		int t2=ex[x2-1][y2]-ex[x2-1][y1-1]-ex[x1-1][y2]+ex[x1-1][y1-1];
		int t3=ey[x2][y2-1]-ey[x2][y1-1]-ey[x1-1][y2-1]+ey[x1-1][y1-1];
		printf("%d\n",t1-t2-t3);
	}
}

#include <cstdio>
#include <iostream>
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define inver(a) power(a, mod - 2)
#define F(x, y) C(x - 1 + y, y)
using namespace std;
inline char nc()
{
    return getchar();
	static char buf[100000], *l = buf, *r = buf;
	return l==r&&(r=(l=buf)+fread(buf,1,100000,stdin),l==r)?EOF:*l++;
}
template<class T> void read(T &x)
{
	x = 0; int f = 1, ch = nc();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=nc();}
	while(ch>='0'&&ch<='9'){x=x*10-'0'+ch;ch=nc();}
	x *= f;
}
typedef long long ll;
const int mod = 998244353;
const int maxn = 2000050;
int n, m;
int fac[maxn];
int fac_inv[maxn];
inline int dec(int x) {return x < 0 ? x + mod : x;}
inline ll C(int x, int y)
{
    return (ll)fac[x] * fac_inv[y] % mod * fac_inv[x - y] % mod;
}
ll power(ll x, ll y)
{
    ll re = 1;
    while(y)
    {
        if(y & 1) re = re * x % mod;
        x = x * x % mod;
        y >>= 1;
    }
    return re;
}
void init(int n)
{
    fac[0] = 1;
    for(int i = 1; i <= n; ++i)
    {
        fac[i] = (ll)fac[i - 1] * i % mod;
    }
    fac_inv[n] = inver(fac[n]);
    for(int i = n; i >= 1; --i)
    {
        fac_inv[i - 1] = (ll)fac_inv[i] * i % mod;
    }
}
int cal(int x, int y)
{
    int re = F(n, x);
    re = dec(re - y * F(n, x - m) % mod);
    if(x > m) re = dec(re - (n - y) * F(n, x - m - 1) % mod);
    return re;
}
int solve()
{
    int an = 0;
    for(int i = 0; i <= min(m, n); ++i)
    {
        if((3 * m - i) & 1) continue;
        an = (an + cal((3 * m - i) >> 1, i) * C(n, i)) % mod;
    }
    return an;
}
int main()
{
    read(n), read(m);
    init(n + (m << 1));
    printf("%d\n", solve());
    return 0;
}


#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(v) v.begin(),v.end()
#define len(x) (ll)(x).length()
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
ll gcd(int a,int b){if(a%b==0){return b;}else{return(gcd(b,a%b));}}
ll lcm(int a,int b){return a*b/gcd(a,b);}
const int INF=1e9;
const ll INFS=1e18;
const int MOD=INF+7;
const int di[] = {-1,0,1,0};
const int dj[] = {0,-1,0,1};
const double PI=acos(-1);

int main() {
  int n;
  cin>>n;
  map<int,int> mp;
  rep(i,n){
    int a;
    cin>>a;
    mp[a]++;
  }
  if(mp.size()==n){
    cout<<"YES"<<endl;
    return 0;
  }
  cout<<"NO"<<endl;
}

#include <bits/stdc++.h>
#define N 1000020
#define ll long long
using namespace std;
inline int read(){
  int x=0,f=1;char ch=getchar();
  while(ch>'9'||ch<'0')ch=='-'&&(f=0)||(ch=getchar());
  while(ch<='9'&&ch>='0')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
  return f?x:-x;
}
char s[N], t[N];
queue<int> q;

int main(int argc, char const *argv[]) {
  int n = read();
  scanf("%s", s + 1);
  scanf("%s", t + 1);

  if (!strcmp(s + 1, t + 1)) {
    return puts("0"), 0;
  }

  int ans = 0;

  for (int i = n, j = n; i; -- i) {
    if (t[i] == t[i - 1]) continue;
    j = min(j, i);
    while (j && s[j] != t[i]) {
      -- j;
    }
    if (!j) {
      return puts("-1"), 0;
    }

    while (!q.empty() && q.front() >= i + q.size()) {
      q.pop();
    }

    q.push(j);

    if (i != j) {
      ans = max(ans, (int) q.size());
    }
  }

  printf("%d\n", ans + 1);

  return 0;
}
/*
7
atcoder
aaaccce
*/


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
typedef struct{
	string name;
	int p,abc,de,f,s,m;
	int sum;
	double KORITU;	//sum(収入) / 時間(abc+de)+付加時間(de*(m-1)
}K;

int main(){
	int n;
	int A,B,C,D,E;
	while(1){
	cin >> n;
	if(n==0)break;
	K data[51];
	K temp;
	for(int i=0;i<n;i++){
		cin >> data[i].name >> data[i].p >> A >> B >> C >> D >> E >> data[i].f >> data[i].s >> data[i].m;
		data[i].abc=A+B+C;
		data[i].de=D+E;

		data[i].sum= data[i].f * data[i].s * data[i].m - data[i].p;
		data[i].KORITU = (double)data[i].sum / (double)(data[i].abc+data[i].de + data[i].de*(data[i].m-1) );
	}
	for(int i=0;i<n-1;i++){
		for(int j=n-1;j>i;j--){
			if(data[j].KORITU > data[j-1].KORITU){
				temp=data[j];
				data[j]=data[j-1];
				data[j-1]=temp;
			}
		}
	}
	for(int i=0;i<n-1;i++){
	for(int j=n-1;j>i;j--){
		if(data[j].KORITU==data[j-1].KORITU&& data[j].name < data[j-1].name){
			temp=data[j];
				data[j]=data[j-1];
				data[j-1]=temp;
		}
	}
	}
	for(int i=0;i<n;i++){
		cout << data[i].name << endl;
	}
	cout << "#" << endl;
	}
}

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

#define each(i,a) for (auto&& i : a)
#define FOR(i,a,b) for (ll i=(a),__last_##i=(b);i<__last_##i;i++)
#define RFOR(i,a,b) for (ll i=(b)-1,__last_##i=(a);i>=__last_##i;i--)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define __GET_MACRO3(_1, _2, _3, NAME, ...) NAME
#define rep(...) __GET_MACRO3(__VA_ARGS__, FOR, REP)(__VA_ARGS__)
#define rrep(...) __GET_MACRO3(__VA_ARGS__, RFOR, RREP)(__VA_ARGS__)
#define pb push_back
#define all(a) (a).begin(),(a).end()
#define chmin(x,v) x = min(x, v)
#define chmax(x,v) x = max(x, v)

const ll linf = 1e18;
const int inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);

template<typename T>
istream& operator>>(istream& is, vector<T>& vec) {
    each(x,vec) is >> x;
    return is;
}
template<typename T>
ostream& operator<<(ostream& os, const vector<T>& vec) {
    rep(i,vec.size()) {
        if (i) os << " ";
        os << vec[i];
    }
    return os;
}
template<typename T>
ostream& operator<<(ostream& os, const vector< vector<T> >& vec) {
    rep(i,vec.size()) {
        if (i) os << endl;
        os << vec[i];
    }
    return os;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll n, a, b;
    while (cin >> n >> a >> b, n || a || b) {
        vector<ll> s(n); cin >> s;
        P ans(-linf, -linf);
        rep(i, n-1) {
            if (a <= i+1 && i+1 <= b) {
                chmax(ans, P(s[i] - s[i+1], i+1));
            }
        }
        cout << ans.second << endl;
    }
}


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

const char *solve(){
	int c[20] = {};
	int a;
	for(int i = 0; i < 5; ++i){
		if(scanf("%d,", &a) == EOF){ return 0; }
		++c[a];
	}
	c[14] = c[1];

	int mc = 0, ms = 0, p = 0;
	for(int i = 1; i < 14; ++i){
		mc = max(mc, c[i]);
		if(c[i] > 1){ ++p; }
		int s = 0;
		for(int j = 0; j < 5 && c[i + j]; ++j){
			++s;
		}
		ms = max(ms, s);
	}

	if(mc == 4){ return "four card"; }
	if(p > 1 && mc > 2){ return "full house"; }
	if(ms > 4){ return "straight"; }
	if(mc > 2){ return "three card"; }
	if(p > 1){ return "two pair"; }
	if(p){ return "one pair"; }
	return "null";
}

int main(){
	const char *ans;
	while(ans = solve()){
		puts(ans);
	}
}

#include <bits/stdc++.h>
using namespace std;
#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++)
typedef long long ll;

int main() {
    int N; cin >> N;
    vector<int> primes;
    rep2(i, 2, 55555) {
        bool flag = true;
        rep(j, primes.size()) {
            if (i%primes[j] == 0) {
                flag = false;
                break;
            }
        }
        if (flag) primes.push_back(i);
    }
    vector<int> ans;
    rep(i, primes.size()) {
        if (primes[i]%5 == 1) ans.push_back(primes[i]);
    }
    rep(i, N) {
        if (i) cout << " ";
        cout << ans[i];
    }
    cout << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
ll ans = 0;
bool cmp(const pair<ll,ll> &a, const pair<ll,ll> &b){
  if(a.first==b.first)return a.second > b.second;
  else return a.first < b.first;
}
int main()
{
ll n ;
cin >> n ;
vector<pair<ll,ll>> v(n);
ll rmin=0,lmax=0;
rep(i, n){
  cin >> v[i].first >> v[i].second;
  ans=max(ans,v[i].second-v[i].first+1);
  if(v[rmin].second>v[i].second)rmin=i;
  if(v[lmax].first <v[i].first)lmax=i;
}
ans+=max(0LL,v[rmin].second-v[lmax].first+1);
ll ss=v[rmin].second,t=v[lmax].first;
sort(v.begin(),v.end(),cmp);
multiset<ll> s;
rep(i,n)s.insert(v[i].second);
rep(i,n){
  s.erase(s.find(v[i].second));
  ans=max(ans,max(ss-v[i].first+1,0LL)+max(*s.begin()-t+1,0LL));
}
cout << ans << endl;
return 0;
}

#include<bits/stdc++.h>
#include<tr1/unordered_map>
#define ri register int
using namespace std;
#define fi first
#define se second
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> poly;
#define pb push_back
const int rlen=1<<18|1,inf=0x3f3f3f3f;
const ll Inf=1e18;
char buf[rlen],*ib=buf,*ob=buf;
#define gc() (((ib==ob)&&(ob=(ib=buf)+fread(buf,1,rlen,stdin)),ib==ob)?-1:*ib++)
inline int read() {
  int ans=0;
  bool f=1;
  char ch=gc();
  while(!isdigit(ch)) f^=ch=='-',ch=gc();
  while(isdigit(ch)) ans=((ans<<2)+ans<<1)+(ch^48),ch=gc();
  return f?ans:-ans;
}
inline ll readl() {
  ll ans=0;
  bool f=1;
  char ch=gc();
  while(!isdigit(ch)) f^=ch=='-',ch=gc();
  while(isdigit(ch)) ans=((ans<<2)+ans<<1)+(ch^48),ch=gc();
  return f?ans:-ans;
}
inline int Read(char*s) {
  int tp=0;
  char ch=gc();
  while(!isdigit(ch)&&!isalpha(ch)) ch=gc();
  while(isdigit(ch)||isalpha(ch)) s[++tp]=ch,ch=gc();
  return tp;
}
namespace modular {
  const int mod=1e9+7;
  inline int add(int a,int b) { return a+b<mod?a+b:a+b-mod; }
  inline int dec(int a,int b) { return a<b?a-b+mod:a-b; }
  inline int mul(int a,int b) { return (ll)a*b%mod; }
  inline void Add(int&a,int b) { a=a+b<mod?a+b:a+b-mod; }
  inline void Dec(int&a,int b) { a=a<b?a-b+mod:a-b; }
  inline void Mul(int&a,int b) { a=(ll)a*b%mod; }
  inline int ksm(int a,int p) { int ret=1;for(;p;p>>=1,Mul(a,a)) (p&1)&&(Mul(ret,a),1);return ret; }
  inline int Inv(int a) { return ksm(a,mod-2); }
  inline int sqr(int a) { return mul(a,a); }
  inline int cub(int a) { return (ll)a*a%mod*a%mod; }
}
using namespace modular;
template<typename T> inline void ckmax(T&a,T b) { a<b?a=b:0; }
template<typename T> inline void ckmin(T&a,T b) { a>b?a=b:0; }
template<typename T> inline T gcd(T a,T b) { T t;while(b)t=a,a=b,b=t-t/a*a;return a; }
template<typename T> inline T Abs(T x) { return x<0?-x:x; }
inline int rd() { return rand()|(rand()<<15); }
const int N=5e5+5;
int n,pre[N],suf[N];
pii a[N];
int main() {
  #ifdef ldxcaicai
  freopen("lx.in","r",stdin);
  #endif
  n=read();
  for(ri i=1;i<=n;++i) a[i].fi=read(),a[i].se=read();
  sort(a+1,a+n+1);
  pre[0]=0x3f3f3f3f,suf[n]=0x3f3f3f3f;
  for(ri i=1;i<n;++i) pre[i]=min(pre[i-1],a[i].se);
  for(ri i=n-1;i;--i) suf[i]=min(suf[i+1],a[i].se);
  int res=0;
  for(ri mn,r1,r2,t,i=1;i<n;++i) {
    r1=a[i].se,r2=min(max(suf[i+1],a[n].fi-1),a[n].se);
    t=(r1-a[i].fi+1)+(r2-a[n].fi+1);
    mn=pre[i-1];
    ckmax(res,t-max(0,min(r1-max(a[i].fi-1,mn),r2-max(a[n].fi-1,mn))));
  }
  cout<<res;
  return 0;
}


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

typedef long long int ll;
#define endl "\n"
#define pll pair<ll, ll>
#define pii pair<int, int>
#define pb push_back
#define vi vector<int>
#define vl vector<ll>
#define vpii vector<pair<int, int>>
#define mems(x, y) memset(x, y, sizeof(x))
#define all(x) (x).begin(), (x).end()
#define forn(i, s, e) for (int i = s; i < (e); ++i)
#define FASTIO                        \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);
#define FILEIO                          \
    freopen("./input.txt", "r", stdin); \
    freopen("./output.txt", "w", stdout);
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define time__(d)                                                               \
    for (                                                                       \
        auto blockTime = make_pair(chrono::high_resolution_clock::now(), true); \
        blockTime.second;                                                       \
        debug("%s : %lld ms\n ", d, chrono::duration_cast<chrono::milliseconds>(chrono::high_resolution_clock::now() - blockTime.first).count()), blockTime.second = false)
const int M = 1e9 + 7;

template <class T>
T ABS(const T &x) { return x > 0 ? x : -x; }
ll gcd(ll n1, ll n2) { return n2 == 0 ? ABS(n1) : gcd(n2, n1 % n2); }
ll lcm(ll n1, ll n2) { return n1 == 0 && n2 == 0 ? 0 : ABS(n1 * n2) / gcd(n1, n2); }

int main()
{
    FASTIO
#ifdef LOCAL
    FILEIO
#endif
    int n, i;
    cin >> n;
    if (n % 111 == 0)
    {
        cout << n << endl;
        return 0;
    }
    for (i = 1; i < 10; ++i)
    {
        if (n < i * 111)
        {
            break;
        }
    }
    cout << i * 111 << endl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
int main(void){
    int a; cin>>a;
    if(a<=111){
        cout<<111<<endl;
    }else if(a<=222){
        cout<<222<<endl;
    }else if(a<=333){
        cout<<333<<endl;
    }else if(a<=444){
        cout<<444<<endl;
    }else if(a<=555){
        cout<<555<<endl;
    }else if(a<=666){
        cout<<666<<endl;
    }else if(a<=777){
        cout<<777<<endl;
    }else if(a<=888){
        cout<<888<<endl;
    }else{
        cout<<999<<endl;
    }

}


/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2020.08.20 03:18:30
**/

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;

class UnionFind {
private:
    vector<int> par;
public:
    UnionFind(int n) {
        par.resize(n, -1);
    }
    int root(int x) {
        if (par[x] < 0) return x;
        return par[x] = root(par[x]);
    }
    bool unite(int x, int y) {
        int rx = root(x);
        int ry = root(y);
        if (rx == ry) return false;
        if (size(rx) < size(ry)) swap(rx, ry);
        par[rx] += par[ry];
        par[ry] = rx;
        return true;
    }
    bool same(int x, int y) {
        int rx = root(x);
        int ry = root(y);
        return rx == ry;
    }
    int size(int x) {
        return -par[root(x)];
    }
};
int main() {
    int n;cin >> n;
    vector<pair<int,int>> p(n),q(n);
    for (int i = 0; i < n; i++) {
        int a,b;cin >> a >> b;
        p[i].first = a;
        p[i].second = q[i].second = i;
        q[i].first = b;
    }
    sort(p.begin(), p.end());
    sort(q.begin(), q.end());
    UnionFind uf(n);
    typedef tuple<int,int,int> T;
    priority_queue<T,vector<T>,greater<T>> pq;
    for (int i = 0; i < n-1; i++) {
        pq.push(make_tuple(p[i+1].first - p[i].first,p[i+1].second,p[i].second));
        pq.push(make_tuple(q[i+1].first - q[i].first,q[i+1].second,q[i].second));
    }
    ll ans = 0;
    while(!pq.empty()) {
        auto t = pq.top();
        pq.pop();
        int x = get<1>(t);
        int y = get<2>(t);
        if (!uf.same(x,y)) {
            ans += get<0>(t);
            uf.unite(x,y);
        }
    }
    cout << ans << endl;
    return 0;
}

#include <iostream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <string>
#include <algorithm>
#include <utility>
#define llint long long
#define inf 1e18
#define rep(x, s, t) for(llint (x) = (s); (x) < (t); (x)++)
#define Rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))

using namespace std;
typedef pair<llint, llint> P;

llint n;
llint x[200005], y[200005], u[200005], v[200005];
char c[200005];
vector<P> vec[6][4000005];

llint calc(vector<P> &vec)
{
	llint ret = inf, l = -inf;
	for(int i = 0; i < vec.size(); i++){
		if(vec[i].second == -1) l = vec[i].first;
		else ret = min(ret, vec[i].first - l);
	}
	return ret;
}

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

	cin >> n;
	for(int i = 1; i <= n; i++){
		cin >> x[i] >> y[i] >> c[i];
		x[i] *= 10, y[i] *= 10;
		u[i] = x[i] + y[i], v[i] = x[i] - y[i] + 2000002;
	}

	for(int i = 1; i <= n; i++){
		if(c[i] == 'R') vec[0][y[i]].push_back(P(x[i], -1));
		if(c[i] == 'L') vec[0][y[i]].push_back(P(x[i], 1));

		if(c[i] == 'U') vec[1][x[i]].push_back(P(y[i], -1));
		if(c[i] == 'D') vec[1][x[i]].push_back(P(y[i], 1));

		if(c[i] == 'R') vec[2][u[i]].push_back(P(v[i], -1));
		if(c[i] == 'U') vec[2][u[i]].push_back(P(v[i], 1));

		if(c[i] == 'D') vec[3][u[i]].push_back(P(v[i], -1));
		if(c[i] == 'L') vec[3][u[i]].push_back(P(v[i], 1));

		if(c[i] == 'L') vec[4][v[i]].push_back(P(u[i], 1));
		if(c[i] == 'U') vec[4][v[i]].push_back(P(u[i], -1));

		if(c[i] == 'R') vec[5][v[i]].push_back(P(u[i], -1));
		if(c[i] == 'D') vec[5][v[i]].push_back(P(u[i], 1));
	}

	llint ans = inf;
	for(int i = 0; i < 6; i++){
		for(int j = 0; j < 4000005; j++){
			sort(vec[i][j].begin(), vec[i][j].end());
			ans = min(ans, calc(vec[i][j]));
		}
	}

	if(ans > inf/2) cout << "SAFE" << endl;
	else cout << ans / 2 << endl;

	return 0;
}


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

#define fs first
#define sc second
#define pb emplace_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

using pii = pair<int, int>;
using vi = vector<int>;
using lint = long long;

const int inf = 1001001001;
const lint linf = 1001001001001001001ll;
const int mod = 1e9 + 7;
const int dx[]{0, 1, 0, -1, -1, -1, 1, 1}, dy[]{1, 0, -1, 0, -1, 1, -1, 1};

template<typename T> inline bool chmin(T &a, T b) { if (a > b) { a = b; } return a > b; }
template<typename T> inline bool chmax(T &a, T b) { if (a < b) { a = b; } return a < b; }
template<typename T> inline void print(const T &x, string s = "\n") { cout << x << s; }
template<typename T> inline void print(const vector<T> &v, string s = " ")
{ if (!v.size()) puts(""); rep(i, v.size()) cout << v[i] << (i + 1 == v.size() ? "\n" : s); }
inline bool inside(int y, int x, int H, int W) { return 0 <= y && y < H && 0 <= x && x < W; }
inline lint in() { lint x; std::cin>>x; return x; }

bool izryt(pair<pair<int, int>, string> a, pair<pair<int, int>, string> b) {
  if (a.fs.fs * b.fs.sc == b.fs.fs * a.fs.sc) return a.sc < b.sc;
  return a.fs.fs * b.fs.sc > b.fs.fs * a.fs.sc;
}
signed main() {
  int n;

  while (cin >> n, n) {
    vector<pair<pair<int, int>, string>> v;

    rep(i, n) {
      string l;
      cin >> l;

      int p = in();
      int u = 0, t = 0;

      rep(i, 3) u += in();
      rep(i, 2) t += in();

      int f = in();
      int s = in();
      int m = in();

      v.pb(mp(mp((f * m * s - p), (u + t * m)), l));
    }
    sort(all(v), izryt);

    rep(i, n) {
      cout << v[i].sc << endl;
    }
    cout << "#" << endl;
  }
}

#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "math.h"
#include "utility"
#include "string"
#include "map"
#include "unordered_map"
#include "iomanip"
#include "random"

using namespace std;
const long long int MOD = 1000000007;

long long int power(long long int x, long long int n, long long int M) {
	long long int tmp = 1;

	if (n > 0) {
		tmp = power(x, n / 2, M);
		if (n % 2 == 0) tmp = (tmp*tmp) % M;
		else tmp = (((tmp*tmp) % M)*x) % M;
	}
	return tmp;
}

long long int N, M, K, Q, W, H, L, R;
long long int ans;

int main() {
	ios::sync_with_stdio(false);
	cin >> N;
	while (N) {
		set<pair<long double, string> >s;
		for (int i = 0; i < N; i++) {
			string t;
			long double  p, a, b, c, d, e, f, n, m;
			cin >> t >> p >> a >> b >> c >> d >> e >> f >> n >> m;
			s.insert({ 1000-((f*n*m - p) / (a + b + c + d*m + e*m)) ,t });
		}
		for (auto i : s) {
			cout << i.second << endl;
		}
		cout << "#\n";
		cin >> N;
	}
	return 0;
}


#include "bits/stdc++.h"
using namespace std;
///////////////////////////////////////////
const long long int INF = 1LL<<60;
const long long int Mod = 1000000007;
using ll = long long int; using ci = const int;
using vi = vector<int>;  using Vi = vector<long long int>;
using P = pair<int, int>;  using PLL = pair<ll, ll>;
using matrix = vector<vector<ll>>;
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define all(x) (x).begin(),(x).end()
#define rep(i,N) for(ll i = 0; i < (ll)N; i++)
#define repi(i,a,b) for(ll i = ll(a); i < ll(b); ++i)
template<class T>bool chmax(T &former, const T &b) { if (former<b) { former=b; return true; } return false; }
template<class T>bool chmin(T &former, const T &b) { if (b<former) { former=b; return true; } return false; }
template<class T>T sqar(T x){ return x*x; }//sqrt(x)は平方根;
#define Sort(v) std::sort(v.begin(), v.end(), std::greater<decltype(v[0])>()) //降順でVをソート
#define p_queue(v) priority_queue<v, vector<v>, greater<v> >
template<class T> inline void princ(T x){cout<<x<<" ";};
template<class T> inline void print(T x){cout<<x<<"\n";};
template<class T> inline void Yes(T condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; }
template<class T> inline void YES(T condition){ if(condition) cout << "YES" << endl; else cout << "NO" << endl; }
///////////////////////////////////////////////////////////////////////////////////

ll m;
map<ll,ll> ma;
Vi d,c;

bool ck(){
	ll sum = 0;
	rep(i,10){
		sum += ma[i];
		if(sum>=2)return true;
	}
	return false;
}

int main(){
	cin.tie(0);ios::sync_with_stdio(false);
	std::cout<<std::fixed<<std::setprecision(30);
	cin>>m;
	d.resize(m);
	c.resize(m);
	rep(i,m){
		cin>>d[i]>>c[i];
		ma[d[i]] += c[i];
	}
	ll ans = 0;
	while(ck()){
		rep(i,10){
			ll sum = 0;
			if(i==0){
				if(ma[i]<2)continue;
				ans += ma[i]-1;
				ma[i] = 1;
			}else{
				while(ma[i]>1){
				if(ma[i]*i<10){
					sum = ma[i]*i;
					ans += ma[i]-1;
					ma[i] = 0;
					ma[sum]++;
				}else{
					ll tmp = 10/i;
					if(10%i==0){
						tmp--;
					}
					tmp++;
					ll ret = ma[i]/tmp;
					ma[i]%=tmp;
					sum = tmp*i;
					sum = sum%10 + sum/10;
					ans += tmp*ret;
					ma[sum]+=ret;
				}
				}
			}
		}
		rep(i,10){
			if(ma[i]==0)continue;
			repi(j,i+1,10){
				if(ma[i]==0)continue;
				if(ma[j]==0)continue;
				ll sum = i+j;
				ans++;
				if(sum>=10)ans++;
				sum = sum/10 + sum%10;
				ma[i]--;
				ma[j]--;
				ma[sum]++;
			}
		}
	}
	print(ans);
	return 0;
}

#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define pb push_back
#define fastcin() ios_base::sync_with_stdio(false); cin.tie(0)
using namespace std;
typedef pair<int, int> ii;

int Ceil(int a, int b) { return (a + b - 1) / b; }

int m;
signed main()
{
	int s = 0, dig = 0;
	cin >> m;
	for(int i = 1; i <= m; i++)
	{
		int d, c; cin >> d >> c;
		dig += c;
		s += c * d;
	}
	cout << (dig - 1) + Ceil(s-9, 9);
}


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

long long N, K;
double P[200000];

int main(){
	cin >> N >> K;
	for(long long i = 0; i < N; i++) {
		double p;
		cin >> p;
		P[i] = (1.0 + p)/2.0;
	}

	double ans = 0;
	double sum = 0;
	for(long long i = 0; i < K; i++) {
		sum += P[i];
	}
	ans  = sum;

	for(long long i = K; i < N; i++) {
		sum += P[i];
		sum -= P[i-K];
		ans = max(ans, sum);
	}

	printf("%.10f\n", ans);

}

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

const ll Mod = 1000000007;
bool exists(vector<ll>& vec,ll x)
{
    auto pos = lower_bound(vec.begin(),vec.end(),x);
    if(pos == vec.end())return false;
    return *pos == x;
}
void solve(ll N, ll M, vector<ll> A, vector<ll> B)
{
    ll ans = 1;
    sort(A.begin(),A.end());
    sort(B.begin(),B.end());
    ll empty = 0;
    ll numrow=0,numcol=0;
    for(ll x = M*N;x>=1;--x)
    {
        if(exists(A,x))
        {
            if(exists(B,x))
            {
                empty += numcol + numrow;
                numcol++;
                numrow++;
            }else
            {
                empty += numcol - 1;
                ans *= numcol;
                ans %= Mod;
                numrow++;
            }
        }else
        {
            if(exists(B,x))
            {
                empty += numrow - 1;
                ans *= numrow;
                ans %= Mod;
                numcol++;
            }else
            {
                if(empty <= 0)
                {
                    cout<<0<<endl;
                    return;
                }
                ans *= empty;
                empty--;
                ans %= Mod;
            }
        }
    }
    cout<<ans<<endl;
}

int main()
{
    ll N;
    cin >> N;
    ll M;
    cin >> M;
    vector<ll> A(N);
    for(int i = 0 ; i < N ; i++)
    {
        cin >> A[i];
    }
    vector<ll> B(M);
    for(int i = 0 ; i < M ; i++)
    {
        cin >> B[i];
    }
    solve(N, M, move(A), move(B));
    return 0;
}


#include <iostream>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;

int main()
{
	double x1,y1,x2,y2;
	scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2);
	printf("%4lf\n",sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)));
	return 0;

}

#include <iostream>
#include <math.h>

using namespace std;

class point{
public:
	double x, y;
	int set();
};

int point::set()
{
	cin >> x >> y;
	return 0;
}

int main()
{
	point P1, P2;
	P1.set();
	P2.set();

	cout << fixed << sqrt((P1.x - P2.x)*(P1.x - P2.x) + (P1.y - P2.y)*(P1.y - P2.y)) << endl;

	return 0;
}


#include <bits/stdc++.h>

#define reg register
#define pr std::pair<int, int>
#define fi first
#define se second
#define FIN(s) freopen(s, "r", stdin)
#define FOUT(s) freopen(s, "w", stdout)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define rep(i, l, r) for (int i = l; i <= r; ++i)
#define lep(i, l, r) for (int i = l; i < r; ++i)
#define irep(i, r, l) for (int i = r; i >= l; --i)
#define ilep(i, r, l) for (int i = r; i > l; --i)
#define Rep(i, n) rep(i, 1, n)
#define Lep(i, n) lep(i, 1, n)
#define IRep(i, n) irep(i, n, 1)
#define ILep(i, n) ilep(i, n, 1)
typedef long long ll;
typedef long double ld;

namespace modular {
    const int MOD = 1000000007;
    inline int add(int x, int y) { return (x += y) >= MOD ? x -= MOD : x; }
    inline void inc(int &x, int y) { (x += y) >= MOD ? x -= MOD : 0; }
    inline int mul(int x, int y) { return 1LL * x * y % MOD; }
    inline int qpow(int x, int y) {
        int ans = 1;
        for (; y; y >>= 1, x = mul(x, x))
            if (y & 1) ans = mul(ans, x);
        return ans;
    }
};  // namespace modular

namespace Base {
    template <typename Tp>
    inline Tp input() {
        Tp x = 0, y = 1;
        char c = getchar();
        while ((c < '0' || '9' < c) && c != EOF) {
            if (c == '-') y = -1;
            c = getchar();
        }
        if (c == EOF) return 0;
        while ('0' <= c && c <= '9') x = x * 10 + c - '0', c = getchar();
        return x *= y;
    }
    template <typename Tp>
    inline void read(Tp &x) {
        x = input<Tp>();
    }
    template <typename Tp>
    inline void chmax(Tp &x, Tp y) {
        x < y ? x = y : 0;
    }
    template <typename Tp>
    inline void chmin(Tp &x, Tp y) {
        x > y ? x = y : 0;
    }
};  // namespace Base
using namespace Base;
/*----------------------------------------------------------------------------*/

#define MAX_N 507
#define MAX_M 1000007

int pri[MAX_M], tot;
bool check[MAX_M];

void sieve() {
    int MX = 1000000;
    rep(i, 2, MX) {
        if (!check[i]) pri[++tot] = i;
        if (tot > 1000) break;
        rep(j, 1, tot) {
            if (i * pri[j] > MX) break;
            check[i * pri[j]] = true;
            if (i % pri[j] == 0) break;
        }
    }
}

int N;
ll ans[MAX_N * 2][MAX_N * 2], num1[MAX_N * 2], num2[MAX_N * 2];
void solve() {
    if (N == 2) {
        puts("4 7");
        puts("23 10");
        return;
    }
    int cur = 0;
    Rep(i, N) if (i + 1 & 1) num1[1 - i + N] = pri[++cur];
    rep(i, 2, N) if (i + 1 & 1) num1[i - 1 + N] = pri[++cur];
    rep(i, 2, N + N) if (i & 1) num2[i] = pri[++cur];
    // rep(i, 1, N * 2 - 1) printf("%d ", num1[i]);
    // puts("");
    rep(i, 0, N + 1) {
        rep(j, 0, N + 1) if (i + j & 1) {
            ans[i][j] =
                std::max(1LL, num1[i - j + N]) * std::max(1LL, num2[i + j]);
        }
    }
    Rep(i, N) {
        Rep(j, N) if (!(i + j & 1)) {
            ans[i][j] = ans[i - 1][j] * ans[i + 1][j] + 1;
        }
    }
    Rep(i, N) {
        Rep(j, N) {
            if (ans[i][j] > 1e15) exit(1);
            printf("%lld ", ans[i][j]);
        }
        puts("");
    }
}

int main() {
    sieve();
    read(N);
    solve();
    return 0;
}

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int pds=998244353;
const int N=1000086;
int q1[N+5],q2[N+5];
int Pow(int a,int b)
{
    int ans=1;
    for(;b;b>>=1,a=1ll*a*a%pds)
        if(b&1) ans=1ll*ans*a%pds;
    return ans;
}
void Init()
{
    q1[0]=1;
    for(int i=1;i<=N;i++)
        q1[i]=1ll*q1[i-1]*i%pds;
    q2[N]=Pow(q1[N],pds-2);
    for(int i=N-1;i>=0;i--)
        q2[i]=1ll*q2[i+1]*(i+1)%pds;
}
int C(const int &n,const int &m){return 1ll*q1[n]*q2[m]%pds*q2[n-m]%pds;}
int invC(const int &n,const int &m){return 1ll*q2[n]*q1[m]%pds*q1[n-m]%pds;}
int main()
{
    int n,m;Init();
    cin>>n>>m;
    int t=min(n,m),ans=0;
    for(int i=1;i<=t;i++)
    {
    	ans=(ans+1ll*C(2*i,i)*C(n+m-2*i,n-i))%pds;
    }
    ans=1ll*ans*invC(n+m,n)%pds*q2[2]%pds;
    cout<<(ans+max(n,m))%pds;
    return 0;
}


//Heaplax
//别让自己后悔
#include<bits/stdc++.h>
#define N 2005
#define LL long long
#define LOG(x) cerr<<#x<<" = "<<x<<endl
#define add_edge(u,v) nxt[++cnt]=head[u],head[u]=cnt,to[cnt]=v
#define open(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout)
char ch;bool fs;void re(int& x)
{
	while(ch=getchar(),ch<33);
	if(ch=='-')fs=1,x=0;else fs=0,x=ch-48;
	while(ch=getchar(),ch>33)x=x*10+ch-48;
	if(fs)x=-x;
}
using namespace std;
int n,m,q,v[N][N],xe[N][N],ye[N][N];
char s[N][N];
int main()
{
	re(n),re(m),re(q);
	for(int i=1;i<=n;++i)
		scanf("%s",s[i]+1);
	for(int i=1;i<=n;++i)
		for(int j=1;j<=m;++j)
		{
			v[i][j]=s[i][j]-'0'+v[i-1][j];
			xe[i][j]=(s[i][j]=='1'&&s[i-1][j]=='1')+xe[i-1][j];
			ye[i][j]=(s[i][j]=='1'&&s[i][j-1]=='1')+ye[i][j-1];
		}
	for(int i=1;i<=n;++i)
		for(int j=1;j<=m;++j)
		{
			v[i][j]+=v[i][j-1];
			xe[i][j]+=xe[i][j-1];
			ye[i][j]+=ye[i-1][j];
		}
	for(int a,b,c,d;q--;)
	{
		re(a),re(b),re(c),re(d);
		int ans=v[c][d]+v[a-1][b-1]-v[a-1][d]-v[c][b-1];
		ans-=xe[c][d]-xe[a][d]-xe[c][b-1]+xe[a][b-1];
		ans-=ye[c][d]-ye[a-1][d]-ye[c][b]+ye[a-1][b];
		printf("%d\n",ans);
	}
}


// Template
#include <bits/stdc++.h>
#define rep_override(x, y, z, name, ...) name
#define rep2(i, n) for (int i = 0; i < (n); ++i)
#define rep3(i, l, r) for (int i = (l); i < (r); ++i)
#define rep(...) rep_override(__VA_ARGS__, rep3, rep2)(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
using namespace std;
using ll = long long;
constexpr int inf = 1001001001;
constexpr ll INF = 3003003003003003003;
template <class T> inline bool chmin(T &x, const T &y) {if (x > y) {x = y; return 1;} return 0;}
template <class T> inline bool chmax(T &x, const T &y) {if (x < y) {x = y; return 1;} return 0;}
struct IOSET {IOSET() {cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(10);}} ioset;

// Union-Find
struct UnionFind {
    vector<int> par;
    UnionFind(int N) : par(N, -1) { }
    int root(int x) {
        if (par[x] < 0) 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;
        if (par[rx] > par[ry]) swap(rx, ry);
        par[rx] += par[ry];
        par[ry] = rx;
    }
    bool same(int x, int y) {
        return root(x) == root(y);
    }
    int size(int x) {
        return -par[root(x)];
    }
};

// Main Code
int main() {
    int n, m;
    cin >> n >> m;
    UnionFind uf(2 * n);
    vector<int> cnt(n, 0);
    rep(i, m) {
        int a, b;
        cin >> a >> b;
        --a; --b;
        uf.unite(a, b + n); uf.unite(a + n, b);
        ++cnt[a]; ++cnt[b];
    }

    vector<int> siz(2 * n, 0);
    rep(i, n, 2 * n) ++siz[uf.root(i)];
    ll ans = 0;
    rep(i, n) {
        ans += (siz[uf.root(i)] - cnt[i]);
        if (uf.same(i, n + i)) --ans;
    }
    cout << ans / 2 << "\n";
    return 0;
}

#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
const int N=2e5+10;
int n,m;
int col[N];
vector <int> g[N];
int cnt[4];
bool flag=0;
void dfs(int x,int c){
    col[x]=c;cnt[c]++;
    for(int i=0;i<g[x].size();i++){
        if(!col[g[x][i]]) dfs(g[x][i],3-c);
        if(col[g[x][i]]==col[x]) flag=1;
    }
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1,x,y;i<=m;i++){
        scanf("%d%d",&x,&y);
        g[x].push_back(y);
        g[y].push_back(x);
    }
    for(int i=1;i<=n;i++)
        if(!col[i]) dfs(i,1);
    if(flag) printf("%lld\n",(long long)n*(n-1)/2-m);
    else {
        printf("%lld\n",(long long)cnt[1]*cnt[2]-m);
    }
    return 0;
}


#include <bits/stdc++.h>
#define int long long
#define mod 998244353
#define foi(i,n) for(int i = 0; i < n ; ++i)
#define pi pair<int,int>
#define pb push_back
#define debug cout << "here" << endl;
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;

int dp[100000];

int findProfit(int mask,int arr[16][16]){
	int answer =0 ;
	vector<int> points;
	int ctr =0 ;
	while(mask){
		if(mask&1) points.pb(ctr);
		ctr++;
		mask = mask>>1;
	}

	for(int i = 0 ;i < points.size();i++){
		for(int j = i+1 ; j < points.size();j++) answer += arr[points[i]][points[j]];
	}

	//for(auto y : points ) cout << y << " ";
	//cout << endl;
	return answer;
}

void init(int n,int arr[16][16]){
	dp[0] = 0;

	for(int i = 1 ; i <= (((1)<<n)-1) ; i++){
		int answer = findProfit(i,arr);
		for(int j = i-1 ; j>=0 ; j--){
			if((j|i) == i) answer = max(answer , dp[j] + dp[(i^j)]);
		}
		dp[i] = answer;
	}

}

int32_t main(){

    memset(dp,0,sizeof(dp));

	int n;
	cin >> n;

	int arr[16][16];

	foi(i,n){
		foi(j,n) cin >> arr[i][j] ;
	}

	init(n,arr);

	cout << dp[((1LL)<<n)-1];

}

#include<bits/stdc++.h>

#define REP(i,a,b) for(int i=a;i<=b;++i)
typedef long long ll;

using namespace std;

void File(){
	freopen("AGC10D.in","r",stdin);
	freopen("AGC10D.out","w",stdout);
}

const int maxn=1e5+10;
int n;
ll a[maxn],sum;

bool dfs(bool now){
	REP(i,1,n)if(a[i]%2==1)--a[i];
	ll d=a[1];
	REP(i,1,n)d=__gcd(a[i],d);
	int cnt=0; sum=0;
	bool flag=0;
	REP(i,1,n){
		a[i]/=d;
		sum+=a[i];
		if(a[i]%2==1)++cnt;
		if(a[i]==1)flag=1;
	}
	if(sum%2==0)return now^1;
	if(sum%2==1 && (cnt>=2 || flag))return now;
	return dfs(now^1);
}

int main(){
	//File();
	scanf("%d",&n);
	REP(i,1,n){
		scanf("%lld",&a[i]);
		sum+=a[i];
	}
	if(n%2!=sum%2)puts("First");
	else if(n%2==0 && sum%2==0)puts("Second");
	else{
		int cnt=0;
		REP(i,1,n){
			if(a[i]%2==1)++cnt;
			if(cnt>=2 || a[i]==1){
				puts("Second");
				return 0;
			}
		}
		if(dfs(1))puts("First");
		else puts("Second");
	}
	return 0;
}


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

typedef long lint;
typedef long long llint;
typedef pair<int, int> pint;
typedef pair<long long, long long> pllint;

// static const int MAX = 1e6;
// static const int NIL = -1;
// static const llint INF = 1<<21;
// static const llint MOD = 1e9 + 7;

bool compPair(const pint& arg1, const pint& arg2) { return arg1.first > arg2.first; }
template<class T> void chmax(T& a, T b) { if (a < b) { a = b; } }
template<class T> void chmin(T& a, T b) { if (a > b) { a = b; } }

int main(void) {
    int n, t;
    cin >> n >> t;

    int ans = 0, tnow, tpush, tbfr;
    cin >> tbfr;
    for(int in=1;in<n;in++) {
        cin >> tpush;
        if(tpush-tbfr<=t) ans += tpush-tbfr;
        else ans += t;
        tbfr = tpush;
    }
    ans += t;
    cout << ans << endl;
    return 0;
}

#include <bits/stdc++.h>
#include <climits>
#include <cmath>
#include <iomanip>
#include <math.h>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
//#define local
#ifdef local
    #include "dbg-macro/dbg.h"
#endif
//#define hacks
#ifdef hacks
    #include <boost/multiprecision/cpp_int.hpp>
#endif
#define p std::pair
#define ll long long
#define ld long double
#define ull unsigned long long
#define pi std::pair<int, int>
#define stdabs std::abs
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (unsigned long long i = 0; i < (unsigned long long)(n); ++i)
#define vec std::vector
#define oreq |=
#define npm next_permutation
using namespace std;
std::vector<unsigned ll> genprimevec(const unsigned ll N);
ll extgcd(ll a, ll b, ll& x, ll& y);
ll nCr(ll n, ll r);
void stat();
template <typename T> T fact(T num);
constexpr ll mod = 1000000000 + 7;
int main()
{
    stat();
    ll n, t;
    cin >> n >> t;
    ull ans = 0,curtime=0;
    vec<ll> v(n);
    rep(i, n) { cin >> v[i]; }
    rep(i,n-1){
	    curtime+=v[i];
	    ans+=min(t,abs(v[i+1]-v[i]));
    }
    cout<<ans+t<<endl;
    return 0;
}
ll extgcd(ll a, ll b, ll& x, ll& y)
{
    if (b == 0) {
        x = 1, y = 0;
        return a;
    }
    ll d = extgcd(b, a % b, y, x);
    y -= a / b * x;
    return d;
}

std::vector<unsigned ll> genprimevec(const unsigned ll N)
{
    std::vector<bool> is_prime(N + 1);
    for (unsigned ll i = 0; i <= N; i++) {
        is_prime[i] = true;
    }
    std::vector<unsigned ll> P;
    for (unsigned ll i = 2; i <= N; i++) {
        if (is_prime[i]) {
            for (unsigned ll j = 2 * i; j <= N; j += i) {
                is_prime[j] = false;
            }
            P.emplace_back(i);
        }
    }
    return P;
}

void stat(){
#ifdef local
    rep(i, 2){std::cout << "local enable" << std::endl;
}
#endif
#ifdef hacks
rep(i, 2) { std::cout << "boost enable" << std::endl; }
#endif
}

ll nCr(ll n, ll r)
{
    ll num = 1;
    for (ll i = 1; i <= r; i++) {
        num = num * (n - i + 1) / i;
    }
    return num;
}

template <typename T> T fact(T num)
{
    if (num == 1) { return 1; }
    return num * fact(num - 1);
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#define re register
const int MAXN=2e5+10;
int n,K,s[MAXN],a[MAXN],timee;
char fin[10010],*p1=fin,*p2=fin;
char getc()
{
	return p1==p2&&(p2=(p1=fin)+fread(fin,1,10000,stdin),p1==p2)?EOF:*p1++;
}
int read()
{
	re int x=0;
	re char ch=0;
	while(ch<'0'||ch>'9') ch=getc();
	while('0'<=ch&&ch<='9') x=(x<<3)+(x<<1)+(ch^48),ch=getc();
	return x;
}
inline void insert(int x,int w)
{
	for(;x<=n;x+=x&-x) s[x]+=w;
}
inline int query(int x)
{
	re int res=0;
	for(;x;x-=x&-x) res+=s[x];
	return res;
}
int main()
{
	//freopen("t2.out","w",stdout);
	n=read();
	K=read();
	for(re int i=1;i<=n;i++) a[i]=read();
	while(K--)
	{
		re bool flag=1;
		memset(s,0,(n+5)*sizeof(int));
		for(re int i=1;i<=n;i++) insert(std::max(1,i-a[i]),1),insert(std::min(n,i+a[i])+1,-1);
		for(re int i=1;i<=n;i++) a[i]=query(i),flag&=(a[i]==n);
		if(flag) break;
	}
	for(re int i=1;i<=n;i++) printf("%d ",a[i]);
	printf("\n");
	return 0;
}
/*
5 1
1 0 0 1 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++)

int main() {
	int N, K;
	cin >> N >> K;
	vector<int> v(N);
	rep(i, N) {
		cin >> v.at(i);
	}

	int a=0, b=0, c=0, x, y, z;
    rep(i, min(K,50)) {
        vector<int> vv(N+1);
        rep(j, N) {
            x = v.at(j);
            vv.at(max(j-x,  0)) += 1;
            vv.at(min(j+x+1,N)) -= 1;
        }
        v.at(0) = vv.at(0);
        rep1(j, N) {
            v.at(j) = v.at(j-1) + vv.at(j);
        }
    }

    rep(i, N) {
        cout << v.at(i) << " ";
    }
}


#include"bits/stdc++.h"

using namespace std;

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

const long long INF10=1e10+1,ID_MAX=20;
const long mod=(1e9)+7;
const long long INF18=1e18+1;

struct edge{
    int to;
    long cost;
};

void sort2array(double *a,long *b,long n){
    for(int i=0;i<n;i++){
        b[i]=i;
    }
    sort(b,b+n,[a](long i,long j)->bool {return a[i]<a[j];} );
}
//bを何回足せばaを超えるか(O(a/b))
int wtover(int a,int b){
    if(a%b>0)return (a/b)+1;
    else return a/b;
}
int bi_e[ID_MAX]={0};
//2進数表示したときの最高桁(O(log n))
int bi_max(long n){
    int m=0;
    for(m;(1<<m)<=n;m++);m=m-1;
    return m;
}
//bi_eに二進数表示したやつを代入(O(log^2 n))
void bi_exs(long n){
    memset(bi_e,0,sizeof(bi_e));
    if(n<(1<<ID_MAX)){
        for(int i=0;n>0;n=(n>>1),i++)bi_e[i]=n&1;
    }
}
//x^n mod m (nが負の時は０)(O(log n))
long myPow(long x, long n, long m){
  if(n<0)return 0;
  if(n == 0)
    return 1;
  if(n % 2 == 0)
    return myPow(x * x % m, n / 2, m);
  else
    return x * myPow(x, n - 1, m) % m;
}
//m以下のn以下の分割数のmodの表生成(O(nm))d[i][j]=jのi分割
long long d[10001][10001];
void divNum(int n,int m,long long md=mod){
    rep(j,n+1){
        d[j][0]=1;
    }
    rep(j,m){
        d[0][j+1]=0;
    }
    for(int j=1;j<=n;j++){
        for(int k=1;k<=m;k++){
            if(j>k)d[j][k]=d[j-1][k];
            else d[j][k]=(d[j-1][k]+d[j][k-j])%md;
        }
    }
}
const int N_MAX=1e5+1,M_MAX=1e9+1;
int n,m;
vector<int> a;
long long div_res[N_MAX][100];
void divide(int s,int t){
    div_res[0][0]=1;
    rep(i,s){
        rep(j,t+1){
            rep(k,t-j+1){
                div_res[i+1][j+k]=(div_res[i+1][j+k]+div_res[i][j])%mod;
            }
        }
    }
}

int main(){
    int i=0,maxa=0;
    //入力
    cin>>n>>m;
    int mm=m,x=2,r;
    for(int j=2;j<=ceil(sqrt(m));j++){
        r=0;
        while(mm%j==0){
            mm=mm/j;
            r++;
        }
        if(r!=0){
            if(maxa<r)maxa=r;
            a.push_back(r);
        }
    }
    if(mm!=1)a.push_back(1);
    //処理
    long long res=1;
    divide(n,maxa);
    rep(i,a.size()){
        res=(res*div_res[n][a.at(i)])%mod;
    }
    //出力
    cout<<res<<endl;
}

#include<bits/stdc++.h>
#include<unordered_set>
#include<unordered_map>
#include <algorithm>
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define FOR(i,a,b) for(ll i=(a);i<(b);i++)
#define FORR(i,a,b)for(ll i=(a);i<=(b);i++)
#define repR(i,n) for(ll i=n;i>=0;i--)
#define all(v)(v).begin(),(v).end()
#define rall(v)(v).rbegin(),(v).rend()
#define F first
#define S second
#define pb push_back
#define pu push
#define COUT(x) cout<<(x)<<endl
#define PQ priority_queue<ll>
#define PQR priority_queue<ll,vector<ll>,greater<ll>>
#define YES(n) cout << ((n) ? "YES" : "NO"  ) << endl
#define Yes(n) cout << ((n) ? "Yes" : "No"  ) << endl
#define mp make_pair
#define maxs(x,y) (x = max(x,y))
#define mins(x,y) (x = min(x,y))
#define sz(x) (ll)(x).size()
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll MOD = 1000000007LL;
const ll INF = 1LL << 60;
using vll = vector<ll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvll = vector<vll>;
using vstr = vector<string>;
using pll = pair<ll, ll>;
using vc = vector<char>;
using vvc = vector<vc>;
template<class T> inline bool chmax(T& a, T b) {
 if (a < b) { a = b; return true; } return false;
}
template<class T> inline bool chmin(T& a, T b) {
 if (a > b) { a = b; return true; } return false;
}
ll dx[4]={0,1,0,-1};
ll dy[4]={1,0,-1,0};
map<ll,ll> insuu;
ll n;
const int MAX = 700000;
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;
        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;
}
void prime_factor(ll n){
    ll k=n;
	for(ll i=2;i*i<=n;i++){
	    while(k%i==0){
	        insuu[i]++;
	        k/=i;
	    }
	}
	if(k!=1) insuu[k]=1;
	return ;
}
int main(){
  COMinit();
  ll m;
  cin>>n>>m;prime_factor(m);
  ll ans=1;
  ll k=n-1;
  for(auto p:insuu){
    ans*=COM(p.S+k,k);
    ans%=MOD;
  }
  COUT(ans);
}


#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <math.h>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <math.h>
#include <iostream>
#include <random>
#include<map>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <cassert>
#include<fstream>
#include <unordered_map>
#include <cstdlib>
#include <complex>
using namespace std;
#define Ma_PI 3.141592653589793
#define eps 0.00000001
#define LONG_INF 3e18
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007
#define MOD 998244353
#define REP(i,n) for(long long i = 0;i < n;++i)
#define seg_size 524288
double dot(complex<double> a, complex<double> b) {
	return a.real() * b.real() + a.imag() * b.imag();
}
double gyaku_dot(complex<double> a, complex<double> b) {
	return a.real() * b.imag() - a.imag() * b.real();
}
double leng(complex<double> a) {
	return sqrt(a.real() * a.real() + a.imag() * a.imag());
}
double angles(complex<double> a, complex<double> b) {
	double cosine = dot(a, b) / (leng(a) * leng(b));
	double sine = gyaku_dot(a, b) / (leng(a) * leng(b));
	double kaku = acos(cosine);
	if (sine <= 0) {
		kaku = 2 * Ma_PI - kaku;
	}
	return kaku;
}
vector<int> convex_hull(vector<complex<double>> a) {
	vector<int> ans;
	double now_minnest = a[0].real();
	int now_itr = 0;
	REP(i, a.size()) {
		if (now_minnest > a[i].real()) {
			now_minnest = a[i].real();
			now_itr = i;
		}
	}
	ans.push_back(now_itr);
	complex<double> ba(0, 1);
	while (true) {
		int now_go = 0;
		double now_min = 0;
		int starter = ans[ans.size() - 1];
		for (int i = 0; i < a.size(); ++i) {
			if (i != starter) {
				double goa = angles(ba, a[i] - a[starter]);
				if (goa > now_min) {
					now_min = goa;
					now_go = i;
				}
			}
		}
		if (now_go == ans[0]) break;
		ans.push_back(now_go);
		ba = complex<double>(a[now_go] - a[starter]);
	}
	return ans;
}
long long gcd(long long a, long long b) {
	if (b == 0) return a;
	return gcd(b, a % b);
}
vector<tuple<long long, long long, long long, long long>> inputs;
long long required_costs[300000] = {};
long long winning_value[300000] = {};
long long n, x;
long long solve(long long now, long long border) {
	long long cost = winning_value[now] - required_costs[now];
	if (border < now) {
		cost -= x * get<3>(inputs[border]);
		cost += get<1>(inputs[border]) * get<3>(inputs[border]);
		cost -= get<1>(inputs[border]) * get<2>(inputs[border]);
	}
	return cost;
}
int main() {
	iostream::sync_with_stdio(false);
#define int long long
	cin >> n >> x;
	REP(i, n) {
		long long a, b, c;
		cin >> a >> b >> c;
		inputs.push_back(make_tuple(c * (x - a) + b * a, a, b, c));
	}
	sort(inputs.begin(), inputs.end());
	reverse(inputs.begin(), inputs.end());
	long long costing = 0;
	REP(i, n) {
		costing += get<1>(inputs[i]) * get<2>(inputs[i]);
	}
	long long bobo = 0;
	REP(i, n + 1) {
		required_costs[i] = costing;
		winning_value[i] = bobo;
		if (i < n) {
			costing -= get<1>(inputs[i]) * get<2>(inputs[i]);
			bobo += get<3>(inputs[i]) * (x - get<1>(inputs[i]));
		}
	}
	long long ans = LONG_INF;
	for (int i = 0; i < n; ++i) {
		long long bot = 0;
		long long top = n;
		while (top - bot > 1) {
			long long mid = (top + bot) / 2;
			if (solve(mid, i) <= 0) {
				bot = mid;
			}
			else {
				top = mid;
			}
		}
		long long bobo = solve(bot, i);
		bobo *= -1;
		long long pre_ans = bot * x;
		if (i < bot) {
			pre_ans -= x;
		}
		long long now = x + 1;
		now = (bobo + get<2>(inputs[i]) - 1) / get<2>(inputs[i]);
		if (now >= 0 && now <= x) {
			ans = min(ans, pre_ans + now);
		}
		bobo += (get<3>(inputs[i]) - get<2>(inputs[i])) * get<1>(inputs[i]);
		now = (bobo + get<3>(inputs[i]) - 1) / get<3>(inputs[i]);
		if (now >= 0 && now <= x) {
			ans = min(ans, pre_ans + now);
		}
	}
	ans = min(ans, x * n);
	cout << ans << endl;
}

#include <bits/stdc++.h>
using namespace std;
int N, T;
int64_t DP[3010][3010]; // i番目、t分後の最大値

void init()
{
  for (int i = 0; i < 3010; i++)
    for (int j = 0; j < 3010; j++)
      DP[i][j] = -1;
  DP[0][0] = 0;
}

int64_t ans()
{
  int64_t ans = 0;
  for (int j = 0; j <= T; j++)
    ans = max(DP[N][j], ans);
  return ans;
}

int main()
{
  cin >> N >> T;
  vector<pair<int, int>> ab(N);
  for (int i = 0; i < N; i++)
  {
    cin >> ab[i].first >> ab[i].second;
  }
  sort(ab.begin(), ab.end());
  for (int i = 0; i < N; i++)
  {
    for (int t = 0; t <= T; t++)
    {
      if (DP[i][t] == -1)
        continue;
      DP[i + 1][t] = max(DP[i + 1][t], DP[i][t]);
      if (t < T)
      {
        DP[i + 1][min(t + ab[i].first, T)] = max(DP[i + 1][min(t + ab[i].first, T)], DP[i][t] + ab[i].second);
      }
    }
  }
  cout << ans() << endl;
}


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

#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define intll int long long
const int INF = 1e9;
const int MOD = 1e9 + 7;

int main() {
  int n ;
  cin >> n ;
  string s , t  ;
  cin >> s >> t ;
  deque<char> S , T ;
  rep(i,n){
    S.push_back(s.at(i)) ;
    T.push_back(t.at(i));
  }
  if(s == t ) {
    cout << s.size() <<endl ;
    return 0 ;
  }

  int ans = 2 * n ;

  rep(i,n){
    S.pop_front();
    T.pop_back();

    if( S == T){
      ans = S.size() + 2*(i+1) ;
      cout <<  ans <<endl ;
      return 0 ;
    }
  }

}

// agc006_a
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <type_traits>
#include <typeindex>
#include <unordered_map>
#include <unordered_set>
#endif

template <typename A, typename B> bool cmin(A &a, const B &b) {
  return a > b ? (a = b, true) : false;
}
template <typename A, typename B> bool cmax(A &a, const B &b) {
  return a < b ? (a = b, true) : false;
}
const double PI = acos(-1);
const double EPS = 1e-9;
int inf = sizeof(int) == sizeof(long long) ? 2e18 : 1e9 + 10;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main()
{
  int N, index=0;
  string S,T;
  cin>>N>>S>>T;
  rep (i, N) {
    if (S.substr(N-1-i) == T.substr(0, i+1)) {
      index = i+1;
    }
  }
  cout<<2*N-index<<endl;
  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

int main(){
    int n, m, p, sum;

    while(std::cin >> n >> m){
        std::vector<int> price_list = {};

        if(n == 0 && m == 0){
            break;
        } else{
            sum = 0;
            for(int i = 0;i < n;++i){
                std::cin >> p;
                price_list.push_back(p);
                sum += p;
            }
            std::sort(price_list.begin(), price_list.end(), std::greater<int>());
            for(int i = 1;i <= n/m;++i){
                sum -= price_list.at(i * m - 1);
            }
            std::cout << sum << std::endl;
        }
    }
    return 0;
}

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int n,m,sum;
    int p[1010];

    while(cin >> n >> m,n!=0&&m!=0){
        for(int i=0;i<n;i++){
            cin >> p[i];
        }
        sort(p,p+n);
        reverse(p,p+n);
        sum = 0;
        for(int i=0;i<n;i++){
            if( (i+1)%m != 0) sum += p[i];
        }
        cout << sum << endl;
    }
    return 0;
}


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

int main(){
    int d, g;
    cin >> d >> g;
    vector <int> p(d);
    vector <int> c(d);

    for(int i = 0; i < d; ++i) cin >> p[i] >> c[i];

    int ans = 1e9;
    for(int mask = 0; mask < (1 << d); ++mask){
        int point = 0, num  = 0, rest_max = -1;
        for(int i = 0; i < d; ++i){
            if(mask >> i & 1){
                point += 100 * (i + 1) * p[i] + c[i];
                num += p[i];
            }else{
                rest_max = i;
            }
        }
        if(point < g){
             int p1 = 100 * (rest_max + 1);
            int need = (g - point + p1 - 1)/p1;
            if(need >= p[rest_max])continue;
            num+= need;
        }
        ans = min(ans, num);
    }
    cout << ans << endl;
    return 0;
}

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

int main()
{
    ll n ,k;
    cin>>n>>k;
    cout.precision(15);

    double p[n];
    for(int i=0;i<n;i++)
    {
        cin>>p[i];
        p[i] = (p[i] + 1)/2;
    }
    double curr =0, ans =0 ;

    for(int i=0;i<k;i++)
    {
        curr += p[i];
    }
    ans =max( ans, curr);
    for(int i = k ;i< n ;i++)
    {
        curr = curr - p[i - k] + p[i];
        ans = max( ans , curr);
    }
    printf("%.12f" , ans);

}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n, p;
    while (cin >> n >> p, n) {
        int m = 0, q = p, st[50] = {};
        while (1) {
            if (p == 0) {
                p += st[m];
                st[m] = 0;
            } else {
                p--;
                st[m]++;
                if (p == 0 && st[m] == q) {
                    break;
                }
            }
            m = (m + 1) % n;
        }
        cout << m << endl;
    }
    return 0;
}

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <map>
#include <numeric>
#include <cmath>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <complex>
#include <string.h>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <iomanip>
#include <sys/time.h>
#include <tuple>
#include <random>
using namespace std;

#define endl '\n'
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define UNIQ(v) (v).erase(unique((v).begin(), (v).end()), (v).end())

typedef long long ll;
typedef long double ld;
typedef pair<ll, int> P;
typedef tuple<int, int, int> T;
typedef vector< vector<ld> > matrix;
struct pairhash {
public:
    template<typename T, typename U>
    size_t operator()(const pair<T, U> &x) const {
	size_t seed = hash<T>()(x.first);
	return hash<U>()(x.second) + 0x9e3779b9 + (seed<<6) + (seed>>2);
    }
};
const int inf = 1e9 + 9;
const ll mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1);

int n;
string s;

int num[3][4010]; // 0->R, 1->G, 2->B

ll solve() {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < 3; j++) {
            num[j][i+1] = num[j][i];
        }
        switch(s[i]) {
        case 'R': num[0][i+1]++; break;
        case 'G': num[1][i+1]++; break;
        case 'B': num[2][i+1]++; break;
        }
    }

    ll res = 0;
    for (int i = 0; i < n; i++) {
        for (int j = i+1; j < n; j++) {
            if (s[i] == s[j]) continue;
            if ((s[i] == 'R' && s[j] == 'G') || (s[j] == 'R' && s[i] == 'G')) {
                res += num[2][n] - num[2][j];
                if (j + j - i < n && s[j+j-i] == 'B') res--;
            } else if ((s[i] == 'B' && s[j] == 'G') || (s[j] == 'B' && s[i] == 'G')) {
                res += num[0][n] - num[0][j];
                if (j + j - i < n && s[j+j-i] == 'R') res--;
            } else {
                res += num[1][n] - num[1][j];
                if (j + j - i < n && s[j+j-i] == 'G') res--;
            }
        }
    }
    return res;
}

void input() {
    cin >> n >> s;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(15);

    input();
    cout << solve() << endl;
}


#include<bits/stdc++.h>
using namespace std;
int main(void){
	int a[4];
	for(int i=0;i<4;i++)	cin>>a[i];
	int e,f;
	cin>>e>>f;
	sort(a,a+4,greater<int>());
	if(e<f){
		cout<<a[0]+a[1]+a[2]+f<<endl;
	}
	else{
		cout<<a[0]+a[1]+a[2]+e<<endl;
	}
	return 0;
}


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

vector<int> v[10000], rev[10000], order, group(10000, 0);
vector<bool> vis(10000);

void dfs(int x){
    vis[x] = true;
    for(int j : v[x]){
        if(!vis[j]) dfs(j);
    }
    order.push_back(x);
}

void rdfs(int x, int k){
    group[x] = k;
    vis[x] = true;
    for(int j : rev[x]){
        if(!vis[j]) rdfs(j, k);
    }
}

int main(){
    int n, m;
    cin >> n >> m;
    while(m--){
        int s, t;
        cin >> s >> t;
        v[s].push_back(t);
        rev[t].push_back(s);
    }
    fill(vis.begin(),vis.end(),0);
    for(int i = 0; i < n; i++){
        if(!vis[i]) dfs(i);
    }
    fill(vis.begin(),vis.end(),0);
    int k = 0;
    for(int i = order.size()-1; i >= 0; i--){
        if(!vis[order[i]]) rdfs(order[i], k++);
    }
    int q;
    cin >> q;
    while(q--){
        int u, v;
        cin >> u >> v;
        cout << (group[u]==group[v]) << endl;
    }
    return 0;
}

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

int main() {
  int n, m;
  map<int ,bool> AC;
  map<int ,int> WA;
  cin >> n >> m;
  for (int i = 0; i < m; i++){
    int no;
    string res;
    cin >> no >> res;
    if (res == "AC") {
      AC[no] = 1;
    } else {
      if (!AC.count(no)) {
        WA[no]++;
      }
    }
  }
  int ac, wa;
  ac = wa = 0;
  for(auto m: AC){
    int p;
    p = m.second;
    ac += p;
  }
  for(auto m: WA){
    if (AC[m.first] == 1) {
      wa += m.second;
    }
  }
  cout << ac << " " << wa << endl;
}


#include <iostream>
using namespace std;
typedef long long ll;

const ll M=1000005,mod=998244353;
ll F[M];

void Init(){
	F[0]=1;
	for(int i=1;i<M;i++) F[i]=F[i-1]*i%mod;
}

ll Pow(ll n,ll p){
	ll r=1;
	for(;p>0;p>>=1){
		if(p&1) r=(r*n)%mod;
		n=(n*n)%mod;
	}
	return r;
}

ll Div(ll n,ll m){
	return n*Pow(m,mod-2)%mod;
}

ll nCk(ll n,ll k){
	return Div(F[n],F[n-k]*F[k]%mod);
}

int n,m;

int main(){
	Init();
	cin>>n>>m;
	if(n>m) swap(n,m);
	ll t=0;
	for(int i=0;i<n;i++) (t+=nCk(2*i+1,i)*nCk(n+m-2*i-2,n-i-1))%=mod;
	cout<<(m+Div(t,nCk(n+m,n)))%mod<<endl;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
using Pld = pair<ld, ld>;
using Vec = vector<ll>;
using VecP = vector<P>;
using VecB = vector<bool>;
using VecC = vector<char>;
using VecD = vector<ld>;
using VecS = vector<string>;
using Graph = vector<VecP>;
#define REP(i, m, n) for(ll (i) = (m); (i) < (n); ++(i))
#define REPR(i, m, n) for(ll (i) = (m); (i) > (n); --(i))
#define rep(i, n) REP(i, 0, n)
#define R cin>>
#define repr(i, n) REPR(i, n, 0)
#define all(s) (s).begin(), (s).end()
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define in(a) insert(a)
#define P(p) cout<<(p)<<endl;
#define ALL(x) (x).begin(),(x).end()
#define ALLR(x) (x).rbegin(),(x).rend()
#define SORT(a) sort((a).begin(), (a).end())
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<long long int> vll;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, ll> pll;
void sonic(){ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);}
void setp(const ll n){cout << fixed << setprecision(n);}
const ll INF = 1e9+1;
const ll LINF = 1e18+1;
const ll mod = 1e9+7;
//const ll MOD = 998244353;
const ld PI = acos(-1);
const ld EPS = 1e-11;
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(a>b){a=b;return true;}return false;}
template<typename T> void co(T e){cout << e << "\n";}
template<typename T> void co(const vector<T>& v){for(const auto& e : v)
    { cout << e << " "; } cout << "\n";}
ll gcd(ll a, ll b) {
    if (a < b)swap(a, b);
    if (b == 0) return a;
    unsigned r;
    while ((r = a % b)) {
        a = b;
        b = r;
    }
    return b;
}
ll lcm(ll a, ll b) {
    ll g = gcd(a, b);
    return a * b / g;
}
bool prime(ll n) {
    for (ll i = 2; i <= sqrt(n); i++) {
        if (n%i == 0)return false;
    }
    return n != 1;
}

int pow(int x, int y, int mod){
    int ret=1;
    for(;y;y>>=1,x=1ll*x*x%mod){
        if(y&1) ret=1ll*ret*x%mod;
    }
    return ret;
}

int n = 3010, maxn = 3000;
ll fac[3010], inv[3010];

void init(){
    fac[0]=1;
    REP(i, 1, maxn+1)fac[i]=1ll*fac[i-1]*i%mod;
    inv[maxn]=pow(fac[maxn], mod-2, mod);
    REPR(j,maxn-1,-1)inv[j]=1ll*inv[j+1]*(j+1)%mod;
}

int main() {
    sonic();
    map<char, string> mp;
    R mp['a'] >> mp['b'] >> mp['c'];
    char pre = 'a', nxt;
    while(mp[pre]!=""){
        nxt=mp[pre][0];
        mp[pre]=mp[pre].substr(1, mp[pre].length());
        pre=nxt;
    }
    pre-=32;
    co(pre);
}


#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  for(int i=0;i<(n);++i)
#define FORq(i, m, n) for(int i = (m);i <= (n);++i)
#define SCD(n) scanf("%d",&n)
#define SCD2(m,n) scanf("%d%d",&m,&n)
#define SCD3(m,n,k) scanf("%d%d%d",&m,&n,&k)
#define SCLLD(n) scanf("%lld",&n)
#define SCLLD2(m,n) scanf("%lld%lld",&m,&n)
#define SCLLD3(m,n,k) scanf("%lld%lld%lld",&m,&n,&k)
#define PB push_back
#define MP make_pair
#define ARSCD(A,N) REP(i,N){SCD(A[i]);}
#define ARSCD1(A,N) FORq(i,1,N){SCD(A[i]);}
#define PRINTD(n) printf("%d\n",n)
#define PRINTLLD(n) printf("%lld\n",n)
#define DEBUG printf("%s\n","debug")
#define fst first
#define snd second
#define SIN(x,S) (S.count(x) != 0)
using namespace std;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector < VI > VVI;
typedef vector<long long> VL;
typedef long long ll;
typedef long long integer;
//////////////////////////////////////////////////
// Range Query - RMQ and RUQ with Lazy Segment Tree
// https://www.slideshare.net/iwiwi/ss-3578491
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_2_F&lang=jp

struct LazySegmentTree{
    // for vertex n
    // par (n-1)/2
    // chi 2n+1,2n+2

    // index i
    // bottom vertex n+i-1
    const int NONNUM = -1; //ありえない数字
    int segn2; // imply actually vertex number
    vector<ll> data,lazy;

    LazySegmentTree(int n){
        segn2=1;
        while(segn2<n) segn2*=2; //必要分2^Mを取る

        data.resize(segn2*2,INT_MAX);
        lazy.resize(segn2*2,NONNUM);
    }

    //lazy_propagate -- lazy[v] = xはvの表す区間がxに変更されることを表す．
    inline void eval(int k,int l,int r){
        if (lazy[k] != NONNUM){ // lazy nonzero
            data[k] = lazy[k]; // itself

            if (r-l > 1){
                lazy[2*k+1] = lazy[k];
                lazy[2*k+2] = lazy[k];
            }

            lazy[k] = NONNUM;
        }
    }

    ll query(int a,int b,int l=0,int r=-1,int k=0){
        if(r<0) r=segn2; // when calling function at first
        if (r <= a || b <= l) return INT_MAX; //disjoint

        eval(k,l,r);

        if (a <= l && r <= b) return data[k]; //include

        ll vl = query(a,b,l,(l+r)/2,k*2+1);
        ll vr = query(a,b,(l+r)/2,r,k*2+2);
        return min(vl,vr);
    }

    void update(int a,int b ,ll x, int l = 0, int r=-1,int k = 0){
        if(r==-1) r=segn2;

        eval(k,l,r);

        if (b <= l || r <= a) return;

        if (a <= l && r <= b){
            lazy[k] = x;
            eval(k,l,r);
            return;
        }else{
            //一部のみ含むとき，子ノードを計算してその結果をもらう
            update(a,b,x,l,(l+r)/2,2*k+1);
            update(a,b,x,(l+r)/2,r,2*k+2);
            data[k] = min(data[2*k+1] , data[2*k+2]);
        }

    }
};

// 0-indexed
// sum [s,t) --> s,t+1
// update [s,t,x) --> s,t+1,x

int main(){
    int N,q;
    SCD2(N,q);
    LazySegmentTree seg(N);

    REP(i,q){
        int d; SCD(d);
        if (d==0){
            int s,t,x;
            SCD3(s,t,x);
            seg.update(s,t+1,(ll)x);
        }else{
            int s,t; SCD(s);
            PRINTLLD(seg.query(s,s+1));
        }
    }
}

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

int main() {
    int n;
    string s;
    cin >> n >> s;
    int Q;
    cin >> Q;

    for (int q = 0; q < Q; q++) {
        int k;
        cin >> k;

        long long cnt_D = 0, cnt_M = 0, cnt_DM = 0, cnt_DMC = 0;
        for (int i = 0; i < n; i++) {
            if (s.at(i) == 'D') cnt_D++;
            else if (s.at(i) == 'M') {
                cnt_M++;
                cnt_DM += cnt_D;
            }
            else if (s.at(i) == 'C') cnt_DMC += cnt_DM;

            if (k - 1 <= i) {
                if (s.at(i - k + 1) == 'D') {
                    cnt_D--;
                    cnt_DM -= cnt_M;
                }
                else if (s.at(i - k + 1) == 'M') cnt_M--;
            }
        }

        cout << cnt_DMC << endl;
    }
}


/**
*    author:  boutarou
*    created: 10.06.2020 20:12:43
**/

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n); i++)
#define fcout cout << fixed << setprecision(15)
using lint = long long;
using P = pair<int, int>;

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

const long long MOD = 1e9 + 7;
const long long INF = 3e18;
const double PI = 3.1415926535897932;

int n;
vector<P> a, b;
int ans = 0;

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

	cin >> n;
	rep(i, n) {
		int x, y;
		cin >> x >> y;
		a.push_back({x, y});
	}
	rep(i, n) {
		int x, y;
		cin >> x >> y;
		b.push_back({x, y});
	}
	sort(a.begin(), a.end());
	sort(b.begin(), b.end());
	rep(i, n) {
		int now_x = b[i].first, now_y = b[i].second;
		int ma_y = -1;
		rep(j, n) {
			if (a[j].first > now_x) break;
			if (a[j].second < now_y) {
				chmax(ma_y, a[j].second);
			}
		}
		if (ma_y == -1) continue;
		ans++;
		rep(j, n) {
			if (a[j].second == ma_y) {
				a.erase(a.begin() + j);
			}
		}
	}
	cout << ans << endl;

	return 0;
}

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

int main() {
  int n,m;
  cin >> n;
  vector<string> s(n);
  for(int i=0; i<n;i++){
    cin >> s.at(i);
  }
  cin >> m;
  vector<string> t(m);
  for(int i=0; i<m; i++){
    cin >> t.at(i);
  }

  int i;
  int sum=1,ans=0;

  vector<string> u;
  for(i=0;i<n;i++){

    for(int j=i+1;j<n;j++){
      if(s.at(i) == s.at(j)){
        sum++;
      }
    }
    for(int l=0;l<m;l++){
      if(s.at(i)==t.at(l)){
      sum--;
      }
    }

    if(ans<sum){
      ans = sum;
      //u=s.at(i);

    }
    sum=1;
  }

    cout << ans << endl;
}


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

vector< int64_t > divisor(int64_t n) {
  vector< int64_t > ret;
  for(int64_t i = 1; i * i <= n; i++) {
    if(n % i == 0) {
      ret.push_back(i);
      if(i * i != n) ret.push_back(n / i);
    }
  }
  return (ret);
}

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

    int64_t n,a,b,c; cin>>n>>a>>b; if(n==3)cin>>c;
    vector<int64_t> v;
    for(auto t:divisor(a))if(!(b%t))v.push_back(t);
    if(n==3)for(auto t:v)if(c%t)v.erase(find(v.begin(),v.end(),t));
    sort(v.begin(),v.end());
    for(auto t:v)cout<<t<<'\n';
}

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

vector<int> yakusu(int d){
  vector<int> res;
  for(int i=1; i*i <= d; i++){
    if(d % i == 0) {
      res.push_back(i);
      if(i*i != d) res.push_back(d / i);
    }
  }

  sort(res.begin(), res.end());

  return res;
}

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

int gcd(int* a, int n){
  if(n == 2) return gcd(a[0], a[1]);
  int b[n-1];
  for(int i=0; i<n-1; i++){
    b[i] = gcd(a[i], a[i+1]);
  }
  return gcd(b, n-1);
}

int main(){
  int n; cin >> n;
  int a[n];
  for(int i=0; i<n; i++) cin >> a[i];

  int g = gcd(a, n);

  for(const auto& x : yakusu(g)){
    cout << x << endl;
  }

  return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <queue>
#include <deque>
#include <bitset>
#include <iterator>
#include <list>
#include <stack>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <functional>
#include <numeric>
#include <utility>
#include <limits>
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>

using namespace std;

#define FOR(i,n) for(int i = 0; i < n; i++)
#define FORa(i,a,b) for(int i = a; i < b; i++)
#define pb(x) push_back(x)
#define mp(a, b) make_pair(a, b)
#define F first
#define S second
#define SORT(a,n) sort(begin(a), begin(a) + n)
#define VSORT(v) sort(v.begin(), v.end())
#define MAX 1000000
#define ll long long
#define LOW(s) transform(s.begin(),s.end(),s.begin(),::tolower)
#define UP(s) transform(s.begin(),s.end(),s.begin(),::toupper)

//-------------------------------------------------------//

int main() {
	int x;
	cin >> x;

	cout << x/500*1000 + (x-x/500*500)/5*5 << endl;

	return 0;
}

#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<map>

using namespace std;

template<typename A, typename B> bool chmin(A &a, const B &b){ return b < a && (a = b, true); }
template<typename A, typename B> bool chmax(A &a, const B &b){ return a < b && (a = b, true); }

int main(){
  int n;
  cin >> n;
  cout << n / 3 << '\n';
  return 0;
}


#include <vector>
#include <iostream>

using namespace std;

int main() {
    int n;
    cin >> n;
    int m = 1e9;
    for (int i = 0; i < n; i++) {
        int a;
        cin >> a;
        m = min(__builtin_ctz(a), m);
    }
    cout << m << endl;
}

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

int main() {
  int N;
  cin >> N;

  vector<int> A(N), B(N);

  for (int i = 0; i < N; i++) cin >> A.at(i);

  int min_count = 0;

  int x0 = A.at(0);
  while (x0 % 2 == 0) {
    min_count++;
    x0 = x0 / 2;
  }

  for (int x : A) {
      int count = 0;
      while (x % 2 == 0) {
        count++;
        x = x / 2;
      }
    min_count = min (min_count, count);
  }

  cout << min_count << endl;
}