#include<iostream>
#include <climits>
#include<vector>
#include<list>
#include<numeric>
#include<algorithm>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<stack>
#include<queue>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) for(int i=0;i<(n);i++)
using namespace std;
typedef long long ll;

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

	int n;
	cin >> n;
	vector<ll> S(n);
	REP(i, n) cin >> S[i];
	sort(S.begin(), S.end());
	int q;
	cin >> q;
	vector<ll> T(q);
	REP(i, q) cin >> T[i];
	int res = 0;
	REP(i, q) {
		if (binary_search(S.begin(), S.end(), T[i])) res++;
	}
	cout << res << "\n";

	return 0;
}

#include <bits/stdc++.h>

#define fi first
#define se second
#define db double
#define U unsigned
#define P std::pair<int,int>
#define LL long long
#define pb push_back
#define MP std::make_pair
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define FOR(i,a,b) for(int i = a;i <= b;++i)
#define ROF(i,a,b) for(int i = a;i >= b;--i)
#define DEBUG(x) std::cerr << #x << '=' << x << std::endl

const int MAXN = 1e5 + 5;
int n;
int a[MAXN];

int main(){
	scanf("%d",&n);n <<= 1;
	FOR(i,1,n) scanf("%d",a+i);
	std::sort(a+1,a+n+1);
	int ans = 0;
	for(int i = 1;i <= n;i += 2) ans += a[i];
	printf("%d\n",ans);
	return 0;
}


#include <iostream>
#include <cstring>

using namespace std;

int main() {
        string teks;
        getline(cin,teks);
        int n=teks.size();
        for (int i=n-1; i>=0; i--) {
                cout << teks[i];
        }
        cout << endl;
        return 0;
}

#include<iostream>

using namespace std;

int main()
{
        string input;
        int num = 0;
        int i = 0;
        int count = 0;
        char temp[1];

        cin >> input;
        num = input.size();
        char str[num]; //= input.toCharArray();

        sprintf(str,"%s",input.c_str());

        if(num % 2 == 0)
        {
                count = num / 2;
        }
        else
        {
                count = (num - 1) / 2;
        }

        for(i = 0;i < count;i++)
        {
                temp[0] = str[i];
                str[i] = str[num -  1 - i];
                str[num - 1 - i] = temp[0];
        }

        string output = string(str);
        cout << output << endl;

}


#include<iostream>
#include<stdio.h>
using namespace std;
#include<algorithm>
#include<set>
#include<vector>
#define rep(i,n) for(int i=0;i<(n);i++)

const int mod=1000000007;

