#include <bits/stdc++.h>
#include <boost/range/algorithm.hpp>
#include <boost/range/numeric.hpp>
#include <boost/range/irange.hpp>
#include <boost/range/adaptor/indexed.hpp>

using namespace std;
using namespace boost::adaptors;
using namespace std::string_literals;
using ll = int64_t;
using vecint = vector<int>;
using vecll = vector<ll>;
using boost::irange;

int main()
{
  int n,t;
  cin>>n>>t;
  vector<vecint> dp(n+1, vecint(t+3001, 0));
  vector<tuple<int,int>> vt;
  for(int i:irange(0,n)) {
    int a,b;
    cin>>a>>b;
    vt.emplace_back(a,b);
  }
  boost::sort(vt);
  for(int i:irange(0,n)) {
    int a,b;
    tie(a,b) = vt[i];
    for(int j:irange(0,t+3001)) {
      dp[i+1][j] = max(dp[i+1][j], dp[i][j]);
      if (j < t) {
        dp[i+1][j+a] = max(dp[i+1][j+a], dp[i][j] + b);
      }
    }
  }
  cout<<*boost::max_element(dp[n])<<endl;
  return 0;
}

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

long long dp[2][3003];

int main()
{

        long long n,m,a,b,c,d,e,f,g,h,p,q,r,fr,sc,tr,sz,tz,i,j,k,mx=LLONG_MIN,mn=LLONG_MAX;
        long long x=0,y=0,cnt=0,res=0,ttl=0,ans=0,sum=0; long long flg=0,flag=1,na=0,as=1;
        vector<long long>u,v,w; vector< pair<long long,long long> >vct; vector<string>vst;
        set<long long>st,nt,tt; map<long long,long long>mp,nq,qr; string str,ttr,ntr;

        scanf("%lld %lld",&n,&k);

        for(i=1;i<=n;i++)
        {
            scanf("%lld %lld",&a,&b); vct.push_back(make_pair(a,b));
        }

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

        long long idx,cap;

        for(idx=n-1;idx>=0;idx--)
        {
            for(cap=1;cap<=k;cap++)
            {
                fr=idx%2; sc=(idx+1)%2; a=vct[idx].first; b=vct[idx].second;

                p=0; q=0;

                if(idx==n-1 || cap-a<=0){  p=b; }
                else{  p=b+dp[sc][cap-a];   }

                if(idx!=n-1){  q=dp[sc][cap];  }

                dp[fr][cap]=max(p,q);

            }
        }

        printf("%lld\n",dp[0][k]);

    return 0;
}


// includes
#include "bits/stdc++.h"
using namespace std;
// macros
#define ll long long
#define MOD  1000000007 // 998244353 //100000000  //
#define pii pair<ll, ll>
#define piii pair<ll, pii>
#define sz(x) ((ll)(x).size())
#define ft first
#define sd second
#define pb push_back
#define rep(i, n) for(ll i = 0; i < n; i++)
#define repr(i, n) for(ll i = n-1; i >= 0; i--)
#define itr(it, x) for(auto it = x.begin(); it != x.end(); it++)
#define mem(a, b) memset(a, (ll)b, sizeof(a))
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define edge(v, x, y) v[x].pb(y); v[y].pb(x);
#define popc __builtin_popcount
#define ANS(s) {cout << s << "\n"; return;}
// functions
template <typename T> void unique(T& c){c.erase(std::unique(c.begin(), c.end()), c.end());}
template <class T>bool chmax(T &a, const T &b){if(a < b){a = b; return 1;} return 0;}
template <class T>bool chmin(T &a, const T &b){if(a > b){a = b; return 1;} return 0;}
template <typename T> istream &operator>>(istream &is, vector<T> &vec){for(auto &v: vec)is >> v; return is;}
template <typename T> ostream &operator<<(ostream &os, const vector<T>& vec){for(int i = 0; i < vec.size(); i++){ os << vec[i]; if(i + 1 != vec.size())os << " ";} return os;}
template <typename T> ostream &operator<<(ostream &os, const set<T>& st){for(auto itr = st.begin(); itr != st.end(); ++itr){ os << *itr; auto titr = itr; if(++titr != st.end())os << " ";} return os;}
template <typename T> ostream &operator<<(ostream &os, const unordered_set<T>& st){for(auto itr = st.begin(); itr != st.end(); ++itr){ os << *itr; auto titr = itr; if(++titr != st.end())os << " ";} return os;}
template <typename T> ostream &operator<<(ostream &os, const multiset<T>& st){for(auto itr = st.begin(); itr != st.end(); ++itr){ os << *itr; auto titr = itr; if(++titr != st.end())os << " ";} return os;}
template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T>& st){for(auto itr = st.begin(); itr != st.end(); ++itr){ os << *itr; auto titr = itr; if(++titr != st.end())os << " ";} return os;}
template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p){os << "(" << p.first << ", " << p.second << ")"; return os;}
template <typename T1, typename T2> ostream &operator<<(ostream &os, const map<T1, T2> &mp){for(auto itr = mp.begin(); itr != mp.end(); ++itr){ os << "(" << itr->first << ", " << itr->second << ")"; auto titr = itr; if(++titr != mp.end())os << " "; } return os;}
template <typename T1, typename T2> ostream &operator<<(ostream &os, const unordered_map<T1, T2> &mp){for(auto itr = mp.begin(); itr != mp.end(); ++itr){ os << "(" << itr->first << ", " << itr->second << ")"; auto titr = itr; if(++titr != mp.end())os << " "; } return os;}
// constants
const ll N = 1e5+5, M = 1e6+5, A = 1e7+5, inf = 1e9;
const long long linf = 1LL << 60;
const double er = 1e-10, pi = 3.141592653589793238463;
const ll lx[4] = {0, 1, -1, 0};
const ll ly[4] = {1, 0, 0, -1};
const ll dx[8] = {0, 0, 1, -1, 1, -1, 1, -1};
const ll dy[8] = {1, -1, 0, 0, 1, 1, -1, -1};
// io
struct fast_io{
  fast_io(){ios_base::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(20);}
} fast_io_;

void solve(){
    ll n, m;
    cin >> n >> m;
    ll ans = 0;
    for(ll i = 1; i <= sqrt(m); i++){
        if(m%i == 0){
            ll k = m/i;
            if(k >= n) ans = max(ans, i);
            k = i;
            if(k >= n) ans = max(ans, m/i);
        }
    }
    cout << ans;
}
int main(int argc, char const* argv[])
{
    ll t = 1; //cin >> t;
    while(t--) {
        solve();
    }
    return 0;
}

#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<algorithm>

using namespace std;

#define LL long long
#define fgx cerr<<"--------------"<<endl;
#define dgx cerr<<"=============="<<endl;

inline int read(){
	int x=0,f=1;char c=getchar();
	for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
	for(;isdigit(c);c=getchar()) x=x*10+c-'0';
	return x*f;
}
const int MAXN = 100010;
const int INF = 2147483600;

int N,D1,D2;
bool Vis[710][710];

inline void solve(int P){
	int K=0; while(P%4==0) P>>=2,K++;
	if(P%4==3) return ;
	if(P%4==1) {
		for(int i=0;i<N;i++)
			for(int j=0;j<N;j++)
				Vis[i][j]&=((((i>>K)^(j>>K))&1)^1);
	} else{
		for(int i=0;i<N;i++)
			for(int j=0;j<N;j++)
				Vis[i][j]&=(((i>>K)&1)^1);
	} return ;
}

int main(){
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	N=read(),D1=read(),D2=read();
	int cnt=N*N;
	N<<=1; memset(Vis,1,sizeof(Vis));
	solve(D1); solve(D2);
	for(int i=0;i<N;i++){
		for(int j=0;j<N;j++){
			if(Vis[i][j]){
				printf("%d %d\n",i,j); --cnt;
				if(!cnt) return 0;
			}
		}
	}
	return 0;
}


/*
Author : Devil088
*/
#include<bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define sep cout<<"\n";
#define Yes cout<<"Yes\n";
#define No cout<<"No\n";
#define YES cout<<"YES\n";
#define NO cout<<"NO\n";

inline string shift(string s){
	int n = s.length();
	string r = "";
	r+=s[n-1];
	for(int i=0;i<n-1;i++)r+=s[i];
	return r;
}

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

	if(n<1200){
		cout<<"ABC\n";
	}else if(n<2800){
		cout<<"ARC\n";
	}else cout<<"AGC\n";
}
/*
uday
adyu
7 46
45 6
*/

/**
 *    author:  otera
**/
#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<deque>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
using namespace std;

//#define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef long double ld;
const int inf=1e9+7;
const ll INF=1LL<<60 ;
const ll mod=1e9+7 ;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<int, int> P;
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
#define pb push_back
#define debug(x)  cerr << #x << " = " << (x) << endl;
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; }

void solve() {
	int r; cin >> r;
    if(r < 1200) cout << "ABC\n";
    else if(r < 2800) cout << "ARC\n";
    else cout << "AGC\n";
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	//int t; cin >> t; rep(i, t)solve();
	solve();
    return 0;
}


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

using namespace std;

int main() {
  int64_t N;
  cin >> N;
  vector<int64_t> parts_a(N);
  for (int64_t n = 0; n < N; ++n) {
    cin >> parts_a[n];
  }
  sort(parts_a.begin(), parts_a.end());

  vector<int64_t> parts_b(N);
  for (int64_t n = 0; n < N; ++n) {
    cin >> parts_b[n];
  }

  vector<int64_t> parts_c(N);
  for (int64_t n = 0; n < N; ++n) {
    cin >> parts_c[n];
  }
  sort(parts_c.begin(), parts_c.end());

  int64_t result = 0;
  for (int64_t n = 0; n < N; ++n) {
    auto it = lower_bound(parts_a.begin(), parts_a.end(), parts_b[n]);
    int64_t a_num = (it - parts_a.begin());
    auto it2 = upper_bound(parts_c.begin(), parts_c.end(), parts_b[n]);
    int64_t b_num = (parts_c.end() - it2);
    result += a_num * b_num;
  }
  cout << result << endl;

  return 0;
}

#include <iostream>
#include <string>
#include <cstdlib>
#include <vector>
#include <array>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#define _USE_MATH_DEFINES
#include <cmath>
#include <climits>
#include <cstdio>
#include <iomanip>
#include <utility>
#include <unordered_map>

using namespace std;
typedef long long int ll;

#define SP(x) setprecision((int)x)
#define SW(x) setw(x)    // 桁数指定
#define SF(c) setfill(c) // 埋め文字指定('0')
#define ALL(v) v.begin(), v.end()

int main(int argc, char const *argv[])
{
    ll n;
    cin >> n;
    vector<ll> a(n);

    ll cnt = 0;

    for (ll i = 0; i < n; i++)
    {
        cin >> a[i];
        if (a[i] < 0)
            cnt++;
    }

    for (ll i = 0; i < n; i++)
    {
        if (a[i] < 0)
            a[i] *= -1;
    }

    if (cnt % 2 == 1)
    {
        sort(ALL(a));
        a[0] *= -1;
    }

    ll ans = 0;
    for (ll i = 0; i < n; i++)
    {
        ans += a[i];
    }
    cout << ans << endl;
}


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

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
//#define LOG(...)
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define RSORT(c) sort((c).rbegin(),(c).rend())
#define CLR(a) memset((a), 0 ,sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

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

int main() {
	int n, m;
	while (cin >> n >> m, n){
		vector<int> point(n);
		REP(i, n){
			cin >> point[i];
		}
		point.push_back(0);
		vector<int> sum = point;
		REP(i, n + 1){
			REP(j, n + 1){
					sum.push_back(sum[i] + sum[j]);
			}
		}
		SORT(sum);
		int ma = 0;
		REP(i, sum.size()){
			int left = 0;
			int right = sum.size() - 1;
			while (left < right){
				if (sum[left + (right - left) / 2] + sum[i] <= m){
					ma = max(sum[left + (right - left) / 2] + sum[i], ma);
					left = left + (right - left) / 2 + 1;

				}
				else{
					right = left + (right - left) / 2 - 1;
				}
			}

		}
		cout << ma << endl;
	}
}

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

int main() {
	std::int64_t n, m;
	std::vector<std::int64_t> in, pairSum;
	in.reserve(1000);
	pairSum.reserve(1000 * 1000);

	while (std::cin >> n >> m, n + m != 0) {
		in.push_back(0);
		for (int i = 0; i < n; ++i) {
			int p;
			std::cin >> p;
			in.push_back(p);
		}

		for (auto x : in)
			for (auto y : in)
				pairSum.push_back(x + y);

		std::sort(pairSum.begin(), pairSum.end());

		std::int64_t ans = 0;
		for (auto e : pairSum) {
			if (e > m)
				continue;

			auto it = std::upper_bound(pairSum.begin(), pairSum.end(), m - e);
			--it;
			ans = std::max(ans, e + *it);
		}

		std::cout << ans << std::endl;

		in.clear();
		pairSum.clear();
	}
}


#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#define rep(i,st,ed) for (int i=st;i<=ed;++i)
#define lb(x) (x&-x)
#define get(x) ((int)log2(x))

const int INF=0x3f3f3f3f;
const int N=66666;

int f[17][N],sum[N];
int v[17][17];

int read() {
	int x=0,v=1; char ch=getchar();
	for (;ch<'0'||ch>'9';v=(ch=='-')?(-1):v,ch=getchar());
	for (;ch<='9'&&ch>='0';x=x*10+ch-'0',ch=getchar());
	return x*v;
}

