#include <iostream>
using namespace std;

int main(){
    int A,B,C;
    cin >> A >> B >> C;

   if (A != B && A != C && B != C) {
     cout << "No" << endl;
   }
  else if (A == B && B == C) {
     cout << "No" << endl;
  }
  else if (A == B && A != C) {
    cout << "Yes" << endl;
  }
  else if (A == C && A != B) {
    cout << "Yes" << endl;
  }
  else if (C == B && A != C) {
    cout << "Yes" << endl;
  }
}

// GRL_1_A
#include <iostream>
#include <vector>
#include <queue>
#include <math.h>
#define   N   1000000000
using namespace std;

struct edge{
	int adj;
	int dis;
};

int main(){
	int V, E, r, s, t, d, x;
	vector<vector<edge> > A;
	vector<int> distance;
	queue<int> ver;
	edge e;

	cin>>V>>E>>r;

	A.resize(V);
	for(int i=0;i<V;i++){
		distance.push_back(N);
	}
	distance[r] = 0;

	for(int j=0;j<E;j++){
		cin>>s>>t>>d;
		e.adj = t;
		e.dis = d;
		A[s].push_back(e);
	}

	ver.push(r);
	while(!ver.empty()){
		x = ver.front();
		for(int i=0;i<A[x].size();i++){
			if(distance[A[x][i].adj] > distance[x] + A[x][i].dis){
				distance[A[x][i].adj] = distance[x] + A[x][i].dis;
				ver.push(A[x][i].adj);
			}
		}
		ver.pop();
	}

	for(int i=0;i<V;i++){
		if(distance[i] >= N)
			cout<<"INF"<<endl;
		else
			cout<<distance[i]<<endl;
	}

	return 0;
}


// C++ 14
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define loop(__x, __start, __end) for(ll __x = __start; __x < __end; __x++)

typedef pair<int, int> Edge;

int n;
vector<Edge> G[100000];
vector<ll> D(100000, 0);

int main() {
  cin >> n;
  loop(i,0,n-1) {
    int a, b, c; cin >> a >> b >> c;
    a--;b--;
    G[a].emplace_back(b, c);
    G[b].emplace_back(a, c);
  }
  int q, k; cin >> q >> k; k--;
  queue<pair<int, int> > Q;
  Q.push({ k, -1 });
  D[k] = 0;
  while (Q.size()) {
    auto _v = Q.front(); Q.pop();
    int v = _v.first, p = _v.second;
    for (auto &&edge: G[v]) {
      int u = edge.first, w = edge.second;
      if (u == p) continue;
      D[u] = D[v] + w;
      Q.push({ u, v });
    }
  }

  while (q--) {
    int x, y; cin >> x >> y; x--;y--;
    cout << D[x] + D[y] << endl;
  }

  return 0;
}

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<functional>
#include<iostream>
#include<queue>
#define rep(i,a,b) for(i=a;i<b;i++)
#define F1 first.first
#define F2 first.second
#define F3 second
#define INF 2100000000
#define long long int int
using namespace std;
typedef pair<pair<int,int>,int> P1;
typedef pair<int,int> P2;
int main(void)
{
	priority_queue<P2,vector<P2>,greater<P2> > que;
	int i,j,k,n,m,x,s,e;
	int a,b,cos,tim,st,en,way;
	int leng[1001],kami[1002];
	P1 glid1[6000],glid2[6000];
	P2 tori;
	while(1) {
		cin>>n>>m;
		if(n==0&&m==0) break;
		rep(i,0,n) {
			cin>>a>>b>>cos>>tim;
			glid1[i*2].F1=a,glid1[i*2].F2=b,glid1[i*2].F3=cos;
			glid2[i*2].F1=a,glid2[i*2].F2=b,glid2[i*2].F3=tim;
			glid1[i*2+1].F1=b,glid1[i*2+1].F2=a,glid1[i*2+1].F3=cos;
			glid2[i*2+1].F1=b,glid2[i*2+1].F2=a,glid2[i*2+1].F3=tim;
		}
		sort(glid1,glid1+n*2);sort(glid2,glid2+n*2);
		kami[1]=0;
		rep(i,1,n*2) {
			if(glid1[i].F1!=glid1[i-1].F1) {
				x=glid1[i].F1;
				kami[x]=i;
			}
		}
		kami[m+1]=n*2;

		cin>>k;
		rep(i,0,k) {
			cin>>st>>en>>way;
			for(j=1;j<=m;j++) leng[j]=INF;
			while(!que.empty()) que.pop();
			que.push(P2(0,st));
			while(!que.empty()) {
				tori=que.top(),que.pop();
				a=tori.first,b=tori.second;
				if(a<leng[b]) {
					leng[b]=a;
					if(b==en) break;
					s=kami[b],e=kami[b+1];
					if(way==0)rep(j,s,e) que.push(P2(a+glid1[j].F3,glid1[j].F2));
					else rep(j,s,e) que.push(P2(a+glid2[j].F3,glid2[j].F2));
				}
			}
			cout<<leng[en]<<endl;
		}
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
typedef pair<int, int> ii;

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

fastio _fast_io;

const int H = 105;
const int W = 10;
const int MOD = 1e9 + 7;
int h, w, k;
ll dp[H][W];
ll C[W];

void init() {
    C[1] = 2;
    int a = 1, b = 1;
    for (int i = 2; i < w; ++i) {
        int na = b;
        int nb = a + b;
        C[i] = na + nb;
        a = na;
        b = nb;
    }
}

ll cnt(int x) {
    if (x <= 0) return 1;
    return C[x];
}

int main() {
    cin >> h >> w >> k;
    init();
    for (int i = 0; i < w; ++i) {
        if (i == k - 1)
            dp[h][i] = 1;
        else
            dp[h][i] = 0;
    }
    // cout << dp[15][4] << endl;
    // dp[0][0] => dp[h - 1][k - 1]
    for (int i = h - 1; i >= 0; --i) {
        for (int j = 0; j < w; ++j) {
            dp[i][j] = dp[i + 1][j] * cnt(j - 1) % MOD * cnt(w - j - 2) % MOD; // 0 j 0
            if (j > 0)
                dp[i][j] = (dp[i][j] + dp[i + 1][j - 1] * cnt(j - 2) % MOD * cnt(w - j - 2) % MOD) % MOD; // 1 j 0
            if (j < w - 1)
                dp[i][j] = (dp[i][j] + dp[i + 1][j + 1] * cnt(j - 1) % MOD * cnt(w - j - 3) % MOD) % MOD; // 0 j 1

            // cout << i << ' ' << j << ' ' << dp[i][j] << endl;
        }
    }
    cout << dp[0][0] << endl;
    return 0;
}


#include<iostream>
#include<algorithm>
#include<utility>

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

using namespace std;

char field[12][12];
int cnt;
int dx[4] = { 1, 0, -1, 0 };
int dy[4] = { 0, 1, 0, -1 };
char depth;

void dfs(int x,int y){
	/*
	rep(i, 12){
		rep(j, 12){

			if (field[i][j] == '1'){
				cout << field[i][j];
			}
			else{
				cout << field[i][j];
			}

			if (j == 11){
				cout << endl;
			}
		}
	}
	cout << endl;
	*/

	field[x][y] = '0';
	rep(i, 4){
		int ddx = x + dx[i], ddy = y + dy[i];
		if (0<=ddx && ddx<12 && 0<=ddy && ddy<12 && field[ddx][ddy] == '1'){
			dfs(ddx, ddy);
		}

	}

}

int main(){
	while (1){
		cnt = 0;
		rep(i, 12){
			rep(j, 12){
				char c;
				cin >> c;
				if(cin.eof()){
					return 0;
				}
				field[i][j] = c;
				/*if (j == 11){
					char EnTeR;
					cin >> EnTeR;
				}*/
			}
		}

		/*
		rep(i, 12){
			rep(j, 12){
				if (field[i][j] == '1'){
					cout << "テ「ツ鳴?;
				}
				else{
					cout << "テ「ツ鳴。";
				}

				if (j == 11){
					cout << endl;
				}
			}
		}
		*/

		rep(i, 12){
			rep(j, 12){
				if (field[i][j] == '1'){
					cnt++;
					dfs(i, j);
				}
			}
		}

		cout << cnt << endl;

	}

}

#include <bits/stdc++.h>
using namespace std;
int main(){
	int n,t;
	while(cin>>n,n){
		int c[10];fill(c,c+10,0);
		for(int i=0;i<n;i++)cin>>t,c[t]++;
		for(int i=0;i<10;i++){if(!c[i])cout<<'-';else for(int j=0;j<c[i];j++)cout<<'*';cout<<endl;}
	}
}


#include <bits/stdc++.h>

const double PI = 3.14159265358979323846;
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
#define rep(i, n) for (int i = 0; i < (n); ++i)
//#define rep(i, n) for (ll i = 0; i < (n); ++i)
typedef pair<ll, ll> P;
const ll INF = 10e17;
#define cmin(x, y) x = min(x, y)
#define cmax(x, y) x = max(x, y)
#define ret() return 0;

std::istream &operator>>(std::istream &in, set<ll> &o) {
    ll a;
    in >> a;
    o.insert(a);
    return in;
}

std::istream &operator>>(std::istream &in, queue<int> &o) {
    ll a;
    in >> a;
    o.push(a);
    return in;
}

bool contain(set<int> &s, int a) { return s.find(a) != s.end(); }

//ifstream myfile("C:\\Users\\riku\\Downloads\\0_00.txt");
//ofstream outfile("log.txt");
//outfile << setw(6) << setfill('0') << prefecture << setw(6) << setfill('0') << rank << endl;
// std::cout << std::bitset<8>(9);

//const ll mod = 1e10;
//typedef priority_queue<P, vector<P>, greater<P> > PQ_ASK;

vector<ll> bfs(int n, vector<vector<int>> &g) {

    vector<ll> costs(n, INF);
    costs[0] = 0;
    queue<int> q;
    q.push(0);
    while (!q.empty()) {
        int from = q.front();
        q.pop();
        for (int to  : g[from]) {
            if (costs[to] != INF) continue;
            costs[to] = costs[from] + 1;
            q.push(to);
        }
    }
    return costs;
}

bool bfs2(int n, vector<vector<int>> &g) {
    vector<ll> costs(n, INF);
    costs[0] = 0;
    queue<int> q;
    q.push(0);
    while (!q.empty()) {

        int from = q.front();
        q.pop();
        for (int to  : g[from]) {

            ll current_cost = costs[to];
            ll next_cost = costs[from] + 1;

            if (current_cost != INF) {
                if (current_cost % 2 != next_cost % 2) return false;
                continue;
            }
            costs[to] = next_cost;
            q.push(to);
        }
    }
    return true;
}

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

    vector<P> v(m);
    for (P &p : v) cin >> p.first >> p.second, p.first--, p.second--;

    vector<vector<int>> g(n);
    for (P p: v) g[p.first].push_back(p.second);
    for (P p: v) g[p.second].push_back(p.first);

    vector<ll> costs = bfs(n, g);
    bool is_2 = bfs2(n, g);

    if (is_2) {
        ll a = 0;
        for (ll c : costs) if (c % 2 == 0) a++;
        ll b = n - a;
        ll all = a * b;
        ll ans = all - m;
        cout << ans << endl;
        ret();
    }

    ll all = ll(n) * (n - 1) / 2;
    ll ans = all - m;
    cout << ans << endl;
}

%:pragma GCC optimize "-O3"
%:pragma GCC optimize("Ofast")
%:pragma GCC optimize("inline")
%:pragma GCC optimize("fast-math")
%:pragma GCC optimize("unroll-loops")
%:pragma GCC optimize("inline-functions")
%:pragma GCC optimize("no-stack-protector")
%:pragma GCC optimize("inline-small-functions")
%:pragma GCC optimize("inline-functions-called-once")
%:pragma GCC target("sse3","sse2","sse")
#include<bits/stdc++.h>
#define ll long long
#define heap priority_queue
using namespace std;
const ll maxn=15+1;
const ll inf=9e17;
ll ans,sum,dp[maxn][1<<maxn],dis[1<<maxn],g[maxn][maxn],tmp,lim,n,m;
inline ll lowbit(ll x){
	return x&-x;
}
int main()
{
	cin>>n>>m;
	ll u,v,w;
	for(int i=1;i<=m;i++)
	{
		cin>>u>>v>>w;
		--u;
		--v;
		g[u][v]=g[v][u]=w;
		sum+=w;
	}
	lim=(1<<n)-1;
	for(int i=1;i<=lim;i++)
	{
		tmp=log2(lowbit(i));
		dis[i]=dis[i^(1<<tmp)];
		for(int j=0;j<n;j++)
			if(g[tmp][j]&&(i&(1<<j)))
				dis[i]+=g[tmp][j];
	}
	memset(dp,0xc0,sizeof(dp));
	dp[0][1]=0;
	ans=-inf;
	for(int i=1;i<=lim;i++)
	{
		if(!(i&1))
			continue;
		for(int j=0;j<n;j++)
			if(i&(1<<j))
			{
				for(int k=i;k;k=(k-1)&i)
					if(k&(1<<j))
						dp[j][i]=max(dp[j][i],dp[j][i^k^(1<<j)]+dis[k]);
				for(int k=lim^i;k;k-=lowbit(k))
				{
					tmp=log2(lowbit(k));
					if(g[tmp][j])
						dp[tmp][i^(1<<tmp)]=max(dp[tmp][i^(1<<tmp)],dp[j][i]+g[tmp][j]);
				}
			}
		if(i&(1<<(n-1)))
			ans=max(ans,dp[n-1][i]);
	}
	cout<<sum-ans<<"\n";
	return 0;
}


#include <bits/stdc++.h>

#include <boost/multiprecision/cpp_int.hpp>

// increment
#define rep_n(_1, _2, _3, NAME, ...) NAME
#define rep_2(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define rep_3(i, initial, n) \
  for (int(i) = (int)(initial); (i) < (int)(n); ++(i))
#define rep(...) rep_n(__VA_ARGS__, rep_3, rep_2, rep_1)(__VA_ARGS__)
// decrement
#define rep_r_n(_1, _2, _3, NAME, ...) NAME
#define rep_r_2(i, n) for (int(i) = (int)(n); (i) > 0; (i)--)
#define rep_r_3(i, initial, n) \
  for (int(i) = (int)(initial); (i) > (int)(n); (i)--)
#define rep_r(...) rep_r_n(__VA_ARGS__, rep_r_3, rep_r_2)(__VA_ARGS__)
// define type definition
using namespace std;
typedef long long ll;
using mpint = boost::multiprecision::cpp_int;  // 多倍長整数（任意精度）
typedef pair<int, int> P;

// define construct variable
const double EPS = numeric_limits<double>::epsilon();
// 10^9 + 7
const ll MOD = ll(1e9) + 7;
const ll INF = ll(1e10);
const string ALPHABET = "abcdefghijklmnopqrstuvwxyz";
const double PI = acos(-1);

// define methods
// 最大公約数
ll gcd(const ll a, const ll b) { return b ? gcd(b, a % b) : a; }
// 最小公倍数
ll lcm(const ll a, const ll b) { return a / gcd(a, b) * b; }
// char -> int
int ctoi(char c) { return (int)(c - '0'); }
// 素因数分解
vector<pair<ll, ll>> prime_factorize(ll n) {
  vector<pair<ll, ll>> p;
  for (ll i = 2; i * i <= n; i++) {
    if ((n % i) != 0) continue;
    pair<ll, ll> a = make_pair(i, 0);
    while ((n % i) == 0) {
      n /= i;
      a.second += 1;
    }
    p.push_back(a);
  }

  if (n != 1) p.push_back(make_pair(n, 1));
  return p;
}
// 組み合わせの整合性を保つ（後ほど解析する）
ll modinv(ll a) {
  ll b = MOD, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= MOD;
  if (u < 0) {
    u += MOD;
  }
  return u;
}
// 順列
ll permutation(const ll n, const ll m) {
  ll result = 1;
  for (ll i = n; i > n - m; i--) {
    result = (result * i) % MOD;
  }
  return result;
}

// 繰り返し n 乗法
ll repeat_square(const ll n, const ll x) {
  if (x == 0) return 1;
  ll res = repeat_square(n, x / 2);
  res = res * res % MOD;
  if ((x % 2) == 1) res = res * n % MOD;
  return res;
}
// 組み合わせ nCm
ll combination(ll n, ll m) {
  ll p = permutation(n, m);
  ll div = 1;
  for (ll i = 2; i <= m; i++) {
    div = (div * i) % MOD;
  }
  return (p * modinv(div)) % MOD;
}

// アルファベットの取得
string get_alphabet(ll n) {
  n--;
  return string(1, 'a' + (n % 26));
}

int main() {
  int N, Q;
  string S;
  cin >> N >> Q >> S;
  vector<int> L(Q+1), R(Q+1);
  rep(i, Q) {
    cin >> L[i] >> R[i];
  }

  vector<int> p(N, 0);
  int count = 0;
  string prev = S.substr(0, 1);
  rep(i, 1, N) {
    string s = S.substr(i, 1);
    if ((prev + s) == "AC") count++;
    p[i] = count;
    prev = s;
  }

  rep(i, Q) {
    int l = L[i], r = R[i];
    int ans = p[r-1] - p[l-1];
    cout << ans << endl;
  }
  return 0;
}

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

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

    string S;
    vector<int> ac_cnt(N, 0);
    int cnt = 0;
    cin>>S;
    for(int i=0; i<N-1; i++){
        if(S[i]=='A' && S[i+1]=='C'){
            S[i]='a';
            cnt++;
        }
        ac_cnt[i+1] = cnt;
    }

    vector<int> ans;
    for(int i=0; i<Q; i++){
        int l, r;
        cin>>l>>r;

        if(l == 1){
            ans.push_back(ac_cnt[r-1]);
        }else{
            ans.push_back(ac_cnt[r-1] - ac_cnt[l-1]);
        }

    }

    // for(int a : ac_cnt){
    //     cout<<a<<endl;
    // }
    // cout<<"-------"<<endl;
    for(int a : ans){
        cout<<a<<endl;
    }
    return 0;
}


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

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

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