int main(){
  int N;
cin>>N;
vector<int>A(N);
rep(i,N)cin>>A[i];
sort(A.begin(),A.end());
int ans=0;
rep(i,N){
  rep(j,i){
    rep(k,j){
      if(A[i]!=A[j]&&A[j]!=A[k]&&A[i]<A[j]+A[k])ans++;
    }
  }
}
cout<<ans<<endl;
return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(i,a,b) for (int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(x) (x).begin(),(x).end()
#define debug(x) cout << #x << '=' << x << '\n'
const long long MOD=1e9+7;
//const long long MOD=998244353;
const int INF=1e9;
const long long IINF=1e18;
const int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
const char dir[4]={'D','R','U','L'};

template<typename T>
istream &operator>>(istream &is,vector<T> &v){
    for (T &x:v) is >> x;
    return is;
}
template<typename T>
ostream &operator<<(ostream &os,const vector<T> &v){
    for (int i=0;i<v.size();++i){
        os << v[i] << (i+1==v.size()?"": " ");
    }
    return os;
}
template<typename T,typename U>
ostream &operator<<(ostream &os,const pair<T,U> &p){
    cout << '(' << p.first << ',' << p.second << ')';
    return os;
}

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

vector<ll> po(19,1);

ll calc(int i,int j,ll e){
    if (j>i-1-j) return (e==0?1:0);
    ll res=0;
    int diff=(j==i-1-j?0:e/(po[i-1-j]-po[j]));
    for (ll d=diff-1;d<=diff+1;++d){
        if (j==i-1-j&&d!=0) continue;
        ll ne=e-(po[i-1-j]-po[j])*d;
        if (ne<-(po[i-1-j]-po[j])||po[i-1-j]-po[j]<ne) continue;
        for (int k=(!j?1:0),cnt=0;k<10;++k){
            if (k+d>=0&&k+d<10) ++cnt;
            if (k==9) res+=calc(i,j+1,ne)*cnt;
        }
    }
    return res;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    for (int i=0;i<18;++i) po[i+1]=po[i]*10;
    ll D; cin >> D;
    ll ans=0;
    for (int i=2;i<=19;++i){
        ans+=calc(i,0,D);
    }
    cout << ans << '\n';
}


#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <queue>
#include <bitset>
#include <string>
using namespace std;

#define REP(i,n) for(int i=0; i<n; i++)
#define RPA(i,s,e) for(int i=s; i<=e; i++)
#define RPD(i,s,e) for(int i=s; i>=e; i--)
#define PB(a) push_back(a)
#define MP(i,s) make_pair(i,s)
#define ALL(a) (a).begin(), (a).end()
#define PRT(a) cout << (a) << endl

typedef pair<int,int> P;

const int INF = 1000000009;
const int MAX_V = 101;
struct edge { int to, cost; };
vector<edge> G[MAX_V];

int N,K,L;
int f,t,c;
int dist[MAX_V];

void add_edge(int from, int to, int cost) {
	edge e1 = { to, cost };
	edge e2 = { from, cost };
	G[from].PB(e1);
	G[to].PB(e2);
}

int dijkstra(int from, int to) {
	fill(dist, dist+MAX_V, INF);
	priority_queue<P, vector<P>, greater<P> > que;
	dist[from] = 0;
	que.push(MP(0, from));
	while(!que.empty()) {
		P p = que.top();
		que.pop();

		int v = p.second;
		if(dist[v] < p.first) continue;

		REP(i,G[v].size()) {
			edge& e = G[v][i];
			if(dist[e.to] > dist[v] + e.cost) {
				dist[e.to] = dist[v] + e.cost;
				que.push(MP(dist[e.to], e.to));
			}
		}
	}

	return dist[to] == INF ? -1 : dist[to];
}

void coding() {
	while(scanf("%d%d", &N, &K) != EOF) {
		REP(i,N) G[i].clear();
		REP(i,K) {
			scanf("%d", &L);
			if(L == 0) {
				scanf("%d%d", &f, &t);
				f--; t--;
				printf("%d\n", dijkstra(f, t));
			} else if(L == 1) {
				scanf("%d%d%d", &f, &t, &c);
				f--; t--;
				add_edge(f, t, c);
			} else {
				PRT("unknown");
			}
		}
	}
}

// #define _LOCAL_TEST

int main() {
#ifdef _LOCAL_TEST
	clock_t startTime = clock();
	freopen("a.in", "r", stdin);
#endif

	coding();

#ifdef _LOCAL_TEST
	clock_t elapsedTime = clock() - startTime;
	cout << endl;
	cout << (elapsedTime / 1000.0) << " sec elapsed." << endl;
	cout << "This is local test" << endl;
	cout << "Do not forget to comment out _LOCAL_TEST" << endl << endl;
#endif
}

#include<iostream>
#include<vector>
#include<queue>
#include<map>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> P;
#define INF 1000000000

int main(){
	int n,m,flg;
	ll a,b,c;
	while(cin >> n >> m && n && m){
		vector<P> node[101];
		for(int k=0;k<m;k++){
			cin >> flg;
			if(flg){
				cin >> a >> b >> c;
				P p; p.first=b; p.second=c;
				node[a].push_back(p);
				p.first = a;
				node[b].push_back(p);
			}
			else {
				bool f = true;
				cin >> a >> b;
				ll used[102];
				used[a] = 0;
				for(int j=1;j<=n;j++) used[j] = INF;
				priority_queue<P, vector<P>, greater<P> > que;
				que.push(P(0,a));
				while(!que.empty()){
					P p = que.top(); que.pop();
					ll cost = p.first;
					ll now = p.second;
					if(now == b) {
						cout << cost << endl;
						f = false;
						break;
					}
					int len = node[now].size();
					for(int i=0;i<len;i++){
						P p = node[now][i];
						ll next = p.first;
						ll nc = p.second;
						if(used[next]==INF || used[next] > cost+nc){
							used[next] = cost+nc;
							que.push(P(cost+nc,next));
						}
					}
				}
				if(f) cout << "-1" << endl;
			}
		}
	}
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define MN 505
using namespace std;
typedef long long ll;
int n,pr[MN<<2],tt;bool p[MN*MN];
ll a[MN][MN];
const int dx[]={-1,0,1,0};
const int dy[]={0,-1,0,1};
int main(){
	scanf("%d",&n);
	for(int i=2;i<=1e5;i++){
		if(!p[i]){
			pr[++tt]=i;
			if(tt>=4*n)break;
		}
		for(int j=1;j<=tt;j++){
			if(i*pr[j]>1e5)break;
			p[i*pr[j]]=1;
			if(i%pr[j]==0)break;
		}
	}ll tt=0;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			if((i+j)%2==0){
				a[i][j]=1ll*pr[i+j>>1]*pr[(i-j)/2+n];
			}else {
				a[i][j]=1ll*pr[i+j>>1]*pr[(i+j+1)>>1];
				if(i-j<0)a[i][j]*=1ll*pr[(i-j)/2+n]*pr[(i-j-1)/2+n];
				else a[i][j]*=1ll*pr[(i-j)/2+n]*pr[(i-j+1)/2+n];a[i][j]++;
				tt=max(a[i][j],tt);
			}//printf("%lld\n",tt);
/*	for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)for(int k=0;k<4;k++){
		int tx=i+dx[k],ty=j+dy[k];
		if(tx>=1&&tx<=n&&ty>=1&&ty<=n){
			if(max(a[i][j],a[tx][ty])%min(a[i][j],a[tx][ty])!=1)printf("%d %d %d %d\n",i,j,tx,ty);
		}
	}*/
	for(int i=1;i<=n;i++){for(int j=1;j<=n;j++)printf("%lld ",a[i][j]);puts("");}
}

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=502;
int n,ar[N][N],lis[1006],dx[]={0,0,-1,1},dy[]={-1,1,0,0},ar1[N][N];
bool check(int x,int y){
  if(x>0&&x<=n){
    if(y>0&&y<=n){
      return true;
    }
  }
  return false;
}
bool check1(int x){
    int i;
    for(i=2;i*i<=x;i++){
     if(x%i==0){
         return false;
     }
    }
    return true;
}
int LCM(int x,int y){
  return (x/__gcd(x,y)) * y;
}
signed main(){
  ios::sync_with_stdio(0);
  cin.tie(0);
  int i,j,k,l,num=0,hichic=0;
  cin>>n;
  if(n==2){
    cout<<"4 7\n23 10";
    return 0;
  }
  for(i=2;i<=10000;i++){
    //cout<<i<<endl;
        if(check1(i)){
        num++;
         lis[num]=i;
      //cout<<num<<' '<<i<<endl;
          if(num==1002){
        //cout<<num<<' '<<"cac"<<endl;
                break;
            }
        }
    }
    //cout<<num<<endl;
    for(i=1;i<=n;i++){
      for(j=1;j<=n;j++){
        if(!((i+j)&1)){
          continue;
        }
        if(i-1&&j-1){
          ar[i][j]=ar[i-1][j-1];
        }
        else{
          hichic++;
          ar[i][j]=lis[hichic];
        }
      }
    }
    for(i=1;i<=n;i++){
      for(j=1;j<=n;j++){
        if(!((i+j)&1)){
          continue;
        }
        if(i-1&&j+1<=n){
    		ar1[i][j]=ar1[i-1][j+1];
          	ar[i][j]*=ar1[i][j];
        }
        else{
          hichic++;
          ar[i][j]*=lis[hichic];
          ar1[i][j]=lis[hichic];
        }
      }
    }
    //cout<<"cac"<<endl;
    for(i=1;i<=n;i++){
      for(j=1;j<=n;j++){
        if(!((i+j)&1)){
          ar[i][j]=1;
          for(l=0;l<4;l++){
            if(check(i+dx[l],j+dy[l])){
              ar[i][j]=LCM(ar[i][j],ar[i+dx[l]][j+dy[l]]);
            }
          }
          cout<<ar[i][j]+1<<' ';
        }
        else{
            cout<<ar[i][j]<<' ';
        }
      }
      cout<<'\n';
    }
}


#include <bits/stdc++.h>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;
using ll = long long;
using P = pair<int,int>;
using Graph= vector<vector<int>>;
#define rep(i,n) for (ll i=0; i < (n); ++i)
#define rep2(i,n,m) for(ll i=n;i<=m;i++)
#define rep3(i,n,m) for(ll i=n;i>=m;i--)
#define pb push_back
#define eb emplace_back
#define ppb pop_back
#define mpa make_pair
const ll INF=1e18 ;
inline void chmax(ll& a,ll b){a=max(a,b);}
inline void chmin(ll& a,ll b){a=min(a,b);}

int main(){
 int n ;cin>> n;
 cout<<(n-2)*180<<endl ;
    return 0;
  }

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<double> vd;
typedef set<int> si;
typedef set<string> ss;
typedef map<int,int> mii;
typedef map<string,int>msi;
typedef map<string,string>mss;
void solve(){
    string inp,weeks[7];
    weeks[0]="MON";
    weeks[1]="TUE";
    weeks[2]="WED";
    weeks[3]="THU";
    weeks[4]="FRI";
    weeks[5]="SAT";
    weeks[6]="SUN";
    cin>>inp;
    int n;
    for(int i=0;i<7;i++){
        if(weeks[i]==inp){
            n=i;
        }
    }
    int res = 6-n;
    if(res==0){
        res=7;
    }
    cout<<res<<endl;
}
int main(){
    int t;
    t=1;
    while(t--){
       solve();
    }
    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
using ll = long long;

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

    vector<int> a(N);
    for (int i = 0; i < N; i++) {
        cin >> a[i];
    }

    vector<int> b(N, 0);
    for (int i = N; i >= 1; i--) {
        int j = i;
        int count = 0;
        while (j <= N) {
            if (b[j - 1] == 1) count++;
            j += i;
        }
        if (count % 2 != a[i - 1]) b[i - 1] = 1;
    }

    vector<int> output;
    for (int i = 0; i < N; i++) {
        if (b[i] != 0) output.push_back(i + 1);
    }
    cout << output.size() << endl;
    for (int i = 0; i < output.size(); i++) {
        cout << output[i] << " ";
    }
    cout << endl;

    return 0;
}

#include <bits/stdc++.h>

using namespace std;

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

#define fi first
#define se second

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

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

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

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

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

  for (int i = 1; i <= N; i++){
    if (ans[i] % 2 != A[i]){
      cout << -1 << endl;
      return 0;
    }
  }

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

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<ll> vl;
typedef pair<ll, ll> PP;
#define rep(i, n) for(ll i = 0; i < ll(n); i++)
#define all(v) v.begin(), v.end()
bool chmin(ll & a, ll  b) { if (b < a) { a = b; return 1; } return 0; }
bool chmax(ll & a, ll b) { if (b > a) { a = b; return 1; } return 0; }
const ll INF = 999999999999999;
const ll MOD = 1000000007;
const ll MAX_N=500010;
ll a, b, c, d, e, f, p, t, x, y, z, q, m, n, r, h, k, w, l, ans;
int main() {
    cin>>n;
    vl beki;
    multiset<ll> A;
    beki.push_back(1);
    rep(i,40){
        beki.push_back(beki.back()*2);
    }
    rep(i,n){
        cin>>x;
        A.insert(x);
    }

    while(!A.empty()){
        auto ite=A.end();
        ite--;
        x=*ite;
      	A.erase(ite);
        k=upper_bound(all(beki),x*2)-beki.begin()-1;
        if(!A.empty()&&A.count(beki[k]-x)>0){
            auto ite2=A.lower_bound(beki[k]-x);
            A.erase(ite2);
            ans++;
        }
    }
    cout<<ans<<endl;
}

#include<iostream>
#include<algorithm>
#include<cmath>
#include<map>
#include<functional>
using namespace std;
#define maxn long long(5*1e8)

int main(){
	long long n=0;
	scanf("%d",&n);
	map<long long,long long,greater<long long>> num;
	for(long long i=1;i<=n;++i){
		long long temp=0;
		scanf("%d",&temp);
		num[temp]++;
	}
	long long j=pow(2.0,31),ans=0;
	for(auto iter=num.begin();iter!=num.end();++iter){
		while(iter->first<j/2) j/=2;
		if(num[iter->first]>0&&num[j-iter->first]>0){
				if(j==iter->first*2){
					ans+=num[iter->first]/2;
					num[iter->first]=num[iter->first]%2;
				}
				else{
				ans+=min(num[iter->first],num[j-iter->first]);
				num[j-iter->first]-=min(num[iter->first],num[j-iter->first]);
				}
			}
	}
	cout<<ans<<endl;
}
//5 1 2 2 2 3


#include <algorithm>
#include <cstdio>
#define upd(x, y) (x) = std::max(x, y)
char str[105];
int dp[105][105][105][105], row_pre[105][105], col_pre[105][105];
int main()
{
	// freopen("AGC004-E.in", "r", stdin);
	int n, m, x, y;
	scanf("%d%d", &n, &m);
	for (int i = 0; i < n; i++)
	{
		scanf("%s", str);
		for (int j = 0; j < m; j++)
		{
			row_pre[i][j] = (j ? row_pre[i][j - 1] : 0) + (str[j] == 'o');
			col_pre[j][i] = (i ? col_pre[j][i - 1] : 0) + (str[j] == 'o');
			if (str[j] == 'E')
			{
				x = i;
				y = j;
			}
		}
	}
	auto calc_row = [&] (int row, int l, int r)
	{
		int L = std::max(l, r - y), R = std::min(r, l + m - y - 1);
		return L <= R ? row_pre[row][R] - (L ? row_pre[row][L - 1] : 0) : 0;
	};
	auto calc_col = [&] (int col, int l, int r)
	{
		int L = std::max(l, r - x), R = std::min(r, l + n - x - 1);
		return L <= R ? col_pre[col][R] - (L ? col_pre[col][L - 1] : 0) : 0;
	};
	for (int i = 0; i <= x; i++)
	{
		for (int j = n - 1; j >= x; j--)
		{
			for (int k = 0; k <= y; k++)
			{
				for (int t = m - 1; t >= y; t--)
				{
					if (j - i < x)
						upd(dp[i][j][k][t], dp[i - 1][j][k][t] + calc_row(i - 1, k, t));
					if (j - i < n - x - 1)
						upd(dp[i][j][k][t], dp[i][j + 1][k][t] + calc_row(j + 1, k, t));
					if (t - k < y)
						upd(dp[i][j][k][t], dp[i][j][k - 1][t] + calc_col(k - 1, i, j));
					if (t - k < m - y - 1)
						upd(dp[i][j][k][t], dp[i][j][k][t + 1] + calc_col(t + 1, i, j));
				}
			}
		}
	}
	printf("%d\n", dp[x][x][y][y]);
	return 0;
}

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

const int N=110, M=5e3+300;
int h,w,F[M][M],idh[N][N],idw[N][N];
char mp[N][N]; int stx,sty,s[N][N];
inline int& f(int l,int r,int u,int d) {return F[idh[l][r]][idw[u][d]];}
inline int getarea(int xc1,int xc2,int yc1,int yc2) {
    return s[xc2][yc2]-s[xc1-1][yc2]-s[xc2][yc1-1]+s[xc1-1][yc1-1];
}
inline int calcl(int p,int up,int dn,int l,int r,int u,int d) {
    if(p<=r || p+l>w) return 0;
    up=max(up,d+1); dn=min(dn,h-u);
    if(up>dn) return 0;
    return getarea(up,dn,p,p);
}
inline int calcr(int p,int li,int ri,int l,int r,int u,int d) {
    if(p<=d || p+u>h) return 0;
    li=max(li,r+1); ri=min(ri,w-l);
    if(li>ri) return 0;
    return getarea(p,p,li,ri);
}
int main() {
    cin>>h>>w;
    for(int i=1;i<=h;i++) cin>>(mp[i]+1);
    for(int i=1;i<=h;i++)
        for(int j=1;j<=w;j++) {
            if(mp[i][j]=='o') s[i][j]=1;
            if(mp[i][j]=='E') stx=i, sty=j;
            s[i][j]=s[i][j-1]+s[i-1][j]-s[i-1][j-1]+s[i][j];
        }
    for(int l=0,cnt=0;l<=sty;++l)
        for(int r=0;sty+r<=w;++r)
            idh[l][r]=cnt++;
    for(int u=0,cnt=0;u<=stx;++u)
        for(int d=0;d+stx<=h;++d)
            idw[u][d]=cnt++;

    int ans=0;
    for(int l=0;l<=sty;++l)
        for(int r=0;sty+r<=w;++r)
            for(int u=0;u<=stx;++u)
                for(int d=0;d+stx<=h;++d) {
                    int &s=f(l,r,u,d);
                    if(l) s=max(s,f(l-1,r,u,d)+calcl(sty-l,stx-u,stx+d,l-1,r,u,d));
                    if(r) s=max(s,f(l,r-1,u,d)+calcl(sty+r,stx-u,stx+d,l,r-1,u,d));
                    if(u) s=max(s,f(l,r,u-1,d)+calcr(stx-u,sty-l,sty+r,l,r,u-1,d));
                    if(d) s=max(s,f(l,r,u,d-1)+calcr(stx+d,sty-l,sty+r,l,r,u,d-1));
                    ans=max(ans,s);
                }
    cout<<ans<<'\n';
}


#include <iostream>
#include <cmath>
int n;

int cnt(int i){
	if(i == 1)	return 1;
	if(i == 2)	return 2;
	if(i == 3)	return 4;

	return cnt( i-3 ) + cnt( i-2 ) + cnt( i-1 );
}

int main(void){
	while(1){
		std::cin >> n;
		if(n == 0)	return 0;
		std::cout << std::ceil(cnt(n) / 10.0 / 365.0) << std::endl;
	}
}

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<climits>
#include<string>
#include<vector>
#include<list>
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<cstring>
#include<stack>
#include<functional>
using namespace std;

int main(){
	int N;
	while(cin>>N,N){
		int dp[31] = {}; dp[0] = 1;
		for(int i=0;i<N;i++){
			for(int j=1;j<=3;j++) if(i+j<=N) dp[i+j] += dp[i];
		}
		printf("%d\n",((dp[N]-1)/10)/365+1);
	}
	return 0;
}


#include <iostream>
#include <algorithm>
#define flush fflush(stdout)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pl;
const int mod = (int)1e9 + 7, INF = (int)1e9;
const int di[4] = { 1,0,-1,0 }, dj[4] = { 0,1,0,-1 };

int main(void) {
    int l, r, i, j, ans;

    scanf("%d%d", &l, &r);

    if (r - l >= 2019 || l % 2019 > r % 2019)
    {
        printf("0\n");
        return (0);
    }
    ans = 2019;
    i = l % 2019;
    while (i < r % 2019)
    {
        j = i + 1;
        while (j <= r % 2019)
        {
            if (ans > i * j % 2019)
                ans = i * j % 2019;
            j++;
        }
        i++;
    }

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

    return 0;
}

#include"bits/stdc++.h"

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

int main() {
	int x, y;
	cin >> x >> y;
	if (x <= 8 && y <= 8) {
		cout << "Yay!" << endl;
	}
	else {
		cout << ":(" <<endl;
	}

	return 0;
}


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

signed main(){
    int t = 1; //cin >> t;
    while(t--){
    	int n;  cin >> n;
    	map<int,int> mp;
    	while(n--)
    	{
    		int x; cin >> x;
    		if(mp[x] == 0)
    			mp[x] = 1;
    		else mp[x] = 0;
    	}
    	int ans =  0;
    	for(auto x:mp)
    		if(x.second)
    			ans++;
    	cout << ans;

    }
}

#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

size_t isort(vector<int> &A, size_t g) {
    size_t count_=0;
    for (size_t i=g; i<A.size(); ++i) {
        int v=A[i], j;
        for (j=i-g; j>=0&&v<A[j]; j-=g) {
            A[j+g] = A[j];
            ++count_;
        }
        A[j+g] = v;
    }
    return count_;
}

size_t shsort(vector<int> &A) {
    size_t count_=0;
    size_t m=A.size();

    vector<size_t> g; g.push_back(1);
    for (size_t h=4; h<=m&&g.size()<100; ++(h*=3))
        g.push_back(h);

    reverse(g.begin(), g.end());

    printf("%zu\n", g.size());
    for (size_t i=0; i<g.size(); ++i)
        printf("%zu%c", g[i], i<g.size()-1? ' ':'\n');

    for (size_t d: g)
        count_ += isort(A, d);

    return count_;
}

int main() {
    size_t N;
    scanf("%zu", &N);

    vector<int> A(N);
    for (size_t i=0; i<N; ++i)
        scanf("%d", &A[i]);

    size_t t=shsort(A);

    printf("%zu\n", t);
    for (size_t i=0; i<N; ++i)
        printf("%d\n", A[i]);

    return 0;
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#define ll long long
#define maxn 200010
inline ll read()
{
	ll x=0; char c=getchar(),f=1;
	for(;c<'0'||'9'<c;c=getchar())if(c=='-')f=-1;
	for(;'0'<=c&&c<='9';c=getchar())x=x*10+c-'0';
	return x*f;
}
inline void write(ll x)
{
	static int buf[20],len; len=0;
	if(x<0)x=-x,putchar('-');
	for(;x;x/=10)buf[len++]=x%10;
	if(!len)putchar('0');
	else while(len)putchar(buf[--len]+'0');
}
inline void writeln(ll x){write(x); putchar('\n');}
inline void writesp(ll x){write(x); putchar(' ');}
int a[maxn],d[maxn];
int cur[maxn],ans[maxn];
int n,m;
ll k;
int mul(int* a,int* b,int n)
{
	static int t[maxn];
	for(int i=1;i<=n;i++)
		t[i]=a[b[i]];
	for(int i=1;i<=n;i++)
		a[i]=t[i];
}
int main()
{
	n=read();
	for(int i=1;i<=n;i++)
		a[i]=read();
	int S=a[1];
	for(int i=1;i<n;i++)
		d[i]=a[i+1]-a[i];
	m=read(); k=read();
	for(int i=1;i<n;i++)
		cur[i]=ans[i]=i;
	for(int i=1;i<=m;i++){
		int x=read();
		std::swap(cur[x-1],cur[x]);
	}
	for(;k;k>>=1,mul(cur,cur,n-1))
		if(k&1)mul(ans,cur,n-1);
	ll X=a[1];
	for(int i=1;i<=n;i++){
		writeln(X);
		X+=d[ans[i]];
	}
	return 0;
}

#include<bits/stdc++.h>
// Begin Header {{{
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i,n) for(ll i=0; i<n; i++)
#define loop(i, j, n) for(ll i=j; i<n; i++)
#define all(x) (x).begin(), (x).end()
constexpr int INF  = 0x3f3f3f40;
const long long mod=1e9+7;
const long double PI = acos(0);
template <class T>void UNIQUE(vector<T> &x){
	sort(all(x));
	x.erase(unique(all(x)), x.end());
}

// }}} End Header
int main() {
	int n, ans = 0;
	cin >> n;
	vector<ll> l(n);
	map<ll, ll> dict;
	rep(i,n){
		cin >> l[i];
		dict[l[i]]++;
	}
	UNIQUE(l);
	if(l.size()<3){
		cout << 0 << endl;
		return 0;
	}
	rep(i,l.size()-2){
		loop(j, i+1, l.size()-1){
			loop(k, j+1, l.size()){
				if(l[i]+l[j] > l[k]){
					//cout << l[i] << l[j] << l[k] << endl;
					ans+=(dict[l[i]]*dict[l[j]]*dict[l[k]]);
				}
			}
		}
	}
	cout << ans << endl;

	return 0;
}


#include<stdio.h>
#include<map>
#include<algorithm>
using namespace std;
typedef pair<int,int> P;
P s[101];
int r[100];
int main(){
	int n;
	scanf("%d",&n);
	int i,j;
	for(i=0;i<n;i++)s[i]=P(0,i);
	for(i=0;i<n*(n-1)/2;i++){
		int a,b,c,d;
		scanf("%d %d %d %d",&a,&b,&c,&d);
		a--;b--;
		if(c>d)s[a].first+=3;
		if(c<d)s[b].first+=3;
		if(c==d){
			s[a].first+=1;
			s[b].first+=1;
		}
	}
	s[n]=P(-1,-1);
	sort(s,s+n);
	reverse(s,s+n);
	int p=0;
	int pr=0;
	while(p<n){
		r[s[p].second]=pr;
		while(s[p].first==s[p+1].first){
			p++;
			r[s[p].second]=pr;
		}
		p++;
		pr=p;
	}
	for(i=0;i<n;i++)printf("%d\n",r[i]+1);
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)  FOR(i,0,n)
#define req(i,n) for(int i = 1;i <=n;i++)
#define pai 3.14159265358979323846
const int INF = 1001001001;
typedef long long ll;
int A[3][3], N;
bool punched[3][3];
bool ok[3][3];
using Graph = vector<vector<int>>;
vector<vector<int>> field;
vector<bool> seen;

const int MOD = 1000000007;
typedef pair<int,int> P;

//最大公約数
int gcd(int a,int b){
	if (a%b == 0){
		return b;
	}
	else{
		return gcd(b,a%b);
	}

}

//最小公倍数
int lcm(int a,int b){
	return a /gcd(a,b) * b;
}

//素数判定
bool is_prime(long long N) {
    if (N == 1) return false;
    for (long long i = 2; i * i <= N; ++i) {
        if (N % i == 0) return false;
    }
    return true;
}

// 素因数分解
vector<pair<long long, long long> > prime_factorize(long long n) {
    vector<pair<long long, long long> > res;
    for (long long p = 2; p * p <= n; ++p) {
        if (n % p != 0) continue;
        int num = 0;
        while (n % p == 0) { ++num; n /= p; }
        res.push_back(make_pair(p, num));
    }
    if (n != 1) res.push_back(make_pair(n, 1));
    return res;
}

// 10進数から2進数
int binary(int bina){
    int ans = 0;
    for (int i = 0; bina>0 ; i++)
    {
        ans = ans+(bina%2)*pow(10,i);
        bina = bina/2;
    }
    return ans;
}

int main() {
  int n;
  cin >> n;
  ll x,a[n+1];
  cin >> x;
  rep (i,n){
    cin >> a[i];
  }
  int res = 0;
  sort (a,a+n);
  rep (i,n){
    if (x >= a[i]) {
      res ++;
      x -= a[i];
    }
    else break;
  }
  if (res == n && x) --res;
  cout << res << endl;
}


#include<bits/stdc++.h>

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef double lf;
typedef long double llf;
typedef std::pair<int,int> pii;

#define xx first
#define yy second

template<typename T> inline T max(T a,T b){return a>b?a:b;}
template<typename T> inline T min(T a,T b){return a<b?a:b;}
template<typename T> inline T abs(T a){return a>0?a:-a;}
template<typename T> inline bool repr(T &a,T b){return a<b?a=b,1:0;}
template<typename T> inline bool repl(T &a,T b){return a>b?a=b,1:0;}
template<typename T> inline T gcd(T a,T b){T t;if(a<b){while(a){t=a;a=b%a;b=t;}return b;}else{while(b){t=b;b=a%b;a=t;}return a;}}
template<typename T> inline T sqr(T x){return x*x;}
#define mp(a,b) std::make_pair(a,b)
#define pb push_back
#define I inline
#define mset(a,b) memset(a,b,sizeof(a))
#define mcpy(a,b) memcpy(a,b,sizeof(a))

#define fo0(i,n) for(int i=0,i##end=n;i<i##end;i++)
#define fo1(i,n) for(int i=1,i##end=n;i<=i##end;i++)
#define fo(i,a,b) for(int i=a,i##end=b;i<=i##end;i++)
#define fd0(i,n) for(int i=(n)-1;~i;i--)
#define fd1(i,n) for(int i=n;i;i--)
#define fd(i,a,b) for(int i=a,i##end=b;i>=i##end;i--)
#define foe(i,x)for(__typeof((x).end())i=(x).begin();i!=(x).end();++i)

struct Cg{I char operator()(){return getchar();}};
struct Cp{I void operator()(char x){putchar(x);}};
#define OP operator
#define RT return *this;
#define RX x=0;char t=P();while((t<'0'||t>'9')&&t!='-')t=P();bool f=0;\
if(t=='-')t=P(),f=1;x=t-'0';for(t=P();t>='0'&&t<='9';t=P())x=x*10+t-'0'
#define RL if(t=='.'){lf u=0.1;for(t=P();t>='0'&&t<='9';t=P(),u*=0.1)x+=u*(t-'0');}if(f)x=-x
#define RU x=0;char t=P();while(t<'0'||t>'9')t=P();x=t-'0';for(t=P();t>='0'&&t<='9';t=P())x=x*10+t-'0'
#define TR *this,x;return x;
I bool IS(char x){return x==10||x==13||x==' ';}template<typename T>struct Fr{T P;I Fr&OP,(int&x)
{RX;if(f)x=-x;RT}I OP int(){int x;TR}I Fr&OP,(ll &x){RX;if(f)x=-x;RT}I OP ll(){ll x;TR}I Fr&OP,(char&x)
{for(x=P();IS(x);x=P());RT}I OP char(){char x;TR}I Fr&OP,(char*x){char t=P();for(;IS(t);t=P());if(~t){for(;!IS
(t)&&~t;t=P())*x++=t;}*x++=0;RT}I Fr&OP,(lf&x){RX;RL;RT}I OP lf(){lf x;TR}I Fr&OP,(llf&x){RX;RL;RT}I OP llf()
{llf x;TR}I Fr&OP,(uint&x){RU;RT}I OP uint(){uint x;TR}I Fr&OP,(ull&x){RU;RT}I OP ull(){ull x;TR}};Fr<Cg>in;
#define WI(S) if(x){if(x<0)P('-'),x=-x;char s[S],c=0;while(x)s[c++]=x%10+'0',x/=10;while(c--)P(s[c]);}else P('0')
#define WL if(y){lf t=0.5;for(int i=y;i--;)t*=0.1;if(x>=0)x+=t;else x-=t,P('-');*this,(ll)(abs(x));P('.');if(x<0)\
x=-x;while(y--){x*=10;x-=floor(x*0.1)*10;P(((int)x)%10+'0');}}else if(x>=0)*this,(ll)(x+0.5);else *this,(ll)(x-0.5);
#define WU(S) if(x){char s[S],c=0;while(x)s[c++]=x%10+'0',x/=10;while(c--)P(s[c]);}else P('0')
template<typename T>struct Fw{T P;I Fw&OP,(int x){WI(10);RT}I Fw&OP()(int x){WI(10);RT}I Fw&OP,(uint x){WU(10);RT}
I Fw&OP()(uint x){WU(10);RT}I Fw&OP,(ll x){WI(19);RT}I Fw&OP()(ll x){WI(19);RT}I Fw&OP,(ull x){WU(20);RT}I Fw&OP()
(ull x){WU(20);RT}I Fw&OP,(char x){P(x);RT}I Fw&OP()(char x){P(x);RT}I Fw&OP,(const char*x){while(*x)P(*x++);RT}
I Fw&OP()(const char*x){while(*x)P(*x++);RT}I Fw&OP()(lf x,int y){WL;RT}I Fw&OP()(llf x,int y){WL;RT}};Fw<Cp>out;

const int N=100007;

int n,t[N][3],s[N];

int main()
{
	in,n;
	fo0(l,3)fo1(i,n)in,t[i][l];
	fo1(i,n)
	{
		int p=(t[i][0]-1)/3;
		//out,p,'\n';
		bool f1=1,f2=1;
		fo0(j,3)f1&=t[i][j]==p*3+j+1;
		fo0(j,3)f2&=t[i][j]==p*3+3-j;
		if((!f1&&!f2)||(~(i^p)&1))return puts("No"),0;
		s[i]=f1?p+1:-p-1;
	}
	//fo1(i,n)out,s[i],' ';out,'\n';
	int a[2]={0,0},b[2]={0,0};
	fo1(i,n)a[i&1]^=s[i]<0;
	fo1(i,n)while(abs(s[i])!=i)std::swap(s[i],s[abs(s[i])]),b[i&1]^=1;
	out,a[0]==b[1]&&a[1]==b[0]?"Yes":"No",'\n';
}

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int N=100005;
int n,a[5][N],i,j,k,x,y,b[N],c[2];
int main()
{
	scanf("%d",&n);
	for(i=1;i<=3;++i)
		for(j=1;j<=n;++j)
			scanf("%d",a[i]+j);
	for(i=1;i<=n;++i)
	{
		x=y=a[1][i];
		for(j=2;j<=3;++j)
			x=max(x,a[j][i]),y=min(y,a[j][i]);
		if(x-y!=2||y+1!=a[2][i])
		{
			printf("No");
			return 0;
		}
		b[i]=x/3;
		if(a[1][i]!=y)
			c[i&1]=!c[i&1];
		if((x/3-i)%2)
		{
			printf("No");
			return 0;
		}
	}
	for(i=1;i<=n;++i)
	while(i!=b[i])
	{
		swap(b[i],b[b[i]]);
		c[i&1^1]=!c[i&1^1];
	}
	if(c[0]||c[1])
		printf("No");
	else
		printf("Yes");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define lli long long int
# define rep(i,n) for(int i=0;i<n;i++)
void solve(){
    lli n,x,t;
    cin>>n>>x>>t;
    lli y = (t*n)/x;
    if(y%t==0)
        cout<<y<<endl;
    else{
        y +=t;
        cout<<y - y%t<<endl;
    }
}
int main()
{
    int t;
   // cin>>t;
    t=1;
    while(t--){
        solve();
    }
}

#include <bits/stdc++.h>
#include <vector>
# include <cstdint>
#define FOR(i,l,r) for(int i=(l);i<(r);++i)
#define RFOR(i,l,r) for(int i=(l);i>=(int)(r);i--)
#define rep(i,n)  FOR(i,0,n)
#define rrep(i,n)  RFOR(i,n-1,0)

#define int long long
using namespace std;
const int MX = 1e6;
const int inf = 1e13;
const int mod = 1e9+7;
#define ll long long

signed main() {
  int n;
  string s,t;
  cin >> n >> s >>t;
  int sz = s.length();
  int tz = t.length();
  if(sz+tz<=n) {
      cout << n << endl;
      return 0;
  }
  int ma = 0;
  rrep(i,tz) {
      int ss = sz-1;
      int tmp=0;
      while(i>=0 && ss>=0) {
         // cout << s[ss]<<t[i]<<ss<<" "<<i<<" " << endl;
          if(s[ss]==t[i]) {
              if(i==0) {
                  cout << sz+tz-(sz-ss) << endl;
                return 0;
              }
              ss--;
              tmp++;
          }else {
              if(tmp>0) i++;
              break;
          }
          i--;
      }
      ma=max(tmp,ma);
      //cout << ma << endl;
  }
  cout << sz+tz-ma << endl;
  return 0;
}


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

using str = string;
using ss = stringstream;

str a[60];

#define fi first
#define se second

#define DIRMAX 4
pair<int,int> dir[4]=
{
    { 0,-1},
    {-1, 0},
    { 1, 0},
    { 0, 1},
};

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

    int h,w;cin>>h>>w;

    ss maketop;
    for(int i=0;i<w+2;i++)maketop<<".";

    a[0]=maketop.str();
    a[h+1]=maketop.str();

    for(int i=0;i<h;i++)
    {
        str s;
        cin>>s;
        ss makemid;

        makemid<<"."<<s<<".";

        a[i+1]=makemid.str();
    }

    bool ans=true;

    for(int i=1;i<=h;i++)for(int j=1;j<=w;j++)
    {
        if(a[i][j]=='#')
        {
            bool c=true;
            for(int k=0;k<DIRMAX;k++)
            if(a[i+dir[k].fi][j+dir[k].se]=='#')
            {
                c=false;
                break;
            }

            if(c)
            {
                ans=false;
                break;
            }
        }
    }

    puts(ans?"Yes":"No");

    return 0;
}

#include <bits/stdc++.h>
#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++)
using namespace std;
using P = pair<int, int>;
using ll = long long;
using M = map<int, int>;

int main()
{
  int n, m;
  cin >> n >> m;
  char c[n][m];
  rep(i, n){
    rep(j, m) cin >> c[i][j];
  }

  string s = "Yes";
  int cnt;
  rep(i, n){
    rep(j, m){
      if(c[i][j] == '.') continue;
      cnt = 0;
      if(i != 0 && c[i - 1][j] == '#') cnt++;
      if(i != n - 1 && c[i + 1][j] == '#') cnt++;
      if(j != 0 && c[i][j - 1] == '#') cnt++;
      if(j != m - 1 && c[i][j + 1] == '#') cnt++;
      if(cnt == 0) {
        s = "No";
      }
    }
  }
  cout << s << endl;

  return 0;
}


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

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

int N, S;
vector<pii> P, Q;
vector<int> sol;

int main() {
    scanf("%d %d", &N, &S);

    for(int i = 0; i < N; i++) {
        int x, p; scanf("%d %d", &x, &p);
        if(x < S) P.push_back({x, p});
        else Q.push_back({x, p});
    }
    reverse(Q.begin(), Q.end());

    if(P.size() == 0) {
        printf("%d", Q[0].first - S);
        return 0;
    }
    if(Q.size() == 0) {
        printf("%d", S - P[0].first);
        return 0;
    }

    int pos1 = 1, pos2 = 1, dir = (P[0].second < Q[0].second);
    ll sum = 0;
    sol.push_back(P[0].first);
    sol.push_back(Q[0].first);
    sum += P[0].second + Q[0].second;
    if(P[0].second >= Q[0].second) swap(sol[0], sol[1]);
    while(1) {
        if(dir) {
            while(pos1 < P.size() && sum > P[pos1].second) {
                sum += P[pos1++].second;
            }
            if(pos1 < P.size() && sum <= P[pos1].second) {
                sol.push_back(P[pos1].first);
                sum += P[pos1].second;
                pos1++;
                dir ^= 1;
            }
            else break;
        }
        else {
            while(pos2 < Q.size() && sum >= Q[pos2].second) {
                sum += Q[pos2++].second;
            }
            if(pos2 < Q.size() && sum < Q[pos2].second) {
                sol.push_back(Q[pos2].first);
                sum += Q[pos2].second;
                pos2++;
                dir ^= 1;
            }
            else break;
        }
    }
    sol.push_back(S);

    ll ans = 0;
    for(int i = 0; i < (int)sol.size() - 1; i++) {
        ans += abs(sol[i] - sol[i + 1]);
    }
    printf("%lld", ans);
}

#include <iostream>
using namespace std;
long long p[100001],x[100001],ans=0;
int n,s,l,r;
int main(){
	cin>>n>>s;
	for (int i=1;i<=n;i++)
		cin>>x[i]>>p[i];
	l=1;r=n;
	while (l<=r){
	if (s<=x[l]){
		cout<<x[r]-s+ans;
		return 0;
	}
	else if (s>=x[r]){
		cout<<ans+s-x[l];
		return 0;
	}  //所有住处都在s的一侧，可以直接开过去
	if (p[l]>=p[r]){
		ans+=x[r]-x[l];
		while (l<r&&p[l]>=p[r]) p[l]+=p[r--];
	}
	else{
		ans+=x[r]-x[l];
		while (l<r&&p[l]<p[r]) p[r]+=p[l++];
	}
	}
	return 0;
}


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

int n, m;
int a[1020], b[1020];
int vote[1020];

int main()
{
    while(cin >> n>> m)
    {
        memset(vote, 0, sizeof vote);
        for(int i=1;i<=n;i++) scanf("%d",&a[i]);
        for(int i=1;i<=m;i++) scanf("%d",&b[i]);

        for(int i=1;i<=m;i++)
        {
            int j = 1;
            while(a[j] > b[i]) j++;
            vote[j]++;
        }
        printf("%d\n",max_element(vote+1,vote+n+1)-vote);
    }
    return 0;
}


#include<iostream>

int power[101];
int motor[101];
int cable[101];
int a, b, c, N;
int la[1024], lb[1024], lc[1024], lr[1024];

void print(){
	int i;
	for( i = 1; i <= a; i++ ){
		std::cout << power[i] << std::endl;
	}
	for( i = 1; i <= b; i++ ){
		std::cout << motor[i] << std::endl;
	}
	for( i = 1; i <= c; i++ ){
		std::cout << cable[i] << std::endl;
	}
	return;
}

int input(){
	int i;
	std::cin >> a >> b >> c;
	std::cin >> N;
	for( i = 1; i <= a; i++ ){
		power[i] = 2;
	}
	for( i = 1; i <= b; i++ ){
		motor[i] = 2;
	}
	for( i = 1; i <= c; i++ ){
		cable[i] = 2;
	}
	for( i = 0; i < N; i++ ){
		std::cin >> la[i] >> lb[i] >> lc[i] >> lr[i];
	}
	return (a+b+c);
}

void check(){
	int x, y, z, i;
	for( i = 0; i < N; i++ ){
		if( lr[i] == 0 ){ continue; }
		x = la[i];
		y = lb[i] - a;
		z = lc[i] - a - b;
		power[x] = 1;
		motor[y] = 1;
		cable[z] = 1;
	}
	for( i = 0; i < N; i++ ){
		if( lr[i] == 1 ){ continue; }
		x = la[i];
		y = lb[i] - a;
		z = lc[i] - a - b;
		if( motor[y] == 1 && cable[z] == 1 ){
			power[x] = 0;
		}else if( power[x] == 1 && cable[z] == 1 ){
			motor[y] = 0;
		}else if( power[x] == 1 && motor[y] == 1 ){
			cable[z] = 0;
		}
	}
	return;
}

int main(){
	while( input() != 0 ){
		check();
		print();
	}
	return 0;
}

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
using namespace std;
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){return o<<"("<<p.fs<<","<<p.sc<<")";}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){o<<"sz = "<<vc.size()<<endl<<"[";for(const T& v:vc) o<<v<<",";o<<"]";return o;}
vector<int> c2is[26];
vector<int> ls,rs;
int N;
string S,T;
int solve(){
	cin>>N;
	cin>>S>>T;
	if(S==T){
		return 0;
	}
	rep(i,N){
		c2is[S[i]-'a'].pb(i);
	}
	int now=N-1;
	int r=N-1;
	for(int i=N-2;i>=-1;i--){
		if(i==-1 || T[i]!=T[i+1]){
			int l=i+1;
			int c = T[i+1] -'a';
			int id = upper_bound(all(c2is[c]),min(now,l))-c2is[c].begin()-1;
			if(id==-1){
				return -1;
			}
			int v = c2is[c][id];
			// show(l);
			// show(r);
			// show(v);
			// puts("");
			ls.pb(v);
			rs.pb(r);

			r=i;
			now=v-1;
		}
	}
	reverse(all(ls));
	reverse(all(rs));
	int K=ls.size();
	// show(ls);
	// show(rs);
	rep(i,K) ls[i]-=i;
	int ans=0;
	rep(i,K){
		int r = rs[i];
		int id = lower_bound(all(ls),r-i)-ls.begin()-i;
//		show(id);
		chmax(ans,id);
	}
	return ans;
}
int main(){
	cout<<solve()<<endl;
}