void upd(int &x,int v) {
	(x<v)?(x=v):0;
}

int main(void) {
	int n=read(),m=read();
	int prt=0;
	rep(i,1,m) {
		int x=read()-1,y=read()-1;
		v[x][y]=v[y][x]=read(),prt+=v[x][y];
	}
	for (int S=1;S<(1<<n);++S) {
		int x=get(lb(S));
		sum[S]=sum[S-(1<<x)];
		rep(i,0,n-1) if (v[i][x]&&(S>>i&1)) {
			sum[S]+=v[i][x];
		}
	}
	rep(i,0,n-1) for (int S=0;S<(1<<n);++S) f[i][S]=-INF;
	int ans=0; f[0][1]=0;
	for (int S=1;S<(1<<n);++S) {
		if (!(S&1)) continue;
		rep(i,0,n-1) {
			if (!((S>>i))&1) continue;
			for (int T=S;T;T=(T-1)&S) {
				if (T>>i&1) upd(f[i][S],f[i][S-T+(1<<i)]+sum[T]);
			}
			for (int T=(1<<n)-1-S,j;T;T-=lb(T)) {
				j=get(lb(T));
				if (v[i][j]) upd(f[j][S+(1<<j)],f[i][S]+v[i][j]);
			}
		}
		if (S>>(n-1)&1) upd(ans,f[n-1][S]);
	}
	printf("%d\n", prt-ans);
	return 0;
}