int main(){
    int l,r;
    cin>>l>>r;
    long long ans=3000;
    if(r-l>=2200)cout<<0<<endl;
    else{
        for(long long i=l;i<=r-1;i++){
            for(long long j=i+1;j<=r;j++){
                long long tmp=(i*j)%2019;
                ans=min(ans,(i*j)%2019);
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
struct hito{
	long long sum;
	long long H;
	long long P;
};
bool cmp(const hito &a, const hito &b){
    return a.sum < b.sum;
}
int main(){
	int N; cin >> N;
	long long INF = 1e18;
	hito A[5010] = {};
	for(int i = 1; i <= N; i++){
		cin >> A[i].H >> A[i].P;
		A[i].sum = A[i].H + A[i].P;
	}
	sort(A+1, A+N+1, cmp);
	long long dp[5010][5010] = {}; // dp[i人目まで見て][j人のときの最小の高さ]
	for(int i = 0; i <= N; i++){
		for(int j = 1; j <= N+1; j++){
			dp[i][j] = INF;
		}
	}
	for(int i = 1; i <= N; i++){
		for(int j = 1; j <= N; j++){
			if(A[i].H >= dp[i-1][j-1]) dp[i][j] = min(dp[i-1][j], dp[i-1][j-1] + A[i].P);
			else dp[i][j] = dp[i-1][j];
		}
	}
	for(int i = 1; i <= N+1; i++){
		if(dp[N][i] == INF){
			cout << i-1 << endl; return 0;
		}
	}
}

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

struct node
{
    ll x;
    ll y;
};
node a[5007];
int n;

bool cmp(node p,node q)
{
    if (p.x+p.y==q.x+q.y) return p.y<q.y;
    return p.x+p.y<q.x+q.y;
}
int main(){
    ios::sync_with_stdio(false);
    scanf("%d",&n);
    for (int i=1;i<=n;i++)
    scanf("%lld%lld",&a[i].x,&a[i].y);

    priority_queue<int>q;
  //  n=n+1;
  //  a[n].x=1e9+10;
  //  a[n].y=1e9+10;
    sort(a+1,a+n+1,cmp);

    ll tmp=0;

    ll t=0;
    int ans=0;
    for (int i=1;i<=n;i++)
    {
       // cout<<ans<<" "<<a[i].x<<a[]
        if (tmp<=a[i].x)
        {
            tmp+=a[i].y;
            q.push(a[i].y);
            ans++;
        }
        else
        {
            if (a[i].y<q.top())
            {
                tmp-=q.top();
                q.pop();
                tmp+=a[i].y;
                q.push(a[i].y);
            }
        }

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


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

#define debug(x) cout << #x << " = " << x << endl;
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define FOR(it, b, e) for (typeof(b) it = (b); it != (e); ++it)
#define MSET(c, v) memset(c, v, sizeof(c))

const int INF = 0x3F3F3F3F;
const int NEGINF = 0xC0C0C0C0;
const int NULO = -1;
const double EPS = 1e-10;
const ll mod = 1e9 + 7;

inline int cmp(double x, double y = 0)
{
  if(fabs(x - y) < EPS)
    return 0;
  return x > y ? 1 : -1;
}

ll exp(ll a, ll b)
{
  ll ans = 1;
  while(b)
  {
    if(b & 1)
      ans = (ans * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return ans;
}

const int maxn = 85;
ll fat[maxn], inv[maxn];

ll C(int n, int k)
{
  if(n < k)
    return 0;
  if(n < 0 || k < 0)
    return 0;
  return (((fat[n] * inv[k]) % mod) * inv[n - k]) % mod;
}

void initFactorials()
{
  fat[0] = 1;
  inv[0] = 1;
  for(int i = 1; i < maxn; i++)
  {
    fat[i] = (i * fat[i-1]) % mod;
    inv[i] = exp(fat[i], mod - 2);
  }
}

int dp[maxn][maxn][2 * maxn * maxn];

ll a[maxn][maxn];

ll b[maxn][maxn];

int n,m;

int main()
{
  ios::sync_with_stdio(false);
  cin >> n >> m;
  for(int i = 0 ; i < n; i++)
    for(int j = 0; j < m; j++)
      cin >> a[i][j];

  for(int i = 0 ; i < n; i++)
    for(int j = 0; j < m; j++)
      cin >> b[i][j];

  dp[0][0][abs(a[0][0] - b[0][0])] = true;
  for(int i = 0; i < n; i++)
  {
    for(int j = 0 ; j < m; j++)
    {
      for(int k = 0; k < (n + m) * maxn; k++)
      {
        if(i > 0)
        {
          dp[i][j][k] = dp[i - 1][j][k + abs(a[i][j] - b[i][j])] |
                        dp[i - 1][j][abs(k - abs(a[i][j] - b[i][j]))];
        }
        if(j > 0)
        {
          dp[i][j][k] = dp[i][j][k] |
                        dp[i][j - 1][k + abs(a[i][j] - b[i][j])] |
                        dp[i][j - 1][abs(k - abs(a[i][j] - b[i][j]))];
        }
      }
    }
  }

  for(int i = 0; i < (n + m) * maxn; i++)
  {
    if(dp[n - 1][m - 1][i] == 1)
    {
      cout << i << endl;
      return 0;
    }
  }
  return 0;
}

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

#define rep(i,n) for(i = 0;i < n;++i)
#define all(v) v.begin(), v.end()
using ll = int;

int main()
{
    ll i,j,k;
    ll h,w;
    cin >> h >> w;
    ll inf = 160*80;
    vector<vector<ll>> a(h,vector<ll>(w,0));
    vector<vector<ll>> b(h,vector<ll>(w,0));
    for(i = 0;i < h;++i){
        rep(j,w) cin >> a.at(i).at(j);
    }
    for(i = 0;i < h;++i){
        rep(j,w) cin >> b.at(i).at(j);
    }
    vector<vector<vector<ll>>> dp(h, vector<vector<ll>>(w, vector<ll>(2*inf+1,0)));
    ll tmp = a.at(0).at(0) - b.at(0).at(0);
    dp.at(0).at(0).at(inf + tmp) = 1;
    dp.at(0).at(0).at(inf - tmp) = 1;
    for(i = 0;i < h;++i){
        rep(j,w){
            ll ni = i + 1;
            ll nj = j + 1;
            ll t1, t2;
            if(ni < h) t1 = a.at(ni).at(j) - b.at(ni).at(j);
            if(nj < w) t2 = a.at(i).at(nj) - b.at(i).at(nj);
            rep(k, 2*inf+1){
                if(dp.at(i).at(j).at(k)){
                    if(ni < h){
                        dp.at(ni).at(j).at(k + t1) = 1;
                        dp.at(ni).at(j).at(k - t1) = 1;
                    }
                    if(nj < w){
                        dp.at(i).at(nj).at(k + t2) = 1;
                        dp.at(i).at(nj).at(k - t2) = 1;
                    }
                }
            }
        }
    }
    ll ans = 1e9;
    for(i = 0;i < 2*inf + 1;++i){
        if(dp.at(h-1).at(w-1).at(i)){
            ans = min(ans, abs(i - inf));
        }
    }
    cout << ans << endl;

    return 0;
}


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

using i64 = int64_t;

const i64 MOD = 1e9+7;

const i64 INF = 1e18+7;

struct UnionFind{
	int n;
	vector<int> par;
	UnionFind(int n) : n(n), par(n, -1){}
	int Find(int x){return par[x] < 0 ? x : Find(par[x]);}
	bool Unite(int x, int y){
		x = Find(x), y = Find(y);
		if(x == y)
			return false;
		if(par[x] > par[y])
			swap(x, y);
		par[x] += par[y];
		par[y] = x;
		return true;
	}
};

template<typename T>
struct Segtree{
	const int n;
	const T op;
	vector<T> elm;
	function<T(T, T)> f;

	Segtree(int n, T init, function<T(T, T)> f, T op = T()) :
			n(n),
			op(op),
			f(f),
			elm(2 * n, init)
	{
	}

	void set(int x, T val){
		x += n;
		elm[x] = val;
		while(x >>= 1)
			elm[x] = f(elm[2 * x], elm[2 * x + 1]);
	}

	void update(int x, T val){
		x += n;
		elm[x] = f(val, elm[x]);
		while(x >>= 1)
			elm[x] = f(elm[2 * x], elm[2 * x + 1]);
	}

	T get(int x, int y) const{
		T val = op;
		for(x += n, y += n - 1; x <= y; x >>= 1, y >>= 1){
			if(x & 1)
				val = f(val, elm[x++]);
			if(!(y & 1))
				val = f(val, elm[y--]);
		}
		return val;
	}
};

signed main(){

	int n, d;
	cin >> n >> d;
	vector<int> v(n);
	for(auto& x : v)
		cin >> x;

	auto f = [](pair<i64,int> x, pair<i64,int> y){
		return min(x, y);
	};

	Segtree<pair<i64,int>> seg_a(n, pair<i64,int>(), f, pair<i64,int>(INF, 0));
	Segtree<pair<i64,int>> seg_b(n, pair<i64,int>(), f, pair<i64,int>(INF, 0));

	auto s = [&](int i){
		seg_a.set(i, make_pair(v[i] + i64(1) * d * i, i));
		seg_b.set(i, make_pair(v[i] + i64(1) * d * (n - i - 1), i));
	};

	auto t = [&](int i){
		seg_a.set(i, make_pair(INF, i));
		seg_b.set(i, make_pair(INF, i));
	};

	for(int i = 0; i < n; ++i)
		s(i);

	UnionFind u(n);

	i64 ans = 0;

	while(u.par[u.Find(0)] != -n){
		// 子ノードの一覧
		vector<vector<int>> ch(n);
		// 接合するノード
		vector<pair<i64, int>> nex(n, make_pair(INF, 0));

		for(int i = 0; i < n; ++i)
			ch[u.Find(i)].push_back(i);

		for(int i = 0; i < n; ++i){
			if(ch[i].empty())
				continue;
			for(auto& x : ch[i])
				t(x);

			pair<i64,int> val(INF, 0);

			for(auto& x : ch[i]){
				auto p_a = seg_a.get(x + 1, n);
				p_a.first -= i64(1) * d * x;
				p_a.first += v[x];
				val = f(val, p_a);

				auto p_b = seg_b.get(0, x);
				p_b.first -= i64(1) * d * (n - 1 - x);
				p_b.first += v[x];
				val = f(val, p_b);
			}

			for(auto& x : ch[i])
				s(x);

			nex[i] = val;
		}

		for(int i = 0; i < n; ++i){
			if(nex[i].first != INF && u.Unite(i, nex[i].second))
				ans += nex[i].first;
		}
	}
	cout << ans << endl;
}

#include<bits/stdc++.h>
#define ll long long int
#define endl '\n'
#define M 1000000007
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define f first
#define s second
#define b begin
#define e end
#define pb push_back
#define mp make_pair
#define FOR(i,a,b) for(i=a;i<b;i++)
#define RFOR(i,a,b) for(i=a;i>=b;i--)
#define all(x) x.begin(),x.end()
#define itfr(it,x) for(it=x.begin();it!=x.end();it++)
#define flash ios_base::sync_with_stdio(false); cin.tie(NULL)
using namespace std;

int main()
{
    flash;
    //sieve();
    int T=1,t,n,m,k,i;
    // cin>>T;
    while(T--)
    {
        cin>>n>>k;
        int a[10]={0};
        FOR(i,0,k){
          cin>>t;
          a[t]=1;
        }
        vector<int> vs;
        FOR(i,0,10)
            if(a[i]==0)
                vs.pb(i);
        // for(auto it:vs)cout<<it<<" ";cout<<endl;
        int dg=0;
        for(i=n;i>0;i/=10)dg++;
        ll ans=0;
        k = pow(10,dg-1);
        int fl=0;
        int num=n;
        // cout<<dg<<" "<<k<<endl;
        while(ans<num)
        {
            if(k==0){
                ans = ans*10 + vs[0];
                continue;
            }
            m = n/k;
            n = n%k;
            k/=10;
            if(a[m]==0&fl==0){
                ans = ans*10 + m;
                continue;
            }
            dg=0;
            if(fl==0)
            {
                for(auto it:vs)
                    if(it>=m)
                    {
                        dg=it;
                        break;
                    }
                if(dg==0)
                {
                    if(vs[0]!=0)
                        dg=vs[0];
                    else
                    {
                        if(ans==0)
                            dg=vs[1];
                        else
                            dg=vs[0];
                    }
                }
                ans = ans*10+dg;
                fl=1;
            }
            else
            {
                ans = ans*10 + vs[0];
            }
        }
        cout<<ans<<endl;
    }
}


#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <fstream>
#include <sstream>
#include <iomanip>
#define ll long long

using namespace std;

long long MOD = 1000000007;
template<typename T>
void cout_vec(vector<T> &vec){
    for(int i=0; i<vec.size(); i++){
        if (i!=0){
            cout << ' ';
        }
        cout << vec[i];
    }
    cout << endl;
}
template<typename T>
void cout_mat(vector<vector<T>  > &mat){
    for(int i=0; i<mat.size(); i++){
        vector<T> vec = mat[i];
        cout_vec(mat[i]);
    }
}

int main(){
    int H, W;
    cin >> H >> W;

    vector<vector<int>> grid(H,vector<int>(W,0));
    for(auto &line:grid){
        for(auto &a:line){
            cin >> a;
        }
    }

    for(auto &line:grid){
        for(auto &a:line){
            int x;
            cin >> x;
            a-=x;
        }
    }
    int N=160*160;

    vector<vector<vector<int>>> dp(H+1,vector<vector<int>>(W+1,vector<int>(N+1,0)));
    dp[0][0][N/2]=1;

    for(int i=0; i<H; i++){
        for(int j=0; j<W; j++){
            int diff=grid[i][j];
            for(int k=0; k<=N; k++){
                if(0<=k+diff && k+diff<=N){
                    dp[i+1][j][k+diff]|=dp[i][j][k];
                    dp[i][j+1][k+diff]|=dp[i][j][k];
                }
                if(0<=k-diff && k-diff<=N){
                    dp[i][j+1][k-diff]|=dp[i][j][k];
                    dp[i+1][j][k-diff]|=dp[i][j][k];
                }
            }
        }
    }

    for(int i=0; i<N; i++){
        if(dp[H][W-1][N/2-i] || dp[H][W-1][N/2+i]){
            cout << i << endl;
            return 0;
        }
    }

}

#include <bits/stdc++.h>
#define fi first
#define se second
#define ii pair<int, int>
#define int long long
using namespace std;

const int INF = 1e9;
int n, m;
int ans = INF;
int a[85][85], b[85][85];
bool f[85][85][52000];

void attempt(int i, int j, int diff)
{

    if (i == n && j == m)
    {
        ans = min(ans, abs(diff + a[i][j] - b[i][j]));
        ans = min(ans, abs(diff - a[i][j] + b[i][j]));
        return;
    }
    if (f[i][j][diff])
        return;
    f[i][j][diff] = 1;

    if (i < n){
        attempt(i+1, j, abs(diff + a[i][j] - b[i][j]));
        attempt(i+1, j, abs(diff - a[i][j] + b[i][j]));
    }
    if (j < m){
        attempt(i, j+1, abs(diff + a[i][j] - b[i][j]));
        attempt(i, j+1, abs(diff - a[i][j] + b[i][j]));
    }

}

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

//    freopen("in.inp", "r", stdin);
//    freopen("out.out", "w", stdout);

    cin >> n >> m;

    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            cin >> a[i][j];

    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            cin >> b[i][j];

    attempt(1, 1, 0);
    cout << ans;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,m,i,x,y;
    cin>>n>>m>>x>>y;
    int ara[n],bra[m];
    for(i=0;i<n;i++)
        cin>>ara[i];
    for(i=0;i<m;i++)
        cin>>bra[i];
    sort(ara,ara+n);
    sort(bra,bra+m);
    for(i=ara[n-1]+1;i<=bra[0];i++)
    {
        if(i>x && i<=y)
        {
            cout<<"No War"<<endl;
            return 0;
        }
    }
    cout<<"War"<<endl;
    return 0;
}

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

bool hantei( vector<int>& x, vector<int>& y ) {
  rep( Z, -100, 100+1 ) {
    bool flag = true;
    rep( i, 0, x.size() ) {
      if ( x.at(i) >= Z ) flag = false;
    }

    rep( i, 0, y.size() ) {
      if ( y.at(i) < Z ) flag = false;
    }
    if (flag) return false;
  }

  return true;
}

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

  vector<int> x(N+1), y(M+1);
  cin >> x.at(0) >> y.at(0);
  rep(i, 1, N+1) cin >> x.at(i);
  rep(i, 1, M+1) cin >> y.at(i);

  cout << ( hantei(x, y) ? "War" : "No War" ) << endl;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <iomanip>
#include <utility>
#include <tuple>
#include <functional>
#include <bitset>
#include <cassert>
#include <complex>
#include <stdio.h>
#include <time.h>
#include <numeric>
#include <random>
#include <unordered_map>
#include <unordered_set>
#define all(a) a.begin(),a.end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pb push_back
#define debug(x) cerr << #x << ':' << (x) << '\n'
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;
typedef complex<ld> com;
constexpr int inf = 1000000010;
constexpr ll INF = 1000000000000000010;
constexpr ld eps = 1e-12;
constexpr ld pi = 3.141592653589793238;
template<class T, class U> inline bool chmax(T &a, const U &b) { if (a < b) { a = b; return true; } return false; }
template<class T, class U> inline bool chmin(T &a, const U &b) { if (a > b) { a = b; return true; } return false; }

constexpr ll mod = 1000000007;
const int sz = 1 << 20;

ll modpow(ll a, ll b, ll m = mod) {
	ll ans = 1;
	a %= m;
	while (b) {
		if (b & 1) ans = ans * a % m;
		a = a * a % m;
		b >>= 1;
	}
	return ans;
}

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

	int n;
	cin >> n;
	int now = 0;
	vector<int> a(n), x(n), zero(n);
	vector<vector<int>> pl(sz, vector<int>());
	rep(i, n) {
		cin >> a[i];
		now ^= a[i];
		x[i] = now;
		int pre = 0;
		if (i) pre = zero[i - 1];
		if (now == 0) pre++;
		zero[i] = pre;
		pl[now].pb(i);
	}
	if (now == 0) {
		ll ans = modpow(2, pl[0].size() - 1);
		for (int num = 1; num < sz; num++) {
			vector<int> vec;
			int s = pl[num].size();
			if (s == 0) continue;
			rep(i, s - 1) {
				vec.pb(1);
				vec.pb(zero[pl[num][i + 1]] - zero[pl[num][i]]);
			}
			vec.pb(1);
			vector<vector<ll>> dp(2 * s, vector<ll>(2));
			dp[0][0] = 1;
			rep(i, 2 * s - 1) {
				dp[i + 1][0] += dp[i][0];
				if (i % 2 == 1) {
					dp[i + 1][0] += dp[i][1] * vec[i];
				}
				dp[i + 1][1] += dp[i][1];
				if (i % 2 == 0) {
					dp[i + 1][1] += dp[i][0] * vec[i];
				}
				dp[i + 1][0] %= mod;
				dp[i + 1][1] %= mod;
			}
			ans += dp[2 * s - 1][1];
		}
		ans %= mod;
		cout << ans << '\n';
	}
	else {
		int num = now;
		vector<int> vec;
		int s = pl[num].size();
		rep(i, s - 1) {
			vec.pb(1);
			vec.pb(zero[pl[num][i + 1]] - zero[pl[num][i]]);
		}
		vector<vector<ll>> dp(2 * s - 1, vector<ll>(2));
		dp[0][0] = 1;
		rep(i, 2 * s - 2) {
			dp[i + 1][0] += dp[i][0];
			if (i % 2 == 1) {
				dp[i + 1][0] += dp[i][1] * vec[i];
			}
			dp[i + 1][1] += dp[i][1];
			if (i % 2 == 0) {
				dp[i + 1][1] += dp[i][0] * vec[i];
			}
			dp[i + 1][0] %= mod;
			dp[i + 1][1] %= mod;
		}
		cout << dp[2 * s - 2][0] << '\n';
	}
}

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

#define task "test"
#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define openfile  if (fopen(task ".inp","r")){freopen(task ".inp","r",stdin);freopen(task ".out","w",stdout);}
#define ll long long
#define pii pair <int, int>
#define pll pair <ll, ll>
#define all(c) c.begin(), c.end()
#define pb push_back
#define mp make_pair
#define X first
#define Y second

const int INF = 1E9;
const int MOD = 1E9 + 7;
const int N = 1000 + 10;

int t = 1;

ll n, a, b, c, d;
ll Fact[N], Inv[N], F[N][N];

ll POW(ll n, ll k)
{
    if (k == 1) return n;
    ll tmp = POW(n, k / 2);
    tmp = tmp * tmp % MOD;

    if (k & 1) return tmp * n % MOD;
    return tmp;
}

void Input()
{
    cin >> n >> a >> b >> c >> d;
}

void Prepare()
{
    Fact[0] = Inv[0] = Fact[1] = Inv[1] = 1;
    for (int i = 2; i <= n; i++) Fact[i] = Fact[i - 1] * (ll) (i) % MOD;
    for (int i = 2; i <= n; i++) Inv[i] = POW(Fact[i], MOD - 2);
}

ll Comb(ll n, ll k)
{
    if (n < k) return 0;
    return Fact[n] * Inv[k] % MOD * Inv[n - k] % MOD;
}

void Solve()
{
    F[a - 1][0] = 1;

    for (int i = a; i <= b; i++)
        for (int j = 0; j <= n; j++) {
            F[i][j] = F[i - 1][j];
            //cout << "Pre : " << F[i][j] <<" - > ";
            for (int k = c; k <= d && i * k <= j; k++) {
                F[i][j] += F[i - 1][j - i * k] * Comb(j, j - i * k) % MOD * Fact[i * k] % MOD * POW(Inv[i], k) % MOD * Inv[k] % MOD;
                F[i][j] %= MOD;
              //  if (i == 1 && j == 3) cout << F[i][j] << " " << k << endl;
            }
          //  cout << i << " " << j << " " << F[i][j] << endl;
        }
    cout << F[b][n];
}

int main()
{
    openfile;
    // cin >> t;
    while (t--) {
        Input();
        Prepare();
        Solve();
    }
}


#include <bits/stdc++.h>
#define sz(arr) (int)arr.size()
#define all(arr) arr.begin(), arr.end()

using namespace std;

void _cin() {}
template <class Head, class... Tail>
void _cin(Head&& head, Tail&&... tail) {
  cin >> head;
  _cin(forward<Tail>(tail)...);
}

#define Cin(T, ...) \
  T __VA_ARGS__;    \
  _cin(__VA_ARGS__)

int main() {
  Cin(int, n);
  cout << 180 * (n - 2) << endl;

  return 0;
}

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <math.h>
#include <map>

using namespace std;

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


//Be Name Khoda, Besmellah Rahmane Rahim, In The Name Of God;
//#include<bits/stdc++.h>
#include<iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <queue>
#include <deque>
#include <map>
#include <stack>
#include<bitset>
#include<list>
#include<cassert>
#include<numeric>
#include <stdio.h>
#include <string.h>
#include<iomanip>
#include<unordered_map>
#include<unordered_set>
#include <fstream>

using namespace std;

using namespace std;

const int N = 1e6 + 5;
const int mod = 998244353;

long long fact[N];
int n, m;

void pre()
{
    fact[0] = 1;
    for (int i = 1; i < N; i++)
    {
        fact[i] = fact[i - 1] * i;
        fact[i] = fact[i] % mod;
    }
}

int power(int a, int b)
{
    if (!b)
    {
        return 1;
    }
    long long ans = power(a, b / 2);
    ans = ans * ans;
    ans = ans % mod;
    if (b % 2)
    {
        ans = ans * a;
        ans = ans % mod;
    }
    return ans;
}

long long choose(int a, int b)
{
    if (b > a)
    {
        return 0;
    }
    long long ans = fact[a];
    ans = ans * power(fact[b], mod - 2);
    ans = ans % mod;
    ans = ans * power(fact[a - b], mod - 2);
    ans = ans % mod;
    return ans;
}

int main()
{
    pre();
    //cout << choose(5, 2) << endl;
    cin >> n >> m;
    long long ans = 0;
    for (int i = 1; i <= n && i <= m; i++)
    {
        long long tmp = choose(i * 2, i) * choose(n + m - 2 * i, n - i);
        tmp = tmp % mod;
        ans += tmp;
        ans = ans % mod;
    }
    long long tmp = choose(n + m, n) * 2;
    tmp = power(tmp, mod - 2);
    ans = ans * tmp;
    ans = ans % mod;
    ans += max(n, m);
    ans = ans % mod;
    cout << ans << endl;
}

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define MAXN 1000005
#define eps 1e-12
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
    res = 0;T f = 1;char c = getchar();
    while(c < '0' || c > '9') {
        if(c == '-') f = -1;
        c = getchar();
    }
    while(c >= '0' && c <= '9') {
        res = res * 10 + c - '0';
        c = getchar();
    }
    res *= f;
}
template<class T>
void out(T x) {
    if(x < 0) {x = -x;putchar('-');}
    if(x >= 10) {
        out(x / 10);
    }
    putchar('0' + x % 10);
}
const int MOD = 998244353;
int N,M;
int fac[MAXN],invfac[MAXN],f[MAXN],v[MAXN],h[MAXN],inv[MAXN];
int inc(int a,int b) {
    return a + b >= MOD ? a + b - MOD : a + b;
}
int mul(int a,int b) {
    return 1LL * a * b % MOD;
}
int C(int n,int m) {
    if(n < m) return 0;
    return mul(fac[n],mul(invfac[m],invfac[n - m]));
}
int way(int x1,int y1,int x2,int y2) {
    return C(x2 - x1 + y2 - y1,y2 - y1);
}
void update(int &x,int y) {
    x = inc(x,y);
}
int fpow(int x,int c) {
    int res = 1,t = x;
    while(c) {
        if(c & 1) res = mul(res,t);
        t = mul(t,t);
        c >>= 1;
    }
    return res;
}
void Solve() {
    read(N);read(M);
    fac[0] = 1;
    inv[1] = 1;
    for(int i = 1 ; i <= N + M ; ++i) {
        fac[i] = mul(fac[i - 1],i);
        if(i > 1) inv[i] = mul(inv[MOD % i],MOD - MOD / i);
    }
    invfac[N + M] = fpow(fac[N + M],MOD - 2);
    for(int i = N + M - 1 ; i >= 0 ; --i) {
        invfac[i] = mul(invfac[i + 1],i + 1);
    }
    if(M > N) h[0] = 1;
    if(N > M) v[0] = 1;
    for(int i = 1 ; i <= N + M ; ++i) {
        h[i] = h[i - 1];
        int r = min(i - 1,N) - h[i - 1] + 1,c = i - 1 - r + 1;
        if(i <= N && M - (N - r) >= c + 1) ++h[i];
        if(i > N && M - (N - r) <= c) --h[i];
        v[i] = v[i - 1];
        c = min(i - 1,M) - v[i - 1] + 1,r = i - 1 - c + 1;
        if(i <= M && N - (M - c) >= r + 1) ++v[i];
        if(i > M && N - (M - c) <= r) --v[i];
    }
    f[0] = mul(C(N + M, N),max(N,M));
    int valh = 0,valv = 0;
    if(h[0]) valh = C(N + M - 1, M - 1);
    if(v[0]) valv = C(N + M - 1, N - 1);
    for(int i = 1 ; i < N + M ; ++i) {
        f[i] = f[i - 1];
        if(h[i - 1]) f[i] = inc(f[i],MOD - valh);
        if(v[i - 1]) f[i] = inc(f[i],MOD - valv);
        if(!h[i - 1] && h[i]) valh = way(i,0,N,M - 1);
        if(!v[i - 1] && v[i]) valv = way(0,i,N - 1,M);
        int r1 = min(i - 1,N) - h[i - 1] + 1,c1 = i - 1 - r1;
        int r2 = min(i,N) - h[i] + 1,c2 = i - r2;
        if(h[i - 1]) {
            if(r2 > r1) {
                valh = inc(valh, MOD - mul(way(0,0,r1,c1),way(r1,c1 + 1,N,M - 1)));
            }
            else {
                if(r2 >= 1) valh = inc(valh,mul(way(0,0,r2 - 1,c2),way(r2,c2,N,M - 1)));
            }
        }
        c1 = min(i - 1,M) - v[i - 1] + 1,r1 = i - 1 - c1;
        c2 = min(i,M) - v[i] + 1,r2 = i - c2;
        if(v[i - 1]) {
            if(c2 > c1) {
                valv = inc(valv, MOD - mul(way(0,0,r1,c1),way(r1 + 1,c1,N - 1,M)));
            }
            else {
                if(c2 >= 1) valv = inc(valv,mul(way(0,0,r2,c2 - 1),way(r2,c2,N - 1,M)));
            }
        }
    }
    int ans = 0;
    for(int i = 0 ; i < N + M ; ++i) {
        ans = inc(ans,mul(f[i],inv[N + M - i]));
    }
    ans = mul(ans,fpow(C(N + M,N),MOD - 2));
    out(ans);enter;
}
int main() {
#ifdef ivorysi
	freopen("f1.in","r",stdin);
#endif
    Solve();
}


#include <stdio.h>

using namespace std;

int final_board[8][8] = {{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0}};

void deletePossibility(int tmp_board[8][8],int row,int column){
	tmp_board[row][column] = 1;
	for(int i = 0; i < 8; i++){
		if(i != column){
			tmp_board[row][i] = -1;
		}
	}
	for(int i = 0; i < 8; i++){
		if(i != row){
			tmp_board[i][column] = -1;
		}
	}

	for(int r = row-1,c = column+1;r >= 0 && c <= 7; r--,c++){
		tmp_board[r][c] = -1;
	}
	for(int r = row+1,c = column+1;r <= 7 && c <= 7; r++,c++){
		tmp_board[r][c] = -1;
	}
	for(int r = row-1,c = column-1;r >= 0 && c >= 0; r--,c--){
		tmp_board[r][c] = -1;
	}
	for(int r = row+1,c = column-1;r <= 7 && c >= 0; r++,c--){
		tmp_board[r][c] = -1;
	}
}

void func(int tmp_board[8][8],int num_of_putQ){
	if(num_of_putQ == 8){
		for(int i = 0; i < 8; i++){
			for(int k = 0; k < 8; k++){
				final_board[i][k] = tmp_board[i][k];
			}
		}
	}else{

		int row,column,count = 0;
		for(row = 0; row < 8; row++){
			for(column = 0; column < 8; column++){
				if(tmp_board[row][column] == 0){
					count++;
				}
			}
			if(count > 0) break;
		}
		if(row == 8 && column == 8){
			//Do nothing
		}else{
			int tri_board[count][8][8];
			int index = 0;

			for(int a = 0; a < count; a++){
				for(int b = 0; b < 8; b++){
					for(int c = 0; c < 8; c++){
						tri_board[a][b][c] = tmp_board[b][c];
					}
				}
			}

			for(int i = 0; i < 8; i++){
				if(tmp_board[row][i] == 0){
					deletePossibility(tri_board[index],row,i);
					func(tri_board[index++],num_of_putQ+1);
				}
			}
		}

	}
}

int main(){

	int k,row,colum;
	int tmp_board[8][8];

	for(int i = 0; i < 8; i++){
		for(int k = 0; k < 8; k++){
			tmp_board[i][k] = 0;
		}
	}

	scanf("%d",&k);

	for(int i = 0; i < k; i++){
		scanf("%d %d",&row,&colum);
		deletePossibility(tmp_board,row,colum);
	}

	func(tmp_board,k);

	for(int i = 0; i < 8; i++){
		for(int p = 0; p < 8; p++){
			if(final_board[i][p] == 1){
				printf("Q");
			}else{
				printf(".");
			}
		}
		printf("\n");
	}

    return 0;
}

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

int main(){
    int n;
    cin>>n;
    vector<pair<int,int>> a,b;
    rep(i,0,n){
        int x,y;
        cin>>x>>y;
        a.push_back(make_pair(y,x));
    }
    rep(i,0,n){
        int x,y;
        cin>>x>>y;
        b.push_back(make_pair(x,y));
    }
    sort(a.begin(),a.end());
    sort(b.begin(),b.end());
    rep(i,0,n){
        int x,y;
        x=a.at(i).second;
        y=a.at(i).first;
        a.at(i)=make_pair(x,y);
    }

    int numPair=0;
    rep(i,0,n){ //blue
        _rep(j,n-1,0){ //red
            if(a.at(j).first<b.at(i).first && a.at(j).second<b.at(i).second){
                numPair++;
                a.at(j)=make_pair(2*n,2*n);
                break;
            }
        }
    }

    cout<<numPair<<endl;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n); i++)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
using P = pair<int, int>;
const int INF = 1e9;
const long long LINF = 1e18;
const long long MOD = 1e9 + 7;

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

#include <iostream>
using namespace std;

int main()
{
  string name;
  int count = 0;
  getline(cin,name);

  /*if (name == "FESTIVAL")
  {
    cout << "Yes" << endl;
  }*/

  if(name[name.length()-1] == 'L')
  {
    for(int i = 0; i < name.length()-8; i++)
    {
      cout << name[i];
    }
  }
  cout << endl;
}


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

using namespace std;

int w,h;
vector<vector<int> >  b;
int sx,sy,gx,gy;
int ans;

void test(int x,int y,int n){
  int bx,by;
  int xx=x,yy=y;
  if(++n>=ans) return;

  while(b[y][x+1]==0){
    x++;
    if(x==gx&&y==gy){
      ans=n;
      return;
    }
  }
  if((x!=xx)&&(x+1!=w-1)){
    bx=x+1; by=y;
    b[by][bx]=0;
    test(x,y,n);
    b[by][bx]=1;
  }
  x=xx;y=yy;

  while(b[y][x-1]==0){
    x--;
    if(x==gx&&y==gy){
      ans=n;
      return;
    }
  }
  if(x!=xx&&x-1!=0){
    bx=x-1; by=y;
    b[by][bx]=0;
    test(x,y,n);
    b[by][bx]=1;
  }
  x=xx;y=yy;

  while(b[y+1][x]==0){
    y++;
    if(x==gx&&y==gy){
      ans=n;
      return;
    }
  }
  if(y!=yy&&y+1!=h-1){
    bx=x; by=y+1;
    b[by][bx]=0;
    test(x,y,n);
    b[by][bx]=1;
  }
  x=xx;y=yy;

  while(b[y-1][x]==0){
    y--;
    if(x==gx&&y==gy){
      ans=n;
      return;
    }
  }
  if(y!=yy&&y-1!=0){
    bx=x; by=y-1;
    b[by][bx]=0;
    test(x,y,n);
    b[by][bx]=1;
  }
  x=xx;y=yy;
}

int main(){
  while(cin>>w>>h,w+h){
    w+=2;h+=2;
    ans=11;
    b=vector<vector<int> >(h+2,vector<int>(w+2,1));
    for(int i=1;i<h-1;i++){
      for(int j=1;j<w-1;j++){
        cin >> b[i][j];
        if(b[i][j]==2){
          b[i][j]=0;
          sx=j;sy=i;
        }else if(b[i][j]==3){
          b[i][j]=0;
          gx=j;gy=i;
        }
      }
    }
    test(sx,sy,0);
    if(ans==11) cout << -1 << endl;
    else cout << ans << endl;
  }
  return 0;
}

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

int w,h;

int dd[5]={0,1,0,-1,0};

void dfs_aux(int sx, int sy, int map[WMAX][HMAX],int *min,int count){
  rep(r,4){
    int nx=sx,ny=sy;
    if(map[nx+dd[r]][ny+dd[r+1]]==1)
      continue;
    while(0<=nx+dd[r] && nx+dd[r]<w && 0<=ny+dd[r+1] && ny+dd[r+1]<h){
      nx+=dd[r];
      ny+=dd[r+1];
      if(map[nx][ny]==3)
	if(*min > count){
	  *min = count;
	  break;
	}
      if(map[nx][ny]==1){
	break;
      }
    }

    if(map[nx][ny]==1 && count <10){
      map[nx][ny]=0;
      nx-=dd[r];ny-=dd[r+1];
      dfs_aux(nx,ny,map,min,count+1);
      nx+=dd[r];ny+=dd[r+1];
      map[nx][ny]=1;
    }
  }
}

int dfs(int sx, int sy, int map[WMAX][HMAX]){
  int min=11;
  dfs_aux(sx,sy,map,&min,1);
  if(min>10)
    return -1;
  return min;
}

int main(){
  for(;;){
    scanf("%d %d",&w,&h);
    if(!(w+h)) break;
    int map[WMAX][HMAX],sx,sy;
    rep(i,h){
      rep(j,w){
	scanf("%d",&map[j][i]);
	if(map[j][i]==2){
	  sx=j;
	  sy=i;
	}
      }
    }
    printf("%d\n",dfs(sx,sy,map));
  }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define mp make_pair
#define PI pair<ll,ll>
#define poly vector<ll>
#define mem(a) memset((a),0,sizeof(a))
#define For(i,l,r) for(int i=(int)(l);i<=(int)(r);i++)
#define Rep(i,r,l) for(int i=(int)(r);i>=(int)(l);i--)
#define pb push_back
#define fi first
#define se second
#define SZ(x) ((int)(x.size()))
inline char gc(){
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
#define gc getchar
inline ll read(){
	ll x = 0; char ch = gc(); bool positive = 1;
	for (; !isdigit(ch); ch = gc())	if (ch == '-')	positive = 0;
	for (; isdigit(ch); ch = gc())	x = x * 10 + ch - '0';
	return positive ? x : -x;
}
inline void write(ll a){
    if(a<0){
    	a=-a; putchar('-');
	}
    if(a>=10)write(a/10);
    putchar('0'+a%10);
}
inline void writeln(ll a){write(a); puts("");}
inline void wri(ll a){write(a); putchar(' ');}
ll rnd(){
	ull ans=0;
	For(i,0,4)ans=ans<<15^rand();
	return ans%((ull)1<<63);
}
const int N=205;
ll b[N],a[63];
char ch[N];
int main(){
	#ifdef Brollan
		freopen("1.in","r",stdin);
	#endif
	int T=read();
	while(T--){
		int n=read();
		For(i,1,n)b[i]=read();
		scanf("%s",ch+1);
		mem(a);
		int f=0;
		Rep(i,n,1){
			ll x=b[i];
			Rep(j,60,0)if(x>>j&1){
				if(a[j])x^=a[j];
				else{
					if(ch[i]=='0')a[j]=x;
					else f=1; break;
				}
			}
		}
		writeln(f);
	}
}

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

typedef long long ll;
typedef long double ld;
typedef vector<ll> v1;
typedef vector<v1> v2;
typedef vector<v2> v3;

typedef unordered_map<ll, unordered_map<ll, ll>> graph;

const ll INF = 1ll << 50;
const ll mod = 1000000007;

ll n;

ll query(ll x){
    cout << x << endl;
    cout << flush;
    string s;
    cin >> s;
    if(s[0] == 'V') exit(0);
    if(s[0] == 'M') return 0;
    if(s[0] == 'F') return 1;
    return -1;
}

int main(){
    cin >> n;
    ll left_state = query(0);
    // ll right_state = query(n-1);
    ll l = 0;
    ll r = n;
    for(ll i = 0;i < 19;i++){
        ll x = (l+r)/2;
        cerr << i << " " << l << " " << r << " " << x << endl;

        ll ret = query(x);
        if((x % 2) == ((left_state+ret)%2)){
            l = x;
        }else{
            r = x;
        }
    }
}


#include <bits/stdc++.h>
#define ll long long
#define rep(i,n) for(int i=0;i<n;i++)
#define rrep(i,n) for(int i=1;i<=n;i++)
#define drep(i,n) for(int i=n;i>=0;i--)
#define INF 100000005
#define MAX 100000001
#define dmp make_pair
#define dpb push_back
#define fi first
#define se second
using namespace std;
//__gcd(a,b), __builtin_popcount(a);

int n, m, a[10], ans;
map<int,int> mp;

int solve(int tmp[4], int k, int sum, int p){
	int t, u[4];
	rep(i,4)u[i] = tmp[i];
	u[k++] = p;
	if(a[p] >= 10)sum = sum*100+a[p];
	else sum = sum*10+a[p];

	if(m == k){
		if(mp[sum]==0)ans++;
		mp[sum] = 1;
		return 0;
	}

	for(int i = 0;i < n;i++){
		t = 1;
		rep(j,k)if(u[j] == i)t = 0;
		if(t)solve(u, k, sum, i);
	}
	return 0;
}

int main(){
	int u[4] = {0,0,0,0};
	while(1){
		ans = 0;
		mp.clear();
		scanf("%d%d", &n, &m);
		if(!n)break;
		rep(i,n)scanf("%d", &a[i]);
		for(int i = 0;i < n;i++){
			solve(u, 0, 0, i);
		}
		printf("%d\n", ans);
	}
	return 0;
}

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

int value( char c )
{
	switch( c ){
		case 'I':
			return 1;
			break;
		case 'V':
			return 5;
			break;
		case 'X':
			return 10;
			break;
		case 'L':
			return 50;
			break;
		case 'C':
			return 100;
			break;
		case 'D':
			return 500;
			break;
		case 'M':
			return 1000;
			break;
	}
}

int main()
{
	char str[101];
	while( scanf( "%s", str ) != EOF ){

		int ans = 0;

		for( int i = 1; i <= strlen( str ); i++ ){
			if( value( str[i-1] ) >= value( str[i] ) ){
				ans += value( str[i-1] );
			}
			else{
				ans += value( str[i] ) - value( str[i-1] );
				i++;
			}
		}

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

 	return 0;
}


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

long long n;
string s,sw = "SW";

string solve();
string check(string now);

int main() {
    cin >> n >> s;
    cout << solve() << endl;
    return 0;
}

string solve(){
    string ans;
    for(int i = 0;i < 4;++i){
        string now;
        now += sw[i % 2];
        now += sw[i / 2];
        ans = check(now);
        if(ans.size() == n)return ans;
    }
    return "-1";
}

string check(string now){
    for(int i = 2;i < n;++i){
        if(now[i-1] == 'S'){
            if(s[i-1] == 'o')now += now[i-2];
            else now += now[i-2] == 'S'? 'W':'S';
        }
        else {
            if(s[i-1] != 'o')now += now[i-2];
            else now += now[i-2] == 'S'? 'W':'S';
        }
    }
    bool ch = 1;
    if(now[0] == 'S'){
        if(now[1] == now[n-1] && s[0] != 'o')ch = 0;
        if(now[1] != now[n-1] && s[0] == 'o')ch = 0;
    }
    else {
       if(now[1] == now[n-1] && s[0] == 'o')ch = 0;
       if(now[1] != now[n-1] && s[0] != 'o')ch = 0;
    }
    if(now[n-1] == 'S'){
        if(now[n-2] == now[0] && s[n-1] != 'o')ch = 0;
        if(now[n-2] != now[0] && s[n-1] == 'o')ch = 0;
    }
    else {
        if(now[n-2] == now[0] && s[n-1] == 'o')ch = 0;
        if(now[n-2] != now[0] && s[n-1] != 'o')ch = 0;
    }
    if(ch)return now;
    return "";
}

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

using namespace std;

int main() {
	int n,p;
	while (cin >> n >> p && n != 0 && p != 0) {
		vector<int> a(n, 0);
		int flag = n;
		int winner;

		int i = 0;
		while (1) {
			if (p != 0) {
				if (a[i % n] == 0) flag--;
				a[i % n] += 1;
				p--;
			} else if (a[i % n] != 0) {
				flag++;
				p += a[i % n];
				a[i % n] = 0;
			}
			if (p == 0 && flag == n - 1) {
				winner = i % n;
				break;
			}

			i++;
		}
		cout << winner << endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=100010;
pair<int,int> p[maxn];
int n;
int L1,R1,L2,R2;
int L[maxn],R[maxn];

void solve(){
    scanf("%d",&n);
    for(int i=1;i<=n;++i){
        scanf("%d%d",&p[i].first,&p[i].second);
    }
    sort(p+1,p+n+1);

    L1=p[1].first;R1=p[1].second;
    L[n]=p[n].first;R[n]=p[n].second;

    for(int i=n-1;i>1;--i){
        R[i]=min(R[i+1],p[i].second);
        L[i]=max(L[i+1],p[i].first);
    }
    int ans=max(R1-L1+1,0)+max(R[2]-L[2]+1,0),res=0;
    for(int i=3;i<=n;++i){
        L1=max(L1,p[i-1].first);
        R1=min(R1,p[i-1].second);
        ans=max(max(R1-L1+1,0)+max(R[i]-L[i]+1,0),ans);
    }
    for(int i=1;i<=n;++i)res=max(res,p[i].second-p[i].first+1);
	int maxl=INT_MIN,minr=INT_MAX;
	for(int i=1;i<=n;++i){
		maxl=max(maxl,p[i].first);
		minr=min(minr,p[i].second);
	}
	res+=max(0,minr-maxl+1);
    printf("%d\n",max(ans,res));
}
int main(){
    solve();

    return 0;
}

/*
 * @Author: RBQRBQ
 * @Date: 2020-04-05 15:08:15
 * @LastEditors: RBQRBQ
 * @LastEditTime: 2020-04-05 19:16:50
 */
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef pair<ll,ll> pii;
namespace IO{
    template<typename T>inline void read(T &x){
        x=0;ll f=1;char ch=getchar();
        while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();}
        while(isdigit(ch)){x=x*10+ch-48;ch=getchar();}
        x=x*f;
    }
}
using namespace IO;
vector<pii> pp;
vector<pii> T;
ll mxL,minR,mxR,minL;

int main()
{
    int N;
    cin>>N;
    mxL=mxR=-1;
    minL=minR=10000000000ll;
    int P,Q;
    for(int i=1;i<=N;i++)
    {
        ll x,y;
        read(x),read(y);
        y++;
        pp.push_back(make_pair(x,y));
        if(x>mxL)
        {
            mxL=x;
            P=i;
        }
        if(minR>y)
        {
            minR=y;
            Q=i;
        }
    }
    ll ans1=0,ans2=0;
    minL=0;
    for(int i=1;i<=N;i++)
    {
            minL=max(minL,pp[i-1].second-pp[i-1].first);
    }

    ans1=max((minL),0ll)+max((minR-mxL),0ll);

    int ca=1;
    for(auto p:pp)
    {
        if(ca!=P&&ca!=Q)
        T.push_back(make_pair(max(p.second-mxL,0ll),max(minR-p.first,0ll)));
        ca++;
    }
    sort(T.begin(),T.end());
    ll P1=pp[P-1].second-pp[P-1].first;
    ll Q1=pp[Q-1].second-pp[Q-1].first;
    ll x1=0,x2=Q1;
    // for(auto p:T)
    // {
    //     cout<<p.first<<" "<<p.second<<endl;
    // }
    for(int i=0;i<T.size();i++)
    {

        x1=min(T[i].first,P1);
        ans2=max(ans2,x1+x2);
        x2=min(T[i].second,x2);
    }
    ans2=max(ans2,P1+x2);
    cout<<max(ans1,ans2);

}


//hate penalty

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

int dsu[200001], dsu2[200001];
map <pair<int, int> ,int> mp;

int find(int x){
	if (dsu[x]) return dsu[x]=find(dsu[x]);
	return x;
}

int find2(int x){
	if (dsu2[x]) return dsu2[x]=find2(dsu2[x]);
	return x;
}

int main(){
	int n, k, l;
	cin >> n >> k >> l;
	int x, y;
	for (int i=1; i<=k; i++){
		cin >> x >> y;
		if (find(x)!=find(y)){
			dsu[find(x)]=find(y);
		}
	}
	for (int i=1; i<=l; i++){
		cin >> x >> y;
		if (find2(x)!=find2(y)){
			dsu2[find2(x)]=find2(y);
		}
	}
	for (int i=1; i<=n; i++){
		mp[make_pair(find(i),find2(i))]++;
	}
	for (int i=1; i<n; i++){
		cout << mp[make_pair(find(i), find2(i))] << " ";
	}
	cout << mp[make_pair(find(n), find2(n))] << "\n";
}

#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include<algorithm>
#include <iomanip>
#include <math.h>
#include <iostream>
#include <bitset>
#include <map>

#define db double
#define vi vector<int>
#define pb push_back
#define fi first
#define se second
#define ll long long
#define ld long double
#define maxn 5005
#define mod 998244353
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vll vector<ll>
#define vpi vector<pii>
#define vpll vector<pll>
#define vv(z,a,b) vector<vector<int>>z(a,vector<int>(b))
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(c) begin(c),end(c)
#define pp pair<int,int>
#define fix(n) fixed << setprecision(n)
#define gmax(a,b) __gcd(a,b)
#define gmin(a,b) a/__gcd(a,b)*b
using namespace std;
//obj;
class binary{
public:
    bool isOK(int index, int key,vi a) {
    if (a[index] >= key) return true;
    else return false;
}
int binary_search(int key,vi a) {

    int ng = -1; //「index = 0」が条件を満たすこともあるので、初期値は -1
    int ok = (int)a.size(); // 「index = a.size()-1」が条件を満たさないこともあるので、初期値は a.size()

    /* ok と ng のどちらが大きいかわからないことを考慮 */
    while (abs(ok - ng) > 1) {
        int mid = (ok + ng) / 2;

        if (isOK(mid, key,a)) ok = mid;
        else ng = mid;
    }
    return ok;
}
}obj;

int main(){
int n,x,t;
cin>>n>>x>>t;
int b=n%x;
int a=n/x;
if(b!=0)a++;
int ans=t*a;
cout<<ans<<endl;

return 0;
 }


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

using namespace std;
#define SZ(x) (int)(x.size())
#define REP(i, n) for(int i=0;i<n;++i)
#define FOR(i, a, b) for(int i=a;i<b;++i)
#define RREP(i, n) for(int i=n-1;i>=0;--i)
#define RFOR(i, a, b) for(int i=b-1;i>=a;--i)

#define all(x) begin(x),end(x)
#define dump(x) cerr<<#x<<" = "<<(x)<<endl
#define debug(x) cerr<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<< endl;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using P = pair<int, int>;
const double eps = 1e-8;
const ll MOD = 1000000007;
const int INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;

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

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

template<typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> p) {
    os << p.first << ":" << p.second;
    return os;
}

template<class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
    os << "[";
    REP(i, SZ(v)) {
        if (i) os << ", ";
        os << v[i];
    }
    return os << "]";
}

// edit
int N;
vector<ll> A;

vector<vector<ll>> dp;
vector<vector<bool>> vis;

ll dfs(int n, int c) {
    if (vis[n][c]) return dp[n][c];
    if (n == 0) {
        if (c <= 1) {
            return 0;
        } else {
            return -LINF;
        }
    }
    if (n == 1) {
        if (c == 0) {
            return A[0];
        } else if (c <= 2) {
            return 0;
        } else {
            return -LINF;
        }
    }

    vis[n][c] = true;

    ll &ret = dp[n][c];
    ret = dfs(n - 2, c) + A[n - 1];
    if (c >= 1) {
        chmax(ret, dfs(n - 1, c - 1));
    }

    return ret;
}

void solve() {
    cin >> N;
    A.resize(N);
    REP(i, N) cin >> A[i];

    dp.resize(N + 1, vector<ll>(3));
    vis.resize(N + 1, vector<bool>(3));

    ll ans = dfs(N, 1 + (N + 0) % 2);
    cout << ans << endl;

}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
//    std::ifstream in("input.txt");
//    std::cin.rdbuf(in.rdbuf());

    solve();

    return 0;
}

#include<bits/stdc++.h>
#define ll long long

const ll mod = 1000000007 ;

using namespace std ;

int main()
{
	int n;
	cin>>n;
	int A[n];
	int cnt = 0 ;
	for(int i=0 ; i<n ; i++)
		cin>>A[i];
	for(int i=0 ; i<n ; i++)
	{
		for(int j=i+1 ; j<n ; j++)
		{
			for(int k=j+1 ; k<n ; k++)
			{
				if(A[i]+A[j]>A[k] && A[k]+A[j]>A[i] && A[i]+A[k]>A[j] && A[i]!=A[j] && A[j]!=A[k] && A[k]!=A[i])
					cnt++ ;
			}
		}
	}
	cout<<cnt;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
typedef pair<ll,ll> l_l;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
const int INF=1001001000;
const int mINF=-1001001000;
const ll LINF=1010010010010010000;
template<class T> inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template<class T> inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
//グリッド：（典型）dp,dfs,bfs,最短経路,その他
int main(){
    int m;cin>>m;
    vl d(m),c(m);
    ll D=0,S=0;
    rep(i,m)cin>>d[i]>>c[i];
    rep(i,m){
        D+=c[i];
        S+=d[i]*c[i];
    }
    cout<<(D-1)+(S-1)/9<<endl;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (ll i = m; i < n; ++i)
#define FORR(i, m, n) for (ll i = m; i >= n; --i)
#define ALL(v) (v).begin(),(v).end()
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
const ll INF=1LL<<60;
const int inf=(1<<30)-1;
const int mod=1e9+7;
int dx[8]={1,0,-1,0,-1,-1,1,1};
int dy[8]={0,1,0,-1,-1,1,-1,1};
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n,a,b,c,d,e;cin >> n >> a >> b >> c >> d >> e;
    ll m=min({a,b,c,d,e});
    cout << 4+(n+m-1)/m << endl;
}


#include <bits/stdc++.h>
#define enter putchar('\n')
#define space putchar(' ')
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
map<pii, int> M;
vector<pii> E[500005];
set<pii> s;
int n, m, ncnt, d[500005];
template <class T> int read(T &x) {
	x = 0; int f = 1; char c = getchar();
	while (c > -1 && c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); }
	if (c == -1) return 0;
	while (c >= '0' && c <= '9') { x = x * 10 + c - 48; c = getchar(); }
	return x *= f, 1;
}
template <class T> void write(T x) {
	if (x < 0) x = -x, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + 48);
}
int main() {
	while (read(n), read(m)) {
		M.clear();
		ncnt = n;
		for (int i = 1; i <= n; ++i)
			E[i].clear();
		for (int i = 1; i <= m; ++i) {
			int u, v, c;
			read(u), read(v), read(c);
			if (!M[mp(u, c)]) {
				M[mp(u, c)] = ++ncnt;
				E[ncnt].clear();
				E[ncnt].pb(mp(u, 1));
				E[u].pb(mp(ncnt, 1));
			}
			if (!M[mp(v, c)]) {
				M[mp(v, c)] = ++ncnt;
				E[ncnt].clear();
				E[ncnt].pb(mp(v, 1));
				E[v].pb(mp(ncnt, 1));
			}
			E[M[mp(u, c)]].pb(mp(M[mp(v, c)], 0));
			E[M[mp(v, c)]].pb(mp(M[mp(u, c)], 0));
		}
		fill(d, d + ncnt + 1, -1);
		d[1] = 0;
		s.insert(mp(d[1], 1));
		while (!s.empty()) {
			int u = s.begin() -> se;
			s.erase(s.begin());
			for (int i = 0; i < E[u].size(); ++i) {
				int v = E[u][i].fi;
				if (d[v] > d[u] + E[u][i].se || d[v] == -1) {
					s.erase(mp(d[v], v));
					d[v] = d[u] + E[u][i].se;
					s.insert(mp(d[v], v));
				}
			}
		}
		write(d[n] >> 1), enter;
	}
	return 0;
}

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

using namespace std;

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

int main()
{
    int m, a, b;

    while(cin >> m >> a >> b, m){
        vector<int> pt(m);
        for(int i=0; i<m; ++i){
            cin >> pt[i];
        }

        pair<int, int> ans{-1, -1};

        rep(p, 10000){
            vector<int> ok, ng;

            rep(i, m){
                if(pt[i] < p) ng.push_back(pt[i]);
                else ok.push_back(pt[i]);
            }

            int n = ok.size();
            if(a <= n && n <= b){
                int gap = *min_element(ok.begin(), ok.end()) - *max_element(ng.begin(), ng.end());
                ans = max(ans, {gap, ok.size()});
            }
        }

        cout << ans.second << endl;
    }
}


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

#define rep(i, m, n) for(int i=int(m);i<int(n);i++)
#define all(c) begin(c),end(c)

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; }

//改造
typedef long long int ll;
using ll = long long int;
using ull = long long unsigned int;
using Int = long long int;
using namespace std;
#define INF (1 << 30) - 1
#define INFl (ll)5e15
#define DEBUG 0 //デバッグする時1にしてね
#define dump(x)  cerr << #x << " = " << (x) << endl
#define MOD 1000000007

//ここから編集する
class Solve {
public:
    void solve() {
        Int N, M;
        cin >> N >> M;
        vector<Int> A(M);
        int ki = 0;
        for (int i = 0; i < M; ++i) {
            cin >> A[i];
            if (A[i] % 2) ki++;
        }

        if (ki > 2) {
            cout << "Impossible" << endl;
            return;
        }

        sort(all(A), [](Int a, Int b) {
            return a % 2 > b % 2;
        });

        if (A.size() > 1 && A[1] % 2 == 1) {
            swap(A[1], A.back());
        }
        vector<Int> B = A;
        if (B.size() == 1) {
            B.push_back(1);
            B[0]--;
            if (B[0] == 0) {
                swap(B[0], B[1]);
            }
        }

        B[0]--;
        B.back()++;
        if (B[0] == 0) {
            B.erase(B.begin());
        }
        if (B.back() == 0) {
            B.pop_back();
        }

        for (int i = 0; i < M; ++i) {
            cout << A[i] << " ";
        }
        cout << endl;
        cout << B.size() << endl;
        for (auto e : B) {
            cout << e << " ";
        }
        cout << endl;

    }
};

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

    Solve().solve();

    return 0;
}

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define Rep(i, n) for(int i=0;i<n;++i)
#define rep(i, n) for(int i=1;i<=n;++i)
#define ll long long int
#define INF 1e9
#define LINF 1e18
#define MOD 1000000007
#define PI 3.14159265359
#define EPS 1e-10
#define All(a) (a).begin(), (a).end()

using namespace std;
using P = pair<ll, ll>;

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

/**
 * Usage:
 *
 * G[from].push_back(Edge(to, weight));
 * using Graph = vector<vector<Edge>>;
 *
 */
struct Edge {
  int to;
  ll weight;
  Edge(int t, ll w) : to(t), weight(w) { }
};

/**
 * Usage:
 *
 * chmin(old, new);
 * chmax(old, new);
 *
 */
template<class T> inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template<class T> inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template<typename T>
void printv(vector<T>& v) {
  if (v.size() == 0) {
    cout << "\n";
    return;
  }

  Rep(i, v.size()) {
    cout << v[i] << ((i + 1 < v.size()) ? ' ' : '\n');
  }
}

int H, W;
char maze[1100][1100];
int d[1100][1100];

int main () {
  cin >> H >> W;
  queue<P> que;
  Rep(i, H) {
    Rep(j, W) {
      cin >> maze[i][j];
      if (maze[i][j] == '#') que.emplace(i, j);
    }
  }

  int ans = 0;

  while (!que.empty()) {
    int n = que.size();

    queue<P> new_que;
    Rep(i, n) {
      P p = que.front(); que.pop();
      Rep(k, 4) {
        int new_y = p.first + dy[k];
        int new_x = p.second + dx[k];

        if (new_y >= 0 & new_y < H & new_x >= 0 & new_x < W) {
          if (maze[new_y][new_x] == '.') {
            new_que.emplace(new_y, new_x);
            //cout << new_y << new_x << "\n";
            maze[new_y][new_x] = '#';
          }
        }
      }
    }
    que = new_que;

    ++ans;
    //cout << ans << "\n";
  }

  cout << ans -1<< "\n";
}


#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
using namespace std;
using Graph = vector<vector<int>>;
#define MOD 1000000007
#define MOD2 998244353
#define INF ((1<<30)-1)
#define LINF (1LL<<60)
#define EPS (1e-10)
typedef long long ll;
typedef pair<ll, ll> P;

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

    vector<ll> ab;
    ll cnt = 0;
    rep(i, n){
        int a, b;
        cin >> a >> b;
        ab.push_back(a+b);
        cnt += b;
    }
    sort(ab.begin(), ab.end(), greater<ll>());
    ll ans = 0;
    rep(i, (n+1)/2){
        ans += ab[2 * i];
    }
    ans -= cnt;

    cout << ans << endl;

    return 0;
}

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define FASTIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define inf 1e18
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define mod 1000000007
#define pb push_back
#define all(s) s.begin(),s.end()
#define pie 3.14159265358979323846
#define fr(i,a,b)  for(ll i=a;i<b;i++)
#define frr(i,a,b) for(ll i=b-1;i>=a;i--)
#define gcd __gcd
#define con continue
#define pii pair<ll,ll>
signed main(){
    FASTIO;
    ll tt=1;
    //cin>>tt;
    while(tt--){
         ll n,ans=0,x,mx=0;cin>>n;
         for(ll i=0;i<n;i++){
            cin>>x;
            if(mx>x)ans+=mx-x;
            mx=max(mx,x);
         }
         cout<<ans<<"\n";
    }
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define FASTIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define inf 1e9
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define mod 1000000007
#define pb push_back
#define all(s) s.begin(),s.end()
#define pie 3.14159265358979323846
#define fr(i,a,b) for(ll i=a;i<b;i++)
#define frr(i,a,b) for(ll i=b-1;i>=a;i--)
#define gcd __gcd
#define con continue
#define pii pair<ll,ll>
const ll N=5e5+6;
ll seg[4*N];
void build(ll s,ll e,ll idx,ll a[]){
    if(s==e){
       //cout<<s<<" ";
       seg[idx]=a[s];
       return;
    }
    ll mid=(s+e)/2;
    build(s,mid,2*idx+1,a);
    build(mid+1,e,2*idx+2,a);
    seg[idx]=seg[2*idx+1]+seg[2*idx+2];
    return ;
}
void update(ll s,ll e,ll idx,ll pos,ll val){
    if(s==e){
       seg[idx]=val;
       return;
    }
    ll mid=(s+e)/2;
    if(pos<=mid)
    update(s,mid,2*idx+1,pos,val);
    else
    update(mid+1,e,2*idx+2,pos,val);
    seg[idx]=seg[2*idx+1]+seg[2*idx+2];
    return ;
}
ll query(ll s,ll e,ll idx,ll l,ll r){
   if(s>=l&&e<=r)return seg[idx];
   if(s>r||e<l)return 0;
   ll mid=(s+e)/2;
   return query(s,mid,2*idx+1,l,r)+query(mid+1,e,2*idx+2,l,r);
}
signed main(){
    FASTIO;
    ll tt=1;
    //cin>>tt;
    while(tt--){
         ll n,q,x,l,r;cin>>n>>q;
         ll a[n];
         for(ll i=0;i<n;i++){
            cin>>a[i];
         }
         build(0,n-1,0,a);
         while(q--){
            cin>>x;
            if(x){
               cin>>l>>r;
               cout<<query(0,n-1,0,l,r-1)<<"\n";
            }
            else{
               cin>>l>>x;
               a[l]+=x;
               update(0,n-1,0,l,a[l]);
            }
         }
    }
}

#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define all(c) (c).begin(),(c).end()
#define pb push_back
#define dbg(...) do{cerr<<__LINE__<<": ";dbgprint(#__VA_ARGS__, __VA_ARGS__);}while(0);

using namespace std;

namespace std{template<class S,class T>struct hash<pair<S,T>>{size_t operator()(const pair<S,T>&p)const{return ((size_t)1e9+7)*hash<S>()(p.first)+hash<T>()(p.second);}};template<class T>struct hash<vector<T>>{size_t operator()(const vector<T> &v)const{size_t h=0;for(auto i : v)h=h*((size_t)1e9+7)+hash<T>()(i)+1;return h;}};}
template<class T>ostream& operator<<(ostream &os, const vector<T> &v){os<<"[ ";rep(i,v.size())os<<v[i]<<(i==v.size()-1?" ]":", ");return os;}template<class T>ostream& operator<<(ostream &os,const set<T> &v){os<<"{ "; for(const auto &i:v)os<<i<<", ";return os<<"}";}
template<class T,class U>ostream& operator<<(ostream &os,const map<T,U> &v){os<<"{";for(const auto &i:v)os<<" "<<i.first<<": "<<i.second<<",";return os<<"}";}template<class T,class U>ostream& operator<<(ostream &os,const pair<T,U> &p){return os<<"("<<p.first<<", "<<p.second<<")";}
void dbgprint(const string &fmt){cerr<<endl;}template<class H,class... T>void dbgprint(const string &fmt,const H &h,const T&... r){cerr<<fmt.substr(0,fmt.find(","))<<"= "<<h<<" ";dbgprint(fmt.substr(fmt.find(",")+1),r...);}
typedef long long ll;typedef vector<int> vi;typedef pair<int,int> pi;const int inf = (int)1e9;const double INF = 1e12, EPS = 1e-9;

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

	int h, w; cin >> h >> w;
	vector<string> in(h); rep(i, h) cin >> in[i];

	vector<vi> d1(h), d2(h - 1), dp(h - 1, vi(w - 1));
	rep(i, h) rep(j, w - 1) d1[i].pb(in[i][j] != in[i][j + 1]);
	rep(i, h - 1) rep(j, w - 1) d2[i].pb(d1[i][j] != d1[i + 1][j]);

	//rep(i, h - 1) dbg(d2[i]);

	ll ans = max(h, w);
	rep(i, h - 1){
		vi ls(w - 1), rs(w - 1), q;
		rep(j, w - 1){
			dp[i][j] = d2[i][j] ? 0 : (i ? dp[i - 1][j] : 0) + 1;
			ls[j] = j;
			while(q.size() && dp[i][q.back()] >= dp[i][j]){
				ls[j] = ls[q.back()];
				q.pop_back();
			}
			q.pb(j);
		}
		q.clear();
		for(int j = w - 2; j >= 0; j--){
			rs[j] = j;
			while(q.size() && dp[i][q.back()] >= dp[i][j]){
				rs[j] = rs[q.back()];
				q.pop_back();
			}
			q.pb(j);
			ans = max(ans, (rs[j] - ls[j] + 2ll) * (dp[i][j] + 1ll));
		}
		//dbg(i, ls);
	}
	cout << ans << endl;
	return 0;
}


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

int fact[2000005];
int mod = 998244353;
long long qexp(long long b, long long p){
    long long res = 1;
    while(p){
        if(p&1) res = (res * b) % mod;
        b = (b * b) % mod;
        p >>= 1;
    }
    return res;
}
int inv(long long b){ return qexp(b, mod-2); }
int choose(int N, int C){
	if(N < C) return 0;
	int res = fact[N];
	res *= inv(fact[C]);
	res %= mod;
	res *= inv(fact[N-C]);
	res %= mod;
	return res;
}
int split(int total, int N){ ///split total objects to N groups
	return choose(N + total - 1, N - 1);
}

int ans = 0;
void fix(int &x){
	x %= mod;
	if(x < 0) x += mod;
}
signed main(){
	ios_base::sync_with_stdio(false); cin.tie(0);

	fact[0] = 1;
	for(int i = 1;i <= 2000000;i++) fact[i] = (fact[i-1] * i) % mod;

	int N, M; cin >> N >> M;

	for(int ones = 0;ones <= M;ones++){
		int zeros = N - ones;

		int leftover = 3*M - ones;
		if(leftover & 1) continue;

		leftover /= 2; ///The number of 2s to assign elsewhere

		int ways01 = choose(N, ones); ///ways to arrange the 0s and 1s

		int total2 = split(leftover, N); ///total ways to assign
		int oneTooBig = ones * split(leftover - M, N);
		int zeroTooBig = zeros * split(leftover - M - 1, N);

		total2 -= oneTooBig;
		total2 -= zeroTooBig;

		fix(total2);
		ans += total2 * ways01;
		fix(ans);
	}

	cout << ans;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int,int> pii;
#define fir first
#define sec second
#define rep(i,a,b) for (int i = (a) ; i <= (b) ; ++ i)
#define rrp(i,a,b) for (int i = (a) ; i >= (b) ; -- i)
#define gc() getchar()
template <typename tp>
inline void read(tp& x) {
  x = 0; char tmp; bool key = 0;
  for (tmp = gc() ; !isdigit(tmp) ; tmp = gc())
    key = (tmp == '-');
  for ( ; isdigit(tmp) ; tmp = gc())
    x = (x << 3) + (x << 1) + (tmp ^ '0');
  if (key) x = -x;
}
template <typename tp>
inline void ckmn(tp& x,tp y) {
  x = x < y ? x : y;
}
template <typename tp>
inline void ckmx(tp& x,tp y) {
  x = x < y ? y : x;
}

const int MOD = 998244353;
inline void Add(int& x,int y) {
  x = x + y >= MOD ? x + y - MOD : x + y;
}
inline void Sub(int& x,int y) {
  x = x - y < 0 ? x - y + MOD : x - y;
}
int power(int a,int b) {
  int ret = 1;
  while (b) {
    if (b&1) ret = 1ll * ret * a % MOD;
    a = 1ll * a * a % MOD;
    b >>= 1;
  }
  return ret;
}
const int N = 3000000;
int jc[N], ijc[N], n, m, ans;
void init(int t) {
  jc[0] = 1;
  rep (i, 1, t) jc[i] = 1ll * jc[i-1] * i % MOD;
  ijc[t] = power(jc[t], MOD - 2);
  rrp (i, t-1, 0) ijc[i] = 1ll * ijc[i+1] * (i+1) % MOD;
}
int comb(int a,int b) {
  if (a < b || b < 0) return 0;
  return 1ll * jc[a] * ijc[b] % MOD * ijc[a-b] % MOD;
}
int main() {
  read(n), read(m);
  init(n + 3 * m);
  rep (i, 0, min(n, m)) if ((m-i) % 2 == 0) {
    Add(ans, 1ll * comb(n, i) * comb((3 * m - i) / 2 + n - 1, n - 1) % MOD);
    if (i <= m-2)
      Sub(ans, 1ll * n * comb(n-1,i) % MOD * comb((3*m-i-2*(m+1)) / 2 + n - 1, n - 1) % MOD);
    if (i <= m)
      Sub(ans, 1ll * n * comb(n-1,i-1) % MOD * comb((3*m-i-2*m)/2 + n-1, n-1) % MOD);
  }
  printf("%d\n", ans);
  return 0;
}


#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <functional>
#include <limits.h>
#include <set>
#include <map>
#include <tuple>
using namespace std;

#define ll long long
#define ull unsigned long long
#define rep(i,N) for (ll i=0;i<N;i++)
#define loop(i,N,M) for(ll i=N;i<M;i++)
#define MAX(v) *max_element(v.begin(),v.end())
#define MIN(v) *min_element(v.begin(),v.end())
#define SORTL2S(type,v) sort(v.begin(),v.end(),greater<type>())
#define SORTS2L(type,v) sort(v.begin(),v.end())
#define SORTMF(v,func) sort(v.begin(),v.end(),func)
#define MP(a,b) make_pair(a,b)

ll debugFlag = false;
template<typename T> void debug(T&);
template<typename T> void debug(T&, bool);
template<typename T> void debug(vector<T>&);
template<typename T> void debug(vector<T>&, bool);
template<typename T, typename U> void debug(pair<T, U>&);
template<typename T, typename U> void debug(pair<T, U>&, bool);

template<typename T>
void debug(T& obj) {
	debug(obj, true);
}

template<typename T>
void debug(T& obj, bool lnFlag) {
	if (!debugFlag) return;
	cout << obj;
	if (lnFlag) cout << endl;
}

template<typename T>
void debug(vector<T>& obj) {
	debug(obj, true);
}

template<typename T>
void debug(vector<T>& obj, bool lnFlag) {
	if (!debugFlag) return;
	cout << "[";
	rep(i, obj.size()) {
		if (i != 0) cout << ",";
		debug(obj[i], false);
	}
	cout << "]";
	if (lnFlag) cout << endl;
}

template<typename T, typename U>
void debug(pair<T, U>& obj) {
	debug(obj, true);
}

template<typename T, typename U>
void debug(pair<T, U>& obj, bool lnFlag) {
	if (!debugFlag) return;
	cout << "(";
	debug(obj.first, false);
	cout << ",";
	debug(obj.second, false);
	cout << ")";
	if (lnFlag) cout << endl;
}

void solve() {

	string S,T;
	cin >> S >> T;
	cout << T << S;

}

int main(int argc, char* argv[]) {
	if (argc == 2 && string(argv[1]) == "debug") debugFlag = true;
	solve();
	return 0;
}

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

//マクロ
//forループ関係
//引数は、(ループ内変数,動く範囲)か(ループ内変数,始めの数,終わりの数)、のどちらか
//Dがついてないものはループ変数は1ずつインクリメントされ、Dがついてるものはループ変数は1ずつデクリメントされる
#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 FOR(i,a,b) for(ll i=a;i<=(ll)(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=(ll)(b);i--)
//xにはvectorなどのコンテナ
#define ALL(x) (x).begin(),(x).end() //sortなどの引数を省略したい
#define SIZE(x) ((ll)(x).size()) //sizeをsize_tからllに直しておく
#define MAX(x) *max_element(ALL(x)) //最大値を求める
#define MIN(x) *min_element(ALL(x)) //最小値を求める
//定数
#define INF 1000000000000 //10^12:極めて大きい値,∞
#define MOD 1000000007 //10^9+7:合同式の法
#define MAXR 100000 //10^5:配列の最大のrange(素数列挙などで使用)
#define PI 3.14159265359 //πの値
//略記
#define PB push_back //vectorヘの挿入
#define MP make_pair //pairのコンストラクタ
#define F first //pairの一つ目の要素
#define S second //pairの二つ目の要素

int main() {
cin.tie(0);
ios::sync_with_stdio(false);
string A, B; cin >> A >> B;
cout << B << A;
}


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

vector<int> primes={2,3,5,7};
int primes_extend(int calc_from, int calc_to);
int factoring(int n, map<int,int> &factors);

const int combination_max=100000+1000;
const int combination_modulo=1000000007;
vector<int64_t> factorials(combination_max);
vector<int64_t> factorials_inv(combination_max);
vector<int64_t> inverse(combination_max);
int combination_mod(int n, int k);
int prepare_combination_mod();

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

	primes_extend(10,100);
	primes_extend(100,10000);
	primes_extend(10000,32767);

	map<int,int> factors;
	factoring(m,factors);
	for (pair<int,int> p:factors)
	{
	 	// fprintf(stderr,"%d: %d\n",p.first,p.second);
	}

	prepare_combination_mod();

	int64_t patterns=1;
	for (pair<int,int> p:factors)
	{
		int c=combination_mod(p.second+n-1,n-1);
		// fprintf(stderr,"%d: %d patterns\n",p.first,c);
		patterns*=c;
		patterns%=combination_modulo;
	}
	cout << patterns << endl;
}

int primes_extend(int calc_from, int calc_to)
{

	for (int i=calc_from;i<=calc_to;++i)
	{
		int j;
		int loop_max=sqrt(i);
		for (j=0;(primes[j]<=loop_max)&&(j<primes.size());++j)
		{
			if ((i%primes[j])==0)
			{
				// not prime...
				break;
			}
		}
		if ((j>=primes.size())||(primes[j]>loop_max))
		{
			primes.push_back(i);
		}
	}
	//fprintf(stderr,"count: %d\n",primes.size());
}

int factoring(int n, map<int,int> &factors)
{
	int maxfactor=sqrt(n);
	for (int i=0;(n>1)&&(primes[i]<=maxfactor);++i)
	{
		while ((n%primes[i])==0)
		{
			n/=primes[i];
			factors[primes[i]]++;
		}
	}
	if (n!=1)
	{
		factors[n]++;
	}

	return 0;
}

int prepare_combination_mod()
{
	factorials[0]=1;
	factorials[1]=1;
	factorials_inv[0]=1;
	factorials_inv[1]=1;
	inverse[1]=1;
	for (int i=2;i<combination_max;++i)
	{
		factorials[i]=factorials[i-1]*i%combination_modulo;
		inverse[i]=combination_modulo - inverse[combination_modulo%i]*(combination_modulo/i)%combination_modulo;
		factorials_inv[i]=factorials_inv[i-1]*inverse[i]%combination_modulo;
	}
}

int combination_mod(int n, int k)
{
	return factorials[n]*(factorials_inv[k]*factorials_inv[n-k]%combination_modulo)%combination_modulo;
}

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

int main() {
    int N, K, A = 1;
    cin >> N >> K;

    for (int i = 0; i < N; i++) {
        if (A * 2 < A + K) A *= 2;
        else {
            for (; i < N; i++) A += K;
            break;
        }
    }

    cout << A << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
  int v,e;
  cin>>v>>e;
  int g[v][v];
  int INF=1LL<<55LL;
  for(int i=0;i<v;i++)
    for(int j=0;j<v;j++)
      g[i][j]=(i!=j)*INF;
  for(int i=0;i<e;i++){
    int s,t,d;
    cin>>s>>t>>d;
    g[s][t]=d;
  }
  for(int k=0;k<v;k++)
    for(int i=0;i<v;i++)
      for(int j=0;j<v;j++)
	g[i][j]=min(g[i][j],g[i][k]+g[k][j]);
  for(int i=0;i<v;i++){
    if(g[i][i]<0){
      cout<<"NEGATIVE CYCLE"<<endl;
      return 0;
    }
  }
  for(int i=0;i<v;i++){
    for(int j=0;j<v;j++){
      if(j) cout<<" ";
      if(g[i][j]>=INF/2) cout<<"INF";
      else cout<<g[i][j];
    }
    cout<<endl;
  }
  return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>

using namespace std;

const int MAX_N = 1005;

int c[MAX_N][MAX_N];

int main()
{
	string X,Y;
	cin >> X;
	cin >> Y;
	for(int i=0;i<=X.length();i++){
		c[i][0] = i;
	}
	for(int j=0;j<=Y.length();j++){
		c[0][j] = j;
	}
	for(int i=1;i<=X.length();i++){
		for(int j=1;j<=Y.length();j++){
			int p,q,r;
			if(X[i-1] == Y[j-1]){
				p = c[i-1][j-1];
			}else{
				p = c[i-1][j-1] + 1;
			}
			q = c[i][j-1] + 1;
			r = c[i-1][j] + 1;
			c[i][j] = min(min(p,q),r);
		}
	}
	cout << c[X.length()][Y.length()] << "\n";
}

#include <bits/stdc++.h>
#define max 1000007
#define ll long long int
using namespace std;
vector <ll> primes;
void sieve()
{
    bool isprime[max+1];
    for(ll i=0;i<=max;i++) {
        isprime[i] = true;
    }
    isprime[0]=false;
    isprime[1]=false;
    for(ll i=3;i*i<=(max);i=i+2)
    {
        if(isprime[i]) {
            for (ll j = i*i; j  <= max; j=j+i) {
                isprime[j] = false;
            }
        }
    }
    primes.push_back(2);
    for(ll i=3;i<max;i=i+2)
    {
        if(isprime[i])
            primes.push_back(i);
    }
}
int main() {
    sieve();
    ll n,d,b,i,flag,x;
   scanf("%lld %lld",&d,&b);
    n=__gcd(d%b,b);
    set <int> s;
    s.insert(1);
    for(i=0;primes[i]*primes[i]<=n;i++)
    {
        while(n%primes[i]==0)
        {
            s.insert(primes[i]);
            n=n/primes[i];
        }
    }
    if(n>1)
        s.insert(n);
    printf("%lu",s.size());
    return 0;
}


#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int,int>;
template<typename T> void chmax(T &a, T b) { a = max(a, b); }
template<typename T> void chmin(T &a, T b) { a = min(a, b); }

const ll INF = 1e15;

int main() {
    ll x, y;
    cin >> x >> y;

    ll ans = INF;
    if (x <= y) ans = min(ans, y - x);
    if (-x <= y) ans = min(ans, y+x+1);
    if (x <= -y) ans = min(ans, -y-x+1);
    if (-x <= -y) ans = min(ans, -y+x+2);
    cout << ans << endl;
    return 0;
}

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

int main(void) {
    int a,b,c,k;
    int64_t target;
    int64_t remains;
    cin >> a >> b >> c >> k;
    target = max(a,max(b,c));
    remains = a + b + c - target;
    target = target * pow(2,k);
    cout << target + remains << endl;
    return 0;
}


#include <stdio.h>
#include <algorithm>
#define INF 100000000
using namespace std;

int n;
int graph[100][100];

void floyd(){
    for(int k=0;k<n;k++)
      for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
          graph[i][j]=min(graph[i][j],graph[i][k]+graph[k][j]);
}

int main(){
  int k,i,j,a,b,c,d,e,x;
  while(scanf("%d %d",&n,&k),n,k){
    for(i=0;i<n;i++)
      for(j=0;j<n;j++)
        graph[i][j]=i==j?0:INF;
    for(i=0;i<k;i++){
      if (scanf("%d",&x),x){
        scanf("%d %d %d",&c,&d,&e);
        if (graph[--c][--d]>e){
          graph[c][d]=graph[d][c]=e;
          floyd();
        }
      }else{
        scanf("%d %d",&a,&b);
        if (graph[--a][--b]==INF)
          printf("-1\n");
        else
          printf("%d\n",graph[a][b]);
      }
    }
  }
  return 0;
}

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

struct edge{
  int to,cost;
  edge(){}
  edge(int to,int cost):to(to),cost(cost){}
};
typedef pair<int,int> P;

int d[101];
const int INF=(int)1e9;

int main(void){
  int n,k;
  while(cin>>n>>k,!(n==0&&k==0)){
    vector<edge> G[1000];
    for(int i=0;i<k;i++){
      int r,a,b,cost;
      cin>>r;
      if(r==1){
	cin>>a>>b>>cost;
	G[a].push_back(edge(b,cost));
	G[b].push_back(edge(a,cost));
      }
      if(r==0){
	cin>>a>>b;
	priority_queue<P,vector<P>,greater<P> > que;
	fill(d,d+101,INF);
	d[a]=0;
	que.push(P(0,a));
	while(!que.empty()){
	  P p=que.top();
	  que.pop();
	  if(d[p.second]<p.first)continue;
	  for(int j=0;j<G[p.second].size();j++){
	    edge e=G[p.second][j];
	    if(d[e.to]>d[p.second]+e.cost){
	      d[e.to]=d[p.second]+e.cost;
	      que.push(P(d[e.to],e.to));
	    }
	  }
	}
	if(d[b]==(int)INF){
	  cout<<"-1"<<endl;
	}else{
	  cout<<d[b]<<endl;
	}
      }
    }
  }
  return 0;
}


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

int main(){

	int A,B,M;
	cin>>A>>B>>M;

	vector<int> a(A),b(B);

	for(int i=0;i<A;i++)cin>>a[i];
	for(int i=0;i<B;i++)cin>>b[i];

	int ans = 1000000;

	for(int i=0;i<M;i++){
		int x,y,c;
		cin>>x>>y>>c;
		x--;y--;

		ans = min(ans,a[x]+b[y]-c);
	}

	sort(a.begin(),a.end());
	sort(b.begin(),b.end());

	ans = min(ans,a[0]+b[0]);

	cout<<ans<<endl;

	return 0;
}

#include <iostream>
using namespace std;

struct Dice
{
	Dice(int d[6]){
		for(int i = 0; i < 6; ++i)
			n[i] = d[i];
	}
	void toN(){ shift( n[0], n[1], n[5], n[4] ); }
	void toS(){ shift( n[0], n[4], n[5], n[1] ); }
	void toW(){ shift( n[0], n[2], n[5], n[3] ); }
	void toE(){ shift( n[0], n[3], n[5], n[2] ); }
	void shift( int& a, int& b, int& c, int& d ){
		int t = a;
		a = b;
		b = c;
		c = d;
		d = t;
	}
	int n[6];
};

int main()
{
	int i, d[6] = {};
	for(i = 0; i < 6; ++i)
		cin >> d[i];

	Dice dice(d);
	char c;
	cin.get(c);
	while( cin.get(c) ){
		if( c == '\n' )
			break;
		switch(c)
		{
		case 'N': dice.toN(); break;
		case 'S': dice.toS(); break;
		case 'W': dice.toW(); break;
		case 'E': dice.toE(); break;
		}
	}

	cout << dice.n[0] << "\n";

	return 0;
}


#include <iostream>
#include<math.h>
#include<iomanip>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;

int main()
{
    int  a=0,b=0,a1=0,b1=0,i=0,n=0,j=0;
    for(i=1;;i++){
        cin >> n;
        if(n==0)break;
        for(j=0;j<n;j++){
            cin >> a >> b;
            if(a>b){
                a1+=a+b;
            }
            if(a<b){
                b1+=a+b;
            }
            if(a==b){
                a1+=a;
                b1+=b;
            }

        }
        cout << a1 << ' ' << b1 <<endl;
        a1=b1=0;
    }
    //cout << b;
}

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define each(a, b) for(auto (a): (b))
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
#define pb push_back
#define show(x) cout <<#x<<" = "<<(x)<<endl
#define spair(p) cout <<#p<<": "<<p.fi<<" "<<p.se<<endl
#define svec(v) cout<<#v<<":";rep(kbrni,v.size())cout<<" "<<v[kbrni];cout<<endl
#define sset(s) cout<<#s<<":";each(kbrni,s)cout <<" "<<kbrni;cout<<endl

using namespace std;

typedef pair<int,int>P;

template<class V> class segtree {
private:
    int n;
    vector<V> node, lazy;
    vector<bool> lazyFlag;

public:
    segtree(vector<V> v) {
        int sz = (int)v.size();
        n = 1;
        while(n < sz){
            n *= 2;
        }
        node.resize(2*n-1);
        lazy.resize(2*n-1, 0);
        lazyFlag.resize(2*n-1,false);
        rep(i,sz){
            node[i+n-1] = v[i];
        }
        for(int i=n-2; i>=0; i--){
            node[i] = min(node[i*2+1],node[i*2+2]);
        }
    }
    void eval(int k, int l, int r) {
        if(lazyFlag[k]) {
            node[k] = lazy[k];
            if(r - l > 1) {
                lazy[k*2+1] = lazy[k*2+2] = lazy[k];
                lazyFlag[k*2+1] = lazyFlag[k*2+2] = true;
            }
            lazyFlag[k] = false;
        }
    }
    void range(int a, int b, V x, int k=0, int l=0, int r=-1) {
        if(r < 0) r = n;
        eval(k, l, r);
        if(b <= l || r <= a){
            return;
        }
        if(a <= l && r <= b) {
            lazy[k] = x;
            lazyFlag[k] = true;
            eval(k, l, r);
        }
        else {
            range(a, b, x, 2*k+1, l, (l+r)/2);
            range(a, b, x, 2*k+2, (l+r)/2, r);
            node[k] = min(node[2*k+1],node[2*k+2]);
        }
    }
    V query(int a, int b, int k=0, int l=0, int r=-1) {
        if(r < 0) r = n;
        eval(k, l, r);
        if(b <= l || r <= a){
            return INT_MAX;
        }
        if(a <= l && r <= b){
            return node[k];
        }
        V vl = query(a, b, 2*k+1, l, (l+r)/2);
        V vr = query(a, b, 2*k+2, (l+r)/2, r);
        return min(vl,vr);
    }
};

int main()
{
    int n,q;
    cin >> n >> q;
    segtree<int> sg(vector<int>(n,INT_MAX));
    rep(i,q){
        int k;
        cin >> k;
        if(k){
            int x;
            cin >> x;
            cout << sg.query(x,x+1) << "\n";
        }else{
            int x,y,z;
            cin >> x >> y >> z;
            sg.range(x,y+1,z);
        }
    }
}


#include <stdio.h>

int main() {
    char str[100];
    scanf("%s", str);
    if (str[3] == '7') {
        str[3] = '8';
    }
    printf("%s\n", str);
    return 0;
}

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

#define int long long int

int32_t main()
{
    string str;
    cin>>str;
    cout<<2018<<str.substr(str.find('7')+1)<<endl;
}


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

int main() {
  string A,B,C; cin >> A >> B >> C;
  int a,b,c;
  a = A.size();
  b = B.size();
  c = C.size();
  char s,t,u,v;
  s = A.at(a-1);
  t = B.at(0);
  u = B.at(b-1);
  v = C.at(0);
  if (s == t && v == u){
    cout << "YES";
  }
  else cout << "NO";
}

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

int main() {
    int N,M,X,Y;
    cin >> N >> M >> X >> Y;
    vector<int>x(N);
    vector<int>y(M);
    for(int i = 0; i < N; i++) {
        cin >> x[i];
    }
    for(int i = 0; i < M; i++) {
        cin >> y[i];
    }
    sort(x.begin(),x.end());
    sort(y.begin(),y.end());
    if(x[N-1] < y[0]) {
        if(y[0] > X && x[N-1] < Y) {
            cout << "No War" << endl;
            return 0;
        }
    }
    cout << "War" << endl;
}


#include <iostream>
#include <cstdlib>

using namespace std;

typedef struct node {
    int key;
    struct node *parent;
    struct node *left;
    struct node *right;
} Tree;

Tree *root, *nil;

void insert(int key);
Tree *findNode(Tree *node, int key);
bool isFindTree(Tree *node, int key);
Tree *findMaximumTree(Tree *node);
Tree *treeSuccessor(Tree *node);
void treeDelete(Tree *z);
void preorder(Tree *node);
void inorder(Tree *node);

int main() {
    int n, key, i;
    string command;
    cin >> n;
    for (i = 0; i < n; i++) {
        cin >> command;
        if (command[0] == 'i') {
            cin >> key;
            insert(key);
        }
        else if (command[0] == 'd') {
            cin >> key;
            treeDelete(findNode(root, key));
        }
        else if (command[0] == 'f') {
            cin >> key;
            if (isFindTree(root, key)) {
                cout << "yes" << endl;
            } else {
                cout << "no" << endl;
            }
        }
        else if (command[0] == 'p') {
            inorder(root); cout << endl;
            preorder(root); cout << endl;
        }
    }

    return 0;
}

void insert(int key) {
    Tree *y = nil;
    Tree *x = root;
    Tree *z = (struct node*)malloc(sizeof(struct node));
    z->key = key;
    z->left = nil;
    z->right = nil;
    while (x != nil) {
        y = x;
        if (key < x->key) {
            x = x->left;
        } else {
            x = x->right;
        }
    };
    z->parent = y;
    if (y == nil) {
        root = z;
        return;
    }
    if (key <= y->key) {
        y->left = z;
    } else {
        y->right = z;
    }
}

Tree *findNode(Tree *node, int key) {
    Tree* target = node;
    while (target != nil && target->key != key) {
        if (key < target->key) {
            target = target->left;
        } else {
            target = target->right;
        }
    }
    return target;
}

bool isFindTree(Tree *node, int key) {
    return findNode(node, key) != nil;
}

Tree *findMaximumTree(Tree *node) {
    while (node->left != nil) {
        node = node->left;
    }
    return node;
}

Tree *treeSuccessor(Tree *node) {
    if (node->right != nil) {
        return findMaximumTree(node->right);
    }
    Tree *y = node->parent;
    while (y != nil && node == y->right) {
        node = y;
        y = y->parent;
    }
    return y;
}

void treeDelete(Tree *z) {
    Tree *y, *key;
    if (z->left == nil || z->right == nil) y = z;
    else y = treeSuccessor(z);
    if (y->left != nil) {
        key = y->left;
    } else {
        key = y->right;
    }
    if (key != nil) {
        key->parent = y->parent;
    }
    if (y->parent == nil) {
        root = key;
    } else {
        if (y == y->parent->left) {
            y->parent->left = key;
        } else {
            y->parent->right = key;
        }
    }
    if (y != z) {
        z->key = y->key;
    }
}

void preorder(Tree *node) {
    if (node == nil) return;
    cout << " " << node->key;
    preorder(node->left);
    preorder(node->right);
}

void inorder(Tree *node) {
    if (node == nil) return;
    inorder(node->left);
    cout << " " << node->key;
    inorder(node->right);
}

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;

#define N 600000
#define NIL -1

//array
int key[N];
int parent[N];
int leftc[N];
int rightc[N];
//root
int root=NIL;

//function insert
void insert(int z){
  int x,y;

  y=NIL;
  x=root;

  while(x!=NIL){
    y=x;
    if(key[z]<key[x]) x=leftc[x];
    else x=rightc[x];
  }

  parent[z]=y;

  if(y==NIL) root=z;

  else if(key[z]<key[y]) leftc[y]=z;

  else rightc[y]=z;

}

//function search
int find(int now,int data){
  if(now==NIL ||data==key[now]) return now;

  if(data<key[now]) return find(leftc[now],data);
  else return find(rightc[now],data);
}

int treeminimum(int x){
  while(leftc[x]!=NIL){
    x=leftc[x];
  }
  return x;
}

int treesuccessor(int x){
  int y;
  if(rightc[x]!=NIL) return treeminimum(rightc[x]);
  y=parent[x];
  while(y!=NIL &&x==rightc[x]){
    x=y;
    y=parent[y];
  }
  return y;
}

//function delete
void treedelete(int z){
  int x,y;
  if(leftc[z]==NIL ||rightc[z]==NIL) y=z;
  else y=treesuccessor(z);

  if(leftc[y]!=NIL) x=leftc[y];
  else x=rightc[y];

  if(x!=NIL) parent[x]=parent[y];
  if(parent[y]==NIL) root=x;
  else if(y==leftc[parent[y]]) leftc[parent[y]]=x;
  else rightc[parent[y]]=x;

  if(y!=z) key[z]=key[y];

}

//print
void inorderprint(int now){
  if(now!=NIL){
    inorderprint(leftc[now]);
    printf(" %d",key[now]);
    inorderprint(rightc[now]);
  }
}

void preorderprint(int now){
  if(now!=NIL){
    printf(" %d",key[now]);
    preorderprint(leftc[now]);
    preorderprint(rightc[now]);
  }
}

int main(){

  int n,i,j,data;
  int z=0;
  int del;
  char str[10];
  //initialize array
  for(i=0;i<N;i++){
    parent[i]=NIL;
    key[i]=NIL;
    rightc[i]=NIL;
    leftc[i]=NIL;
  }
  //load n
  scanf("%d",&n);
  //load commnad and key
  for(i=0;i<n;i++){
    scanf("%s",str);
    if(strcmp(str,"insert")==0) {
      scanf("%d",&data);
      key[z]=data;
      insert(z);
      z++;
    }
    else if(strcmp(str,"find")==0){
      scanf("%d",&data);
      if(find(root,data)!=NIL) printf("yes\n");
      else printf("no\n");
    }
    else if(strcmp(str,"delete")==0){
      scanf("%d",&data);
      del=find(root,data);
      treedelete(del);
    }
    else if(strcmp(str,"print")==0){
      inorderprint(root);
      cout <<endl;
      preorderprint(root);
      cout <<endl;
    }
  }

  return 0;
}