#include <iostream>
#include <cstdint>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cctype>
#include <cassert>
#include <climits>
#include <string>
#include <bitset>
#include <cfloat>
#include <unordered_set>
#pragma GCC optimize("Ofast")
using namespace std;
typedef long double ld;
typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<ll> vll;
typedef vector<pair<int, int>> vpii;
typedef vector<vector<int>> vvi;
typedef vector<vector<char>> vvc;
typedef vector<vector<string>> vvs;
typedef vector<vector<ll>> vvll;

#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rrep(i, n) for (ll i = 1; i <= (n); ++i)
#define irep(it, stl) for (auto it = stl.begin(); it != stl.end(); it++)
#define drep(i, n) for (ll i = (n)-1; i >= 0; --i)
#define mes(a) cout << (a) << endl
#define dmes(a, b) cout << (a) << " " << (b) << endl
#define re0 return 0
#define mp(p, q) make_pair(p, q)
#define pb(n) push_back(n)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define Sort(a) sort(a.begin(), a.end())
#define rSort(a) sort(a.rbegin(), a.rend())
#define MATHPI acos(-1)
#define itn int
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
template <class T>
inline bool chmax(T &a, T b)
{
    if (a < b)
    {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
inline bool chmin(T &a, T b)
{
    if (a > b)
    {
        a = b;
        return 1;
    }
    return 0;
}
struct io
{
    io()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
    }
};
const int INF = INT_MAX;
const ll LLINF = 1LL << 60;
const ll MOD = 1000000007;
const double EPS = 1e-19;

int main() {
    ll n, y;
    cin >> n >> y;
    y /= 1000;
    ll yc = 0;

    for (ll i = 0; i < n+1; i++) {
        for(ll j = 0; j < n+1; j++) {
            if (i+j > n) continue;
            yc = 10*i+5*j+(n-(i+j));
            if (yc == y) {
                printf("%lld %lld %lld", i, j, n-(i+j));
                re0;
            }
        }
    }

    mes("-1 -1 -1");

}


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

using namespace std;

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

#define BIL ((ll)1e9)
#define MOD ((ll)1e9+7)
#define INF (1LL<<60)           //1LL<<63でオーバーフロー
#define inf (1<<29)             //1<<29でオーバーフロー

int main(int argc,char* argv[]){
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(20);

    int n;
    ll y;
    cin >> n >> y;

    for(ll i=0;10000*i<=y;i++){
        ll x=y-10000*i;
        for(ll j=0;5000*j<=x;j++){
            ll k=(x-5000*j)/1000;
            if(i+j+k==n){
                cout << i << " " << j << " " << k << endl;
                return 0;
            }
        }
    }

    cout << -1 << " " << -1 << " " << -1 << endl;

    return 0;
}

#include<iostream>
#include<cmath>

using namespace std;

int main() {
  int N, Y;
  cin >> N >> Y;
  string ans = "-1 -1 -1";
  for (int i = 0; i <= N; ++i) {
    for (int j = 0; j <= N; ++j) {
      int k = N - j - i;
      if (k < 0) continue;
      if (i * 10000 + j * 5000 + k * 1000 == Y) {
        ans = to_string(i) + ' ' + to_string(j) + ' ' + to_string(k);
        break;
      }
    }
    if (ans != "-1 -1 -1") break;
  }
  cout << ans << endl;
}


#include<bits/stdc++.h>

using namespace std;

int main(){

  int k;
  string str;
  cin>>k;
  cin.ignore();
  getline(cin,str);
  int len = str.length();
  if(len>k){
   str[k]='.';
    k++;
   str[k] ='.';
    k++;
    str[k]='.';
    for(int i=0;i<k+1;i++)
      cout<<str[i];
  }
  else
    cout<<str;

}

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

int main()
{
    int t=1;
    //cin>>t;

    while( t-- )
    {
        int k;
        cin>>k;

        string s;
        cin>>s;

        if( s.size()<=k )
            cout<<s;
        else
        {
            for( int i=0 ; i<k ; i++ )
                cout<<s[i];
            cout<<"..."<<endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define repd(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
#define all(x) (x).begin(),(x).end()
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; }
typedef long long ll;
const long long INF = 1LL << 60;
typedef pair<int, int> P;
int main()
{
    int M;
    cin >> M;
    ll S = 0;
    ll C = 0;
    rep(i, M) {
        ll d, c;
        cin >> d >> c;
        S += d * c;
        C += c;
    }

    ll ans = C - 1;
    ans += (S - 1) / 9;
    cout << ans << endl;
    return 0;
}

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

int main(){
    long long a;
    cin>>a;
    vector<long long>v(2*a);
    for(long long i=0;i<2*a;i++){
         cin>>v[i];
    }
    priority_queue<long long> p(v.begin(),v.end());
    long long sum=0;
    while(!p.empty()){
        p.pop();
        sum+=p.top();
        p.pop();
    }
    cout<<sum;
}


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

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

	string ans;

	do {
		ans.push_back(97 + (--n % 26));
		n /= 26;
	} while (n > 0);

	reverse(ans.begin(), ans.end());
	cout << ans << endl;
}

#include <bits/stdc++.h>

#define rng(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, b) rng(i, 0, b)
#define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define per(i, b) gnr(i, 0, b)

using namespace std;

using ll = long long;
using P = pair<int, int>;

int main() {
  string s;
  cin >> s;
  vector<int> cnt(26, 0);
  for (char c : s) {
    cnt.at(c - 'a')++;
  }
  bool exists = false;
  string res(s);
  bool nextExists = false;
  char next;
  rep(i, cnt.size()) {
    if (cnt.at(i) == 0) {
      nextExists = true;
      next = 'a' + i;
      break;
    }
  }
  if (nextExists) {
    res.push_back(next);
    exists = true;
  } else {
    while (!res.empty()) {
      char c = res.substr(res.size() - 1, 1).c_str()[0];
      res.pop_back();
      cnt.at(c - 'a')--;
      bool replaceExists = false;
      char replace;
      for (int i = c - 'a' + 1; i < cnt.size(); i++) {
        if (cnt.at(i) == 0) {
          replaceExists = true;
          replace = 'a' + i;
          break;
        }
      }
      if (replaceExists) {
        res.push_back(replace);
        exists = true;
        break;
      }
    }
  }
  if (exists) {
    cout << res << endl;
  } else {
    cout << -1 << endl;
  }
  return 0;
}


#define fi first
#define se second
#define mp make_pair
#define pb push_back

#define LOCAL 1

#include<bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define dbg(...) cout<<#__VA_ARGS__ ": ["; for(auto e: __VA_ARGS__){cout<<e<<", ";} cout<<"]\n"
#define dbg1(...) cout<<#__VA_ARGS__ " "<< __VA_ARGS__
#else
#define dbg(...) 1;
#define dbg1(...) 1;
#endif

typedef long long ll;
typedef unsigned long long ull;
constexpr int R=5e5+69, INF=2e9+6969;

int qq;

int main()
{
	scanf("%d", &qq);
	while(qq--)
	{
		ll n,t[R];
		string s;
		scanf("%lld", &n);
		for(int i=0;i<n;++i) scanf("%lld", &t[i]);
		cin >> s;
		vector <ll> base;
		base.clear();
		bool ok=1;
		for(int i=n-1;i>=0;--i)
		{
			ll x = t[i];
			for(ll e: base) x = min(x,e^x);
			if(x==0) continue;
			if(s[i]=='0') base.pb(x);
			else
			{
				ok=0;
				break;
			}
		}
		puts(ok ? "0" : "1");
	}
	return 0;
}

#include <iostream>
#include <algorithm>
using namespace std;
int n, a[1111111], c, g[20];
int main() {
	// cin.tie(0);
	// ios::sync_with_stdio(false);
	cin >> n;
	for(int i = 0; i < n; i++) {
		cin >> a[i];
	}
	g[0] = 1; c = 0;
	while(true) {
		g[c + 1] = g[c] * 3 + 1;
		if(g[++c] >= n) break;
	}
	int ret = 0;
	for(int i = c - 1; i >= 0; i--) {
		for(int j = g[i]; j < n; j++) {
			int v = a[j], w = j - g[i];
			while(w >= 0 && a[w] > v) {
				a[w + g[i]] = a[w];
				w -= g[i];
				ret++;
			}
			a[w + g[i]] = v;
		}
	}
	cout << c << endl;
	for(int i = c - 1; i >= 0; i--) {
		if(i) cout << g[i] << ' ';
		else cout << g[i] << endl;
	}
	cout << ret << endl;
	for(int i = 0; i < n; i++) {
		cout << a[i] << endl;
	}
	return 0;
}


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

bool match( char ch, const string& p ) {
	for( auto a: p ) {
		if( a == ch ) { return true; }
	}
	return false;
}

int main() {
	ios_base::sync_with_stdio( false );

	string s;
	while( cin >> s ) {
		int n = (int)s.size();
		int result = 0;

		for( int i = n-1; i >= 0; --i ) {
			if( s[i] == 'I' ) {
				result += i < n-1 && s[i+1] != 'I' ? -1 : 1;
			}
			else if( s[i] == 'V' ) {
				result += i < n-1 && match( s[i+1], "XLCDM" ) ? -5 : 5;
			}
			else if( s[i] == 'X' ) {
				result += i < n-1 && match( s[i+1], "LCDM" ) ? -10 : 10;
			}
			else if( s[i] == 'L' ) {
				result += i < n-1 && match( s[i+1], "CDM" ) ? -50 : 50;
			}
			else if( s[i] == 'C' ) {
				result += i < n-1 && match( s[i+1], "DM" ) ? -100 : 100;
			}
			else if( s[i] == 'D' ) {
				result += i < n-1 && match( s[i+1], "M" ) ? -500 : 500;
			}
			else if( s[i] == 'M' ) {
				result += 1000;
			}
		}

		cout << result << endl;
	}

	return 0;
}

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

int main() {
	char c[101],m[7]={'I','V','X','L','C','D','M'};
	int s,i,j,h,n[7]={1,5,10,50,100,500,1000};
  while(gets(c)!=NULL) {
    s=0; h=10;
	for (i=0;i<strlen(c);i++) {
     for (j=0;j<7;j++) if (c[i]==m[j]) break;
	 s+=n[j];
	 if (h<j) s-=n[h]*2;
	 h=j;
	}
    cout << s << endl;
  }
  return 0;
}


#include <iostream>
#include <utility>
#include <string>
#include <cstring>
#include <vector>
#include <map>
#include <algorithm>
#include <stack>

using namespace std;

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

const int INF = 1 << 28;

int w, h;
int field[32][32];
int sx, sy;
int gx, gy;

int ofs[4][2] = {
  {0,1},
  {1,0},
  {-1,0},
  {0,-1}
};

bool ok(int p, int n)
{
  return 0 <= p && p < n;
}

bool dfs(int x, int y, int d, int n)
{
  if( d >= n ) {
    return false;
  }

  bool res = false;
  rep(i, 4) {
    int dx = ofs[i][0];
    int dy = ofs[i][1];
    int nx = x;
    int ny = y;
    int idx = 0;
    while(ok(nx, w) && ok(ny, h) && !field[ny][nx]) {
      nx += dx;
      ny += dy;
      if(nx == gx && ny == gy) {
        return true;
      }
      ++idx;
    }
    if( idx > 1 && ok(nx, w) && ok(ny, h) ) {
      field[ny][nx] = 0;
      res |= dfs(nx - dx, ny - dy, d + 1, n);
      field[ny][nx] = 1;
    }
  }
  return res;
}

int idfs(int sx, int sy)
{
  int idx = 1;
  while( idx <= 10 && !dfs(sx, sy, 0, idx) ) {
    ++idx;
  }
  if( idx > 10 ) {
    return -1;
  } else {
    return idx;
  }
}

int main()
{
  while( cin >> w >> h, (h|w) ) {
    rep(i, h) rep(j, w) {
      int v;
      cin >> v;
      field[i][j] = v & 1;
      if( v & 2 ) {
        if( v & 1 ) {
          gx = j;
          gy = i;
        } else {
          sx = j;
          sy = i;
        }
      }
    }

    cout << idfs(sx, sy) << endl;
  }
  return 0;
}

#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;
using ll = long long;

#define all(c) (c).begin(), (c).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second

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

int w, h;
int board[21][21];
int ans = INF;
bool valid(int y, int x) { return 0 <= y && y < h && 0 <= x && x < w; }
void print() {
    rep(y, h) {
        rep(x, w) printf("%d ", board[y][x]);
        printf("\n");
    }
}
void dfs(int y, int x, int cnt) {
    // printf("debug: %d %d %d\n", y, x, cnt);
    // print();
    if (cnt == 10) return;
    rep(k, 4) {
        int ny = y;
        int nx = x;
        if (!valid(ny + dy[k], nx + dx[k])) continue;
        if (board[ny + dy[k]][nx + dx[k]] == 1) continue;
        while (1) {
            if (!valid(ny + dy[k], nx + dx[k])) break;
            ny += dy[k];
            nx += dx[k];
            if (board[ny][nx] == 3) {
                ans = min(cnt + 1, ans);
                return;
            }
            if (board[ny][nx] == 1) {
                board[ny][nx] = 0;
                dfs(ny - dy[k], nx - dx[k], cnt + 1);
                board[ny][nx] = 1;
                break;
            }
        }
    }
}
int main() {
    while (cin >> w >> h, w) {
        ans = INF;
        rep(i, h) rep(j, w) cin >> board[i][j];
        rep(y, h) rep(x, w) if (board[y][x] == 2) dfs(y, x, 0);
        if (ans == INF) ans = -1;
        cout << ans << endl;
    }
    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() {
  string S;
  cin>>S;
  map<char, int> amap;
  bool can = false;
  if(S.size()!=26) {
    can = true;
    for(int i=0;i<S.size();++i) {
      amap[S[i]]++;
    }
    for(char a='a';a<='z';++a) {
      if(amap[a]==0) {
        S.push_back(a);
        break;
      }
    }
  }
  else {
    vector<char> arr;
    arr.push_back(S[25]);
    for(int i=24;i>=0;--i) {
      for(int j=0;j<arr.size();++j) {
        if(arr[j]>S[i]) {
          can = true;
          S[i] = arr[j];
          for(int j=0;j<25-i;++j) S.pop_back();
          break;
        }
      }
      if(can) break;
      arr.push_back(S[i]);
      sort(arr.begin(), arr.end());
    }
  }
  if(can) cout<<S<<endl;
  else cout<<-1<<endl;
}

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

int main(){
    string s;
    cin>>s;
    vector<int> cnt(26,0);

    for(int i=0; i<s.size(); i++){
        cnt[s[i] - 'a']++;
    }

    string ans;

    if(s.size() != 26){
        ans = s;
        for(int i=0; i<26; i++){
            if(cnt[i] == 0){
                ans += (char)('a'+i);
                break;
            }
        }
    }

    else{

        if(s == "zyxwvutsrqponmlkjihgfedcba") ans = "-1";

        else{
            string tmp = s;
            next_permutation(s.begin(),s.end());

            ans = "";

            for(int i=0; i<s.size(); i++){
                ans += s[i];
                if(s[i] > tmp[i]) break;
            }
        }
    }

    cout<<ans<<endl;

    return 0;
}


#include <atcoder/scc>;
#include <atcoder/convolution>
#include <atcoder/modint>
#include <atcoder/math>
#include <atcoder/fenwicktree>
#include <atcoder/dsu>
#include <iostream>
#include <numeric>
#include <cmath>
#include <limits>
#include <stdio.h>
#include <iomanip>
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
using namespace std;
using namespace atcoder;
using ll = long long;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
ll Max(ll(a), ll(b), ll(c)) {
	return max(max(a, b), c);
}
ll Min(ll(a), ll(b), ll(c)) {
	return min(min(a, b), c);
}
ll mod = 998244353;
int main() {
	ll N, Q;
	cin >> N >> Q;
	vector<ll> t(Q);
	vector<ll> u(Q);
	vector<ll> v(Q);
	fenwick_tree<long long> fw(N);
	rep(i, N) {
		ll a;
		cin >> a;
		fw.add(i, a);
	}
	rep(i,Q) {
		cin >> t.at(i) >> u[i] >> v[i];

	}

	rep(i, Q) {
		if (t[i] == 0) {
			fw.add(u[i], v[i]);
		}
		else {
			cout << fw.sum(u[i], v[i]) << endl;;
		}
	}
}

#define FILEIO(a) freopen(a".in", "r", stdin); freopen(a".out", "w", stdout);
#include <cstdio>
#include <algorithm>
#include <numeric>

typedef long long ll;

const int MaxN = 100005;

inline int div_ceil(ll a, int b) {
	return (a + b - 1) / b;
}

int n, W;
struct node {
	int b, l, r; ll val;

	node() = default;
	node(int b, int l, int r): b(b), l(l), r(r) {
		val = (ll)r * (W - b) + (ll)l * b;
	}

	inline int calc(ll s) const {
		if (s >= val) return W;
		ll tmp = s - (ll)b * l;
		return tmp > 0 ? b + div_ceil(tmp, r) : div_ceil(s, l);
	}
} dat[MaxN];

ll solve() {
	scanf("%d%d", &n, &W);

	ll sum = 0;
	for (int i = 0; i < n; ++i) {
		int b, l, r;
		scanf("%d%d%d", &b, &l, &r);
		dat[i] = node(b, l, r);
		sum -= (ll)l * b;
	}

	std::sort(dat, dat + n, [](const node &a, const node &b) {
		return a.val > b.val;
	});

	for (int i = 0; i < n; ++i) {
		ll val = dat[i].val;
		if (sum + val >= 0) {
			int res = W;
			for (int j = 0; j < i; ++j) {
				res = std::min(res, dat[j].calc(-sum + dat[j].val - val));
			}
			for (int j = i; j < n; ++j) {
				res = std::min(res, dat[j].calc(-sum));
			}
			return (ll)i * W + res;
		}
		sum += val;
	}

	return (ll)n * W;
}

int main() {
	printf("%lld\n", solve());
	return 0;
}


/*
  Contest 090
  A - Diagonal String
  Rakesh Kumar --> 18/09/2020
 */

#include <bits/stdc++.h>

inline bool palindrome(const std::string& s) {
    int i = 0;
    int j = s.size() - 1;
    while (i < j) {
        if (s[i++] != s[j--])
            return false;
    }
    return true;
}

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

    int a = 0, b = 0;
    std::cin >> a >> b;

    int count = 0;
    for (int n = a; n <= b; ++n)
        if (palindrome(std::to_string(n)))
            ++count;
    std::cout << count << std::endl;

    return 0;
}

#include<iostream>
#include<string>
#include<vector>
#include <algorithm>
using namespace std;
int kaibunnsuu (int n){
    vector<int> N;
    while(n > 0){
        int ind_digit = n % 10;
        N.push_back(ind_digit) ;
        n = n /10;
    }
    int size = N.size() ;
    vector<int> rev_N(size);
    for(int i=0;i<size;i++){
        rev_N.at(i) = N.at(i);
    }
    reverse(rev_N.begin(), rev_N.end());

    bool judge = false;
    for(int i=0;i<size;i++){
        if( rev_N.at(i)!=N.at(i) ){
            break;
        }
        else if((i==size -1)&&(rev_N.at(i)==N.at(i))){
            judge = true;
        }
    }
    if(judge){
        return 1;
    }
    else{
        return 0;
    }
}

int main() {
    int A,B;
    cin >> A >> B;
    int count =0;
    for(int i=A;i<=B;i++){
        count = count +kaibunnsuu(i);
    }
    cout << count << endl;
}


#include <bits/stdc++.h>
using namespace std;
bool is(int i){
	if(i%3==0||i%10==3)return true;
	while(i/=10)if(i%10==3)return true;
	return false;
}
int main(){
	int a=1,u;cin>>u;for(;a<=u;a++)if(is(a))cout<<" "<<a;cout<<endl;
}

#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <ctype.h>
#include <math.h>
#include <stack>
#include <string>
#include <string.h>
using namespace std;
double PI = 3.1415926535897932;
long mod = 1000000007;

const long INF = (1<<30);

int main() {
  string s;
  cin >> s;
  if(s[0] == '7' || s[1] == '7' || s[2] == '7')
    cout << "Yes";
  else cout << "No";
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#define SENTINEL 2e9
using namespace std;

long long  cnt;
void Merge(int* A, int left, int mid, int right){
  int n1 = mid - left;
  int n2 = right - mid;
  int *L,*R;
  L = new int[n1+1]; R=new int[n2+1];
  for(int i=0;i<n1;i++){
      L[i] = A[left + i];
  }
  for(int i = 0; i< n2;i++){
      R[i] = A[mid + i];
  }
  L[n1] = SENTINEL;
  R[n2] = SENTINEL;
  int i = 0;
  int j = 0;
  for(int k = left; k < right; k++){
    if(L[i] <= R[j]){
        A[k] = L[i];
        i = i + 1;
    } else {
		cnt += n1-i;
        A[k] = R[j];
        j = j + 1;
    }
  }
}

void Merge_Sort(int* A, int left, int right){
  if(left+1 < right){
    int mid = (left + right)/2;
    Merge_Sort(A, left, mid);
    Merge_Sort(A, mid, right);
    Merge(A, left, mid, right);
  }
}

int main(){
    int *a;
    int n;
    cnt = 0;
    scanf("%d",&n);
    a = new int[n];
    for(int i=0;i<n;i++){
        scanf(" %d",&a[i]);
    }
    Merge_Sort(a, 0, n);
    cout << cnt << endl;
    return 0;
}

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

template <typename T>
class FenwickTree
{
public:
  int N;
  vector<T> dat;

  FenwickTree(int n, T m = 0)
  {
    init(n, m);
  }

  void init(int n, int m)
  {
    N = n;
    dat.resize(n + 1);
    for (int i = 0; i < dat.size(); i++)
    {
      dat[i] = m;
    }
  }

  int64_t sum(int i)
  {
    T s = 0;
    while (i > 0)
    {
      s += dat[i];
      i -= i & -i;
    }
    return s;
  }

  void add(int i, T x)
  {
    while (i <= N)
    {
      dat[i] += x;
      i += i & -i;
    }
  }
};

int main()
{
  int n;
  cin >> n;
  vector<int64_t> A(n);
  for (int i = 0; i < n; i++)
  {
    cin >> A.at(i);
  }
  vector<int64_t> copy = A;
  map<int64_t, int64_t> mp;
  sort(copy.begin(), copy.end());
  copy.erase(unique(copy.begin(), copy.end()), copy.end());
  int m = copy.size();
  for (int i = 0; i < m; i++)
  {
    mp[copy[i]] = i + 1;
  }
  for (int i = 0; i < n; i++)
  {
    A[i] = mp[A[i]];
  }
  int64_t ans = 0;
  FenwickTree<int64_t> ft(m + 1);
  for (int i = 0; i < n; i++)
  {
    ans += i - ft.sum(A[i]);
    ft.add(A[i], 1);
  }
  cout << ans << endl;
}


#include <iostream>
#include <iomanip>
#include <utility>
#include <cmath>
#include <random>
#include <vector>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <algorithm>
using namespace std;

#define rep(i,n) for(int i = 0; i<n; ++i)
#define REP(i,n) for(int i = 1; i<=n; ++i)
#define all(x) begin(x),end(x)
#define show(obj) {for(auto x:obj)cout<<x<<' ';cout<<endl;}
#define line "----------"
typedef long long ll;
typedef pair<int,int> P;
typedef pair<ll,ll> LP;
const int inf = 1001001000;
const ll INF = 1LL<<60;
const int MOD = (int)1e9 + 7;

int main(){
	int N;
	cin >> N;
	vector<int> A(N);
	rep(i,N)cin >> A[i];
	multiset<int> ans;
	rep(i,N){
		auto it = ans.lower_bound(A[i]);
		if(it != ans.begin()){
			ans.erase(--it);
		}
		ans.insert(A[i]);
	}
	cout << ans.size() << endl;
	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 q[N];

int main()
{
	scanf("%d", &n);

	int cnt = 0;
	for (int i = 1; i <= n; i ++ )
	{
		int x;
		scanf("%d", &x);

		int l = 1, r = cnt;
		while (l < r)
		{
			int mid = l + r >> 1;
			if (q[mid] < x) r = mid;
			else l = mid + 1;
		}

		if (!cnt || q[cnt] >= x) q[ ++ cnt] = x;
		else q[r] = x;
	}

	printf("%d\n", cnt);
    return 0;
}


#include<bits/stdc++.h>
#define rep(i,j,k) for(int i=j;i<=k;i++)
#define rep2(i,j,k) for(int i=j;i>=k;i--)
using namespace std;
template<typename T> void read(T &num){
	char c=getchar();T f=1;num=0;
	while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9'){num=(num<<3)+(num<<1)+(c^48);c=getchar();}
	num*=f;
}
template<typename T> void qwq(T x){
	if(x>9)qwq(x/10);
	putchar(x%10+'0');
}
template<typename T> void write(T x){
	if(x<0){x=-x;putchar('-');}
	qwq(x);putchar('\n');
}
template<typename T> void chkmax(T &x,T y){x=x>y?x:y;}
template<typename T> void chkmin(T &x,T y){x=x<y?x:y;}

struct wzy{
	int l,r;
}co[200010];
inline bool cmp(wzy a,wzy b){
	return a.l<b.l;
}
inline int d(int ll,int rr){
	return max(0,rr-ll+1);
}

int main(){
	int n;read(n);int ans=0;
	int pos1=0;int pos2=0;int mx_l=0;int n1=0;int n2=0;int mi_r=INT_MAX;
	rep(i,1,n){
		read(co[i].l);read(co[i].r);
		if(co[i].l>mx_l){mx_l=co[i].l;pos1=i;}
		if(co[i].r<mi_r){mi_r=co[i].r;pos2=i;}
	}
	n1=co[pos1].r;n2=co[pos2].l;int len=0;
	rep(i,1,n){
		int q1=co[i].l;int q2=co[i].r;
		if(i!=pos1&&i!=pos2){co[++len].l=q1;co[len].r=q2;}
	}
	sort(co+1,co+len+1,cmp);

	int mx=co[1].r;int ret=d(co[1].l,co[1].r);
	rep(i,2,len){
		if(mx>=co[i].l)chkmax(ret,d(min(mx,co[i].r),co[i].l));
		chkmax(mx,co[i].r);chkmax(ret,d(co[i].l,co[i].r));
	}
	chkmax(ans,ret+d(mx_l,mi_r));

	if(pos1==pos2){write(ans);return 0;}
	int mi=n1;
	rep2(i,len,1){
		chkmax(ans,d(mx_l,mi)+d(co[i].l,mi_r));
		chkmin(mi,co[i].r);
	}
	chkmax(ans,d(mx_l,mi)+d(n2,mi_r));write(ans);
	return 0;
}

//#define _GLIBCXX_DEBUG
#include <iostream>
#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define ld long double
#define  P pair<ll,ll>
#define FOR(i,n,m) for(ll i=n; i<(ll)m;i++)
#define FORr(i,m,n) for(ll i=n; i>=(ll)m; i--)
#define FORm(i,m) for(auto i=m.begin();i!=m.end();i++)
#define sortAl(v) sort(v.begin(),v.end())
#define sortAlr(v) sort(v.begin(),v.end()); reverse(v.begin(),v.end())
#define cout(n) cout<<fixed<<setprecision(n)

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

  ll n;
  cin>>n;
  ll l_max=0,r_min=1e10;
  vector<vector<ll>> v(n);
  ll res = 0;
  FOR(i,0,n){
    ll in,in1;
    cin>>in>>in1;
    in1++;
    v[i].push_back(0);
    v[i].push_back(in);
    v[i].push_back(in1);
    l_max = max(l_max,in);
    r_min = min(r_min,in1);
    res = max(res, in1-in);//max_width
  }
  res = res + max(r_min - l_max, (ll) 0);

  FOR(i,0,n) v[i][0] = max((ll)0, v[i][2]-l_max);
  sortAl(v);
  ll r=1e10,l=0;
  vector<ll> a(n),b(n);
  FOR(i,0,n){
    r = min(r,v[i][2]);
    l = max(l,v[i][1]);
    a[i] = max(r-l,(ll)0);
  }
  r = 1e10;
  l = 0;
  FORr(i,0,n-1){
    r = min(r,v[i][2]);
    l = max(l,v[i][1]);
    b[i] = max(r-l,(ll)0);
  }

  ll ans=0;
  FOR(i,0,n-1) ans = max(ans, a[i] + b[i+1]);
  cout<<max(ans,res)<<endl;

}


#include <iostream>
#include <string>

using namespace std;

void Solution()
{
    string str;

    int a;
    int b;
    int l;
    int x;

    char c;

    while (getline(cin, str)) //enter
    {
        x = 0;

        l = (int)str.size();

        for (int i = 0; i < l; i++)
        {
            c = str.at(i); //string text = "ABCDEF";
                            //char ch = text.at( 2 );

                 if (c == 'I') a = 1;
            else if (c == 'V') a = 5;
            else if (c == 'X') a = 10;
            else if (c == 'L') a = 50;
            else if (c == 'C') a = 100;
            else if (c == 'D') a = 500;
            else if (c == 'M') a = 1000;

            if (i)
            {
                if (b >= a)
                {
                    x += b;
                }
                else
                {
                    x -= b;
                }
            }

            b = a;
        }

        x += b;

        cout << x << endl;
    }

}

int main()
{
    Solution();
    return 0;
}

#include <bits/stdc++.h>
#include <vector>
# include <cstdint>
#define FOR(i,l,r) for(int i=(l);i<(r);++i)
#define RFOR(i,l,r) for(int i=(l);i>=(int)(r);i--)
#define rep(i,n)  FOR(i,0,n)
#define rrep(i,n)  RFOR(i,n-1,0)

#define int long long
using namespace std;
const int MX = 1e6;
const int inf = 1e13;
const int mod = 1e9+7;
#define ll long long

signed main() {
    string s;
    cin >> s;
    vector<int> a(26);
    int n = s.length();
    rep(i,n) a[(int)(s[i]-'a')]++;
    int id = -1;
    rep(i,26) {
        if(a[i]==0) {
            id=i;
            break;
        }
    }
    if(id!=-1) {
        cout << s+(char)('a'+id) << endl;
        return 0;
    }
    id = (int)(s[n-1]-'a');
    rrep(i,n-1) {
        if(s[i]>s[i+1]) {
            continue;
        }
        char cc = 'z';
        rrep(j,n) {
            if(i==j) break;
            if(s[i]>s[j]) continue;
            else cc = min(cc, s[j]);
        }
        cout << s.substr(0,i)+cc << endl;
        return 0;
    }
    cout << -1 << endl;

    return 0;
}


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

const int match[10] = { 0, 2, 5, 5, 4, 5, 6, 3, 7, 6 };
const int INF = 1e9 + 7;

int main() {
  int n, m;
  cin >> n >> m;
  int a[m];
  for ( int i = 0; i < m; i++ ) { cin >> a[i]; }
  sort(a, a+m, greater<int>());

  int dp[n+1];
  dp[0] = 0;
  for ( int i = 1; i <= n; i++ ) {
    dp[i] = -INF;
    for ( int j = 0; j < m; j++ ) {
      if ( i-match[a[j]] < 0 ) { continue; }
      if ( dp[i-match[a[j]]] < 0 ) { continue; }
      dp[i] = max(dp[i-match[a[j]]]+1, dp[i]);
    }
  }

  while ( n > 0 ) {
    for ( int i = 0; i < m; i++ ) {
      if ( n-match[a[i]] < 0 ) { continue; }
      if ( dp[n-match[a[i]]] == dp[n]-1 ) {
        cout << a[i];
        n -= match[a[i]];
        break;
      }
    }
  }
  cout << endl;
  return 0;
}

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep(i,n) for(ll i=0;i<(n);i++)
#define pll pair<ll,ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define lb(c,x) distance(c.begin(),lower_bound(all(c),x))
#define ub(c,x) distance(c.begin(),upper_bound(all(c),x))

using namespace std;

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

const ll mod=1e9+7;

int main(){
    ll n,k;
    cin >> n >> k;
    vector<ll> l(n);
    rep(i,n){
        cin >> l[i];
    }
    sort(l.rbegin(),l.rend());
    ll ans=0;
    rep(i,k){
        ans+=l[i];
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); ++i)
#define all(a) (a).begin(), (a).end()

int main()
{
    int a,b;
    cin >> a>>b;

    cout << ((a>8 ||b>8) ? ":(" : "Yay!") << '\n';
    return (0);
}

#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
#include<vector>
#include <bitset>
#include <cmath>
#include <limits>
#include <iostream>
#include<set>
#include<tuple>
using namespace std;
#define INF 11000000000
#define MAX 100000
#define MOD 1000000007
typedef long long ll;
typedef pair<int,int> P;
typedef pair<pair<int,int>,int> p;
typedef pair< pair<int,int>, int> p;
#define bit(n,k) ((n>>k)&1) /*nのk bit目*/
#define rad_to_deg(rad) (((rad)/2/M_PI)*360)

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


#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, s, n) for (ll i = s; i < (ll)(n); i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define fi first
#define sc second
#define pb push_back
#define COUT(x) cout << (x) << "\n"
#define COUTF(x) cout << setprecision(15) << (x) << "\n"
#define ENDL cout << "\n"
#define DF(x) x.erase(x.begin())  // 先頭文字削除
#define ALL(x) x.begin(), x.end()
#define SZ(x) (ll) x.size()
#define SORT(x) sort(ALL(x))
#define REVERSE(x) reverse(ALL(x))
#define MAX(x, y, z) max(x, max(y, z))
#define MIN(x, y, z) min(x, min(y, z))
#define ANS cout << ans << "\n"
#define RETURN(x)    \
  cout << x << "\n"; \
  return 0
clock_t CLOCK;
#define START_TIMER CLOCK = clock()
#define SHOW_TIMER cerr << "time: " << (ld)(clock() - CLOCK) / 1000000 << "\n"
#define init() \
  cin.tie(0);  \
  ios::sync_with_stdio(false)
#define LINE cerr << "[debug] line: " << __LINE__ << "\n";
#define debug(x) cerr << "[debug] " << #x << ": " << x << "\n";
#define debugV(v)                       \
  cerr << "[debugV] " << #v << ":";     \
  rep(i, v.size()) cerr << " " << v[i]; \
  cerr << "\n";
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using mll = map<ll, ll>;
using qll = queue<ll>;
using P = pair<ll, ll>;

constexpr ll INF = 0x3f3f3f3f3f3f3f3f;
constexpr ld PI = 3.141592653589793238462643383279;
ll get_digit(ll x) {
  return to_string(x).size();
}

ll gcd(ll x, ll y) {
  return y ? gcd(y, x % y) : x;
}

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

vector<P> factorize(ll n) {
  vector<P> result;
  for (ll i = 2; i * i <= n; ++i) {
    if (n % i == 0) {
      result.pb({i, 0});
      while (n % i == 0) {
        n /= i;
        result.back().second++;
      }
    }
  }
  if (n != 1) {
    result.pb({n, 1});
  }
  return result;
}

vll divisor(ll n) {
  vll ret;
  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n) ret.push_back(n / i);
    }
  }
  SORT(ret);
  return (ret);
}

signed main() {
  init();
  ll N, T;
  cin >> N >> T;

  ll ans = 0;
  ll before = -INF;
  rep(i, N) {
    ll a;
    cin >> a;
    ans += T;
    if (before + T >= a) ans -= ((before + T) - a);
    before = a;
  }
  ANS;
  return 0;
}

#include<bits/stdc++.h>
#define rep(i,n) for(int i = 0;i<n;i++)
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0;}
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0;}