#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN=2010;
const int MOD=1e9+7;
int n,k;
long long fac[MAXN*MAXN],inv[MAXN*MAXN],f[MAXN][MAXN];
long long C(long long x,long long y)
{
	return fac[x]*inv[y]%MOD*inv[x-y]%MOD;
}
long long qpow(long long x,long long y)
{
	long long tmp=1;
	for(;y;y>>=1,x=x*x%MOD)if(y&1)tmp=tmp*x%MOD;
	return tmp;
}
int main()
{
//	freopen("test.in","r",stdin);
//	freopen("test.out","w",stdout);
	scanf("%d%d",&n,&k);
	if(k==1)
	{
		printf("1");
		return 0;
	}
	fac[0]=1;
	for(int i=1;i<=n*k;i++)fac[i]=fac[i-1]*i%MOD;
	inv[n*k]=qpow(fac[n*k],MOD-2);
	for(int i=n*k;i>=1;i--)inv[i-1]=inv[i]*i%MOD;
	f[0][0]=1;
	for(int i=1;i<=n;i++)
		for(int j=0;j<=i;j++)
		{
			f[i][j]=f[i-1][j];
			if(!j)continue;
			f[i][j]=(f[i][j]+f[i][j-1]*(n-j+1)%MOD*C(n-i+(n-j+1)*(k-1)-1,k-2))%MOD;
		}
	printf("%lld\n",f[n][n]);
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#define SF scanf
#define PF printf
#define MAXN 100010
using namespace std;
typedef long long ll;
ll a[MAXN];
int n,now,cnt0,cnt1;
ll gcd(ll x,ll y){
	if(y==0)
		return x;
	return gcd(y,x%y);
}
bool solve(){
	int cnt0=0,cnt1=0,now=0;
	for(int i=1;i<=n;i++){
		if(a[i]%2ll==0)
			cnt0++;
		else{
			cnt1++;
			now=i;
		}
	}
	if(cnt1==1&&a[now]!=1){
		ll g=0;
		a[now]--;
		for(int i=1;i<=n;i++)
			g=gcd(g,a[i]);
		for(int i=1;i<=n;i++)
			a[i]/=g;
		if(solve()==0)
			return 1;
	}
	if(cnt0%2==1)
		return 1;
	else
		return 0;
}
int main(){
	//freopen("game.in","r",stdin);
	//freopen("game.out","w",stdout);
	SF("%d",&n);
	for(int i=1;i<=n;i++)
		SF("%lld",&a[i]);
	if(solve())
		PF("First");
	else
		PF("Second");
}

#include <bits/stdc++.h>
using namespace std;
int main()
{
	// freopen("AGC010-D.in", "r", stdin);
	int n, cur = 0;
	cin >> n;
	vector<int> arr(n);
	copy(istream_iterator<int>(cin), istream_iterator<int>(), arr.begin());
	string str[] = {"First", "Second"}, ans;
	while (ans.empty()) {
		int cnt = count_if(arr.begin(), arr.end(), [&] (int x) { return x % 2 == 0; });
		if (cnt & 1) {
			ans = str[cur];
			break;
		}
		cur ^= 1;
		if (cnt != n - 1) {
			ans = str[cur];
		}
		int g = 0;
		for_each(arr.begin(), arr.end(), [&] (int x) {
			if (x == 1) {
				ans = str[cur];
			}
			x -= x & 1;
			while (x) {
				int r = g % x;
				g = x;
				x = r;
			}
		});
		if (!g) {
			break;
		}
		for_each(arr.begin(), arr.end(), [&] (int &x) { x /= g; });
	}
	cout << ans << endl;
	return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define ppb pop_back
#define fi first
#define se second
#define mid ((x + y) / 2)
#define left (ind * 2)
#define right (ind * 2 + 1)
#define mp make_pair
#define timer ((double)clock() / CLOCKS_PER_SEC)
#define endl "\n"
#define spc " "
#define d1(x) cerr<<#x<<":"<<x<<endl
#define d2(x, y) cerr<<#x<<":"<<x<<" "<<#y<<":"<<y<<endl
#define d3(x, y, z) cerr<<#x<<":"<<x<<" "<<#y<<":"<<y<<" "<<#z<<":"<<z<<endl
#define fast_io() ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;

typedef long long int lli;
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef pair<double, double> dd;

const int N = (int)(1e6 + 5);
const int LOG = (int)(20);

int n, nx[N], ans, k, f, nd[N], fl, one;
vector<int> v[N];

int dfs(int x) {
	int re = 0;
	for(auto i : v[x]) {
		int val = dfs(i);
		if(val == k)
			ans++;
		else
			re = max(re, val);
	}
	return re + 1;
}

int main() {
	fast_io();
	// freopen("inp.in", "r", stdin);

	cin >> n >> k;
	if(k > n)
		k = n + 10;
	for(int i = 1; i <= n; i++)
		cin >> nx[i];
	if(nx[1] != 1) {
		fl = 1;
		nx[1] = 1;
	}
	for(int i = 1; i <= n; i++)
		if(i != nx[i])
			v[nx[i]].pb(i);
	for(auto i : v[1])
		dfs(i);
	cout << ans + fl;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define Would
#define you
#define please

//世界の皆は頭ええな

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

	ll N, D;
	cin >> N >> D;
	ll kotae = (N - 1) * D;
	ll A[200001], C[200001];
	C[0] = 1e18;
	rep1(i, N) {
		cin >> A[i];
		kotae += A[i];
		C[i] = min(C[i - 1] + D, A[i]);
	}
	ll mae = 1e18;
	for (int i = N - 1; i > 1; i--) {
		mae = min(mae + D, A[i + 1]);
		ll kari = min(mae, C[i - 1]) + D;
		kotae += min(A[i], kari);
	}
	co(kotae);

	Would you please return 0;
}


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

#define MAX_V 200005
int V;
vector<int> nG[MAX_V], rG[MAX_V], G[MAX_V], vs; // 新しいグラフはG
bool used[MAX_V];
int cmp[MAX_V]; // ここに入る.

void add_edge(int from, int to){
    nG[from].push_back(to);
    rG[to].push_back(from);
}

void dfs(int v){
    used[v] = 1;
    for(int i = 0; i < nG[v].size(); i++) if(!used[nG[v][i]]) dfs(nG[v][i]);
    vs.push_back(v);
}

void rdfs(int v, int k){
    used[v] = 1;
    cmp[v] = k;
    for(int i = 0; i < rG[v].size(); i++) if(!used[rG[v][i]]) rdfs(rG[v][i],k);
}

int scc(){ // 新しい頂点数を返す
    memset(used, 0, sizeof(used));
    vs.clear();
    for(int v = 0; v < V; v++) if(!used[v]) dfs(v);
    memset(used, 0, sizeof(used));
    int k = 0;
    for(int i = vs.size() - 1; i >= 0; i--) if(!used[vs[i]]) rdfs(vs[i], k++);

    rep(i,V){
        int x = cmp[i];
        rep(j,nG[i].size()){
            int y = cmp[nG[i][j]];
            if(x == y)continue;
            G[x].push_back(y);
        }
    }

    rep(i,k){
        set<int> se;
        rep(j,G[i].size()) se.insert(G[i][j]);
        G[i].clear();
        for(auto x : se) G[i].push_back(x);
    }

    return k;
}

int main(){
    int n, m;
    cin >> n >> m;
    V = n;
    for(int i = 0; i < m; i++){
        int a, b;
        scanf("%d %d", &a, &b);
        // a--; b--;
        add_edge(a, b);
    }

    int nn = scc();
    /*
    cout << nn << endl;
    rep(i,n) cout << cmp[i] << ' ';
    cout << endl;

    rep(i,nn){
        rep(j,G[i].size()) cout << G[i][j] << ' ';
        cout << endl;
    }
    */
    int q;
    cin >> q;
    rep(i,q){
        int a, b;
        cin >> a >> b;
        if(cmp[a] == cmp[b]){
            cout << 1 << endl;
        }else{
            cout << 0 << endl;
        }
    }

    return 0;

}

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int MAXN=100005;
int n,m;
int head[MAXN];
struct node
{
    int from;
    int to;
    int next;
}edge[MAXN<<1];
int edge_cnt;
void add_edge(int fr,int to)
{
    edge[++edge_cnt].from=fr;
    edge[edge_cnt].to=to;
    edge[edge_cnt].next=head[fr];
    head[fr]=edge_cnt;
}
int col[MAXN];
bool flag;
void dfs(int x)
{
    for(int i=head[x];i;i=edge[i].next)
    {
        int u=edge[i].to;
        if(col[u]==-1)
        {
            col[u]=col[x]^1;
            dfs(u);
        }
        else if(col[x]==col[u]) flag=1;
    }
}
bool check()
{
    for(int i=1;i<=n;i++) col[i]=-1;
    for(int i=1;i<=n;i++)
    {
        if(col[i]==-1)
        {
            col[i]=0;
            dfs(i);
        }
    }
    return !flag;
}
long long ans;
signed main()
{
    cin>>n>>m;
    for(int i=1;i<=m;i++)
    {
        int u,v;
        scanf("%lld%lld",&u,&v);
        add_edge(u,v),add_edge(v,u);
    }
    if(check())
    {
        int temp=0;
        for(int i=1;i<=n;i++)
        {
            if(col[i]) temp++;
        }
        ans=temp*(n-temp)-m;
    }
    else ans=n*(n-1)/2-m;
    cout<<ans<<endl;
    // system("pause");
    return 0;
}


#include <iostream>
#include <set>

using namespace std;

set<int> f(int x) {
  set<int> s;
  if (x == 0) s.insert(0);
  while (x > 0) {
    s.insert(x % 10);
    x /= 10;
  }
  return s;
}

bool has_common(const set<int> &a, const set<int> &b) {
  auto ai = a.begin(), bi = b.begin();
  while (ai != a.end() && bi != b.end()) {
    if (*ai < *bi) ai++;
    else if (*ai > *bi) bi++;
    else return true;
  }
  return false;
}

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

  set<int> D;
  for (int i = 0; i < K; ++i) {
    int d;
    cin >> d;
    D.insert(d);
  }

  while (has_common(D, f(N))) N++;

  cout << N << endl;
}

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

int main(){
    int n,k;
    cin >> n >> k;
    set<int> s;
    rep(i,10) s.insert(i);
    rep(i,k){
        int m;cin >> m;
        s.erase(s.find(m));
    }
    vector<int> temp(5);
    int ans = 100000;
    for(auto m:s)for(auto i:s)for(auto j:s)for(auto k:s)for(auto l:s){
        temp[4] = 10000*m+1000*i+100*j+10*k+l;
        temp[3] = temp[4]%10000;
        temp[2] = temp[4]%1000;
        temp[1] = temp[4]%100;
        temp[0] = temp[4]%10;
        rep(j,5){
            if(temp[j]>=n) ans = min(ans,temp[j]);
        }
    }
    cout << ans << endl;

}


#include<iostream>
#include<algorithm>

using namespace std;

int main(){
	string s;
	cin>>s;
	if(s[0]=='R'){
		if(s[1]=='R'){
			if(s[2]=='R'){
				cout<<"3"<<endl;
			}
			else{
				cout<<"2"<<endl;
			}
		}
		else{
			cout<<"1"<<endl;
		}
	}
	else{
		if(s[1]=='R'){
			if(s[2]=='R'){
				cout<<"2"<<endl;
			}
			else{
				cout<<"1"<<endl;
			}
		}
		else{
			if(s[2]=='R') cout<<"1"<<endl;
			else cout<<"0"<<endl;
		}
	}
}

#include<iostream>

using namespace std;

int main() {
	string s;
	cin >> s;
	for (int i = 0;i <= (signed)s.length() - 3;i++) {
		if (s[i] == s[i + 1] || s[i] == s[i + 2] || s[i + 1] == s[i + 2]) {
			cout << i + 1 << " " << i + 3 << endl;
			return 0;
		}
	}
	if (s.length() >= 2) {
		if (s[s.length() - 1] == s[s.length() - 2]) {
			cout << s.length() - 1 << " " << s.length() << endl;
			return 0;
		}
	}
	cout << "-1 -1\n";
}


#include <iostream>
#include <string>
#include <algorithm>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cmath>
#include <stdio.h>
#include <queue>
#include <deque>
#include <cstdio>
#include <set>
#include <map>
#include <bitset>
#include <stack>
#include <cctype>
using namespace std;
set<pair<long long, long long>> st;
long long sum = 0;
int main() {
	long long n, m;
	cin >> n >> m;
	long long ans = 0;
	st.insert(make_pair(0, 1));
	for (int i = 0; i < n; i++) {
		long long a;
		cin >> a;
		sum = (sum + a) % m;
		pair<long long, long long> pa;
		if (st.lower_bound(make_pair(sum, 0)) != st.end()) {
			pa = *st.lower_bound(make_pair(sum, 0));
			if (sum == pa.first) {
				ans += pa.second;
				st.erase(*st.lower_bound(make_pair(sum, 0)));
				st.insert(make_pair(sum, pa.second + 1));
			}
			else {
				st.insert(make_pair(sum, 1));
			}
		}
		else {
			st.insert(make_pair(sum, 1));
		}
	}
	cout << ans << endl;
	return 0;
}

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

int main(){
    ll n, m; cin >> n >> m;
    map<ll,ll> data;
    ll sum = 0; data[0]++;
    for(int i=0; i<n; i++){
        ll a; cin >> a;
        sum += a;
        sum %= m;
        data[sum]++;
    }
    ll ans = 0;
    for(auto x : data) ans += x.second*(x.second-1)/2;
    cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
struct edge { ll s, t, x; };
typedef pair<ll, ll> P;
typedef pair<ll,P> event;
#define MOD 1000000007
const int MAX = 510000;

int main(){
  ll N;
  cin>>N;
  ll num=N;
  queue<char>memo;
  for(ll i=35;i>=0;i--){
    if(i%2==0){
      ll k=i/2ll;
      ll upper=((ll)pow(4,k+1)-1ll)/3ll;
      ll neupper=((ll)pow(4,k)-1ll)/3ll;;
      if(neupper<num&&num<=upper){memo.push('1');num-=(ll)pow(2,i);}
      else{memo.push('0');}
    }
    if(i%2==1){
      ll k=i/2ll;
      ll lower=((-2)*((ll)pow(4,k+1)-1ll))/3ll;;
      ll nelower=(-2)*((ll)pow(4,k)-1ll)/3ll;;
      if(lower<=num&&num<nelower){num+=(ll)pow(2,i);memo.push('1');}
      else{memo.push('0');}
    }
  }
  while(!memo.empty()){
    auto t=memo.front();
    if(t=='1'){break;}
    else{memo.pop();}
  }
  if(memo.empty()){cout<<0<<endl;}
  else{
    while(!memo.empty()){
      cout<<memo.front();
      memo.pop();
    }
    cout<<endl;
  }

}

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,n) for(ll i=0;i<n;++i)
#define INF (1ll<<60)
#define mod 1000000007
#define P pair<ll,ll>
#define Graph vector<vector<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; }

ll mypow(ll a,ll b){
    ll ret=1;
    while(b--){
        ret*=a;
    }
    return ret;
}

int main(){
    ll n;cin>>n;
    vector<ll> ans;

    if(n==0){
        cout<<0<<endl;
        return 0;
    }

    ll num=0;
    while(n){
        ans.push_back(abs(n%2));
        n-=abs(n%2)*mypow(-1,num);
        n/=2;
        num++;

        //cout<<n<<endl;
        //if(num==10) break;
    }

    reverse(ans.begin(),ans.end());
    rep(i,ans.size()) cout<<ans[i];
    cout<<endl;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int Mod = 998244353;
int fpow(int x, int y) {
	int res = 1;
	while (y) {
		if (y & 1) res = 1ll * res * x % Mod;
		x = 1ll * x * x % Mod;
		y >>= 1;
	}
	return res;
}
const int MAX_N = 1e6 + 5;
int N = 1e6, M, fac[MAX_N], ifc[MAX_N];
int C(int n, int m) {
	if (n < m || n < 0 || m < 0) return 0;
	else return 1ll * fac[n] * ifc[m] % Mod * ifc[n - m] % Mod;
}
int main () {
	for (int i = fac[0] = 1; i <= N; i++) fac[i] = 1ll * fac[i - 1] * i % Mod;
	ifc[N] = fpow(fac[N], Mod - 2);
	for (int i = N - 1; ~i; i--) ifc[i] = 1ll * ifc[i + 1] * (i + 1) % Mod;
	cin >> N >> M;
	if (N > M) swap(N, M);
	int ans = 0;
	for (int i = 1; i <= N; i++) ans = (ans + 1ll * C(i << 1, i) * C(N - i + M - i, N - i)) % Mod;
	ans = 1ll * ans * fpow(2 * C(N + M, M) % Mod, Mod - 2) % Mod;
	printf("%d\n", (ans + M) % Mod);
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

const int md = 998244353;

inline void add(int &a, int b) {
  a += b;
  if (a >= md) {
    a -= md;
  }
}

inline int mul(int a, int b) {
  return (long long) a * b % md;
}

inline int power(int a, int b) {
  int res = 1;
  while (b > 0) {
    if (b & 1) {
      res = mul(res, a);
    }
    a = mul(a, a);
    b >>= 1;
  }
  return res;
}

inline int inv(int x) {
  return power(x, md - 2);
}

const int N = 2000010;

int fact[N], inv_fact[N];

inline int C(int n, int k) {
  if (k < 0 || k > n) {
    return 0;
  }
  return mul(fact[n], mul(inv_fact[k], inv_fact[n - k]));
}

int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  fact[0] = inv_fact[0] = 1;
  for (int i = 1; i <= n + m; i++) {
    fact[i] = mul(fact[i - 1], i);
    inv_fact[i] = inv(fact[i]);
  }
  int ans = 0;
  int cur = 0;
  int val = C(n + m, n);
  for (int s = 1; s <= n + m; s++) {
    add(cur, val);
    add(ans, mul(cur, inv(s)));
    int x = C(n + m - (s + 1), n - (s + 1) / 2);
    int y = C(n + m - (s + 1), m - (s + 1) / 2);
    int z = C(s - 1, s / 2);
    int ways = mul(z, x + y);
    if (s % 2 == 0) {
      add(val, ways);
    } else {
      add(val, md - ways);
    }
  }
  ans = mul(ans, inv(C(n + m, n)));
  printf("%d\n", ans);
  cerr << "time = " << clock() << " ms" << 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;
    cin >> n;
    ll ans=0;
    for(ll i=1;i<=100000;i++){
        if(i*i<=n){
            ans=i*i;
        }
    }
    cout << ans << endl;
}

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

int main () {
    int x, ans = 0;
    cin >> x;

    ans += x/500*1000;
    x -= x/500*500;
    ans += x/5*5;
    cout << ans << endl;
}


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

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

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

  int N;
  cin >> N;

  set<int> paper;
  REP(i, N) {
    int A;
    cin >> A;
    if ( paper.count(A) ) {
      paper.erase(A);
    } else {
      paper.insert(A);
    }
  }

  cout << paper.size() << endl;

  return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <utility>
#include <algorithm>
#include <functional>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <iomanip>
#include <sstream>
#include <bitset>
#include <limits>
#include <numeric>
#include <valarray>
#include <fstream>
#include <array>
#include <random>
#include <unordered_set>
#include <unordered_map>

using namespace std;
using uint = uint32_t;
using LL = int64_t;
using ULL = uint64_t;
using PP = pair<LL, LL>;
template <typename T> using PriorityQ = priority_queue<T, vector<T>, greater<T> >;
#define REP(i, a, n) for(LL i = (a), i##_max_ = (n); i < i##_max_; ++i)
#define REM(i, a, n) for(LL i = (LL)(n) - 1, i##_min_ = (a); i >= i##_min_; --i)
#define FLOAT fixed << setprecision(16)
#define SPEEDUP { cin.tie(NULL); ios::sync_with_stdio(false); }
const int INF = 0x3FFFFFFF;
const LL INFLL = 0x3FFFFFFF3FFFFFFF;
const double INFD = 1.0e+308;
const double EPS = 1.0e-9;

void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
template <class T, class U> istream& operator>>(istream& ist, pair<T, U>& right) { return ist >> right.first >> right.second; }
template <class T, class U> ostream& operator<<(ostream& ost, const pair<T, U>& right) { return ost << right.first << ' ' << right.second; }
template <class T, class TCompatible, size_t N> void Fill(T(&dest)[N], const TCompatible& val) { fill(dest, dest + N, val); }
template <class T, class TCompatible, size_t M, size_t N> void Fill(T(&dest)[M][N], const TCompatible& val) { for (int i = 0; i < M; ++i) Fill(dest[i], val); }
template <class T> T Next() { T buf; cin >> buf; return buf; }
istream& Ignore(istream& ist) { string s; ist >> s; return ist; }
bool Inside(int i, int j, int h, int w) { return i >= 0 && i < h && j >= 0 && j < w; }

#ifdef ONLY_MY_ENVIR
#include "Accumulator.h"
#include "Algebraic.h"
#include "BinaryMatrix.h"
#include "BinaryTree.h"
#include "Bipartite.h"
#include "BIT.h"
#include "Compressor.h"
#include "Decompositions.h"
#include "DiscreteLog.h"
#include "DynamicMod.h"
#include "Exponential.h"
#include "Factorization.h"
#include "FFT.h"
#include "FlowSolver.h"
#include "Graph.h"
#include "GraphUtil.h"
#include "IntMod.h"
#include "LazySegmentTree.h"
#include "LIS.h"
#include "Math.h"
#include "MathUtil.h"
#include "Matrix.h"
#include "MinCostFlowSolver.h"
#include "MinMax.h"
#include "Numbers.h"
#include "Optimize.h"
#include "Permutation.h"
#include "Polynomial.h"
#include "Position.h"
#include "Random.h"
#include "Range.h"
#include "Rational.h"
#include "RuntimeMod.h"
#include "SegmentTree.h"
#include "SegmentTree2D.h"
#include "Sets.h"
#include "Shortest.h"
#include "SlidingWindow.h"
#include "SpanningTree.h"
#include "StringSearching.h"
#include "SuffixArray.h"
#include "SwitchList.h"
#include "Timer.h"
#include "Tree.h"
#include "TreeUtil.h"
#include "UnionFind.h"
#include "Util.h"
#include "VectorUtil.h"
#endif

#ifdef __GNUC__
typedef __int128 LLL;
istream& operator>>(istream& ist, __int128& val) { LL tmp; ist >> tmp; val = tmp; return ist; }
ostream& operator<<(ostream& ost, __int128 val) { LL tmp = val; ost << tmp; return ost; }
#endif

int N;
int main() {
	cin >> N;
	string s[3];
	REP(i, 0, 3) cin >> s[i];

	int cnt = 0;
	REP(i, 0, N) {
		map<int, int> mp;
		REP(k, 0, 3) {
			++mp[s[k][i]];
		}

		int mx = 0;
		for (auto p : mp) {
			mx = max(mx, p.second);
		}
		cnt += 3 - mx;
	}

	cout << cnt << endl;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

template<typename T, typename ...Ts>
vector<T> fusion(vector<T> bs,Ts... ts){
  auto append=[&](auto vs){for(auto v:vs) bs.emplace_back(v);};
  initializer_list<int>{(void(append(ts)),0)...};
  return bs;
}

// longest common prefix of s and s[i:n]
vector<int> zalgorithm(vector<int> s){
  int n=s.size();
  vector<int> as(n,0);
  as[0]=n;
  int i=1,j=0;
  while(i<n){
    while(i+j<n&&s[j]==s[i+j]) j++;
    as[i]=j;
    if(j==0){
      i++;
      continue;
    }
    int k=1;
    while(i+k<n&&k+as[k]<j) as[i+k]=as[k],k++;
    i+=k;
    j-=k;
  }
  return as;
}

//INSERT ABOVE HERE
signed main(){
  int n;
  cin>>n;
  vector<int> as(n),bs(n);
  for(int i=0;i<n;i++) cin>>as[i];
  for(int i=0;i<n;i++) cin>>bs[i];

  vector<int> cs(n),ds(n);
  for(int i=0;i<n;i++){
    cs[i]=as[i]^as[(i+1)%n];
    ds[i]=bs[i]^bs[(i+1)%n];
  }

  vector<int> ss=fusion(ds,cs,cs);
  auto zs=zalgorithm(ss);
  for(int i=0;i<n;i++)
    if(zs[n+i]>=n) cout<<i<<" "<<(as[i]^bs[0])<<endl;

  return 0;
}

//fold
#ifndef KHALIL
#include <bits/stdc++.h>
#else
#include "header.h"
#endif
#define endl '\n'
#define mp make_pair
#define tostr(x) static_cast<ostringstream&>((ostringstream()<<dec<<x)).str()
#define rep(i,begin,end) for(auto i = begin;i < end;i++)
#define repr(i,begin,end) for(auto i = begin-1;i >= end;i--)
#define pb push_back
#define sz(a) ((int)(a).size())
#define fi first
#define se second
#define abs(a) ((a) < (0) ? (-1)*(a) : (a))
#define SQ(a) ((a)*(a))
#define eqd(a,b) (abs(a-b)<1e-9)
#define X real()
#define Y imag()
using namespace std;
typedef long long ll;
typedef long double ld;
template <typename t> t in(t q){cin >> q;return q;}
template <typename T> ostream& operator<<(ostream& os, const vector<T>& v){os << "[";for (int i = 0; i < sz(v); ++i) { os << v[i]; if (i != sz(v) - 1) os << ",";}os << "]";return os;}
template <typename T, typename S>ostream& operator<<(ostream& os, const map<T, S>& v){for (auto it : v)os << "(" << it.first << ":" << it.second << ")";return os;}
template <typename T, typename S>ostream& operator<<(ostream& os, const pair<T, S>& v){os << "(" << v.first << "," << v.second << ")";return os;}
const long double PI = acosl(-1);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng64(chrono::steady_clock::now().time_since_epoch().count());
inline int rand(int l,int r){return uniform_int_distribution<int>(l, r)(rng);}
inline ll rand(ll l,ll r){return uniform_int_distribution<ll>(l, r)(rng64);}
//endfold
#define  N  (200'005)
#define MOD (1000000000ll + 7ll)
#define OO (1050000000)
#define OOL (1100000000000000000)

//global
int a[N];

int main(){
	//fold
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cout << setprecision(10);
	//endfold
	int n,m;
	cin >> n >> m;
	int eew = 0;
	rep(i,0,m){
		cin >> a[i];
		if(a[i]&1){
			eew++;
		}
	}
	if(eew == 0){
		vector<int> v;
		vector<int> w;
		v.push_back(a[0]-2);
		v.push_back(1);
		rep(i,1,m){
			v.push_back(a[i]);
		}
		v.push_back(1);
		rep(i,0,sz(v)){
			if(v[i] != 0) w.push_back(v[i]);
		}
		rep(i,0,m){
			cout << a[i] << " ";
		}cout << endl;
		cout << sz(w) << endl;
		rep(i,0,sz(w)){
			cout << w[i] << " ";
		}
		return 0;
	}
	if(eew == 1){
		sort(a,a+m,[](int w,int e){
			return (w&1) > (e&1);
		});
		vector<int> v;
		vector<int> w;
		v.push_back(a[0]-1);
		rep(i,1,m){
			v.push_back(a[i]);
		}
		v.push_back(1);
		rep(i,0,sz(v)){
			if(v[i] != 0) w.push_back(v[i]);
		}
		rep(i,0,m){
			cout << a[i] << " ";
		}cout << endl;
		cout << sz(w) << endl;
		rep(i,0,sz(w)){
			cout << w[i] << " ";
		}
		return 0;
	}
	if(eew == 2){
		vector<int> q;
		rep(i,0,m){
			if(a[i]&1) q.push_back(i);
		}
		vector<int> res = {a[q[0]]};
		vector<int> v;
		vector<int> w;
		rep(i,0,m){
			if(i == q[0] || i == q[1]) continue;
			res.push_back(a[i]);
		}
		res.push_back(a[q[1]]);
		v.push_back(a[q[0]]-1);
		rep(i,1,m-1){
			v.push_back(res[i]);
		}
		v.push_back(a[q[1]]+1);
		rep(i,0,sz(v)){
			if(v[i] != 0) w.push_back(v[i]);
		}
		rep(i,0,m){
			cout << res[i] << " ";
		}cout << endl;
		cout << sz(w) << endl;
		rep(i,0,sz(w)){
			cout << w[i] << " ";
		}
		return 0;
	}
	cout << "Impossible";
	return 0;
}


#include<iostream>
using namespace std;

int main(){
	int n, a ;
	cin >> n;
	int day[n];
	for(int i = 0; i < n; i++){
		cin >> a;
		day[i] = a;
	}
	for(int i = 0; i < n; i++){
		int b = n - i - 1;
		printf("%d", day[b]);
		if(b != 0){
			printf(" ");
		}
	}
	printf("\n");
	return 0;
	 }

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

int main() {
    int n;
    string result;
    vector<int> a;
    vector<int>::reverse_iterator begin,end;
    cin >> n;
    for (int c = 0; c < n; c++) {
        int t;
        cin >> t;
        a.push_back(t);
    }
    begin = a.rbegin();
    end = a.rend();

    for (;begin != end; begin++) {
        result += to_string(*begin) + " ";
    }
    result.erase(result.size() - 1);
    cout << result << endl;
}


#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;
    cin >> n;
    vector<int>a(n);
    rep(i,n) cin >> a[i];
    sort(a.rbegin(),a.rend());
    vector<int>ans;
    rep(i,n-1) {
        if(a[i] == a[i+1]) {
            ans.push_back(a[i]);
            i++;
        }
    }
    if(ans.size()<2) {
        cout << 0 << endl;
        return 0;
    }
    ll square = (ll)(ans[0]) * (ll)(ans[1]);
    cout << square << endl;
}

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

int main() {
  int n; cin >> n;
  map<pair<int, int>, int> mp;
  long long ans = 0;
  for(int i = 0; i < n; i++) {
    double num; cin >> num;
    long long x = llround(num*1e9);

    int two = 0, five = 0;
    while(x % 2 == 0) two++, x /= 2;
    while(x % 5 == 0) five++, x /= 5;
    for(auto p: mp) {
      if(p.first.first+two >= 18 && p.first.second+five >= 18) ans += p.second;
    }
    mp[make_pair(two, five)]++;
  }
  cout << ans << endl;
  return 0;
}


#include <iostream>
using namespace std;
int main() {
    int A, B;
    cin >> A >> B;

    bool can;

    if  (A > 8 || B > 8) can = false;
    else can = true;

    if (can) cout << "Yay!" << endl;
    else cout << ":(" << endl;
}

#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
typedef pair<int, int> ii;
#define DEBUG freopen("in.txt", "r", stdin);

struct fastio {
    fastio() {
        ios::sync_with_stdio(false);
        cout << setprecision(10) << fixed;
        cin.tie(0);
    }
};

fastio _fast_io;

string a, b, c;
int main() {
    cin >> a >> b >> c;
    bool ok = true;
    if (a.back() != b.front())
        ok = false;
    if (b.back() != c.front())
        ok = false;
    if (ok)
        cout << "YES" << endl;
    else
        cout << "NO" << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
const int inf=0x3f3f3f3f;
long long a[maxn];
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
    long long d;
    cin>>d;
    a[0]=1;
    for(int i=1;i<=20;i++)
    	a[i]=a[i-1]*10;
    long long ans=0;
    for(int i=2;i<=20;i++)
    {
    	int k=i/2;
    	for(long long state=0;state<1<<k;state++)
    	{
    		long long cha,chan=d;
    		if(i%2==1)
    			cha=10;
    		else
    			cha=1;
    		for(int j=0;j<k;j++)
    		{
    			long long x=abs(chan)%10;
    			if(chan<0)
    			{
    				x=10-x;
    				x%=10;
				}
				if(state>>j&1)
				{
					long long sym=10-x;
					if(j==0)
						sym--;
					cha*=sym;
					chan+=x*a[i-2*j-1];
					chan-=x;
				}
				else
				{
					long long sym=x;
					if(j==0)
						sym--;
					if(sym<0)
					{
						cha=0;
						break;
					}
					cha*=sym;
					chan-=(10-x)*a[i-2*j-1];
					chan+=(10-x);
				}
				chan/=10;
			}
			if(chan==0)
				ans+=cha;
		}
	}
	cout<<ans<<endl;
	return 0;
}

#include<bits/stdc++.h>
#include<vector>
#define LL long long
#define PI pair<LL,LL>
using namespace std;
LL D,St1[25],St2[25],P[25],Last[25],Ans;
vector<LL>V;
void DFS(LL L,LL R,LL Val,LL Cnt){
	if(L==R){
		Ans+=(Val==D)*Cnt;
		return;
	}
	if(abs(Val-D)>Last[L]*10) return;
	for(LL I=-9;I<=9;I++){
		LL Ways=((L==0)?St2[I+10]:St1[I+10]);
		if(V[L]==0) Ways=(I<=0);
		if(Ways==0) continue;
		DFS(L+1,R,Val+I*V[L],Cnt*Ways);
	}
}
void Solve(LL Len){
	V.clear();
	for(LL I=0;Len-I-1>=I;++I) V.push_back(P[Len-I-1]-P[I]);
	vector<PI>An1,An2;
	for(LL I=V.size()-1;I>-1;--I) Last[I]=Last[I+1]+V[I];
	DFS(0,V.size(),0,1);
}
int main(){
    scanf("%lld",&D);
    P[0]=1;
    for(LL I=1;I<=18;I++) P[I]=P[I-1]*10;
    for(LL I=0;I<=9;I++)
    	for(LL J=0;J<=9;J++){
    		St1[I-J+10]++;
			if(I&&J) St2[I-J+10]++;
		}
	for(LL I=1;I<=18;I++) Solve(I);
	printf("%lld",Ans);
	return 0;
}


#include <stdio.h>
int main(){
		long long int s,max,min,k;
		int n;
		scanf("%d",&n);
		max = -1000000;
		min = 1000000;
		s = 0;
		for (int i=0; i<n ; ++i){
			scanf("%lld",&k);
				if(k>max) max = k;
				if(k<min) min = k;
				s =s+k;
		};
		printf("%lld %lld %lld\n",min ,max, s);
	    return 0;
}

#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <numeric>
using namespace std;

int main() {
  vector<int> result;
  string n;
  string input;

  getline(cin, n);
  getline(cin, input);
  istringstream iss(input);
  string num;

  while (getline(iss, num, ' ')) {
    result.push_back(stoi(num));
  }

  long long min = *min_element(result.begin(), result.end());
  long long max = *max_element(result.begin(), result.end());
  long long sum = accumulate(result.begin(), result.end(), 0LL);

  cout << min << " " << max << " " << sum << endl;
}


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/rope>
using namespace std;
using namespace chrono;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
mt19937 rng(high_resolution_clock::now().time_since_epoch().count());
mt19937_64 rngll(high_resolution_clock::now().time_since_epoch().count());
template<typename T> T ctmax(T &x, const T &y){ return x = max(x, y); }
template<typename T> T ctmin(T &x, const T &y){ return x = min(x, y); }
template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

int main(){
	cin.tie(0)->sync_with_stdio(0);
	int n;
	cin >> n;
	vector<vector<int>> a(n, vector<int>(n));
	for(auto i = 0; i < n; ++ i){
		for(auto j = 0; j < n; ++ j){
			cin >> a[i][j];
		}
	}
	const long long minf = numeric_limits<long long>::min() / 8;
	const int mx = 1 << n;
	vector<long long> dp(mx, minf), sum(mx);
	dp[0] = 0;
	for(auto mask = 1; mask < mx; ++ mask){
		for(auto i = 0; i < n; ++ i){
			if(mask & 1 << i){
				for(auto j = i + 1; j < n; ++ j){
					if(mask & 1 << j){
						sum[mask] += a[i][j];
					}
				}
			}
		}
		for(auto submask = mask; submask; submask = (submask - 1) & mask){
			ctmax(dp[mask], dp[mask ^ submask] + sum[submask]);
		}
	}
	cout << dp.back();
	return 0;
}

/*

*/

////////////////////////////////////////////////////////////////////////////////////////
//                                                                                //
//                                   Coded by Aeren                                   //
//                                                                                //
////////////////////////////////////////////////////////////////////////////////////////

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n)  for(long long i=0;i<(long long)(n);i++)
#define REP(i,k,n) for(long long i=k;i<(long long)(n);i++)
#define all(a) a.begin(),a.end()
#define pb emplace_back
#define eb emplace_back
#define lb(v,k) (lower_bound(all(v),k)-v.begin())
#define ub(v,k) (upper_bound(all(v),k)-v.begin())
#define fi first
#define se second
#define pi M_PI
#define PQ(T) priority_queue<T>
#define SPQ(T) priority_queue<T,vector<T>,greater<T>>
#define dame(a) {out(a);return 0;}
#define decimal cout<<fixed<<setprecision(15);
#define dupli(a) a.erase(unique(all(a)),a.end())
typedef long long ll;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> PP;
typedef tuple<ll,ll,ll,ll> PPP;
typedef multiset<ll> S;
using vi=vector<ll>;
using vvi=vector<vi>;
using vvvi=vector<vvi>;
using vvvvi=vector<vvvi>;
using vp=vector<P>;
using vvp=vector<vp>;
using vb=vector<bool>;
using vvb=vector<vb>;
const ll inf=1001001001001001001;
const ll INF=1001001001;
const ll mod=1000000007;
const double eps=1e-10;
template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<class T> void out(T a){cout<<a<<'\n';}
template<class T> void outp(T a){cout<<'('<<a.fi<<','<<a.se<<')'<<'\n';}
template<class T> void outvp(T v){rep(i,v.size())cout<<'('<<v[i].fi<<','<<v[i].se<<')';cout<<'\n';}
template<class T> void outvvp(T v){rep(i,v.size())outvp(v[i]);}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';}
template<class T> void outvv(T v){rep(i,v.size())outv(v[i]);}
template<class T> bool isin(T x,T l,T r){return (l)<=(x)&&(x)<=(r);}
template<class T> void yesno(T b){if(b)out("yes");else out("no");}
template<class T> void YesNo(T b){if(b)out("Yes");else out("No");}
template<class T> void YESNO(T b){if(b)out("YES");else out("NO");}
template<class T> void noyes(T b){if(b)out("no");else out("yes");}
template<class T> void NoYes(T b){if(b)out("No");else out("Yes");}
template<class T> void NOYES(T b){if(b)out("NO");else out("YES");}
void outs(ll a,ll b){if(a>=inf-100)out(b);else out(a);}
ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);}
ll modpow(ll a,ll b){ll res=1;a%=mod;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;}

int main(){
    ll n;cin>>n;
    vvi v(n,vi(n));
    rep(i,n)rep(j,n)cin>>v[i][j];
    vi dp(1<<n);
    vi val(1<<n);
    rep(i,1<<n)rep(j,n)REP(k,j+1,n)if((i>>j&1)&&(i>>k&1))val[i]+=v[j][k];
    dp[0]=0;
    REP(i,1,1<<n){
        for(int j=i;j>=0;j=(j-1)&i){
            chmax(dp[i],dp[j]+val[i&(~j)]);
            if(j==0)break;
        }
    }
    out(dp[(1<<n)-1]);
}


#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif

#include <bits/stdc++.h>

using namespace std;

#define mp make_pair
#define pb push_back
#define sz(s) ((int) ((s).size()))
#define eprintf(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)

#ifdef _WIN32
#define LLD "%I64d"
#else
#define LLD "%lld"
#endif

typedef long long ll;
typedef long double ld;

const int inf = (int) 1e9 + 100;
const ld eps = 1e-11;
const ld pi = acos(-1.0L);

mt19937 mrand(random_device{} ());
int rnd(int x) {
  return mrand() % x;
}

void precalc() {
}

const int maxn = 1e6 + 100;
int n;
char s[maxn], t[maxn];

bool read() {
  if (scanf("%d", &n) < 1) {
    return false;
  }
  scanf("%s", s);
  scanf("%s", t);
  return true;
}