int main() {
    int n,T;
    cin >> n >> T;
    int si = 0;
    int now = 0;
    int ans = 0;
    bool ok = false;
    rep(i,n) {
        int t;
        cin >> t;
        if(i==0) {
            now += T;
            continue;
        }
        if(now>=t) now = t+T;
        else {
            ans += now - si;
            si = t;
            now = t+T;
        }
    }
    ans += now-si;
    cout << ans << endl;
}


#include <iostream>

char detClass(int pm, int pe, int pj)
{

	int mean = pm + pe + pj;
	if((pm == 100) | (pe == 100) | (pj == 100)) {
		return 'A';
	} else if( (pm + pe) >= 180) {
		return 'A';
	} else if( mean >= 240) {
		return 'A';
	} else if( mean >= 210) {
		return 'B';
	} else if( (mean >= 150) && ((pm >= 80) | (pe >= 80))) {
		return 'B';
	} else {
		return 'C';
	}
}

int main()
{
	int n, pm, pe, pj = 0;

	while(true) {
		std::cin >> n;
		if(n == 0) {
			break;
		}
		for(int i = 0; i < n; i++) {
			std::cin >> pm >> pe >> pj;
			std::cout << detClass(pm,pe,pj) << std::endl;
		}
	}
	return 0;
}