vector<int> pos[300];

void solve() {
  {
    bool ok = true;
    for (int i = 0; i < n; i++) {
      if (s[i] != t[i]) {
        ok = false;
      }
    }
    if (ok) {
      printf("0\n");
      return;
    }
  }
  for (int i = 0; i < 300; i++) {
    pos[i].clear();
  }
  for (int i = 0; i < n; i++) {
    pos[s[i]].pb(i);
  }
  vector<int> xs;
  vector<int> want;
  for (int i = n - 1; i >= 0; i--) {
    if (i == 0 || t[i] != t[i - 1]) {
      int mx = sz(xs) ? xs.back() : inf;
      mx = min(mx, i);
      int j = (int) (upper_bound(pos[t[i]].begin(), pos[t[i]].end(), mx) - pos[t[i]].begin());
      j--;
      if (j == -1) {
        printf("-1\n");
        return;
      }
      xs.pb(pos[t[i]][j]);
      want.pb(i);
    }
  }
  reverse(xs.begin(), xs.end());
  reverse(want.begin(), want.end());
  vector<int> magic;
  for (int i = 0; i < sz(xs); i++) {
    magic.pb(xs[i] - i);
  }
  magic.pb(want.back() - sz(xs) + 1);
  int ans = 1;
  for (int i = 0; i < sz(xs); i++) {
    int j = (int) (lower_bound(magic.begin(), magic.end(), want[i] - i) - magic.begin());
    ans = max(ans, 1 + j - i);
  }
  printf("%d\n", ans);
}

int main() {
  precalc();
#ifdef DEBUG
  assert(freopen("text.in", "r", stdin));
  assert(freopen("text.out", "w", stdout));
#endif

  while (true) {
    if (!read()) {
      break;
    }
    solve();
#ifdef DEBUG
    eprintf("Time: %.18f\n", (double) clock() / CLOCKS_PER_SEC);
#endif
  }
  return 0;
}

#include <iostream>
#include <vector>

using namespace std;

const long long INF = 1LL << 60;

template<typename T>
class LazySegTree {
public:
    explicit LazySegTree(int n, T init, T def) : N(calcN_(n)), def(def) {
        mVal.assign(2*N+1, init);
        mLazy.assign(2*N+1, 0);
        mDirty.assign(2*N+1, 0);
    }
    void update(int l, int r, T value){
        updateImpl_(l, r, value, 0, 0, N);
    }
    void setVal(int idx, T value){
        T cur= get(idx, idx+1);
        update(idx, idx+1, value - cur);
    }
    T get(int l, int r){
        return getImpl_(l, r, 0, 0, N);
    }
private:
    int calcN_(int n){
        int res = 1;
        while(res < n) res *= 2;
        return res;
    }
    void updateImpl_(int l, int r, T value, int idx, int rangeL, int rangeR){
        eval_(idx);
        if(r <= rangeL || rangeR <= l) return;
        if(l <= rangeL && rangeR <= r){
            setLazy_(idx, value);
            eval_(idx);
        } else {
            int rangeM = (rangeL+rangeR)/2;
            updateImpl_(l, r, value, 2*idx+1, rangeL, rangeM);
            updateImpl_(l, r, value, 2*idx+2, rangeM, rangeR);
            mVal[idx] = operate_(mVal[2*idx+1], mVal[2*idx+2]);
        }
    }
    void setLazy_(int idx, T value){
        mLazy[idx] += value;
        mDirty[idx] = 1;
    }
    T getImpl_(int l, int r, int idx, int rangeL, int rangeR){
        if(r <= rangeL || rangeR <= l) return def;
        eval_(idx);
        if(l <= rangeL && rangeR <= r) return mVal[idx];
        int rangeM = (rangeL+rangeR)/2;
        T a = getImpl_(l, r, 2*idx+1, rangeL, rangeM);
        T b = getImpl_(l, r, 2*idx+2, rangeM, rangeR);
        return operate_(a, b);
    }
    void eval_(int idx){
        if(!mDirty[idx]) return;
        mVal[idx] += mLazy[idx];
        if(idx < N-1){
            setLazy_(2*idx+1, mLazy[idx]);
            setLazy_(2*idx+2, mLazy[idx]);
        }
        mLazy[idx] = 0;
        mDirty[idx] = 0;
    }
    T operate_(T a, T b) const { return min(a, b); }
    const int N;
    const T def;
    vector<T> mVal;
    vector<T> mLazy;
    vector<int> mDirty;
};

int main(){
    int N, Q, A, B; cin >> N >> Q >> A >> B;
    vector<int> x(Q);
    for(auto& t : x) cin >> t;
    LazySegTree<long long> minus(N+1, 1LL << 40, INF);
    LazySegTree<long long> plus(N+1, 1LL << 40, INF);
    minus.setVal(A, abs(B-x[0]) - A);
    minus.setVal(B, abs(A-x[0]) - B);
    plus.setVal(A, abs(B-x[0]) + A);
    plus.setVal(B, abs(A-x[0]) + B);
    for(int i=1;i<Q;i++){
        auto v = min(minus.get(0, x[i]) + x[i], plus.get(x[i], N+1) - x[i]);
        minus.update(1, N+1, abs(x[i]-x[i-1]));
        plus.update(1, N+1, abs(x[i]-x[i-1]));
        minus.setVal(x[i-1], v - x[i-1]);
        plus.setVal(x[i-1], v + x[i-1]);
    }
    long long res = INF;
    for(int i=1;i<=N;i++) res = min(res, plus.get(i, i+1) - i);
    cout << res << endl;
}


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

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

int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cout<<setprecision(32);

	int n; cin>>n;
	set<pair<int, int> > a, b;
	for(int i = 0; i < n; i++){
		int x, y;
		cin>>x>>y;
		a.insert({x, y});
	}
	for(int i = 0; i < n; i++){
		int x, y;
		cin>>x>>y;
		b.insert({x, y});
	}
	int ans = 0;
	for(auto pp : b){
		int x = -1, y = -1;
		for(auto qq : a){
			if(qq.first < pp.first && qq.second < pp.second){
				if(y < qq.second){
					x = qq.first;
					y = qq.second;
				}
			}
		}
		if(x != -1){
			a.erase({x, y});
			ans++;
		}
	}
	cout<<ans<<endl;

	return 0;
}

#include <bits/stdc++.h>

using namespace std;

template<class T> void ckmin(T &a, T b) { a = min(a, b); }
template<class T> void ckmax(T &a, T b) { a = max(a, b); }

#define pb push_back
#define mp make_pair
#define cotu cout
#define itn int
#define Red ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define F first
#define S second
#define sz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for(int i = 0; i < n; ++i)
#define repr(i,n) for(int i = n - 1; i >= 0; --i)
#define Rep(i, a, n) for(int i = (a); i <=(n); ++i)
#define repst(i, n) for(auto it = n.begin(); it != n.end(); ++it)
#define Repr(i, a, n) for(int i = (n); i >= (a); --i)
#define setp(x) fixed << setprecision(x)
#define ordered_set tree<int , null_type,less<int >, rb_tree_tag,tree_order_statistics_node_update>

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

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

const int inf = int(1e9);
const int mod = inf + 7;
const int N = 3e5 + 555; // TL MB
const double PI = acos(-1.0);
void mul(ll &x, ll y){
	x *= y;
	if(x >= mod) x %= mod;
}

void add(ll &x, ll y){
	x += y;
	if(x >= mod) x -= mod;
	if(x < 0) x += mod;
}
ll bp(ll a, ll n){
	ll r = 1;
	while(n){
		if(n & 1) mul(r, a);
		mul(a, a);
		n >>= 1;
	}
	return r;
}

vector<int> g[N];
int n, k;
vector<int> mt;
vector<char> used;

bool try_kuhn (int v) {
	if (used[v])  return false;
	used[v] = true;
	for (size_t i=0; i<g[v].size(); ++i) {
		int to = g[v][i];
		if (mt[to] == -1 || try_kuhn (mt[to])) {
			mt[to] = v;
			return true;
		}
	}
	return false;
}

void solve()
{
		cin >> n;
		vector<pii> p(n), q(n);
		rep(i, n) cin >> p[i].F >> p[i].S;
		rep(i, n) cin >> q[i].F >> q[i].S;
		sort(all(p));
		sort(all(q));
		rep(i, sz(p)){
			rep(j, sz(q)){
				if(p[i].F < q[j].F && p[i].S < q[j].S) g[i].pb(j + n), g[j + n].pb(i);
			}
		}
	k = 2 * n;
	mt.assign (k, -1);
	for (int v=0; v<n; ++v) {
		used.assign (n, false);
		try_kuhn (v);
	}

 	int res = 0;
	for (int i=n; i<2*n; ++i)
		if (mt[i] != -1) ++res;
	cout << res;
}

int main()
{
	// freopen("input.txt", "r", stdin);
	// freopen("output.txt", "w", stdout);
    // Red;
  	int T = 1;
  	// cin >> T;
  	for(int i = 1; i <= T; ++i){
  		solve();
  	}
  	return 0;

}


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

string S;

int main() {
    cin >> S;

    reverse(S.begin(), S.end());
    string new_S = S.substr(8, S.length()-8);
    reverse(new_S.begin(), new_S.end());

    cout << new_S << endl;

    return 0;
}

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

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	string s; cin >> s;
	for (int i = 0; i < s.length() - 8; i++) cout << s[i];
	return 0;
}


#include <fstream>
#include <vector>
#include <cstring>
#include <iostream>

// BE CAREFUL WITH HASH TABLE & UNORDERED MAP
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
using namespace std;

#define MOD 998244353
#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define F0R(i,b) FOR(i,0,b)
#define RFO(i,a,b) for (int i = (b-1); i >= (a); i--)
#define RF0(i,b) RFO(i,0,b)
#define lli long long int
#define pii pair<int,int>
#define add(a,b) ((int) (((lli) (a) + (b))%MOD))
#define mul(a,b) ((int) (((lli) (a) * (b))%MOD))
#define PB emplace_back
#define F first
#define S second

// ifstream cin ("cinput.in");
// ofstream cout ("coutput.out");

int N, u, v;
vector<int> graph [100000];
int visited [100000][2];
// Helper Functions

void dfs(int node, int dist, int ind) {
    visited[node][ind] = dist;
    for (int neigh: graph[node]) {
        if (visited[neigh][ind] != -1) continue;
        visited[neigh][ind] = dist+1;
        dfs(neigh, dist+1, ind);
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> N >> u >> v;
    u--; v--;
    memset(visited, -1, sizeof(int)*N*2);
    F0R(i,N-1) {
        int a,b;
        cin >> a >> b;
        a--; b--;
        graph[a].PB(b);
        graph[b].PB(a);
    }
    dfs(u,0,0);
    dfs(v,0,1);
    int ans = 0;
    F0R(i,N) {
        // cout << i+1 << ' ' << visited[i][0] << ' ' << visited[i][1] << '\n';
        if (visited[i][0] < visited[i][1]) {
            ans = max(ans, visited[i][1]-1);
        }
    }
    cout << ans << '\n';
}

// even diff = -1. odd diff rts

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

vector<int>adj[100003],leaf;
int par[100003],depth[100005],sparse[100005][20],root=0,lz=0;

int lca(int p,int q)
{

    int a,b,c,d,lp,i,j;

    if(depth[p]<depth[q])
    {
        c=p; p=q; q=c;
    }

    for(j=19;j>=0;j--)
    {
         if((depth[p]-(1 << j))>=depth[q])
         {
             p=sparse[p][j];  if(depth[p]==depth[q])break;
         }
    }

    if(p==q){ return p; }
    else
    {
         for(j=19;j>=0;j--)
         {
             if(sparse[p][j]!=0 && sparse[p][j]!=sparse[q][j])
             {

                 p=sparse[p][j]; q=sparse[q][j];

                 if(par[p]==par[q])break;
             }

         }

         return par[p];
    }

}

void dfs(int src)
{
    int sz=adj[src].size();

    if(sz==1 && src!=root){   leaf.push_back(src);  lz++;   }

    for(int lp=0;lp<sz;lp++)
    {
        int u=adj[src][lp];

        if(u!=root && par[u]==0)
        {
            par[u]=src; sparse[u][0]=src; depth[u]=depth[src]+1;
            dfs(u);

        }

    }

}

int main()
{

    int n,m,a,b,i,j,u,v,ans,z,x,y,fr,sc,tr,w;
    scanf("%d %d %d",&n,&u,&v);

    for(i=1;i<=n-1;i++)
    {
        scanf("%d %d",&a,&b);
        adj[a].push_back(b); if(root==0){  if(adj[a].size()==2)root=a;  }
        adj[b].push_back(a); if(root==0){  if(adj[b].size()==2)root=b;  }

    }

    if(n==2)printf("0\n");
    else
    {
       dfs(root);

       for(j=1;j<=19;j++)
       {
           for(i=1;i<=n;i++)
           {
               if(sparse[i][j-1]!=0)
               {
                   sparse[i][j]=sparse[sparse[i][j-1]][j-1];
               }
           }
       }

       m=lca(u,v);

       ans=(depth[v]-depth[m])+(depth[u]-depth[m]); ans=ans-1;

       for(i=0;i<lz;i++)
       {
           y=leaf[i]; m=lca(u,y); w=lca(v,y);

           fr=(depth[u]-depth[m])+(depth[y]-depth[m]);
           sc=(depth[v]-depth[w])+(depth[y]-depth[w]);

           if(fr<sc)
           {
               if(sc-1>ans)ans=sc-1;
           }

       }

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

    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll=long long;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define inf 1001001001001001001
#define mod 1000000007
#define mod2 998244353
#define pi acos(-1)
#define all(v) v.begin(),v.end()

int main(){
    int n;
    cin>>n;
    int A,B,C,D;
    int sum=0;
    A=n/1000,n%=1000;
    B=n/100,n%=100;
    C=n/10,n%=10;
    D=n;
    string s="=7";
    vector<int>num={A,B,C,D};
    vector<char>op(3);
    /*考え方:
    全探索を考える（オーダー的にもOK)
     ・opの入れ方は+か-の2通り
     ・op1,op2,op3がある
     ->2^3通り ->ビット全探索orループ*/
    for(int bit=0;bit<(1<<3);bit++){
        bitset<3>s(bit);
        /*operantの決定*/
        for(int l=0;l<3;l++){
            if(s.test(l)){//bitがいならtrue
                op[l]='+';
            }else{
                op[l]='-';
            }
        }
        sum+=A;
        rep(l,3){
            if(op[l]=='+'){
                sum+=num.at(l+1);
            }else{
                sum-=num.at(l+1);
            }
        }
        if(sum==7){
            cout<<A<<op.at(0)<<B<<op.at(1)<<C<<op.at(2)<<D;
            cout<<"=7"<<endl;
            return 0;
        }else{
            sum=0;
        }
    }
}

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

typedef long long ll;

int main() {
    ll R[400009];
    string s;
    cin >> s;
    ll n = s.size();
    map<char, int>mp;
    for (int i = 0; i < n; i++) {
        mp[s[i]]++;
    }
    ll ans = n*(n-1)/2;
    for (auto& p : mp) {
        ll v = p.second;
        ans -= v*(v-1)/2;
    }
    cout << ans + 1 << endl;
}


#include <bits/stdc++.h>
using namespace std;
//#define cerr if (false) cerr
#define db(x) cerr << #x << "=" << x << endl
#define db2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define db3(x, y, z) cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z << endl
#define dbv(v) cerr << #v << "="; for (auto _x : v) cerr << _x << ", "; cerr << endl
#define dba(a, n) cerr << #a << "="; for (int _i = 0; _i < (n); ++_i) cerr << a[_i] << ", "; cerr << endl
template <typename A, typename B>
ostream& operator<<(ostream& os, const pair<A, B>& x) {
	return os << "(" << x.first << "," << x.second << ")";
}
typedef long long ll;
typedef long double ld;
int main() {
	ll k;
	scanf("%lld", &k);
	ll n = 50;
	printf("%lld\n", n);
	ll r = k % n;
	for (int i = 0; i < n; ++i) {
		printf("%lld ", i + k / n + (i >= n - r) );
	}
}

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

const ll MX=1E16+1000;

int main() {
	ios_base::sync_with_stdio(0);cin.tie(0);
	ll k;
	cin>>k;
	ll n=50;
	cout<<n<<'\n';
	vector<ll> res(n);
	for(int i=0;i<n;++i) res[i]=i+(k/n);
	for(int i=0;i<k%n;++i)
		for(int j=0;j<n;++j)
			if(j==i) res[j]+=n;
			else res[j]--;
	for(ll x:res) cout<<x<<' ';
	cout<<endl;
}


#include <iostream>
#include <cstring>
#include <map>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <set>
#include <vector>
#include <queue>
#include <list>
#include <numeric>
#include <stdio.h>
#include <string>
#include <cstdlib>
#include <math.h>
#include <stack>
#include <climits>
#include <bitset>
#include <utility>

using namespace std;
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; }

//最大公約数
ll gcd(ll x, ll y) {
    ll tmp = 0;
    if (x < y){
        tmp=x;
        x=y;
        y=tmp;
    }
    while (y > 0) {
        ll r = x % y;
        x = y;
        y = r;
        }
    return x;
}

//最大公倍数
ll lcm(ll x,ll y){
    return x/gcd(x,y)*y;
}

const int MAX = 1e6+1;
const ll MOD = 1e9+7;

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

// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

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

//階乗
ll kaijo(ll k){
    ll sum = 1;
    for (ll i = 1; i <= k; ++i)
    {
        sum *= i;
        sum%=1000000000+7;
    }
    return sum;
}

long long modpow(long long a, long long n, long long mod) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

//sieve(MAX);でベクトルIsPrimeに「MAXまでの数値の素数の真偽」を格納する。
vector<bool> IsPrime;
void sieve(size_t max){
    if(max+1 > IsPrime.size()){     // resizeで要素数が減らないように
        IsPrime.resize(max+1,true); // IsPrimeに必要な要素数を確保
    }
    IsPrime[0] = false; // 0は素数ではない
    IsPrime[1] = false; // 1は素数ではない

    for(size_t i=2; i*i<=max; ++i) // 0からsqrt(max)まで調べる
        if(IsPrime[i]) // iが素数ならば
            for(size_t j=2; i*j<=max; ++j) // (max以下の)iの倍数は
                IsPrime[i*j] = false;      // 素数ではない
}

struct UnionFind {
    vector<int> par;
    UnionFind(int n) : par(n, -1) { }
    int root(int x) {
        if (par[x] < 0) return x;
        else return par[x] = root(par[x]);
    }
    bool issame(int x, int y) {
        return root(x) == root(y);
    }
    bool merge(int x, int y) {
        x = root(x); y = root(y);
        if (x == y) return false;
        if (par[x] > par[y]) swap(x, y); // merge technique
        par[x] += par[y];
        par[y] = x;
        return true;
    }
    int size(int x) {
        return -par[root(x)];
    }
};

ll count(int n, int a){
    ll bunshi = 1;
    for(int i = 0; i < a; i++){
        bunshi*=(n-i);
        bunshi%=MOD;
    }
    ll bunbo = 1;
    for(int i = 1; i < a+1; i++){
        bunbo*=i;
        bunbo%=MOD;
    }
    bunbo = modpow(bunbo,MOD-2,MOD);
    // cout<<(bunshi*bunbo)%MOD<<endl;
    return (bunshi*bunbo)%MOD;
}

// 約数列挙。約数をvector<ll>で返す。計算量はsqrt(n)。
vector<ll> divisor(ll n) {
    vector<ll> 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(begin(ret), end(ret));
    return (ret);
}

// ここから開始
int main(){
    int x,y;
    cin>>x>>y;
    int a[3] = {300000,200000,100000};
    ll ans = 0;
    if(0<x&&x<4){
        ans+=a[x-1];
    }
    if(0<y&&y<4){
        ans+=a[y-1];
    }
    if(x==1&&y==1){
        ans+=400000;
    }
    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 < (int)(n); i++)
/*
*/

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

using Graph = vector<vector<int>>;

ll calc(ll a, ll b, ll p){//aのb乗をpで割った余り
  if(b == 0) return 1;

  else if(b%2 == 0){
    ll d = calc(a,b/2,p);
    return (d*d)%p;
  }
  else if(b%2 == 1){
    return (a * calc(a,b-1,p))%p;
  }
}

typedef pair<int,int> P;

bool is_prime(int n){//nが素数であるかどうか判定 iで割り切れると素数でない
    if(n == 1) return false;//1は例外
    for(int i = 2;i*i <= n;i++){
        if(n%i == 0) return false;
    }

    return true;
}
int factorial(int a){
  int res = 1;
  while(a){
    res *= a;
    a--;
  }
  return res;
}

//r本再利用されない時、N本から再利用して売れる本数
int rec(int m,int n,int N,int r){
  if(N == 0) return 0;

  int k = N/m * n;//新たに作り出す本数
  r = N%m;//再利用されない本数
  int res = N + k;//売った本数

  while(1){
    k = k/m * n;//k本からk/m本再利用
    res += k;
    r += k%m;
    if(k < m){//k本で再利用できなくなったら
      k += r;
      res += rec(m,n,k,r);
    }
  }
}

int main(){
  int X,Y;
  cin >> X >> Y;
  if(X == 1 && Y == 1) cout << 1000000 << endl;
  else if((X == 1 && Y == 2) || X == 2 && Y == 1) cout << 500000 << endl;
  else if((X == 1 && Y == 3) || X == 3 && Y == 1) cout << 400000 << endl;
  else if(X == 2 && Y == 2) cout << 400000 << endl;
  else if((X == 2 && Y == 3) || (X == 3 && Y == 2)) cout << 300000 << endl;
  else if(X == 3 && Y == 3) cout << 200000 << endl;
  else if(X == 1 || Y == 1) cout << 300000 << endl;
  else if(X == 2 || Y == 2) cout << 200000 << endl;
  else if(X == 3 || Y == 3) cout << 100000 << endl;
  else cout << 0 << endl;
}


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

// boost
#include <boost/multiprecision/cpp_int.hpp>
using boost::multiprecision::cpp_int;

using ll = long long;
using ld = long double;

using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vs = vector<string>;
using vld = vector<ld>;
using vvld = vector<vld>;

typedef pair<ll, ll> P;

#define bit(n) (1LL << (n))

//#define int long long

#define all(v) v.begin(), v.end()

#define rep(i, n) for (ll i = 0; i < n; i++)
#define REP(i, n) for (ll i = 1; i < n; i++)

#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORm(i, m) for (auto i = m.begin(); i != m.end(); i++)

template <class T>
inline void chmax(T& a, T b) {
  a = std::max(a, b);
}
template <class T>
inline void chmin(T& a, T b) {
  a = std::min(a, b);
}

#define mod (ll)(1e9 + 7)
// #define mod (998244353ll)

const long long INF = 1LL << 60;

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

  ll n;
  cin >> n;

  multiset<ll> a;
  rep(i, n) {
    ll tmp;
    cin >> tmp;
    a.insert(tmp);
  }

  ll ans = 0;

  while (a.size() >= 2) {
    ll mx = *a.rbegin();
    ll p2 = 2;
    while (p2 <= mx)
      p2 *= 2;
    if (p2 == mx - p2) {
      a.erase(a.lower_bound(mx));
      auto itr = a.lower_bound(mx);
      if (itr != a.end()) {
        a.erase(itr);
        ans++;
      }
    } else {
      a.erase(a.lower_bound(mx));
      auto itr = a.lower_bound(p2 - mx);
      if (*itr == p2 - mx) {
        ans++;
        a.erase(itr);
      }
    }
  }

  cout << ans << endl;

  return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define rep0(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define ll long long
#define coYes cout << "Yes" << endl;
#define coYES cout << "YES" << endl;
#define coyes cout << "yes" << endl;
#define coNo cout << "No" << endl;
#define coNO cout << "NO" << endl;
#define cono cout << "no" << endl;
#define BIT_FLAG_0 (1<<0) // 0000 0000 0000 0001
int gcd(int a, int b){
    if(a%b == 0){
        return b;
    }else{
    return gcd(b, a%b);
    }
}

int main(){
    int a,b,n=1,ans=0;
    cin >> a >> b;
    while(n<b){
        n+=a-1;
        ans++;
    }
    cout << ans << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define f1(a,b,c) for(int c=a;c<=b;c++)
#define f2(a,b,c) for(int c=a;c>=b;c--)
#define f3(a,b,c) for(int c=a;c;c=b)
#define so1(a,n) sort(a+1,a+n+1,mycmp);
#define so2(a,n) sort(a+1,a+n+1);
#define ll long long
#define itn int
#define ubt int
#define pii pair<int,int>
#define mp make_pair
const int twx=1e6+100;
const int inf=0x7fffffff;
ll read()
{
    ll sum=0;
    ll flag=1;
    char c=getchar();
    while(c<'0'||c>'9')
    {
        if(c=='-')
        {
            flag=-1;
        }
        c=getchar();
    }
    while(c>='0'&&c<='9')
    {
        sum=((sum*10)+c-'0');
        c=getchar();
    }
    return sum*flag;
}
int n,m;
struct LV
{
    int Next;
    int y;
    int v;
}a[twx<<1];
int Link[twx];
int len;
map<pii,int> p;
int cnt=0;
bool v[twx];
int d[twx];
void Insert(int x,int y,int z)
{
    a[++len].y=y;
    a[len].Next=Link[x];
    a[len].v=z;
    Link[x]=len;
}
int get(int x,int y)
{
    if(p.find(mp(x,y))!=p.end())
    {
        return p[mp(x,y)];
    }
    else
    {
        return p[mp(x,y)]=++cnt;
    }
}
void spfa()
{
    memset(d,0x3f,sizeof d);
    queue<int> q;
    q.push(1);
    v[1]=1;
    d[1]=0;
    while(!q.empty())
    {
        int x=q.front();
        q.pop();
        f3(Link[x],a[i].Next,i)
        {
            int y=a[i].y;
            int vs=a[i].v;
            if(d[y]>d[x]+vs)
            {
                d[y]=d[x]+vs;
                if(!v[y])
                {
                    v[y]=1;
                    q.push(y);
                }
            }
        }
        v[x]=0;
    }
}
void init()
{
	n=read();
    m=read();
    cnt=n;
    f1(1,m,i)
    {
        int x=read();
        int y=read();
        int z=read();
        int xz=get(x,z);
        int yz=get(y,z);
        Insert(xz,yz,0);
        Insert(yz,xz,0);
        Insert(x,xz,1);
        Insert(xz,x,1);
        Insert(y,yz,1);
        Insert(yz,y,1);
    }
    spfa();
    printf("%d\n",d[n]==0x3f3f3f3f?-1:d[n]/2);
}
void work()
{

}
void print()
{

}
int main()
{
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
    init();
	return 0;
}

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

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

#define mp make_pair
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()
#define eb emplace_back

vector<pii> g[100005];
vector<int> cur[100005];
int dp[100005];
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n, m;
    cin >> n >> m;
    for(int i=1;i<=m;i++){
        int a, b, c;
        cin >> a >> b >> c;
        g[a].eb(mp(b, c));
        g[b].eb(mp(a, c));
    }

    memset(dp, -1, sizeof(dp));
    priority_queue<pii> dq;
    // deque<pii> dq;
    dp[1] = 1;
    dq.push(mp(-dp[1], 1));
    for(int i=1;i<=1000000;i++) cur[1].eb(i);
    while(!dq.empty()){
        int u = dq.top().se;
        int val = -dq.top().fi;
        dq.pop();

        if(val != dp[u]) continue;

        sort(all(cur[u]));
        for(int i=0;i<sz(g[u]);i++){
            int v = g[u][i].fi, c = g[u][i].se;
            if(binary_search(all(cur[u]), c)){
                if(dp[v] == -1 || dp[v] > dp[u]){
                    cur[v].clear();
                    cur[v].eb(c);
                    dp[v] = dp[u];
                    dq.push(mp(-dp[v], v));
                }
                else if(dp[v] == dp[u]){
                    cur[v].eb(c);
                }
            }
            else{
                if(dp[v] == -1 || dp[v] > dp[u] + 1){
                    cur[v].clear();
                    cur[v].eb(c);
                    dp[v] = dp[u] + 1;
                    dq.push(mp(-dp[v], v));
                }
                else if(dp[v] == dp[u] + 1){
                    cur[v].eb(c);
                }
            }
        }
    }
    printf("%d\n", dp[n]);
}


#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#include<set>
#include<complex>
#define vi vector<int>
#define vvi vector<vector<int> >
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i,n) for(int i=0;i<n;i++)
#define CC puts("-------ok--------");
#define all(in) in.begin(), in.end()
#define bv vector<bool>
using namespace std;
typedef pair<int, int>PA;
using namespace std;
#define MAX  999999
int main(){
    while(true){
    int ans = 0;
    vvi v(100, vi (100 ,INF));
    vc c(100,0);vi d(100,0);
    vi x(100,0); vi y(100,0);
        int m ;cin >>m; if(!m) break;
        int tempx = 10,tempy = 10;rep(i,m){cin >> x[i] >> y[i];v[y[i]][x[i]] = 0;}
    int n; cin >> n; rep(i,n) {cin >> c[i] >> d[i];
        if(c[i] == 'N'){
            for(int k =1;k<=d[i];k++)if(v[tempy+k][tempx] == 0){ans++;v[tempy+k][tempx] =INF;}tempy+=d[i];
        }
        if(c[i] == 'E'){
            for(int k =1;k<=d[i];k++)if(v[tempy][tempx+k] == 0) {ans++;v[tempy][tempx+k] = INF;} tempx+=d[i];
        }
        if(c[i] == 'S'){
            for(int k =1;k<=d[i];k++)if(v[tempy-(k)][tempx] == 0){ans++;v[tempy-k][tempx] = INF;}tempy-=d[i];

        }
        if(c[i] == 'W'){
            for(int k =1;k<=d[i];k++)if(v[tempy][tempx-k] == 0){ans++;v[tempy][tempx-k] = INF;}tempx-=d[i];

        }
    }
        if(ans == m)
            cout << "Yes" << endl;
        else
            cout << "No" << endl;

    }
}

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

void ini(int* p,int n);
void print(int *p);
void check(int *p);
bool judge(int *p);

int main(){
  int n;
  while(1){
    bool ans;
    cin >> n;
    if(n == 0) break;
    int state[21];
    for(int i = 0; i<21 ; i++){
      state[i] = 0;
    }

    ini(state,n);
    //print(state);
    check(state);
    ans = judge(state);
    if(ans == 0) printf("Yes\n");
    else printf("No\n");
    //print(state);
  }
  return 0;
}

void ini(int* state,int n){
  int x, y;
  int mask;
  for(int i = 0; i<n; i++){
    cin >>x >> y;
    mask = (04000000 >> x);
    state[y] =( state[y] | mask);
  }
}

void check(int* state){
  int n;
  char dir;
  int step;
  int nowx = 10, nowy = 10;
  int mask;

  cin >> n;
  for(int i  = 0;  i< n; i++){
    //printf("x = %d y = %d",nowx, nowy);
    cin >>dir >> step;
    // printf("%c %d\n",dir,step);
    switch(dir){
    case 'N':
      mask = ~(04000000 >>nowx);
      for(int j = 0; j<step; j++){
	nowy ++;
	//printf("%d\n",nowy);
	state[nowy] = (state[nowy] & mask);
      }
      break;
    case 'S':
      mask = ~(04000000 >>nowx);
      for(int j = 0; j<step; j++){
	nowy --;
	state[nowy] = (state[nowy] & mask);
      }
      break;
    case 'E':
      for(int j = 0; j<step; j++){
	nowx ++;
	mask = ~(04000000 >>nowx);
	state[nowy] = (state[nowy] & mask);
      }
      break;
    case 'W':
      for(int j = 0; j<step; j++){
	nowx --;
	mask = ~(04000000 >>nowx);
	state[nowy] = (state[nowy] & mask);
      }
      break;
    }
  }
}

bool judge(int *state){
  for(int i = 0; i< 21; i++){
    if(state[i] != 0) return 1;
  }
  return 0;
}

void print(int *p){
  for(int i = 0; i<21; i++){
    printf("[ %d] %o\n",i,p[i]);
  }
}


#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
  char S[3];
  int ans=0;
  for(int i=0;i<3;i++){
  	cin >>S[i];
  }
  bool p = S[0] =='R';
  bool q = S[1] =='R';
  bool r = S[2] =='R';
  if(p && q && r){
    ans = 3;
  }else if((p&&q) || (q&&r)){
    ans = 2;
  }else if(p || q || r){
    ans =1;
  }else{
    ans=0;
  }

  cout << ans;
  return 0;

}

#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 = 1e9 + 1;

int main() {
  int n,m,X,Y,x[200],y[200];
  cin >> n >> m >> X >> Y;
  for(int i = 0; i < n; i++)
    cin >> x[i];
  for(int i = 0; i < m; i++)
    cin >> y[i];

  sort(x,x+n); sort(y,y+m);

  if(x[n-1] < y[0] & X < y[0] & x[n-1] < Y)
    cout << "No War" << endl;
  else
    cout << "War" << endl;
}


#include <bits/stdc++.h>

int modnum = 1000000007;

int main(){
    int N;
    std::cin >> N;
    std::vector< int > cnt(N+1, 0);
    cnt[0] = 3;
    long long ans = 1;
    for(int i=0; i<N; i++){
        int a;
        std::cin >> a;
        int tmp = cnt[a] - cnt[a + 1];
        ans *= (long long)tmp;
        ans = ans % modnum;
        cnt[a + 1]++;
    }

    std::cout << ans << std::endl;

    return 0;
}

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <math.h>
#include <utility>
#include <algorithm>
#include <functional>
#include <vector>
#include <numeric>
#include <bits/stdc++.h>
// #include <atcoder/all>

#define int long long

struct edge {
    int to;
    int cost;
};

using namespace std;
// using namespace atcoder;
using vi = vector<int>;
using vii = vector<vector<int>>;
using vb = vector<bool>;
using qi = queue<int>;
using P = pair<int, int>;
using graph = vector<vector<edge>>;
// using graph = vector<vector<int>>;
// using mint = modint1000000007;

#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define REP(i,n) for(int i=0;i<n;i++)
#define delim(i, n) cout << (i == n-1 ? "\n" : " ")
#define print1(v) cout << fixed << setprecision(10) << v << "\n"
#define print2(v1, v2) cout << fixed << setprecision(10) << v1 << " " << v2 << "\n"
#define print3(v1, v2, v3) cout << fixed << setprecision(10) << v1 << " " << v2 << " " << v3 << "\n"
#define printvi(v, n) REP(iiii, n) { cout << v[iiii]; delim(iiii, n); }
#define printvii(v, h, w) REP(iiiii, h) { printvi(v[iiiii], w); }

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

const int inf = 1LL << 60;
// const int inf = 100000000;
const int mod = 1000000007;

const int maxn = 1e5+5;
int n, m;
vi color(3);

vi dx = {-1, 0, 0, 1};
vi dy = {0, -1, 1, 0};

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

    cin >> n;

    int ret = 1;
    REP(i, n) {
        int a; cin >> a;
        int cnt = 0;
        REP(j, 3) {
            if (color[j] == a) {
                if (cnt == 0) color[j]++;
                cnt++;
            }
        }
        ret *= cnt;
        ret %= mod;
    }

    print1(ret);
}