#pragma GCC optimize("Ofast")
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define all(a) (a).begin(), (a).end()

int main(){
    vector<int> A(5);
    for(int i = 0; i < 5; i++){
        cin >> A[i];
    }
    int k;
    cin >> k;
    sort(all(A));
    if(A[4]-A[0] <= k) puts("Yay!");
    else puts(":(");
}

#include<algorithm>//sort,二分探索,など
#include<bitset>//固定長bit集合
#include<cmath>//pow,logなど
#include<complex>//複素数
#include<deque>//両端アクセスのキュー
#include<functional>//sortのgreater
#include<iomanip>//setprecision(浮動小数点の出力の誤差)
#include<iostream>//入出力
#include<iterator>//集合演算(積集合,和集合,差集合など)
#include<map>//map(辞書)
#include<numeric>//iota(整数列の生成),gcdとlcm(c++17)
#include<queue>//キュー
#include<set>//集合
#include<stack>//スタック
#include<string>//文字列
#include<unordered_map>//イテレータあるけど順序保持しないmap
#include<unordered_set>//イテレータあるけど順序保持しないset
#include<utility>//pair
#include<vector>//可変長配列

// vectorの重複削除
// std::sort(test.begin(), test.end());
// test.erase(std::unique(test.begin(), test.end()), test.end());