#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>

using namespace std;

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

  cout << n/3 << endl;
}

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

// Returns floor of square root of x
int floorSqrt(int x)
{
    // Base cases
    if (x == 0 || x == 1)
    return x;

    // Staring from 1, try all numbers until
    // i*i is greater than or equal to x.
    int i = 1, result = 1;
    while (result <= x)
    {
      i++;
      result = i * i;
    }
    return (i - 1)*(i-1);
}

// Driver program
int main()
{
    int x ;
    cin>>x;
    cout << floorSqrt(x) << endl;
    return 0;
}


#include <iostream>
#include <math.h>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;

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

  ll ans=0;
  for(ll i=1; i*i<=n; i++){
    ans=i*i;
  }

  cout << ans;
}

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

int main(){
  int n; cin >> n;
  vector<bool> p(60000,true);
  for(int i = 2; i < 55560; i++){
    if(!p[i]) continue;
    for(int j = 2; j*i < 55560; j++){
      p[i*j] = false;
    }
  }
  vector<int> fp;
  for(int i = 2; i < 55560; i++){
    if(p[i] && i % 5 == 1) fp.push_back(i);
  }
  for(int i = 0; i < n; i++){
    cout << fp[i] << " ";
  }
}


#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

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

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#define mod 1000000007 //1e9+7
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 30

int mm[SIZE][SIZE];
int w,h;

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

int dfs(int y,int x,int depth = 0){
  int ret = INF;

  if(depth >= 10) return INF;

  //
  for(int i=0;i<4;i++){
    int tx = x + dx[i];
    int ty = y + dy[i];

    if(ty<0 || h<=ty || tx<0 || w<=tx || mm[ty][tx]==1) continue;

    if(mm[ty][tx]==3) return depth+1;

    while(1){
      tx += dx[i];
      ty += dy[i];

      if(ty<0 || h<=ty || tx<0 || w<=tx) break;

      if(mm[ty][tx]==3) return depth+1;

      if(mm[ty][tx]==1){
	mm[ty][tx] = 0;
	ret = min(ret,dfs(ty-dy[i],tx-dx[i],depth+1));
	mm[ty][tx] = 1;
	break;
      }
    }

  }

  return ret;
}

int calc(){
  int sy,sx;

  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      scanf("%d",mm[i]+j);
      if(mm[i][j]==2){
	sy = i;
	sx = j;
      }
    }
  }

  int ans = dfs(sy,sx);

  if(ans==INF) ans = -1;

  return ans;
}

int main(){

  while(1){
    scanf("%d%d",&w,&h);
    if(w==0) break;
    printf("%d\n",calc());
  }

  return 0;
}