using namespace std;
using ll = long long;
using P = pair<int, int>; // vector<P> ba(n); みたいな感じですぐ使える ba[i] = P(a, b); make_pairをしなくてもいい
// typedef vector<int> v;
template <class T>ostream &operator<<(ostream &o,const vector<T>&v){o<<"{";for(int i=0;i<(int)v.size();i++)o<<(i>0?", ":"")<<v[i];o<<"}";return o;}
#define debug(v) {cerr<<"\033[1;36m[debug]\033[m "<<#v<<" : "<<v<<endl;}
#define rep(i, c) for(int i = 0; i < int(c); i++)
#define ALL(x) (x).begin(), (x).end() //sort(ALL(obj))みたいな
#define SIZE(x) ((ll)(x).size()) //sizeをsize_tからllに直しておく
#define MAX(x) *max_element(ALL(x)) //最大値
#define pb push_back //
// const int MOD = 1000000007;
// string alpha="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// printf("%.20f\n", ans); あれ
// const double PI = 3.141592653589793238462643383;

int main() {
  //std::vector<int> v(5);
  int nmax = 0;
  int nmin = 10000;
  int num;
  rep(i,5) {
    cin >> num;
    if (num < nmin) {
      nmin = num;
    }
    if (num > nmax) {
      nmax = num;
    }
  }
  int k; cin>>k;
  cout << ((nmax - nmin) <= k ? "Yay!" : ":(" ) << endl;
}


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

const ll C = 1000000007;

int main(){
    int sx, sy, tx, ty;
    cin >> sx >> sy >> tx >> ty;
    int x = tx-sx;
    int y = ty-sy;
    for(int i=0; i<y; i++) cout << "U";
    for(int i=0; i<x; i++) cout << "R";
    for(int i=0; i<y; i++) cout << "D";
    for(int i=0; i<x; i++) cout << "L";

    cout << "L";
    for(int i=0; i<y; i++) cout << "U";
    cout << "U";
    for(int i=0; i<x; i++) cout << "R";
    cout << "R";
    cout << "D";

    cout << "R";
    for(int i=0; i<y; i++) cout << "D";
    cout << "D";
    for(int i=0; i<x; i++) cout << "L";
    cout << "L";
    cout << "U";
}

#include <bits/stdc++.h>
#define REP(i,n) for (long long i=0;i<(n);i++)
using namespace std;
template<typename V,typename Weight>
class Dijekstra {
private:
	struct edge {
		Weight dist;V node;V prev;
		bool operator<(edge const& a) const {
			return dist < a.dist;
		}
		bool operator>(edge const& a) const {
			return dist > a.dist;
		}
	};
	vector<V> trace;
	vector<vector<pair<V,Weight>>> E;
	V sz;
public:
	Weight inf = numeric_limits<Weight>::max();
	vector<Weight> D;
	Dijekstra(V v) :
		E(v),sz(v),D(v,inf),trace(v,numeric_limits<V>::max()) {}

	void addEdgeDirected(V from,V to,Weight dis){
		E[from].push_back(make_pair(to,dis));
	}
	void addEdgeUndirected(V from,V to,Weight dis){
		E[from].push_back(make_pair(to,dis));
		E[to].push_back(make_pair(from,dis));
	}

	V size() {return sz;}
	Weight & operator[] (V n) { if (0 <= n && n < sz) return D[n];}

	void dijekstra(V start){
		priority_queue<edge,vector<edge>,greater<edge>> que;
		D[start] = 0;
		//trace[start] = start;
		que.push(edge{0,start,start});
		Weight d;V v,prev;
		while(!que.empty()){
			edge cur = que.top();que.pop();
			d = cur.dist;v = cur.node;prev = cur.prev;
			if (D[v] < d) continue; //既により最適が見つかっている
			D[v] = d;trace[v] = prev;
			for(pair<V,Weight> e : E[v]){
				if (D[e.first] <= d + e.second) continue;
				que.push(edge{d+e.second,e.first,v});
			}
		}
	}

	vector<V> show_trace(V start,V goal){ // startからgoalの最短距離上のノード
		vector<V> ret;
		for (V node = goal;node != start;node = trace[node]) ret.push_back(node);
		ret.push_back(start);
		return ret; // 配列の先頭がgoal,末尾がstart
	}
};

int main(void){
	long V,E,R;
	long s,t,d;
	cin >> V >> E >> R;
	Dijekstra<long,long> dij(V);

	REP(i,E){
		cin >> s >> t >> d;
		dij.addEdgeDirected(s,t,d);
	}

	dij.dijekstra(R);
	REP(i,V) {
		if (dij[i] == dij.inf) cout << "INF" << endl;
		else cout << dij[i] << endl;
	}

	return 0;
}


#include <iostream>

using namespace std;

int main()
{
    int A, B;
    cin >> A >> B;
    int X = A*B;
    int Y = A+B;
    int Z = A-B;
    if((X>=Y)&&(X>=Z))
    {
        cout << X;

    }
    else if((Y>=X)&&(Y>=Z))
    {
        cout << Y;
    }
    else if((Z>=Y)&&(Z>=X))
    {
        cout << Z;
    }

    return 0;

}

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

int main() {
  int a,b;
  cin >> a >> b;
  int ans = a+b;
  ans = max(ans,a-b);
  ans = max(ans,a*b);
  cout << ans << endl;
}


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

#define ll long long
#define ld long double
#define ar array

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

template <typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define vt vector
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define sz(x) (int)(x).size()

#define F_OR(i, a, b, s) for (int i=(a); (s)>0?i<(b):i>(b); i+=(s))
#define F_OR1(e) F_OR(i, 0, e, 1)
#define F_OR2(i, e) F_OR(i, 0, e, 1)
#define F_OR3(i, b, e) F_OR(i, b, e, 1)
#define F_OR4(i, b, e, s) F_OR(i, b, e, s)
#define GET5(a, b, c, d, e, ...) e
#define F_ORC(...) GET5(__VA_ARGS__, F_OR4, F_OR3, F_OR2, F_OR1)
#define FOR(...) F_ORC(__VA_ARGS__)(__VA_ARGS__)
#define EACH(x, a) for (auto& x: a)

template<class A> void read(vt<A>& v);
template<class A, size_t S> void read(ar<A, S>& a);
template<class T> void read(T& x) {
	cin >> x;
}
void read(double& d) {
	string t;
	read(t);
	d=stod(t);
}
void read(long double& d) {
	string t;
	read(t);
	d=stold(t);
}
template<class H, class... T> void read(H& h, T&... t) {
	read(h);
	read(t...);
}
template<class A> void read(vt<A>& x) {
	EACH(a, x)
		read(a);
}
template<class A, size_t S> void read(array<A, S>& x) {
	EACH(a, x)
		read(a);
}

string to_string(char c) {
	return string(1, c);
}
string to_string(bool b) {
	return b?"true":"false";
}
string to_string(const char* s) {
	return string(s);
}
string to_string(string s) {
	return s;
}
string to_string(vt<bool> v) {
	string res;
	FOR(sz(v))
		res+=char('0'+v[i]);
	return res;
}

template<size_t S> string to_string(bitset<S> b) {
	string res;
	FOR(S)
		res+=char('0'+b[i]);
	return res;
}
template<class T> string to_string(T v) {
    bool f=1;
    string res;
    EACH(x, v) {
		if(!f)
			res+=' ';
		f=0;
		res+=to_string(x);
	}
    return res;
}

template<class A> void write(A x) {
	cout << to_string(x);
}
template<class H, class... T> void write(const H& h, const T&... t) {
	write(h);
	write(t...);
}
void print() {
	write("\n");
}
template<class H, class... T> void print(const H& h, const T&... t) {
	write(h);
	if(sizeof...(t))
		write(' ');
	print(t...);
}

void DBG() {
	cerr << "]" << endl;
}
template<class H, class... T> void DBG(H h, T... t) {
	cerr << to_string(h);
	if(sizeof...(t))
		cerr << ", ";
	DBG(t...);
}
#ifdef _DEBUG
#define dbg(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define dbg(...) 0
#endif

template<class T> void offset(ll o, T& x) {
	x+=o;
}
template<class T> void offset(ll o, vt<T>& x) {
	EACH(a, x)
		offset(o, a);
}
template<class T, size_t S> void offset(ll o, ar<T, S>& x) {
	EACH(a, x)
		offset(o, a);
}

mt19937 mt_rng(chrono::steady_clock::now().time_since_epoch().count());
ll randint(ll a, ll b) {
	return uniform_int_distribution<ll>(a, b)(mt_rng);
}
const int d4i[4]={-1, 0, 1, 0}, d4j[4]={0, 1, 0, -1};
const int d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1};

template<class T> T modpow(T a, T b,ll mod){
T res=1; a%=mod;
while(b){
if(b&1){ res=(res*a)%mod;}
a=(a*a)%mod;
b>>=1;
}
return res;
}
#define lcm(a,b) ((a)*(b))/(__gcd(a,b))

ll mod=1e9+7;
const int N=20;
const ld PI=2*acos(0.0);
vt<int> v(N,0);
vt<int> g(N,0);
int n;
void solve() {
ll mx=0;
read(n);
FOR(n){ int c; read(c); g[i]=(1<<i);  FOR(j,c){ int a,b; read(a,b); if(!b){v[i]|=(1<<(a-1)); } else{ g[i]|=(1<<(a-1)); } } }
FOR(i,1,1<<n){
int tmp1=0,tmp2=0;
FOR(j,n){
if(i&(1<<j))  { tmp1|=g[j]; tmp2|=v[j]; }
}
if(tmp1==i&&(!(tmp1&tmp2)))mx=max(mx,(ll)__builtin_popcount(i));
}
print(mx);
}

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

#include<bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(n); ++i)
#define rep2(i,n) for(int i=1; i<=(n); ++i)
#define all(x) (x).begin(),(x).end()
#define sort_u(x) sort((x).begin(),(x).end())
#define sort_d(x) sort((x).begin(),(x).end(), greater<int>())
#define pb push_back
#define INF ((1LL<<62)-(1LL<<31))  // 浮動小数点型
#define bit(n,m)(((n)>>(m))&1)  // nのmビット目が1
#define MOD 1000000007
using namespace std;
using ll = long long;
using P = pair<int,int>;
using Graph = vector<vector<P>>;

int main() {
    int n; cin >> n;
    Graph p(n);
    rep(i,n){
        int a; cin >> a;
        rep(j, a){
            int x, y; cin >> x >> y;
            x--;
            p[i].pb(P(x,y));
        }
    }
    int ans = 0;
    rep(s, 1<<n){
        vector<int> flg(n, -1);
        bool flg2 = false;
        int a = 0;
        rep(i,n){
            if(bit(s,i)){
                if(flg[i] == -1) flg[i] = 1;
                if(flg[i] != 1) {
                    flg2 = true;
                    break;
                }
                a++;
                for(auto q : p[i]){
                    if(flg[q.first] == -1) flg[q.first] = q.second;
                    if(flg[q.first] != q.second){
                        flg2 = true;
                        break;
                    }
                }
            }else{
                if(flg[i] == -1) flg[i] = 0;
                if(flg[i] != 0) {
                    flg2 = true;
                    break;
                }
                // for(auto q : p[i]){
                //     int q2 = 1;
                //     if(q.second == 1) q2 = 0;
                //     if(flg[q.first] == -1) flg[q.first] = q2;
                //     if(flg[q.first] == q.second){
                //         flg2 = true;
                //         break;
                //     }
                // }
            }
            if(flg2) break;
        }
        if(!flg2) ans = max(a,ans);
    }
    cout << ans << endl;

}

/*
g++ c.cpp
./a.out

oj d
g++ c.cpp
oj t
*/


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;
using pll = pair<ll,ll>;

#define INF (1LL << 60)
#define MOD 1000000007
#define PI 3.14159265358979323846