#include "bits/stdc++.h"
using namespace std;
#define int long long
#define FOR(i, a, b) for(int i=(a);i<(b);i++)
#define RFOR(i, a, b) for(int i=(b-1);i>=(a);i--)
#define REP(i, n) for(int i=0; i<(n); i++)
#define RREP(i, n) for(int i=(n-1); i>=0; i--)
#define REP1(i, n) for(int i=1; i<=(n); i++)
#define RREP1(i, n) for(int i=(n); i>=1; i--)
#define ALL(a) (a).begin(),(a).end()
#define UNIQUE_SORT(l) sort(ALL(l)); l.erase(unique(ALL(l)), l.end());
#define CONTAIN(a, b) find(ALL(a), (b)) != (a).end()
#define out(...) printf(__VA_ARGS__)

int dxy[] = {0, 1, 0, -1, 0};

void solve();
signed main()
{
#if DEBUG
    std::ifstream in("input.txt");
    std::cin.rdbuf(in.rdbuf());
#endif
    cin.tie(0);
    ios::sync_with_stdio(false);
    solve();
    return 0;
}

/*================================*/
#if DEBUG
#define SIZE 100
#else
#define SIZE 123450
#endif

int N,M,K;

const int MAX = 5e5;
const int MOD = 1e9+7;

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

void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

long long COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    if (!fac[n]) COMinit();
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

void solve() {
    cin>>N>>M>>K;

    int ans = 0;
    REP1(d, N-1) {
        int add = d*(N-d) % MOD;
        (add *= M*M)%=MOD;
        (ans += add)%=MOD;
    }
    REP1(d, M-1) {
        int add = d*(M-d) % MOD;
        (add *= N*N)%=MOD;
        (ans += add)%=MOD;
    }
    (ans *= COM(N*M-2,K-2))%=MOD;
    cout << ans << endl;
}

#include <bits/stdc++.h>

using namespace std;

#ifdef zxc

#include <sys/resource.h>
#include "debug.h"

#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

using ll = int64_t;
using ld = long double;
const ld EPS = 1e-9;
//const ll MOD = 924844033;
const ld PI = 3.141592653589793;
const int maxn = 300001;
const ll MOD = 1e9 + 7;

void fast_io() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
}

ll pw(ll a, ll b) {
    ll res = 1;
    for (; b; b >>= 1) {
        if (b & 1) {
            res = res * a % MOD;
        }
        a = a * a % MOD;
    }
    return res;
}

template<typename Cont>
vector<vector<int>> accum2d(Cont a, int n, int m) {
    vector<vector<int>> res(n, vector<int>(m));

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            res[i][j] = a[i][j];
            if (i > 0) {
                res[i][j] += res[i - 1][j];
            }
            if (j > 0) {
                res[i][j] += res[i][j - 1];
            }
            if (i > 0 && j > 0) {
                res[i][j] -= res[i - 1][j - 1];
            }
        }
    }
    return res;
}

vector<vector<int>> resize2d(vector<vector<int>> a, int n, int m) {
    a.resize(n);
    for (auto &x:a) {
        x.resize(m);
    }
    return a;
}

int up[1030][1030];
int down[1030][1030];
int lft[1030][1030];
int rght[1030][1030];
int ans[1030][1030];
int pref[1030][1030];
int arifm[1030][1030];
int dp1[1030][1030];
int dp2[1030][1030];

signed main() {
#ifdef zxc
    struct rlimit rl;
    const rlim_t kStackSize = 512L * 1024L * 1024L;
    assert(!getrlimit(RLIMIT_STACK, &rl));
    rl.rlim_cur = kStackSize;
    assert(!setrlimit(RLIMIT_STACK, &rl));
    freopen("../kolya_input.txt", "r", stdin);
//    freopen("../kolya_output.txt", "w", stdout);
#else
    //    mt19937 mt(std::chrono::high_resolution_clock::now().time_since_epoch().count());
    //            freopen("", "r", stdin);
    //        freopen("hack.out", "w", stdout);
#endif
    auto solve = [](int _) {
        int n;
        cin >> n;
        string s;
        cin >> s;
        vector<int> v;
        for (char c:s) {
            v.push_back(c == 'B' ? 1 : 0);
        }
        bool xx = true;
        for (int i = 2 * n - 1; i >= 0; --i) {
            if (xx) {
                v[i] ^= 1;
            }
            xx ^= 1;
        }
        ll cock = 1;
        ll cnt_zeros = 0;
        for (int i = 2 * n - 1; i >= 0; --i) {
            if (v[i]) {
                cock *= cnt_zeros;
                cock %= MOD;
                --cnt_zeros;
            } else {
                ++cnt_zeros;
            }
        }
        if (cnt_zeros) {
            cock = 0;
        }
        for (int i = 1; i <= n; ++i) {
            cock *= i;
            cock %= MOD;
        }
        cout << cock;
    };

    fast_io();
    cout.precision(9);
    cout << fixed;
    auto start = chrono::steady_clock::now();
    for (int i = 1; i <= 1; ++i)solve(i);
    auto end = chrono::steady_clock::now();

    debug("time", chrono::duration_cast<chrono::milliseconds>(end - start).count() / 1000.0);

}


#include<iostream>
#include<string>
#include<algorithm>

using namespace std;

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

int main(void){
    string ro;
    int m[256]={};
    m['I']=1,
        m['V']=5,
        m['X']=10,
        m['L']=50,
        m['C']=100,
        m['D']=500,
        m['M']=1000;
    while(cin>>ro){
        int n=0;
        for(auto c:ro)n+=m[c];
        rep(i,ro.size()) if(m[ro[i]]<m[ro[i+1]])n-=2*m[ro[i]];
        cout<<n<<endl;
    }
}

//ヘッダー
#include<bits/stdc++.h>
using namespace std;

//型定義
typedef long long ll;

//定数
const int INF=1e+9;
const int MOD=1e+9+7;

//REPマクロ
#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define REP2(i,a,b) for(ll i=a;i<(ll)(b);i++)
#define REPD2(i,a,b) for(ll i=a;i>(ll)(b);i--)

// 多次元 vector 生成
template<class T>
vector<T> make_vec(size_t a){
    return vector<T>(a);
}
template<class T, class... Ts>
auto make_vec(size_t a, Ts... ts){
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}

//vectorの扱い
#define ALL(x) (x).begin(),(x).end() //sortなどの引数省略
#define SIZE(x) ((ll)(x).size()) //size
#define MAX(x) *max_element(ALL(x)) //最大値
#define MIN(x) *min_element(ALL(x)) //最小値

int main(){
    string A;
    cin>>A;
    int N=A.size();
    vector<ll> cnt(26,0);
    vector<ll> dp(N+1,0);
    dp[0]=1;
    REP(i,N){
        dp[i+1]=dp[i];
        dp[i+1]+=(i-cnt[A[i]-'a']);
        cnt[A[i]-'a']++;
    }
    cout<<dp[N]<<endl;

}


#include<iostream>
#include<cstring>
#include<algorithm>
#define Dist(x,y) ((x)*(x)+(y)*(y))
using namespace std;

typedef struct{
  int height;
  int weight;
} Rectangle;

bool operator<(Rectangle r1,Rectangle r2){
  int di1=Dist(r1.height,r1.weight);
  int di2=Dist(r2.height,r2.weight);
  if(di1 < di2) return true;
  else if(di1 > di2) return false;
  else return (r1.height < r2.height);
}

int main(){
  int h,w;
  while(cin>>h>>w,w+h){
    Rectangle r;
    r.height=h;
    r.weight=w;
    Rectangle min;
    min.height=150;
    min.weight=150;
    for(int i=1;i<150;i++){
      for(int j=1;j<i;j++){
	Rectangle tmp;
	tmp.height=j;
	tmp.weight=i;
	if(r < tmp && tmp < min) min = tmp;
      }
    }
    cout<<min.height<<" "<<min.weight<<endl;
  }
  return 0;
}

#include<iostream>
#include<iomanip>
#include<string>
#include<algorithm>
#include<vector>
#include<utility>
#include<tuple>
#include<map>
#include<queue>
#include<stack>
#include<deque>
#include<bitset>
#include<math.h>
using namespace std;
using ll = int64_t;
using Graph = vector<vector<int> >;
const ll M = 1000000007;

int main(){
    ll l,r;
    cin >> l >> r;
    if(l+1==r){
        cout << ((l%2019)*(r%2019))%2019 << endl;
        return 0;
    }

    if(l==0){
        cout << 0 << endl;
        return 0;
    }

    if(r-l+1>=673){
        cout << 0 << endl;
    }else{
        int m=2018;
        for(int i=l;i<r;i++){
            for(int j=i+1;j<=r;j++){
                m = min(m,((i%2019)*(j%2019))%2019);
            }
        }

        cout << m << endl;
    }
}


#include <stdio.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include <vector>
#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

int main(){
    string x;
    cin >> x;
    int n = x.length();

    if(n == 2){
        if(x[0] == x[1]){
            printf("1 2");
        }
        else {
            printf("-1 -1");
        }
        return 0;
    }

    for(int i = 0; i < n-2; i++){
        if(x[i] == x[i + 1] || x[i + 1] == x[i + 2] || x[i + 2] == x[i]){
            printf("%d %d", i+1, i+3);
            return 0;
        }
    }
    printf("-1 -1");

    return 0;
}

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define OP ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define F first
#define S second
#define sz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for(int i = 0; i < n; ++i)
#define repr(i,n) for(int i = n - 1; i >= 0; --i)
#define Rep(i, a, n) for(int i = (a); i <=(n); ++i)
#define repst(i, n) for(auto it = n.begin(); it != n.end(); ++it)
#define Repr(i, a, n) for(int i = (n); i >= (a); --i)
typedef long long ll;
typedef std::vector<int> vi;
typedef std::pair<int, int> pii;
const int inf = int(1e9);
const int mod = inf + 7;
const double PI = acos(-1.0);
using namespace std;
const int N = 1e5 + 155;
template<typename T>
inline void read(T&x){
    x=0;int f=1;char ch = getchar();
    while(ch<'0' ||ch>'9'){ if(ch=='-')f=-1;ch=getchar(); }
    while(ch>='0' && ch<='9'){ x=x*10+ch-'0'; ch=getchar(); }
    x*=f;
}

ll bp(ll a, ll n){
	ll r = 1;
	while(n){
		if(n & 1) r = (r * a) % mod;
		a = (a * a) % mod;
		n >>= 1;
	}
	return r;
}

void solve(){
	string s;
	cin >> s;
	unordered_map<char, vector<int>> m;
	rep(i, sz(s)) {
		m[s[i]].pb(i + 1);
	}
	for(auto it: m) {
		vector<int> v = it.second;
		for(int i = 0; i < v.size() - 1; i++) {
			if (v[i + 1] - v[i] <= 2) {
				cout << v[i] << ' ' << v[i + 1];
				return;
			}
		}
	}
	cout << "-1 -1";
}

int main(){
    OP
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    int t = 1;
	//cin >> t;
    for(int i = 1; i <= t; i++) {
        solve();
    }
    return 0;
}


#pragma GCC optimze("Ofast")
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define mod 1000000007
#define md 998244353
#define trace(...)
#define ll long long int
#define ld long double
#define fi first
#define se second
#define fr(i,a,b) for(ll i=a;i<=b;i++)
#define frr(i,b,a) for(ll i=b;i>=a;i--)
#define mem(a) memset(a, 0, sizeof(a))
#define vpi std::vector<pair<int,int>>
#define pii pair<int,int>
#define pb push_back
#define sall(v) sort(v.begin(),v.end())
#define all(v) (v.begin(),v.end())
#define minf -10000000
#define vi vector<int>
const ll infl=0x3f3f3f3f3f3f3f3fLL;
const int infi=0x3f3f3f3f;
typedef tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> oset;
auto clk=clock();
mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
int rng(int lim) {
    uniform_int_distribution<int> uid(0,lim-1);
    return uid(rang);
}
ll power(ll x, ll y,ll p) {
    ll res = 1;
    x = x % p;
    while (y > 0) {
        if (y & 1){
            res = (res*x) % p;
        }
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}
ll power(ll x, ll y) {
    ll res = 1;
    while (y > 0) {
        if (y & 1)
            res = (res*x) ;
        y = y>>1;
        x = (x*x);
    }
    return res;
}

ll max3(ll a,ll b,ll c){
    return max(a,max(b,c));
}
ll min3(ll a,ll b,ll c){
    return min(a,min(b,c));
}
bool isPrime(ll n) { // Corner cases
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;
    if (n % 2 == 0 || n % 3 == 0)
        return false;
    for (ll i = 5; i * i <= n; i = i + 6)
        if (n % i == 0 || n % (i + 2) == 0)
            return false;
    return true;
}
const int nax=2e5+5;
void solve(){
    int n;cin>>n;
    vector<ll> v(n);fr(i,0,n-1)cin>>v[i];
    ll cnt=0,mx=v[0];
    fr(i,1,n-1){cnt+=max(0ll,mx-v[i]);mx=max(mx,v[i]);}
    cout<<cnt<<endl;
    return;
}

int main(){
    ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());
    cout<<fixed<<setprecision(8);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    #endif
    int t;
    // cin>>t;
    t=1;
    while(t--){
        solve();
    }
    #ifndef ONLINE_JUDGE
    cout<<endl<<endl<<endl<<endl<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl;
    #endif
    return 0;
}

#include <iostream>
#define ll long long
using namespace std;
int main () {
	int q,p = 0,n; cin >> q; ll ans = 0;
	while (q--){
		cin >> n;
		if (n<p) ans += p-n;
		else p=n;
	} cout << ans << "\n";
	return 0;
}