#define REP(i,m,n) for(ll (i)=(m),(i_len)=(n);(i)<(i_len);++(i))
#define FORR(i,v) for(auto (i):v)
#define ALL(x) (x).begin(), (x).end()
#define PR(x) cout << (x) << endl
#define PS(x) cout << (x) << " "
#define SZ(x) ((ll)(x).size())
#define MAX(a,b) (((a)>(b))?(a):(b))
#define MIN(a,b) (((a)<(b))?(a):(b))
#define REV(x) reverse(ALL((x)))
#define ASC(x) sort(ALL((x)))
#define DESC(x) ASC((x)); REV((x))
#define pb push_back
#define eb emplace_back

int main()
{
    string S;
    cin >> S;

    char sign[2] = {'-', '+'};
    bool f = false;
    for(ll i=-1; i<2; i+=2) {
        for(ll j=-1; j<2; j+=2) {
            for(ll k=-1; k<2; k+=2) {
                if((S[0]-'0')+i*(S[1]-'0')+j*(S[2]-'0')+k*(S[3]-'0')==7) {
                    cout << S[0] << sign[(i+1)/2] << S[1] << sign[(j+1)/2] << S[2] << sign[(k+1)/2] << S[3] << "=7" << endl;
                    f = true;
                    break;
                }
            }
            if(f) break;
        }
        if(f) break;
    }

    return 0;
}

/*

*/


#include <bits/stdc++.h>
#define ll long long int
#define yorn(f) cout<<((f)?"Yes":"No")<<endl;
#define YORN(f) cout<<((f)?"YES":"NO")<<endl;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, n) for (int i = 1; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define put(x) cout << x << endl;
#define println(x) cout << x << endl;
using namespace std;
int main()
{
    int n, k;
    cin >> n >> k;
    vector<int> l(n);
    rep(i, n) cin >> l[i];
    sort(all(l));
    reverse(all(l));
    int sum = 0;
    rep(i, k) sum += l[i];
    put(sum)
    return 0;
}

#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;
#define ALL(v) v.begin(), v.end()
#define vmax(v) *max_element(ALL(v))
#define vmin(v) *min_element(ALL(v))
#define chmax(x, a) x = max(x, a)
#define chmin(x, a) x = min(x, a)

int main() {
  string s; cin >> s;
  int w; cin >> w;
  int tmp = 0;
  while (tmp < s.length()) {
    cout << s[tmp];
    tmp += w;
  }
  cout << endl;
}


#include<bits/stdc++.h>
using namespace std;
///Welcome to Nasif's Code
#define bug printf("bug\n");
#define bug2(var) cout<<#var<<" "<<var<<endl;
#define co(q) cout<<q<<endl;
typedef long long int ll;
typedef unsigned long long int ull;
const int MOD = (int)1e9+7;
const int MAX = 1e6;
#define pi acos(-1)
#define inf 1000000000000000LL
#define FastRead    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
vector<char>v[105];
int main()
{
    FastRead
    //freopen("output.txt", "w", stdout);
    int a,b,r=0,c=0;
    cin>>a>>b;
    for(int i=0; i<50; i++)
    {
        for(int j=0; j<100; j++)
            v[i].push_back('.');
    }
    for(int i=50; i<100; i++)
    {
        for(int j=0; j<100; j++)
            v[i].push_back('#');
    }
    a--,b--;
    for(int i=0; i<50 && b; i+=2)
    {
        for(int j=0; j<100 && b; j+=2)
        {
            v[i][j]='#';
            b--;
        }
    }
    for(int i=99; i>=50 && a; i-=2)
    {
        for(int j=0; j<100 && a; j+=2)
        {
            v[i][j]='.';
            a--;
        }
    }
    cout<<"100 100"<<endl;
    for(int i=0; i<100; i++)
    {
        for(int j=0; j<100; j++)
            cout<<v[i][j];
        cout<<endl;
    }

    return 0;
}

#include <bits/stdc++.h>
#define ll long long
#define MODV 1000000007
#define INFLL LLONG_MAX // 9223372036854775807
#define EPS 1e-9
#define rep(i, n) for(ll i=0, i##_len=(ll)(n); i<i##_len; i++)
#define repf(i, n) for(ll i=1, i##_len=(ll)(n+1); i<i##_len; i++)
#define all(v) v.begin(), v.end()
#define endl "\n"
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define Yes() cout << "Yes" << endl
#define YES() cout << "YES" << endl
#define No() cout << "No" << endl
#define NO() cout << "NO" << endl
#define Init() std::ios::sync_with_stdio(false); std::cin.tie(0); std::cout<<fixed<<setprecision(15);
template<class T>bool chmax(T &a, const T &b){ if(a<b){ a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b){ if(b<a){ a=b; return 1; } return 0; }
using namespace std;

void printString(vector<string>& ans){
    ll h = ans.size(), w = ans[0].size();
    cout << h << " " << w << endl;
    rep(i, h) cout << ans[i] << endl;
}

int main(){
    Init();
    ll a, b; cin >> a >> b;
    string wb = string(100, '.');
    string bw = string(100, '#');
    vector<string> ans;
    for(ll i=1; i<100; i+=2){
        wb[i] = '#';
        bw[i] = '.';
    }

    a--; b--;
    ll bh = ((ll)ceil(b/50.0));
    ll wh = ((ll)ceil(a/50.0));

    // □■□■□■...□■
    // □□□□□□...□□　で白1個に対して黒50個おける。（20行あれば500個おける。）
    rep(i, bh){
        if(i == bh-1){
            if(b%50 != 0) ans.emplace_back(wb.substr(0, (b%50)*2+1) + string(100-((b%50)*2+1), '.'));
            else ans.emplace_back(wb);
        }
        else{
            ans.emplace_back(wb);
            ans.emplace_back(string(100, '.'));
        }
    }
    // □□□□□□...□□
    // ■■■■■■...■■　を境界に置く。
    ans.emplace_back(string(100, '.'));
    ans.emplace_back(string(100, '#'));

    // ■□■□■□...■□
    // ■■■■■■...■■　で黒1個に対して白50個おける。（20行あれば500個おける。）
    rep(i, wh){
        if(i == wh-1){
            if(a%50 != 0) ans.emplace_back(bw.substr(0, (a%50)*2+1) + string(100-((a%50)*2+1), '#'));
            else ans.emplace_back(bw);
        }
        else{
            ans.emplace_back(bw);
            ans.emplace_back(string(100, '#'));
        }
    }

    printString(ans);
}


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

/* 大文字を小文字に変換 */
char tolower(char c) {return (c + 0x20);}
/* 小文字を大文字に変換 */
char toupr(char c) {return (c - 0x20);}

// if('A'<=s[i] && s[i]<='Z') s[i] += 'a'-'A';

/*
string s = "abcdefg"
s.substr(4) "efg"
s.substr(0,3) "abc"
s.substr(2,4) "cdef"

// イテレータ要素のインデックス
distance(A.begin(), itr);
*/

int main()
{
    map<int,int> m;
    int n; cin >> n;
    int a[n];
    rep(i,n) cin >> a[i];

    rep(i,n) m[a[i]]++;

    bool isok = true;
    rep(i,n){
        if(m[a[i]] > 1){
            isok = false;
            break;
        }
    }
    if(isok) cout << "YES" << endl;
    else cout << "NO" << endl;
}

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

int main()
{
  int N, ai;
  cin >> N;
  map <int,int> A;
  for(int i=0;i<N;i++){
    cin >> ai;
    if(A.count(ai) > 0){ cout << "NO" << endl; return 0;}
    else A[ai] = 1;
  }
  cout << "YES" << endl;
}


//2017-11-7
//miaomiao
//
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>

using namespace std;

#define LL long long
#define For(i, a, b) for(int i = (a); i <= (int)(b); ++i)

#define N (100000+5)

bool Check(int a, int b, int c){
	if(a > c) swap(a, c);
	if(b != a+1 || c != a+2) return false;
	if(a % 3 != 1) return false;
	return true;
}

int a[N][5], num[N];
bool op[N], rev[2], swp[2];

int main(){
	int n;

	scanf("%d", &n);
	For(j, 1, 3) For(i, 1, n) scanf("%d", &a[i][j]);

	For(i, 1, n) if(!Check(a[i][1], a[i][2], a[i][3])){
		puts("No"); return 0;
	}

	For(i, 1, n){
		num[i] = max(a[i][1], a[i][3]) / 3;
		op[i] = a[i][1] > a[i][3];
		if((num[i]&1) ^ (i&1)){
			puts("No"); return 0;
		}
	}

//	For(i, 1, n) printf("%d%c ", num[i], op[i]? '-': '+');
//	puts("");

	For(i, 1, n) rev[i&1] ^= op[i];

	int t;
	For(i, 1, n)
		while(i != num[i]){
			t = num[i];
			swap(num[i], num[t]);
			swp[i&1] ^= 1;
		}

	if((swp[0] ^ rev[1]) || (swp[1] ^ rev[0])) puts("No");
	else puts("Yes");

	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define modulo 1000000007
#define mod(mod_x) ((((long long)mod_x+modulo))%modulo)
#define Inf 1000000000000000002

int main(){

	int N;
	cin>>N;

	vector<int> A(N);
	for(int i=0;i<N;i++)scanf("%d",&A[i]);

	for(int i=1;i<N;i++)A[i] ^= A[i-1];

	vector<vector<int>> ind(1<<20,vector<int>());

	for(int i=0;i<N;i++){
		ind[A[i]].push_back(i);
	}

	int ans = 0;
	if(A.back()==0){
		int t = 1;
		for(int i=0;i<ind[0].size()-1;i++){
			t = mod(t * 2);
		}
		ans = mod(ans + t);
	}

	for(int i=1;i<(1<<20);i++){
		if(A.back()!=0&&A.back()!=i)continue;

		int sum = 0;
		int dp_sum = 0;
		for(int j=0;j<ind[i].size();j++){
			if(j==0){
				//sum += 1;
				dp_sum += 1;
			}
			else{
				int t = distance(lower_bound(ind[0].begin(),ind[0].end(),ind[i][j-1]),lower_bound(ind[0].begin(),ind[0].end(),ind[i][j]));
				int dp = mod(sum + mod(dp_sum * t));
				sum = dp;
				dp = mod(dp + 1);

				dp_sum = mod(dp_sum + dp);
			}
		}

		if(A.back()==0){
			ans = mod(ans + dp_sum);
		}
		else{
			ans = mod(ans + sum + 1);
		}

	}

	cout<<ans<<endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

using ll = long long;

#define ALL(A) A.begin(),A.end()

void ALLIN1_NUMBER(vector<ll>& V)
{
    for(auto& x : V)
    {
        cin >> x;
    }
}

template<class T>
void OUT0(T N)
{
    cout << N << endl;
}

static const double pi = acos(-1.0);

double Cos(double D)
{
    return cos(pi/180 * D);
}

vector<ll> Divs(ll N)
{
    map<ll,ll> ret_map;
    for(ll i=1;i*i<=N;i++)
    {
        if(N%i==0)
        {
            ret_map[i]++;
            ret_map[N/i]++;
        }
    }
    vector<ll> ret_vec;
    for(auto x : ret_map)
    {
        ret_vec.push_back(x.first);
    }
    return ret_vec;
}

ll Ans(ll N, ll M, vector<ll> div)
{
    sort(ALL(div),greater<ll>());
    for(auto x : div)
    {
        if(N * x <= M) return x;
    }
}

int main()
{
    ll N,M;
    cin >> N >> M;

    OUT0(Ans(N,M,Divs(M)));

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
#define pb push_back
#define ld long double
#define f first
#define s second
#define in insert
#define sz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()
#define speed ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int INF = 1e18 + 5;
const int mod = 1e9 + 7;
const int  N = 2e5 + 314;
const long double pi = 3.1415926535897932384626433832795;
void solve(){
    int n, m;
    cin>>n>>m;
    if(n==1){
        cout<<m;
        return;
    }
    int mx = 0;
    for(int i = 2; i*i <= m; i++){
        if(m%i==0){
            if(m>=n*i)mx=max(mx, i);
            if(i>=n)mx=max(mx, m/i);
        }
    }
    cout<<(mx>0?mx:1);
}
signed main(){
    speed;
    int t=1;
    //cin>>t;
    while(t--)solve();
}


#include<iostream>
#include<algorithm>
#include<math.h>
#include<vector>
#define rep(i,N) for(int i=0; i<N; i++)
using namespace std;

int main(){
    int D, T, S;
    cin >> D >> T >> S;
    if(D<=T*S){
        cout<<"Yes"<<endl;
    }else{
        cout << "No"<<endl;
    }

}

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

// GCD, LCM
ll gcd(ll a, ll b) { return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) { return a/gcd(a,b)*b;}

int main()
{
    int A,B,K;

    cin>>A>>B>>K;

    for(int i=1;i<=K;i++){
        if(i%2==1){
            A/=2;
            B+=A;
        }else{
            B/=2;
            A+=B;
        }
    }

    cout<< A << " "<<B<<endl;

    return 0;
}


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

int main(void)
{
    int N, Q;
    string S;
    cin >> N >> Q >> S;
    vector<int> l(Q), r(Q);
    for (int i = 0; i < Q; i++)
    {
        cin >> l[i] >> r[i];
    }

    vector<int> acCount(N);
    acCount[0] = 0;
    for (int i = 1; i < N; i++)
    {
        if(S[i] == 'C' && S[i-1] == 'A'){
            acCount[i] = acCount[i-1]+1;
        } else {
            acCount[i] = acCount[i-1];
        }
    }

    for (int i = 0; i < Q; i++)
    {
        cout << acCount[r[i]-1] - acCount[l[i]-1] << endl;
    }

    return 0;
}

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<string>

using namespace std;

int main(){

	while(1){
		int n;
		cin >> n;
		if(n==0) break;
		int pre[n];
		string str;
		for(int i=0;i<n;i++){
			cin >> pre[i];
		}

		cin >> str;

		for(int i=0;i<str.size();i++){
			for(int j=pre[i%n];j>0;j--){
				str[i]--;
				if(str[i]<=('A'-1)) str[i]='z';
				if(str[i]==96 ) str[i] = 'Z';
			}
		}
		cout << str << endl;
	}
}