/*
    Author:zeke

    pass System Test!
    GET AC!!
*/
#include <iostream>
#include <queue>
#include <vector>
#include <iostream>
#include <vector>
#include <string>
#include <cassert>
#include <algorithm>
#include <functional>
#include <cmath>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <map>
#include <iomanip>
#include <math.h>
#include <utility>
#include <stack>
#include <bitset>
using ll = long long;
using ld = long double;
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define rep3(var, min, max) for (ll(var) = (min); (var) < (max); ++(var))
#define repi3(var, min, max) for (ll(var) = (max)-1; (var) + 1 > (min); --(var))
#define Mp(a, b) make_pair((a), (b))
#define F first
#define S second
#define Icin(s) \
    ll(s);      \
    cin >> (s);
#define Scin(s) \
    ll(s);      \
    cin >> (s);
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;
}
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef vector<V> VV;
typedef vector<P> VP;
ll mod = 1e9 + 7;
unsigned long long MOD = 1e9 + 7;
ll INF = 1e18;

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

    int n; // 頂点数
    cin >> n;
    ll m;
    cin >> m;
    m = n - 1 + m; // 辺数

    vector<pair<int, int>> edge(n); // first -> second
 //   cout << "hello" << endl;
    vector<vector<int>> eList(n); // 隣接リスト
    vector<int> inDeg(n, 0);   // 入次数
    rep(i,m)
    {
        ll A,B;
        cin >> A >> B;
        A--;
        B--;
        eList[A].push_back(B);
        inDeg[B]++;
    }
    queue<int> zeroQue; // 入次数 0 の頂点でまだ取られていないものを入れる
    for (int i = 0; i < n; i++)
    {
        if (inDeg[i] == 0){
            zeroQue.push(i);
        }
    }
 //   cout << "hello" << endl;
    V Result(n);
    vector<int> result; // トポロジカルソートの結果を入れる
    while (!zeroQue.empty())
    {
        int u = zeroQue.front(); // 入次数 0 の頂点 u を 1 つ取る
        zeroQue.pop();

        result.push_back(u);
        for (int v : eList[u])
        {               // u を始点とする辺すべてについて、
            inDeg[v]--; // 終点の入次数を減らす
            if (inDeg[v] == 0){
                zeroQue.push(v); // 入次数が 0 になったら取ることができる
                Result[v] = u+1;
             //   cout<<u<<endl;
            }
        }
    }
  //  cout << "hello" << endl;
    rep(i, n)
    {
        cout << Result[i] << endl;
    }
}

//#include <bits/stdc++.h>
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
//#include "boost/multiprecision/cpp_int.hpp"
//typedef boost::multiprecision::cpp_int ll;
typedef long double dd;
#define i_7 (ll)(1E9+7)
//#define i_7 998244353
#define i_5 i_7-2
ll mod(ll a){
    ll c=a%i_7;
    if(c>=0)return c;
    return c+i_7;
}
typedef pair<ll,ll> l_l;
ll inf=(ll)1E16;
#define rep(i,l,r) for(ll i=l;i<=r;i++)
#define pb push_back
ll max(ll a,ll b){if(a<b)return b;else return a;}
ll min(ll a,ll b){if(a>b)return b;else return a;}
void Max(ll &pos,ll val){pos=max(pos,val);}//Max(dp[n],dp[n-1]);
void Min(ll &pos,ll val){pos=min(pos,val);}
void Add(ll &pos,ll val){pos=mod(pos+val);}
dd EPS=1E-9;
#define fastio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define fi first
#define se second
////////////////////////////

ll n,m;
#define N 1005
vector<ll>v[N];
ll used[N];

vector<ll>row;
vector<ll>cyc;
bool dfs(ll k){
    if(cyc.size()!=0)return 0;
    for(auto x:v[k]){
        if(used[x]==1){
            if(cyc.size()!=0)return 0;
            bool fl=false;
            rep(i,0,row.size()-1){
                if(fl)cyc.pb(row[i]);
                else if(row[i]==x){
                    cyc.pb(row[i]);fl=true;
                }
            }
            return 0;
        }else if(used[x]==0){
            row.pb(x);
            used[x]=1;
            dfs(x);
            row.pop_back();
        }
    }
    return 0;
}

int main(){fastio
    cin>>n>>m;
    rep(i,1,m){
        ll a,b;cin>>a>>b;
        v[a].pb(b);
    }
    rep(i,1,n){
        if(used[i]==0){
            row.pb(i);
            dfs(i);
            rep(j,1,n){
                if(used[j]==1)used[j]=2;
            }
        }
    }
    if(cyc.size()==0){
        cout<<-1;return 0;
    }

    rep(i,1,n)sort(v[i].begin(),v[i].end());
    bool ok[n+1];memset(ok,false,sizeof(ok));
    while(1){
        bool flag=true;
        vector<ll>newcyc;
        ll cs=cyc.size();
        rep(i,0,cs-1){
            if(!flag)break;
            ll top=cyc[i];
            if(ok[top])continue;
            for(ll qj=(i+cs-1);qj>=i+2;qj--){
                ll j=qj%cs;
                if(*lower_bound(v[top].begin(),v[top].end(),cyc[j])==cyc[j]){

                    if(i<j){
                        rep(k,0,i)newcyc.pb(cyc[k]);
                        rep(k,j,cs-1)newcyc.pb(cyc[k]);
                    }else{
                        rep(k,j,i)newcyc.pb(cyc[k]);
                    }
                    ok[top]=true;
                    flag=false;
                    break;
                }
            }
        }
        if(flag){
            cout<<cyc.size()<<endl;
            for(auto x:cyc)cout<<x<<endl;
            return 0;
        }else{
            cyc.clear();
            for(auto x:newcyc)cyc.pb(x);
        }
    }
    return 0;
}


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

template <typename TYPE>
void print_vec(const vector<TYPE>& v){
	for(int i=0; i<v.size(); i++){
		cout << v[i] << " ";
	}
	cout << endl;
}

template <typename TYPE>
void print_vec2(const vector<vector<TYPE>>& v){
  cout << endl; cout << "       ";
  for(int i=0; i<v[0].size(); i++) cout << i << "   ";
  cout << endl;
	for(int i=0; i<v.size(); i++){
		cout << "i=" << i << ":   ";
		for(int j=0; j<v[i].size(); j++){
			if(v[i][j] == 0) cout << "\x1B[0m" << v[i][j] << "   ";
      else cout << "\x1B[31m" << v[i][j] << "   ";//https://stackoverrun.com/ja/q/12618775
		}
		cout << "\x1B[0m" << endl;
	}
}

using P = pair<int ,int>;
using LP = pair<ll, int>;
int main(){
	int N;
	ll M;
	cin >> N >> M;

	vector<ll> divs;
	for(ll i=1; i*i<=M;  i++){
		if(M%i == 0){
			divs.push_back(i);
			if(i*i != M)divs.push_back(M/i);
		}
	}
	sort(divs.begin(), divs.end());
	ll ans = 0;
	for(int i=0; i<(int)divs.size(); i++){
		if(M/divs[i] >= N) ans = max(ans, divs[i]);
	}
	// cout << " divs: "; print_vec(divs);
	cout << ans << endl;

	// vector<LP> prime_divisors;
	// ll m = M;
	// //エラトステネスの篩
	// for(ll p=2; p*p<m; p++){
	// 	if(m % p != 0) continue;
	// 	int cnt = 0;
	// 	while(m%p == 0){
	// 		m /= p;
	// 		cnt++;
	// 	}
	// 	prime_divisors.emplace_back(p, cnt);
	// }
	// if(m != 1) prime_divisors.emplace_back(m, 1);

	// vector<ll> divs;// Mの因数となるモノを格納
	// ll ans = 0;

}

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

int main() {
  ll n, m; cin >> n >> m;
  ll res = 1;
  for (ll i = 1; i*i <= m; i++) {
    if (m%i == 0) {
      if (i*n <= m) res = max(res, i);
      if (m/i != i) {
        if (m/i*n <= m) res = max(res, m/i);
      }
    }
  }
  cout << res << endl;
}


#include <bits/stdc++.h>

using namespace std;

#define FOR(i, j, k) for(int i = j; i < k; ++i)
#define rep(i, j) FOR(i, 0, j)
#define repr(i, j) for(int i = j; i >= 0; --i)
#define INF (1 << 30)
#define MOD 1e9 + 7

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;

int dp[1001][1001];

int main() {
	string s1, s2;
	cin >> s1 >> s2;
	int A = s1.size(), B = s2.size();
	rep(i, A + 1) dp[i][0] = i; rep(i, B + 1) dp[0][i] = i;
	FOR(i, 1, A + 1) FOR(j, 1, B + 1) {
		int cost = (s1[i - 1] == s2[j - 1]) ? 0 : 1;
		int tmp = min(dp[i - 1][j] + 1, dp[i][j - 1] + 1);
		dp[i][j] = min(tmp, dp[i - 1][j - 1] + cost);
	}
	cout << dp[A][B] << endl;
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
ll GetDigit(ll num){ return log10(num)+1; } //numの桁数を求める

int main()
{
    int n,l;
    cin >> n >> l;
    vector<int> taste(n);
    rep(i,n){
        taste[i] = l + i;
    }
    int total = 0;
    rep(i,n) total += taste[i];
    if(l >= 0) total -= taste[0];
    else if(n+l <= 0) total -= taste[n-1];
    cout << total << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=200000;
int sq[555555],d1,d2,xx,yy,p[555555],cnt,n,col[555555],msk[555555],pos,sum[5],id;
int cnt1,cnt2,cpt,pa[555555],val[555555];
pair<int,int> v1[1111],v2[1111];
int root(int x)
{
	if (pa[x]==x) return x;
	int rt=root(pa[x]);
	val[x]^=val[pa[x]];
	return pa[x]=rt;
}
void Union(int x,int y)
{
	int rx=root(x),ry=root(y);
	if (rx==ry) return;
	pa[rx]=ry;val[rx]=(1^val[x]^val[y]);
}
void Init()
{
	memset(sq,-1,sizeof(sq));
	for (int i=0;i<=500;i++) sq[i*i]=i;
}
int get(int x,int y)
{
	return (x-1)*n+y;
}
bool In(int x,int y)
{
	return (x>=1 && x<=n && y>=1 && y<=n);
}
void addedge(int x,int y)
{
	Union(x,y);
}
int main()
{
	scanf("%d%d%d",&n,&d1,&d2);
	Init();n<<=1;
	for (int k=0;k<=n;k++)
	{
		if (d1-k*k>=0 && ~sq[d1-k*k])
		{
			v1[++cnt1]=make_pair(k,sq[d1-k*k]);
		}
		if (d2-k*k>=0 && ~sq[d2-k*k])
		{
			v2[++cnt2]=make_pair(k,sq[d2-k*k]);
		}
	}
	for (int i=1;i<=n*n;i++)
	{
		pa[i]=i;
		val[i]=0;
	}
	for (int i=1;i<=n;i++)
	{
		for (int j=1;j<=n;j++)
		{
			id=get(i,j);
			for (int k=1;k<=cnt1;k++)
			{
				xx=i+v1[k].first;yy=j+v1[k].second;
				if (In(xx,yy)) addedge(id,get(xx,yy));
				xx=i-v1[k].first;yy=j+v1[k].second;
				if (In(xx,yy)) addedge(id,get(xx,yy));
			}
		}
	}
	for (int i=1;i<=n*n;i++)
	{
		root(i);
		col[i]=val[i];
	}
	for (int i=1;i<=n*n;i++) msk[i]=col[i];
	for (int i=1;i<=n*n;i++)
	{
		pa[i]=i;
		val[i]=0;
	}
	for (int i=1;i<=n;i++)
	{
		for (int j=1;j<=n;j++)
		{
			id=get(i,j);
			for (int k=1;k<=cnt2;k++)
			{
				xx=i+v2[k].first;yy=j+v2[k].second;
				if (In(xx,yy)) addedge(id,get(xx,yy));
				xx=i-v2[k].first;yy=j+v2[k].second;
				if (In(xx,yy)) addedge(id,get(xx,yy));
			}
		}
	}
	for (int i=1;i<=n*n;i++)
	{
		root(i);
		col[i]=val[i];
	}
	for (int i=1;i<=n*n;i++) msk[i]|=(col[i]<<1);
	for (int i=1;i<=n*n;i++) sum[msk[i]]++;
	for (int i=0;i<4;i++)
	{
		if (sum[i]>=n*n/4)
		{
			pos=i;
			break;
		}
	}
	for (int i=1;i<=n*n;i++)
	{
		if (cnt==n*n/4) break;
		if (msk[i]==pos)
		{
			printf("%d %d\n",(i-1)/n,(i-1)%n);
			cnt++;
		}
	}
	return 0;
}

#include <cstdio>
typedef long long ll;
#define ri register int
namespace io {
	const int SIZE = (1 << 21) + 1;
	char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr;
	// getchar
	#define gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
	// print the remaining part
	inline void flush () {
		fwrite (obuf, 1, oS - obuf, stdout);
		oS = obuf;
	}
	// putchar
	inline void putc (char x) {
		*oS ++ = x;
		if (oS == oT) flush ();
	}
	// input a signed integer
	template <class I>
	inline void gi (I &x) {
		for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;
		for (x = 0; c <= '9' && c >= '0'; c = gc()) x = (x << 1) + (x << 3) + (c & 15); x *= f;
	}
	// print a signed integer
	template <class I>
	inline void print (I x) {
		if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x;
		while (x) qu[++ qr] = x % 10 + '0',  x /= 10;
		while (qr) putc (qu[qr --]);
	}
	//no need to call flush at the end manually!
	struct Flusher_ {~Flusher_(){flush();}}io_flusher_;
}
using io :: gi;
using io :: putc;
using io :: print;
const int N=605;
int n,m,col1[N][N],col2[N][N],tag[2][2];
inline void solve(ri d,int col[N][N]){
	ri i,j,k;
	k=0;
	while(d%4==0) d>>=2,++k;
	if(d&1){
		for(i=0;i<m;++i)
			for(j=0;j<m;++j)
				if((i>>k)+(j>>k)&1)
					col[i][j]=1;
	}
	else{
		for(i=0;i<m;++i)
			for(j=0;j<m;++j)
				if((i>>k)&1)
					col[i][j]=1;
	}
}
int main(){
	ri d1,d2,i,j,x,y,cnt;
	gi(n);gi(d1);gi(d2);
	m=n<<1;
	solve(d1,col1);
	solve(d2,col2);
	for(i=0;i<m;++i)
		for(j=0;j<m;++j)
			++tag[col1[i][j]][col2[i][j]];
	for(i=0;i<=1;++i)
		for(j=0;j<=1;++j)
			if(tag[i][j]>=n*n){
				cnt=0;
				for(x=0;x<m;++x)
					for(y=0;y<m;++y)
						if(col1[x][y]==i&&col2[x][y]==j){
							print(x);putc(' ');print(y);putc('\n');
							if(++cnt==n*n) 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>;
int main()
{
    string s;
    cin >> s;
    map<char, int> dic;
    rep(i, 26) dic[i + 'a'] = 0;
    int n = s.size();
    rep(i, n) dic[s[i]]++;
    rep(i, 26)
    {
        if (dic['a' + i] < 1)
        {
            cout << char('a' + i) << endl;
            return 0;
        }
    }
    cout << "None" << endl;
    return 0;
}

#include<iostream>
#include<set>
#include<map>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long li;
#define repa(i,a,n) for(int i=(a);i<(n);i++)
#define yn(i) print((i)?"Yes":"No")
#define rep(i,n) for(int i=0;i<(n);i++)

#define df 0
template<class T> void print(const T& t){ cout << t << "\n"; }
template<class T, class... Ts> void print(const T& t, const Ts&... ts) { cout << t; if (sizeof...(ts)) cout << " "; print(ts...); }
// pair 型
template<class S,class T>
std::ostream& operator<<(std::ostream& os, const pair<T,S>& p){
  cout << "(" << p.first << "," << p.second << ")";
  return os;
}

// Container コンテナ型, map以外
template< template<class ...> class Ctn,class T>
std::ostream& operator<<(std::ostream& os,const Ctn<T>& v){
  auto itr=v.begin();
  while(itr!=v.end()){
    if(itr!=v.begin())cout << " ";
    cout << *(itr++);
  }
  return os;
}
// map 型
template<class S,class T>
std::ostream& operator<<(std::ostream& os, const map<S,T>& mp){
  for(const pair<S,T>& x: mp){
    cout << "[" << x.first << "]=" << x.second << " ";
  }
  return os;
}

int main(){
  set<char> st;
  rep(i,26) st.insert('a'+i);
  string s; cin >>s;
  int n=s.size();
  rep(i,n) st.erase(s[i]);
  if(st.empty()){
    print("None");
  }else{
    print(*st.begin());
  }
}


#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <math.h>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <math.h>
#include <iostream>
#include <random>
#include<map>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <cassert>
#include<fstream>
#include <unordered_map>
#include <cstdlib>
#include <complex>
using namespace std;
#define Ma_PI 3.141592653589793
#define eps 0.00000001
#define LONG_INF 30000000000000LL
#define GOLD 1.61803398874989484820458
const long long MAX_MOD = 1e6 + 3;
#define REP(i,n) for(long long i = 0;i < n;++i)
#define seg_size 524288
long long powering(long long now,long long now_go){
	long long ans = 1;
	while (now_go != 0) {
		if (now_go % 2 == 1) {
			ans *= now;
			ans %= MAX_MOD;
		}
		now *= now;
		now %= MAX_MOD;
		now_go /= 2;
	}
	return ans;
}
long long inv(long long now) {
	return powering(now, MAX_MOD - 2LL);
}
long long d;
long long geko[32] = {};
long long bobo[30] = {};
long long solve(long long now, long long now_digit, long long L) {
	if (L / 2 == now_digit) {
		if (L % 2 == 0) {
			if (d == now) {
				return 1;
			}
			else {
				return 0;
			}
		}
		else {
			if (d == now) {
				return 10LL;
			}
			else {
				return 0LL;
			}
		}
	}
	long long ans = 0;
	for (long long i = -9; i <= 9; ++i) {
		long long tea = geko[L - now_digit - 1] - geko[now_digit];
		tea *= i;
		tea += now;
		if (tea >= 0) {
			if (tea % geko[now_digit + 1] == d % geko[now_digit + 1]) {
				//ok
				long long geko = solve(tea, now_digit + 1, L);
				if (geko == 0) continue;
				if (now_digit == 0) {
					bobo[i + 9]--;
				}
				ans += geko * bobo[i+9];
				if (now_digit == 0) {
						bobo[i + 9]++;
				}
			}
		}
		else {
			tea += geko[18];
			if (tea % geko[now_digit + 1] == d % geko[now_digit + 1]) {
				long long geko = solve(tea - bobo[18], now_digit + 1, L);
				if (geko == 0)continue;
				if (now_digit == 0) {
						bobo[i + 9]--;
				}
				ans += geko * bobo[i + 9];
				if (now_digit == 0) {
						bobo[i + 9]++;
				}
			}
			tea -= geko[18];
		}
	}
	return ans;
}
int main() {
	for (int i = 0; i <= 9; ++i) {
		for (int q = 0; q <= 9; ++q) {
			bobo[i - q + 9]++;
		}
	}
	geko[0] = 1;
	for (int i = 1; i <= 18; ++i) {
		geko[i] = 10LL * geko[i-1];
	}
	cin >> d;
	if (d % 9 != 0) {
		cout << 0 << endl;
		return 0;
	}
	int len = to_string(d).length();
	long long ans = 0;
	for (long long digit = len; digit <= len * 2; ++digit) {
		ans += solve(0, 0, digit);
	}
	cout << ans << endl;
}

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

typedef long long ll;
#define REP(i,n) for(int i=0,_n=(int)(n);i<_n;++i)
#define ALL(v) (v).begin(),(v).end()
#define CLR(t,v) memset(t,(v),sizeof(t))
template<class T1,class T2>ostream& operator<<(ostream& os,const pair<T1,T2>&a){return os<<"("<<a.first<<","<<a.second<< ")";}
template<class T>void pv(T a,T b){for(T i=a;i!=b;++i)cout<<(*i)<<" ";cout<<endl;}
template<class T>void chmin(T&a,const T&b){if(a>b)a=b;}
template<class T>void chmax(T&a,const T&b){if(a<b)a=b;}

ll nextLong() { ll x; scanf("%lld", &x); return x;}

ll ten[20];

ll val = 0;
void f(int L, int R, ll D, ll cur) {
  if (L > R) {
    if (D == 0) val += cur;
    return;
  }

  if (abs(D) > ten[R+1]) return;
  // cout << L << " " << R << " " << D << endl;

  if (L == R) {
    f(L+1, R-1, D, cur * 10);
  } else {

    for (int d = -9; d <= 9; d++) {
      ll D2 = D - (ten[R] - ten[L]) * d;
      ll ways = 10 - abs(d);
      if (L == 0) ways = 9 - abs(d);
      f(L+1, R-1, D2, cur * ways);
    }
  }

}

int main2() {
  ll D = nextLong();

  ten[0] = 1;
  REP(i, 19) ten[i+1] = ten[i] * 10;

  ll ans = 0;
  for (int k = 1; k <= 18; k++) {
    val = 0;
    f(0, k-1, D, 1);
    ans += val;
  }
  cout << ans << endl;
  return 0;
}

int main() {

#ifdef LOCAL
  for (;!cin.eof();cin>>ws)
#endif
    main2();
  return 0;
}


#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, s, n) for (ll i = s; i < (ll)(n); i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define pb push_back
#define COUT(x) cout << (x) << endl
#define COUTF(x) cout << setprecision(15) << (x) << endl
#define ENDL cout << endl
#define DF(x) x.erase(x.begin())  // 先頭文字削除
#define ALL(x) x.begin(), x.end()
#define SORT(x) sort(ALL(x))
#define REVERSE(x) reverse(ALL(x))
#define init() \
  cin.tie(0);  \
  ios::sync_with_stdio(false)
#define debug(x) cerr << "[debug] " << #x << ": " << x << endl;
#define debugV(v)                       \
  cerr << "[debugV] " << #v << ":";     \
  rep(i, v.size()) cerr << " " << v[i]; \
  cerr << endl;
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using P = pair<ll, ll>;
constexpr ll INF = 0x3f3f3f3f3f3f3f3f;
constexpr double PI = 3.141592653589793238462643383279;
ll getDigit(ll x) {
  return x == 0 ? 1 : log10(x) + 1;
}

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

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

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

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

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

signed main() {
  init();
  ll N;
  cin >> N;
  string S;
  cin >> S;
  ll count_W = 0;
  ll count_E = 0;

  rep(i, S.size()) {
    if (S[i] == 'W') {
      count_W++;
    } else {
      count_E++;
    }
  }

  ll t_W = 0;
  ll t_E = 0;
  ll mini = INF;
  rep(i, S.size()) {
    if (S[i] == 'W') {
      ll count = t_W + count_E - (t_E + 0);
      if (count < mini) {
        mini = count;
      }
    } else {
      ll count = t_W + count_E - (t_E + 1);
      if (count < mini) {
        mini = count;
      }
    }

    if (S[i] == 'W') {
      t_W++;
    } else {
      t_E++;
    }
  }
  COUT(mini);
  return 0;
}

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

vector<int> input(int N){
    vector<int> vec;
    int temp = 0;
    for (int i = 0; i < N; i++ )    {
        cin >> temp;
        vec.push_back(temp);
    }
    return vec;
}

int main(){
    int N; // 100
    cin >> N;
    string str;
    cin >> str;
    vector<int> east_num(N);
    vector<int> west_num(N);

    if (str.at(0)=='E'){
        east_num.at(0) = 1;
        west_num.at(0) = 0;
    }else{
        east_num.at(0) = 0;
        west_num.at(0) = 1;
    }
    for (int i =0; i <N-1; i++)    {
        if(str.at(i+1) == 'E'){
            east_num.at(i+1) = east_num.at(i)+1;
            west_num.at(i+1) = west_num.at(i);
        }
        else{
            east_num.at(i+1) = east_num.at(i);
            west_num.at(i+1) = west_num.at(i)+1;
        }
    }
    int min_val = east_num.at(N-1) - east_num.at(0);
    for (int i =0; i <N-1; i++)    {
        int value = east_num.at(N-1) - east_num.at(i+1) + west_num.at(i);
        if (value < min_val) min_val = value;
    }
  /*
   for (int i =0; i <N; i++)    {
      cout << east_num.at(i) << " ";
    }
      cout << endl;
  for (int i =0; i <N; i++)    {
      cout << west_num.at(i) << " ";
    }
      cout << endl;
   */
    cout << min_val << endl;
}


#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
#define ALL(x) (x).begin(), (x).end()
#define REP(i ,n) for(int i = 0; i < (int)(n); i++)
#define pb push_back
typedef vector<int>vint;
typedef vector<ll>vll;
template<typename T> istream &operator>>(istream &is, vector<T> &vec){ for (auto &v : vec) is >> v; return is; }
template<typename A,typename B>inline bool chmin(A &a,const B &b){if(a>b){a=b;return true;}else{return false;}}
template<typename A,typename B>inline bool chmax(A &a,const B &b){if(a<b){a=b;return true;}else{return false;}}

int main()
{
    int N,Y;
    cin >> N >> Y;
    int yen[] = {10000, 5000, 1000};
    int total,sub;
    REP(i,2001){
        REP(j,2001){
            total = yen[0]*i + yen[1]*j;
            sub = Y-total;
            if(0 != sub%yen[2] || sub < 0)break;
            sub /= yen[2];
            if(N == (i+j+sub)){
                cout << i << " " << j << " " << sub << endl;
                return 0;
            }
        }
    }
    cout << -1 << " " << -1 << " " << -1 << endl;
}

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

#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
#define point pair<int,int>

int N, K;
int mem[200010];

signed main()
{
    cin >> N >> K;
    rep(i, N)cin >> mem[i];
    rep(j, min(K, 100LL))
    {
        int tmp[400010] = {0};
        rep(i, N)
        {
            tmp[max(i - mem[i], 0LL)]++;
            tmp[i + mem[i] + 1]--;
        }
        rep(i, N)if(i)
            tmp[i] += tmp[i - 1];

        rep(i, N)mem[i] = tmp[i];
    }

    rep(i, N)
    {
        if(i)cout << " ";
        cout << mem[i];
    }cout << endl;

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <time.h>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <iterator>
#include <sstream>
#include <string>
#include <bitset>
using namespace std;
#define FOR(I,F,N) for(int I = F; I < (int)(N); I++)
#define rep(i, n) FOR(i, 0, n)
#define FIN(V) cout<<V<<endl
#define pb push_back
#define INF (1 << 30)
typedef pair<int, int> P;
typedef long long ll;
typedef priority_queue<int> pq;
int StrToInt(string);
string IntToStr(int);
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

int main(void){
	map<string, bool> memo;
	string s;
	int n, m;
	cin >> n;
	rep(i, n){
		cin >> s;
		memo[s] = 1;
	}
	cin >> m;
	int cnt = 0;
	rep(i, m){
		cin >> s;
		if(memo[s])cnt++;
	}
	FIN(cnt);

	return 0;
}

int StrToInt(string s){
	stringstream ss;
	ss << s;
	int val;
	ss >> val;
	return val;
}
string IntToStr(int i){
	stringstream ss;
	ss << i;
	return ss.str();
}

#include<bits/stdc++.h>
using namespace std;
int main(){
  int N,Q;string S;cin>>N>>Q>>S;
  vector<int>V(N);
  int U=0;
  for(int X=0;X<N;X++){
    if(X!=0&&S[X]=='C'&&S[X-1]=='A'){
      U++;
    }
    V[X]=U;
  }
  for(int X=0;X<Q;X++){
    int A,B;cin>>A>>B;
    if(A!=1&&V[A-2]!=V[A-1]){
      cout<<V[B-1]-V[A-1]<<endl;
    }
    else{
      cout<<V[B-1]-V[A-1]<<endl;
    }
  }
}


#include <iostream>
#include <fstream>
#include <list>
using namespace std;

class game{
	int length;
	int cur;
	int *r,*l;
	int r_score,l_score;
public:
	game(int _l){
		length = _l;
		r = new int [length];
		l = new int [length];
		r_score = 0;
		l_score = 0;
		cur = 0;
	}
	~game(){
		delete [] r;
		delete [] l;
	}
	void run();
	void insert(int,int);
};

void game::run(){
	for(int i = 0;i < length;i++){
		if(r[i] < l[i]){
			l_score += r[i] + l[i];
		}else if(r[i] > l[i]){
			r_score += r[i] + l[i];
		}else{
			l_score += l[i];
			r_score += r[i];
		}
	}
	cout<<l_score<<" "<<r_score<<endl;
}

void game::insert(int l_card,int r_card){
	r[cur] = r_card;
	l[cur] = l_card;
	cur++;
}

int main(){
	list<game*> lst;
	while(1){
		int read;
		cin>>read;
		if(read == 0){
			break;
		}
		game *g = new game(read);
		for(int i = 0; i < read;i++){
			int r_card,l_card;
			cin>>l_card>>r_card;
			//cout<<l_card<<","<<r_card<<endl;
			g->insert(l_card,r_card);
		}
		lst.push_back(g);
	}

	for(auto it = lst.begin();it != lst.end();it++){
		(*it)->run();
		delete (*it);
	}
	return 0;
}

#include <iostream>
using namespace std;

int main() {
int n;
cin>>n;
while(n!=0){
	int ap=0,bp=0;
	int a,b;
	for(int k=0;k<n;k++){
		cin>>a>>b;
		if(a>b){
			ap=ap+a+b;
		}
		else if(a<b){
			bp=bp+a+b;
		}
		else{
			ap=ap+a;
			bp=bp+a;
		}
	}
	cout<<ap<<" "<<bp<<endl;
	cin>>n;
}
	return 0;
}


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

using namespace std;
const int dx[] = {1,0,-1,0},dy[] = {0,1,0,-1};
#define INF 999999
#define rep(i,j) for(int i=0;i<(j);++i)
#define reps(i,j,k) for(int i=j;i<k;++i)
typedef long long ll;
typedef unsigned long long ull;
bool us[100100]={false};
int p[100100]={0};
int main(){
	/*Eratosthenes*/
	p[2] = 2;
	for(int i = 0; i < 100101 ; i+=2)us[i] = true;
	for(int i = 3; i < 100101; i+=2){
		if(us[i] == true)continue;
		else if(us[i] == false){
			p[i] = i;
			for(int j = i+i; j < 100101; j += i)us[j] = true;
		}
	}

	int n;

	while(scanf("%d",&n),n){
		for(int i = n; i >= 0; i--){
			if(!us[i] && !us[i-2]){
				printf("%d %d\n",p[i-2],p[i]);
				break;
			}
		}
	}
	return 0;
}

#include <iostream>
using namespace std;

const int MAX = 10000;
int plist[MAX + 1] = {};

void primeNumber(){

	//0 -> prime, 1 -> composite

	plist[0] = 1;
	plist[1] = 1;

	for(int i = 2; i <= MAX; ++i){
		if(plist[i] == 0){
			int c = i * 2;
			while(c <= MAX){
				plist[c] = 1;
				c += i;
			}
		}
	}
}

int main(){
	int n;

	primeNumber();

	while(cin >> n, n){
		int k;
		if(n % 2 == 0){
			k = n - 1;
		}else{
			k = n;
		}

		while(k >= 3){
			if(plist[k] == 0 && plist[k - 2] == 0){
				cout << k - 2 << " " << k << endl;
				break;
			}
			k -= 2;
		}
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using Graph=vector<vector<pair<int,int>>>;
#define INF 1000000000000000000
#define MOD 1000000007

int N;
vector<vector<ll>> a;
vector<bool> flag;
vector<ll> dp;

ll solve(int S){
  if(flag.at(S)){
    return dp.at(S);
  }
  flag.at(S)=true;

  ll ans=0;
  for(int i=0;i<N;i++){
    for(int j=i+1;j<N;j++){
      if((((S>>i)&1)&((S>>j)&1))==1){
        ans+=a.at(i).at(j);
      }
    }
  }

  for(int T=(S-1)&S;T>0;T=(T-1)&S){
    ans=max(ans,solve(T)+solve(S^T));
  }
  return dp.at(S)=ans;
}

int main(){
  cin>>N;
  a.resize(N);
  for(int i=0;i<N;i++){
    a.at(i).resize(N);
    for(int j=0;j<N;j++){
      cin>>a.at(i).at(j);
    }
  }

  flag.resize(1<<N,false);
  dp.resize(1<<N,0);
  cout<<solve((1<<N)-1)<<endl;
}

#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1e9 + 7;

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

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

    long long int r = 0, g = 0, b = 0, ans = 1;
    for(int i=0; i<n; i++){
        if(a[i] == r && a[i] == g && a[i] == b){ ans *= 3; r += 1; }
        else if(a[i] == r && a[i] == g){ ans *= 2; r += 1; }
        else if(a[i] == g && a[i] == b){ ans *= 2; g += 1; }
        else if(a[i] == r) r += 1;
        else if(a[i] == g) g += 1;
        else if(a[i] == b) b += 1;
        else ans = 0;

        ans %= MOD;
    }
    cout << ans << endl;
    return 0;
}


//Mahir Ratanpara (DA-IICT)
#include<bits/stdc++.h>
using namespace std;
#define Ff(i,a,n) for(i=a;i<n;i++)
#define Fr(i,a,n) for(i=a;i>n;i--)
#define ll long long
#define FAST ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define pb push_back
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
typedef pair<ll, ll>	pa;
typedef vector<ll>		vc;
typedef vector<pa>		vp;
typedef vector<vc>		vvc;
int mpow(int base, int exp);
void ipgraph(ll n,ll m);
void dfs(int u, int par);
const int mod = 1000000007;
const int N = 3e5, M = N;
//=======================

vc g[N];

int main() {
    FAST;
    ll t, i, j, k, p, q, r, x, y, u, v, n, m;
    cin>>n>>x>>t;
	y=n/x;
	if(n%x>0)
	y++;
	cout<<y*t;
    return 0;
}

int mpow(int base, int exp) {
  base %= mod;
  int result = 1;
  while (exp > 0) {
    if (exp & 1) result = ((ll)result * base) % mod;
    base = ((ll)base * base) % mod;
    exp >>= 1;
  }
  return result;
}

void ipgraph(ll n, ll m){
	int i, u, v;
	while(m--){
		cin>>u>>v;
		g[u-1].pb(v-1);
		g[v-1].pb(u-1);
	}
}

void dfs(int u, int par){
	for(int v:g[u]){
		if (v == par) continue;
		dfs(v, u);
	}
}


// TeamNichoen
// g++ -O2 -Wall -Wextra -Wshadow problemA.cpp

#include "bits/stdc++.h"

using namespace std;

typedef long long int LL;
typedef long long int ll;

#define FOR(i,a,b) for(LL i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)

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

int solve(vector<vector<int> > &mp,int sx,int sy,int gx,int gy,int dir,int dep){
    if(dep>=11){
        return 9999;
    }
    if(mp[sx+dx[dir]][sy+dy[dir]]==0)
    {
        while(mp[sx+dx[dir]][sy+dy[dir]]==0){
            sx+=dx[dir];
            sy+=dy[dir];
            if(sx==gx&&sy==gy){
                return 0;//goal
            }
        }

        if(mp[sx+dx[dir]][sy+dy[dir]]==1){
            mp[sx+dx[dir]][sy+dy[dir]]=0;
            int mini=99999;
            REP(i,4){
                int val=solve(mp,sx,sy,gx,gy,i,dep+1)+1;
                if(val<mini){
                    mini=val;
                }
            }
            mp[sx+dx[dir]][sy+dy[dir]]=1;
            return mini;
        }else{
            return 9999;
        }
    }
    else
    {
        return 9999;
    }
}

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

    while(true){

    int w,h;
    cin>>w>>h;
    if(w==0){
        break;
    }

    vector<vector<int> > mp(w+2,vector<int>(h+2,-1));

    int startx;
    int starty;
    int goalx;
    int goaly;
    REP(j,h){
        REP(i,w){
            int hoge;
            cin>>hoge;
            if(hoge==0||hoge==1)
            {
                mp[i+1][j+1]=hoge;
            }else
            if(hoge==2){
                mp[i+1][j+1]=0;
                startx=i+1;
                starty=j+1;
            }else{
                mp[i+1][j+1]=0;
                goalx=i+1;
                goaly=j+1;

            }
        }
    }

    int mini=99999;
    REP(i,4){
        int val=solve(mp,startx,starty,goalx,goaly,i,0)+1;
        if(val<mini){
            mini=val;
        }
    }
    if(mini>10){
        cout<<-1<<endl;
    }else{
    cout<<mini<<endl;
    }
    }
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <cctype>
#include <functional>
#include <ctime>
#include <fstream>
#include <cmath>
#include <limits>
#include <chrono>
#include <numeric>
#include <type_traits>
#include <iomanip>
#include <float.h>
#include <math.h>
#include <cassert>
#include <atcoder/all>
using namespace atcoder;
#pragma warning (disable: 4996)
using namespace std;

using ll = long long;
unsigned euclidean_gcd(unsigned a, unsigned b) {
    if (a < b) return euclidean_gcd(b, a);
    unsigned r;
    while ((r = a % b)) {
        a = b;
        b = r;
    }
    return b;
}

ll ll_gcd(ll a, ll b) {
    if (a < b) return ll_gcd(b, a);
    ll r;
    while ((r = a % b)) {
        a = b;
        b = r;
    }
    return b;
}

struct UnionFind {
    vector <ll> par;
    vector <ll> siz;
    UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) {
        for (ll i = 0; i < sz_; ++i) par[i] = i;
    }
    void init(ll sz_) {
        par.resize(sz_);
        siz.assign(sz_, 1LL);
        for (ll i = 0; i < sz_; ++i) par[i] = i;
    }
    ll root(ll x) {
        while (par[x] != x) {
            x = par[x] = par[par[x]];
        }
        return x;
    }
    bool merge(ll x, ll y) {
        x = root(x);
        y = root(y);
        if (x == y) return false;
        if (siz[x] < siz[y]) swap(x, y);
        siz[x] += siz[y];
        par[y] = x;
        return true;
    }

    bool issame(ll x, ll y) {
        return root(x) == root(y);
    }

    ll size(ll x) {
        return siz[root(x)];
    }
};

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

long long modinv(long long a, long long mod) {
    return modpow(a, mod - 2, mod);
}

ll merge_cnt(vector<ll>& a) {
    int n = a.size();
    if (n <= 1) { return 0; }

    ll cnt = 0;
    vector<ll> b(a.begin(), a.begin() + n / 2);
    vector<ll> c(a.begin() + n / 2, a.end());

    cnt += merge_cnt(b);
    cnt += merge_cnt(c);

    int ai = 0, bi = 0, ci = 0;
    while (ai < n) {
        if (bi < b.size() && (ci == c.size() || b[bi] <= c[ci])) {
            a[ai++] = b[bi++];
        }
        else {
            cnt += n / 2 - bi;
            a[ai++] = c[ci++];
        }
    }
    return cnt;
}

int main() {
    ll n, q;
    cin >> n >> q;
    fenwick_tree<ll> z(n);
    for (int i = 0; i < n; i++) {
        ll a;
        cin >> a;
        z.add(i, a);
    }
    for (int i = 0; i < q; i++) {
        ll a, b, c;
        cin >> a >> b >> c;
        if (a == 0) {
            z.add(b, c);
        }
        else {
            cout << z.sum(b, c) << endl;
        }
    }
}

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

// #define int long long
#define rep(i, n) for (long long i = (long long)(0); i < (long long)(n); ++i)
#define reps(i, n) for (long long i = (long long)(1); i <= (long long)(n); ++i)
#define rrep(i, n) for (long long i = ((long long)(n)-1); i >= 0; i--)
#define rreps(i, n) for (long long i = ((long long)(n)); i > 0; i--)
#define irep(i, m, n) for (long long i = (long long)(m); i < (long long)(n); ++i)
#define ireps(i, m, n) for (long long i = (long long)(m); i <= (long long)(n); ++i)
#define SORT(v, n) sort(v, v + n);
#define REVERSE(v, n) reverse(v, v+n);
#define vsort(v) sort(v.begin(), v.end());
#define all(v) v.begin(), v.end()
#define mp(n, m) make_pair(n, m);
#define cout(d) cout<<d<<endl;
#define coutd(d) cout<<std::setprecision(10)<<d<<endl;
#define cinline(n) getline(cin,n);
#define replace_all(s, b, a) replace(s.begin(),s.end(), b, a);
#define PI (acos(-1))
#define FILL(v, n, x) fill(v, v + n, x);
#define sz(x) long long(x.size())

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vs = vector<string>;
using vpll = vector<pair<ll, ll>>;
using vtp = vector<tuple<ll,ll,ll>>;
using vb = vector<bool>;

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

const ll INF = 1e9+10;
const ll MOD = 1e9+7;
const ll LINF = 1e18;

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

inline bool inside(ll y, ll x, ll H, ll W) {
    return (y >= 0 && x >= 0 && y < H && x < W);
}

char G[405][405];

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

  ll h,w; cin>>h>>w;
  rep(i,h) rep(j,w) cin>>G[i][j];
  map<char, int> ctoi;
  ctoi['.']=0, ctoi['#']=1;

  ll ans=0;

  vvi visited(h+1, vi(w+1));

  rep(i,h){
    rep(j,w){
      if(visited[i][j]) continue;
      visited[i][j]=1;

      char st=ctoi[G[i][j]];

      ll cnt0=0, cnt1=0;
      if(st) cnt1++;
      else cnt0++;

      queue<pair<pii,int>> q;
      q.emplace(pii(i,j), st);

      ll tmp=0;

      while(!q.empty()){
        auto p=q.front(); q.pop();
        int y=p.first.first, x=p.first.second, from=p.second;
        rep(ki,4){
          int ny=y+dy[ki], nx=x+dx[ki];
          if(inside(ny,nx,h,w)){
            if(visited[ny][nx]) continue;
            ll now=ctoi[G[ny][nx]];
            if(from==now) continue;
            visited[ny][nx]=1;
            q.emplace(pii(ny,nx), now);
            if(now) cnt1++;
            else cnt0++;
          }
        }
      }

      ans+=cnt1*cnt0;
    }
  }

  cout<<ans<<endl;

}


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

using namespace std;

typedef vector< vector<int> > VVI;

struct edge {
	int to, cost;
	edge(int t, int c) :to(t), cost(c) {}
};
typedef pair<int, int> P; // firstは最短距離, secondは頂点の番号

const int INF = (1e9);

void wFloyd ( VVI &v )
{
	int len = v.size();

	for (int k = 0; k < len; ++k)
	{
		for (int i = 0; i < len; ++i)
		{
			for (int j = 0; j < len; ++j)
			{
				v[i][j] = min(v[i][j], v[i][k] + v[k][j]);
			}
		}
	}
}

int dijkstra ( int s, int e, int size, vector< vector<edge> > &graph )
{
	priority_queue< P, vector<P>, greater<P> > que;
	vector<int> cost(size, INF);

	cost[s] = 0;
	que.push(P(0, s));

	while (!que.empty())
	{
		P p = que.top(); que.pop();
		int v = p.second;

		if (cost[v] < p.first) continue;

		for (int i = 0, ilen = graph[v].size(); i < ilen; ++i)
		{
			edge e = graph[v][i];
			if (cost[e.to] > (cost[v] + e.cost))
			{
				cost[e.to] = cost[v] + e.cost;
				que.push(P(cost[e.to], e.to));
			}
		}
	}

	return cost[e];
}

int main ( void )
{
	int n, k;

	while (cin >> n >> k, (n | k))
	{
		//VVI v(n, vector<int>(n, INF));
		vector< vector<edge> > v(n);

		for (int i = 0; i < k; ++i)
		{
			int x;

			cin >> x;

			if (x)
			{
				int c, d, e;

				cin >> c >> d >> e;

				--c; --d;

				bool hit = false;

				for (int j = 0, jlen = v[c].size(); j < jlen && !hit; ++j)
				{
					if ((v[c][j].to == d) && (v[c][j].cost > e))
					{
						v[c][j].cost = e;
						hit = true;
					}
				}

				if (!hit) v[c].push_back(edge(d, e));

				hit = false;

				for (int j = 0, jlen = v[d].size(); j < jlen && !hit; ++j)
				{
					if ((v[d][j].to == c) && (v[d][j].cost > e))
					{
						v[d][j].cost = e;
						hit = true;
					}
				}

				if (!hit) v[d].push_back(edge(c, e));

				//v[c].push_back(edge(d, e));
				//v[d].push_back(edge(c, e));

				//if ((v[c][d] > e) || (v[d][c] > e))
				//{
				//	v[c][d] = v[d][c] = e;
				//	wFloyd(v);
				//}
			}
			else
			{
				int a, b;

				cin >> a >> b;

				--a; --b;

				int cost = dijkstra(a, b, n, v);

				if (cost == INF)
				{
					cout << -1 << endl;
				}
				else
				{
					cout << cost << endl;
				}

				//if (v[a][b] == INF)
				//{
				//	cout << -1 << endl;
				//}
				//else
				//{
				//	cout << v[a][b] << endl;
				//}
			}
		}
	}

	return 0;
}

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
using P = pair<int,int>;
const vector<int> di{-1,0,1,0};
const vector<int> dj{0,-1,0,1};

int main(){
  int H, W;
  cin >> H >> W;
  vector<vector<char>> A(H,vector<char>(W));
  vector<vector<int>> D(H,vector<int>(W,-1));
  queue<P> q;
  for(int i=0; i<H; i++){
    for(int j=0; j<W; j++){
      cin >> A.at(i).at(j);
      if(A.at(i).at(j)=='#'){
        q.push(make_pair(i,j));
        D.at(i).at(j) = 0;
      }
    }
  }
  while(!q.empty()){
    int i, j;
    tie(i,j) = q.front();
    q.pop();
    for(int v=0; v<4; v++){
      int ni = i+di.at(v);
      int nj = j+dj.at(v);
      if(ni<0||ni>=H) continue;
      if(nj<0||nj>=W) continue;
      if(D.at(ni).at(nj)<0){
        D.at(ni).at(nj) = D.at(i).at(j)+1;
        q.push(make_pair(ni,nj));
      }
    }
  }
  for(int i=0; i<H; i++){
    sort(D.at(i).begin(),D.at(i).end());
  }
  int ans = 0;
  for(int i=0; i<H; i++){
    ans = max(ans,D.at(i).at(W-1));
  }
  cout << ans;
  return 0;
}


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

int main()
{
  int n, m;
  cin >> n >> m;
  vector<int> ac(n, 0);
  vector<int> wa(n, 0);
  rep(i, m)
  {
    int p;
    string s;
    cin >> p >> s;
    p--;
    if (ac[p] == 1)
      continue;
    if (s == "AC")
      ac[p] = 1;
    else
      wa[p]++;
  }
  rep(i, n)
    wa[i] *= ac[i];
  cout << accumulate(ac.begin(), ac.end(), 0) << ' ' << accumulate(wa.begin(), wa.end(), 0) << endl;
  return 0;
}

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

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

	int N,M;
	int p;
	string S;
	int i,k;
	int AC=0;
	int WA=0;

	cin >> N >> M;
	int wrong[N+5]={0};

	for(i=0;i<M;i++){
		cin >> p >> S;
		if(S=="AC" && wrong[p]>=0){
			AC++;
			WA+=wrong[p];
			wrong[p]=-1000000;
		}else{
			wrong[p]++;
		}
	}

	cout << AC << " " << WA << endl;

	return 0;
}


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define cinf(n,x) for(int i=0;i<(n);i++)cin>>x[i];
#define ft first
#define sc second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(),(v).end()
#define mod 1000000007
#define FS fixed<<setprecision(15)
using namespace std;
typedef long long ll;
template<class T> using V=vector<T>;
using Graph = vector<vector<int>>;
using P=pair<ll,ll>;
typedef unsigned long long ull;
typedef long double ldouble;
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; }

const ll INF=1e18;

int main(){
    cin.tie(0);ios::sync_with_stdio(false);
    int n,m;
    cin>>n>>m;
    set<int> st;
    for(int i=1;i<n;i++){
        st.insert(i);
    }
    int k=1;
    int pr=-1;
    set<int> check;
    while(k<=m){
        int v=*rbegin(st);
        st.erase(v);
        while(pr-v==1||check.count(n-v)||2*v==n){
            v=*rbegin(st);
            st.erase(v);
        }
        pr=v;
        check.insert(v);
        printf("%d %d\n",k,k+v);
        k++;
    }
}

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define zero_pad(num) setfill('0') << std::right << setw(num)
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;

int main() {
    ll x, y;
    cin >> x >> y;
    if(x == y)cout << 0 << endl;
    else if(x == -y)cout << 1 << endl;
    else if(abs(x) < abs(y)){
        ll ans = abs(y) - abs(x);
        if(x < 0 && y < 0)ans += 2;
        else if(x < 0 || y < 0)ans++;
        cout << ans << endl;
    }else{
        ll ans = abs(x) - abs(y);
        if(x > 0 && y > 0)ans += 2;
        else if(x > 0 || y > 0)ans++;
        cout << ans << endl;
    }

}


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

int main(void){
	while(1){
		int month, day;
		int days_from_new_year = 0;
		cin >> month >> day;
		if(month == 0 && day == 0) break;
		for(int i = 1; i < month; i++){
			if(i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12) days_from_new_year += 31;
			else if(i == 4 || i == 6 || i == 9 || i == 11) days_from_new_year += 30;
			else if(i == 2) days_from_new_year += 29;
		}
		days_from_new_year += day;
		int mod = days_from_new_year % 7;
		if(mod == 0) cout << "Wednesday";
		else if(mod == 1) cout << "Thursday";
		else if(mod == 2) cout << "Friday";
		else if(mod == 3) cout << "Saturday";
		else if(mod == 4) cout << "Sunday";
		else if(mod == 5) cout << "Monday";
		else if(mod == 6) cout << "Tuesday";
		cout << endl;
	}
	return 0;
}

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

using namespace std;

int main(){
    int N, K;
    while(cin >> N >> K){
        int res = 0;
        vector<int> deg(N, 0);
        vector<int> next(N, -1);
        vector<int> dist(N, 0);
        for(int i=0;i<N;i++){
            cin >> next[i];
            --next[i];
            if(i == 0 && next[i]){
                ++res;
                next[i] = 0;
            }
            ++deg[next[i]];
        }
        queue<int> qu;
        for(int i=1;i<N;i++) if(!deg[i]) qu.push(i);
        while(!qu.empty()){
            int p = qu.front(); qu.pop();
            if(!p) break;
            if((next[p] && dist[p] == K-1) || (!p && !next[p])){
                ++res;
            } else {
                dist[next[p]] = max(dist[next[p]], dist[p]+1);
            }
            --deg[next[p]];
            if(!deg[next[p]]) qu.push(next[p]);
        }
        cout << res << endl;
    }
}


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

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

#define mp make_pair
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()
#define eb emplace_back
const int mod = 1e9+7;

int dp[45][1<<17];
int main(){
    int n, x, y, z;
    scanf("%d%d%d%d", &n, &x, &y, &z);
    int b = (1<<(x+y+z-1))|(1<<(y+z-1))|(1<<(z-1));
    dp[0][0] = 1;
    for(int i=1;i<=n;i++){
        for(int j=0;j<(1<<17);j++){
            if(dp[i-1][j] == 0) continue;
            for(int k=1;k<=10;k++){
                int nt = (j<<k) + (1<<(k-1));
                nt &= (1<<17)-1;
                if((nt&b) == b) continue;
                dp[i][nt] = (dp[i][nt] + dp[i-1][j])%mod;
            }
        }
    }
    ll ans = 1;
    for(int i=1;i<=n;i++) ans = ans*10%mod;
    for(int j=0;j<(1<<17);j++) ans = (ans - dp[n][j] + mod)%mod;
    printf("%lld\n", ans);
}

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#define rep(i,l,r) for(int i=(l);i<=(r);++i)
#define per(i,r,l) for(int i=(r);i>=(l);--i)
using namespace std;
const int N=3e5+10,P=1e9+7;
const int tr[][2]={{1,2},{3,4},{5,6},{1,1},{1,2},{7,2},{6,6},{5,5}};
char s[N];int dp[N][8];
inline void upd(int &x,int y){((x+=y)>=P)&&(x-=P);}
int main(){
	scanf("%s",s);int n=strlen(s);
	dp[0][0]=1;
	rep(i,0,n-1){
		rep(j,0,7){
			if(s[i]^49) upd(dp[i+1][tr[j][0]],dp[i][j]);
			if(s[i]^48) upd(dp[i+1][tr[j][1]],dp[i][j]);
		}
	}
	printf("%d\n",dp[n][2]+dp[n][6]>=P?dp[n][2]+dp[n][6]-P:dp[n][2]+dp[n][6]);
	return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<cmath>
#include<cstdio>
#include<queue>
#include<deque>
#include<map>
#include<stack>
#include<set>
#include<utility>
using namespace std;
typedef pair<int,int> ii;
typedef long long ll;
typedef pair<ll,ll> P;
typedef unsigned long long int ull;
int dy[]={1,0,-1,0};
int dx[]={0,1,0,-1};
const int MAXN=100000;
const int MAXE=100000;
const int MAXV=10000;
const ll INF=2e18;
const ll MOD=1e9+7;

int main(){
    int N,M;cin>>N>>M;
    vector<ll> A(N),S(N+1,0);
    map<int,ll> mp;
    mp[0]++;
    for(int i=0;i<N;++i) cin>>A[i];
    for(int i=0;i<N;++i){
        S[i+1]=S[i]+A[i];
        S[i+1]%=M;
        mp[S[i+1]]++;
    }
    ll ans=0;
    for(auto it:mp){
        ans+=(it.second)*(it.second-1)/2;
    }
    cout<<ans<<endl;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main(){
  int64_t N,M;cin>>N>>M;
  vector<int64_t>A(N);
  for(int i=0;i<N;i++)
    cin>>A.at(i);
  vector<int64_t>B(N+1);B.at(0)=0;
  for(int i=1;i<=N;i++)
    B.at(i)=(B.at(i-1)+A.at(i-1))%M;
  map<int64_t,int64_t>m;
  for(int i=0;i<=N;i++)
    if(m.count(B.at(i)))
       m.at(B.at(i))++;
       else
       m[B.at(i)]=1;
       int64_t ans=0;
       for(auto p:m){
       auto v=p.second;
         ans+=(v*(v-1))/2;
       }cout<<ans<<endl;
  return 0;
}


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

int main(){
  int N;
  cin >> N;
  vector<int> num(4);
  for(int i=0;i<4;i++){
    num[3-i]=N%10;
    N/=10;
  }
  for(int i=0;i<(1<<3);i++){
    vector<char> op(3);
    int ans=num[0];
    for(int j=0;j<3;j++){
      if((i & (1<<j))==(1<<j)){
        ans+=num[j+1];
        op[j]='+';
      }
      else{
        ans-=num[j+1];
        op[j]='-';
      }
    }
    if(ans==7){
      for(int i=0;i<4;i++){
        cout << num[i];
        if(i!=3){
          cout << op[i];
        }
      }
      cout << "=7" << endl;
      return 0;
    }
  }
}


#pragma region header
#include <algorithm>
#include <bitset>
#include <tuple>
#include <cstdint>
#include <cctype>
#include <assert.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include <math.h>
#include <cstring>
#include <array>
// ===============================================================
//using系
#pragma region header
using namespace std;
using lint = long long;
using ld = long double;
using ulint = unsigned long long;
const int dx[] = { 1,0,-1,0 };
const int dy[] = { 0,1,0,-1 };
constexpr lint mod = 1000000007;
constexpr long double pi = 3.141592653589793238462643383279;
#pragma endregion
// ========================================================================
//define
#define INF (lint)10000000000000000;
#define mod (int)1000000007
#pragma region header
template <class T, class U>
inline bool chmin(T& lhs, const U& rhs) {
	if (lhs > rhs) {
		lhs = rhs;
		return 1;
	}
	return 0;
}
template <class T, class U>
inline bool chmax(T& lhs, const U& rhs) {
	if (lhs < rhs) {
		lhs = rhs;
		return 1;
	}
	return 0;
}
#pragma endregion
#pragma endregion

lint dp[1000][2020];

int main(void) {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n; cin >> n;
	vector<int> d(n);
	vector<bool> ans(n, false);
	for (int i = 0; i < n; i++) cin >> d[i];
	for (int i = n - 1; i >= 0; i--) {
		int cnt = 0;
		for (int j = i; j < n; j += i + 1) {
			if (ans[j]) cnt++;
		}
		if (cnt % 2 != d[i]) ans[i] = true;
	}
	int cnt = 0;
	lint a = 0;
	for (int i = 0; i < n; i++) {
		if (ans[i]) a++;
	}
	cout << a << endl;
	for (int i = 0; i < n; i++) {
		if (ans[i]) {
			cout << i + 1;
			cnt++;
			if (cnt == a) cout << endl;
			else cout << ' ';
		}
	}

	return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 5LL << 60;
const ll mod = 1e9 + 7;

int main() {
	int H, W;
	cin >> H >> W;
	int a[H][W];
	for(int i = 0; i < H; i++)
		for(int j = 0; j < W; j++) cin >> a[i][j];

	vector<tuple<int, int, int, int>> v;
	for(int i = 0; i < H; i++) {
		for(int j = 0; j < W; j++) {
			if(a[i][j] % 2 == 0) continue;
			if(i == H - 1 && j == W - 1) continue;

			if(j == W - 1) {
				v.push_back(make_tuple(i + 1, j + 1, i + 2, j + 1));
				a[i + 1][j]++;
			} else {
				v.push_back(make_tuple(i + 1, j + 1, i + 1, j + 2));
				a[i][j + 1]++;
			}
		}
	}

	cout << v.size() << "\n";
	for(int i = 0; i < v.size(); i++) cout << get<0>(v[i]) << " " << get<1>(v[i]) << " " << get<2>(v[i]) << " " << get<3>(v[i]) << "\n";
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for(int i = n-1; i >= 0; i--)
#define  all(x) (x).begin(),(x).end()     // 昇順ソート
#define  rall(v) (v).rbegin(), (v).rend() // 降順ソート
#define  FastIO ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
typedef long long ll;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<vector<int>>;
using VL = vector<ll>;
using VVL = vector<vector<ll>>;
using VP = vector<P>;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return true; } return false; }

const int MAX = 55555;

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

    VI d(MAX + 1);
    for(int i = 1; i <= MAX; i++){
        for(int j = i; j <= MAX; j += i){
            d[j]++;
        }
    }

    int cnt = 0, i = 3;
    VI ans;
    while(cnt < n){
        if(d[i] == 2 && i % 5 == 3){
            ans.push_back(i);
            cnt++;
        }
        i++;
    }

    rep(i,n) printf("%d%c", ans[i], i == n-1? '\n' : ' ');
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll,ll>;
using vl = vector<ll>;
using Map = map<ll,ll>;
using T = tuple<ll,ll,ll>;
using vvl = vector<vector<ll>>;
#define all(v) v.begin(), v.end()
#define prt(v) cout<<v<<"\n";
#define fi(v) get<0>(v)
#define se(v) get<1>(v)
#define th(v) get<2>(v)
#define endl "\n"
template <typename T> bool chmax(T &a, const T &b){if (a<b){a=b;return 1;}return 0;}
template <typename T> bool chmin(T &a, const T &b){if (a>b){a=b;return 1;}return 0;}
const ll INF=1LL<<60;
const ll MOD=1000000007;

vector<ll> Eratosthenes( const ll N )
{
    std::vector<bool> is_prime( N + 1 );
    for( ll i = 0; i <= N; i++ )
    {
        is_prime[ i ] = true;
    }
    std::vector<ll> P;
    for( ll i = 2; i <= N; i++ )
    {
        if( is_prime[ i ] )
        {
            for( ll j = 2 * i; j <= N; j += i )
            {
                is_prime[ j ] = false;
            }
            P.emplace_back( i );
        }
    }
    return P;
}
signed main(void){
    cin.tie(0);cout.tie(0);ios::sync_with_stdio(false);
    ll N;
    cin >> N;
    vl A=Eratosthenes(60000);
    ll count=0;ll now=0;
    while(count<N){
        now++;
        while(A[now]%5!=3)now++;
        cout<<A[now]<<" ";
        count++;
    }cout<<endl;

    return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <cctype>
#include <functional>
#include <ctime>
#include <cmath>
#include <limits>
#include <numeric>
#include <type_traits>
#include <iomanip>
#include <float.h>
#include <math.h>
using namespace std;
using ll = long long;
unsigned euclidean_gcd(unsigned a, unsigned b) {
    if (a < b) return euclidean_gcd(b, a);
    unsigned r;
    while ((r = a % b)) {
        a = b;
        b = r;
    }
    return b;
}

ll ll_gcd(ll a, ll b) {
    if (a < b) return ll_gcd(b, a);
    ll r;
    while ((r = a % b)) {
        a = b;
        b = r;
    }
    return b;
}

struct UnionFind {
    vector <ll> par;
    vector <ll> siz;
    UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) {
        for (ll i = 0; i < sz_; ++i) par[i] = i;
    }
    void init(ll sz_) {
        par.resize(sz_);
        siz.assign(sz_, 1LL);
        for (ll i = 0; i < sz_; ++i) par[i] = i;
    }
    ll root(ll x) {
        while (par[x] != x) {
            x = par[x] = par[par[x]];
        }
        return x;
    }
    bool merge(ll x, ll y) {
        x = root(x);
        y = root(y);
        if (x == y) return false;
        if (siz[x] < siz[y]) swap(x, y);
        siz[x] += siz[y];
        par[y] = x;
        return true;
    }

    bool issame(ll x, ll y) {
        return root(x) == root(y);
    }

    ll size(ll x) {
        return siz[root(x)];
    }
};

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

long long modinv(long long a, long long mod) {
    return modpow(a, mod - 2, mod);
}

vector<int> tpsort(vector<vector<int>>& G) {
    int V = G.size();
    vector<int> sorted_vertices;
    queue<int> que;
    vector<int> indegree(V);
    for (int i = 0; i < V; i++) {
        for (int j = 0; j < G[i].size(); j++) {
            indegree[G[i][j]]++;
        }
    }
    for (int i = 0; i < V; i++) {
        if (indegree[i] == 0) {
            que.push(i);
        }
    }
    while (que.empty() == false) {
        int v = que.front();
        que.pop();
        for (int i = 0; i < G[v].size(); i++) {
            int u = G[v][i];
            indegree[u] -= 1;
            if (indegree[u] == 0) que.push(u);
        }
        sorted_vertices.push_back(v);
    }
    return sorted_vertices;
}

struct Point
{
    double x;
    double y;
};

struct LineSegment
{
    Point start;
    Point end;
};

double tenkyori(const LineSegment& line, const Point& point)
{
    double x0 = point.x, y0 = point.y;
    double x1 = line.start.x, y1 = line.start.y;
    double x2 = line.end.x, y2 = line.end.y;

    double a = x2 - x1;
    double b = y2 - y1;
    double a2 = a * a;
    double b2 = b * b;
    double r2 = a2 + b2;
    double tt = -(a * (x1 - x0) + b * (y1 - y0));

    if (tt < 0)
        return sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0));
    else if (tt > r2)
        return sqrt((x2 - x0) * (x2 - x0) + (y2 - y0) * (y2 - y0));

    double f1 = a * (y1 - y0) - b * (x1 - x0);
    return sqrt((f1 * f1) / r2);
}

int main() {
    ll n;
    cin >> n;
    string s;
    cin >> s;
    ll m = 0;
    ll ans = 1;
    for (int i = 0; i < 2 * n; i++) {
        if (i % 2 == 0 && s[i] == 'W' || i % 2 == 1 && s[i] == 'B') {
            ans *= m;
            m--;
            ans %= 1000000007;
        }
        else {
            m++;
        }
    }
    if (m != 0)cout << 0 << endl;
    else {
        for (ll i = 0; i < n; i++) {
            ans *= (i+1);
            ans %= 1000000007;
        }
        cout << ans << endl;
    }
}

#include <bits/stdc++.h>

using namespace std;

#define rep(i,m,n) for(int (i)=(int)(m);i<(int)(n);i++)
#define rep2(i,m,n) for(int (i)=(int)(n)-1;i>=(int)(m);i--)
#define REP(i,n) rep(i,0,n)
#define REP2(i,n) rep2(i,0,n)
#define FOR(i,c) for(decltype((c).begin())i=(c).begin();i!=(c).end();++i)
#define ll long long
#define ull unsigned long long
#define all(hoge) (hoge).begin(),(hoge).end()
#define en '\n'
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;
typedef pair<ll, ll> P;
constexpr long long INF = 1LL << 60;
constexpr int INF_INT = 1 << 25;
constexpr long long MOD = (ll) 1e9 + 7;
typedef vector<ll> Array;
typedef vector<Array> Matrix;
constexpr int loose = 0;
constexpr int tight = 1;

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

struct Edge {
	ll to, cap, rev;
	Edge(ll _to, ll _cap, ll _rev) {
	to = _to; cap = _cap; rev = _rev;
	}
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

void add_edge(Graph& G, ll from, ll to, ll cap, bool revFlag, ll revCap) {
	G[from].push_back(Edge(to, cap, (ll)G[to].size()));
	if (revFlag)G[to].push_back(Edge(from, revCap, (ll)G[from].size() - 1));
}

void solve(){
	ll n;
	cin>>n;
	Array a(n);
	REP(i,n) cin>>a[i];

	map<ll,ll> mp;
	REP(i,n) mp[a[i]]++;

	sort(all(a));
	ll ans=0;
	rep2(i,1,n){
		ll con1=mp[a[i]];
		if(con1==0) continue;
		ll num=1;
		while(num<=a[i]) num*=2;
		ll con2=mp[num-a[i]];
		if(num==a[i]*2){
			ans+=con1/2;
			mp[a[i]]-=con1;
			continue;
		}
		ans += min(con1,con2);
		mp[a[i]] = 0;
		mp[num-a[i]] -= min(con1,con2);
	}
	cout<<ans<<en;
}

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

	solve();
    return 0;
}


#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define watch(x) cout << (#x) << " is " << (x) << endl
#define debug cout << "hi" << endl

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

const ll mod = 1e9 + 7;
const int INF32 = 1<<30;
const ll INF64 = 1LL<<60;
const ld pi = 3.141592653589793;

void solve(){
     int n;cin >> n;
     vector<ll>a(n);
     for(int i = 0;i<n;i++) cin >> a[i];
     int cnt = 0;ll sum = 0;ll mini = INT_MAX;
     for(int i = 0;i<n;i++){
        if(a[i]<0) cnt++;
        sum += abs(a[i]);
        mini = min(abs(a[i]),mini);
       // cout << sum <<endl;
     }
     if(cnt%2) cout << sum - 2*mini;
     else cout << sum;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    solve();
    return 0;
}

#include <iostream>
using namespace std;

int N;
int A[210];         // 最大 200 個なので余裕を持って 210 に --- 200 以上ならなんでもよいです

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

    int res = 0;

    // 操作が行える限り操作を繰り返す
    while (true) {
        bool exist_odd = false;  // A[i] がすべて偶数かどうかを判定するフラグ
        for (int i = 0; i < N; ++i) {
            if (A[i] % 2 != 0) exist_odd = true;  // 奇数があったらフラグを立てる
        }

        if (exist_odd) break;  // 奇数があったら break

        // 操作を行えるなら操作を実際に行う
        for (int i = 0; i < N; ++i) {
            A[i] /= 2;
        }
        ++res;  // 操作回数をインクリメント
    }

    cout << res << endl;
}


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <bitset>
#include <string>
#include <cmath>
#include <complex>
#include <numeric>
#include <cassert>

#include <vector>
#include <array>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <utility>

#define PI 3.14159265358979323846
#define int64 long long
#define uint64 unsigned long long
using namespace std;

int main()
{
  string ss;
  cin >> ss;
  if(ss[0] != 'A')
  {
    cout << "WA" << endl;
    return 0;
  }
  int64 cc = 0;
  for(int64 ii = 2;ii < ss.length() - 1;ii++)
  {
    if(ss[ii] == 'C')
    {
      cc++;
    }
  }
  if(cc != 1)
  {
    cout << "WA" << endl;
    return 0;
  }
  cc = 0;
  for(int64 ii = 0;ii < ss.length();ii++)
  {
    if(ss[ii] >= 'A' && ss[ii] <= 'Z')
    {
      cc++;
    }
  }
  if(cc != 2)
  {
    cout << "WA" << endl;
    return 0;
  }
  cout << "AC" << endl;
  return 0;
}

#include<stdio.h>

int main(){
	int N;
	scanf("%d", &N);
	if( N%10==7 || (N%100)/10==7 || N/100==7)
		printf("Yes\n");
	else
		printf("No\n");
	return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long ll;

#define FAST ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define mp make_pair
#define pb push_back
#define lp(i,s,f) for(ll i = s; i < ll(f); i++)
#define inF freopen("input.in", "r", stdin);
#define outF freopen("output.in", "w", stdout);
#define endl '\n'
#define MOD 1000000007
#define mm(arr) memset(arr, 0, sizeof(arr))

int main(){
    FAST
    int arr[3];
    for(int i = 0; i < 3; i++){
        cin >> arr[i];
    }
    sort(arr, arr + 3);
    cout << arr[0] + arr[1];
    return 0;
}

// I make this just for fun because i'm done
// Aimi Haraguni >> Konomi Suzuki >> Yui >>  Ikimono Gakari >> Garnidelia >> Kalafina >> Eir Aoi.   .. dude?
// problems that involves any kind of persistent data structures are the best of the best, are not them?

// #pragma GCC optimize ("Ofast,unroll-loops")
// #pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

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

#define pb push_back
#define ff  first
#define ss second
#define tm1 first
#define tm2 second.first
#define tm3 second.second
#define sz(x) ll(x.size())
#define all(v) (v).begin(), (v).end()
#define FER(i,a,b) for(ll i=ll(a); i< ll(b); ++i)
#define IFR(i,a,b) for(ll i=ll(a); i>=ll(b); --i )
#define fastio ios_base::sync_with_stdio(0); cin.tie(0)

#define N 31000000
// #define M 19

// #define mod  11092019
#define mod1 1000000007
#define mod2 1000000009
#define bas 987625403
#define sqr(x) (x)*(x)
#define INF 2000000000000000

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef pair<ll, ll> ii;
typedef pair<ll, ii > tri;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef tree<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag, tree_order_statistics_node_update> S_t;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

#define trace(...) fff(#__VA_ARGS__, __VA_ARGS__)
template<typename t> void fff(const char* x, t&& val1) { cout<<x<< " : "<<val1<<"\n";}
template<typename t1, typename... t2> void fff(const char* x, t1&& val1, t2&&... val2){
    const char* xd=strchr(x+1, ',');
    cout.write(x, xd-x)<<" : "<<val1<<" | ";
    fff(xd+1, val2...);
}

inline ll mul(ll a, ll b, ll mod) { return (long long) a*b%mod;}
inline ll add(ll a, ll b, ll mod) { return a+b < mod? a+b: a+b-mod;}
inline void Mul(ll &a, ll b, ll mod) { a = (long long) a*b%mod;}
inline ll bp(ll a, ll p, ll mod){
	ll ret;
	for(ret = 1; p; p>>=1, Mul(a, a, mod)) (p & 1) && (Mul(ret, a, mod), 1);
	return ret;
}

static inline void read(ll &x) { cin>>x;}

int main(){
	ll a, b, c; read(a), read(b), read(c);
	ll z = min({a+b, b+c, c+a});
	cout<<z<<"\n";
	return 0;
}


/* You can't hack me! */
#include"bits/stdc++.h"
using namespace std;

typedef long long ll;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
#define fo(i,n) for(ll i=0; i<(n); i++)
#define FO(i,a,b) for(auto i=a; i!=(b); i+=(b)>(a)?1:-1)
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(c) c.begin(), c.end()
#define F first
#define S second
#define os(x) cout<<x<<' '
#define on(x) cout<<x<<"\n"
#define nl cout<<"\n"
#define maxe(x) max_element(all(x))-(x).begin()
#define mine(x) min_element(all(x))-(x).begin()
const ll P=998244353;
const ll MaxN=2000010;
const long double pi=3.14159265358979323846264338;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
#ifndef ONLINE_JUDGE
    freopen("C:\\Users\\ASISH\\Desktop\\coding\\input.txt", "r", stdin);
    freopen("C:\\Users\\ASISH\\Desktop\\coding\\output.txt", "w", stdout);
#endif

    ll T=1;
    // cin>>T;
    fo(q, T)
    {
        ll N, ans=0;
        cin>>N;
        vll cnt(3);
        string s;
        cin>>s;
        for(auto x : s){
            if(x=='R') cnt[0]++;
            else if(x=='G') cnt[1]++;
            else cnt[2]++;
        }
        ans=cnt[0]*cnt[1]*cnt[2];
        // on(ans);
        fo(i, N-2){
            FO(k, i+2, N){
                // os(i); on(k);
                if((i+k)%2) continue;
                ll j=(i+k)/2;
                if(s[k]!=s[j] && s[j]!=s[i] && s[i]!=s[k]) ans--;
            }
        }
        on(ans);
    }
    cerr << "Time : " << ((double)clock()) / (double)CLOCKS_PER_SEC << "s\n";
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define ll  long long
#define INF 999999999
#define rep(i,n) for(int i=0;i<n;i++)
const ll MOD = 1000000007;
ll cnt =0,ans=0;
const int MAX = 510000;

int main(){

  ll n;cin >> n;

    string s;   cin >> s;
    ll r=0,g=0,b=0;

    for(int i=0;i<n;i++){
        if(s.at(i) == 'R')r++;
        if(s.at(i) == 'G')g++;
        if(s.at(i) == 'B')b++;
    }

    for(int i=0;i<n-2;i++){
        for(int j=i+1;j<n-1;j++){
            int k = j+(j-i);
            if(k>=n)break;
            if(s.at(i) != s.at(j) && s.at(j) != s.at(k) && s.at(k) != s.at(i))cnt++;
        }

    }
    cout << r*g*b - cnt << endl;
}


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

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int h , w;
    string s[50] = {};
    cin >> h >> w;
    for( int i = 0 ; i < h ; i++ ) cin >> s[i];
    for( int i = 0 ; i < h ; i++ ) {
    	for ( int j = 0 ; j < w ; j++ ) {
    		if ( s[i][j] == '#' ) {
        		bool y = 0;
        		if ( i && s[i - 1][j] == '#' ) y = 1;
        		if ( i + 1 < h && s[i + 1][j] == '#' ) y = 1;
        		if ( j && s[i][j - 1] == '#' ) y = 1;
        		if ( j + 1 < w && s[i][j + 1] == '#' ) y = 1;
        		if ( !y ) {
					cout << "No";
					return 0;
				}
			}
		}
    }
    cout << "Yes";
}

/********************************************************************************

	Code by a weak man who named CYJian, and he hope the code can get more points.

	Algorithm:

 ********************************************************************************/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int __SIZE = 1 << 18;
char ibuf[__SIZE], *iS, *iT;

#define ge (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++)
#define ri read_int()
#define rl read_ll()
#define FILE(s) freopen(s"in", "r", stdin), freopen(s"out", "w", stdout)

template<typename T>
inline void read(T &x) {
	char ch, t = 0; x = 0;
	while(!isdigit(ch = ge)) t |= ch == '-';
	while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = ge;
	x = t ? -x : x;
}
inline int read_int() { int x; return read(x), x; }
inline ll read_ll() { ll x; return read(x), x; }

template<typename T>
inline void chkmin(T&a, T b) { a = a < b ? a : b; }

const string S4[4] = {"abcc", "abdd", "ccab", "ddab"};
const string S5[5] = {"abbcc", "ad..a", "bd..a", "b.eez", "oorrz"};
const string S6[6] = {"oorrzz", ".a.b.c", ".a.b.c", "d.e.f.", "d.e.f.", "oorrzz"};
const string S7[7] = {".oorrzz", "oabb...", "oa.c...", "rddc...", "r...abb", "z...a.c", "z...ddc"};

char s[1010][1010];

inline void solve(int&n, int x) {
	if(x == 4)
		for(int i = 0; i < x; i++)
			for(int j = 0; j < x; j++)
				s[n + i][n + j] = S4[i][j];
	if(x == 5)
		for(int i = 0; i < x; i++)
			for(int j = 0; j < x; j++)
				s[n + i][n + j] = S5[i][j];
	if(x == 6)
		for(int i = 0; i < x; i++)
			for(int j = 0; j < x; j++)
				s[n + i][n + j] = S6[i][j];
	if(x == 7)
		for(int i = 0; i < x; i++)
			for(int j = 0; j < x; j++)
				s[n + i][n + j] = S7[i][j];
	n += x;
}

int main() {
	int n = ri;
	if(n <= 2) return puts("-1"), 0;
	if(n == 3) {
		puts("a..");
		puts("a..");
		puts(".bb");
		return 0;
	}
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= n; j++)
			s[i][j] = '.';
	int x = n % 4, N = 1;
	if(x == 0) solve(N, 4);
	if(x == 1) solve(N, 5);
	if(x == 2) solve(N, 6);
	if(x == 3) solve(N, 7);
	while(N <= n) solve(N, 4);
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= n; j++)
			putchar(s[i][j]);
		puts("");
	}
	return 0;
}


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

using namespace std;
int main()
{
    string a,b;
    cin>>a>>b;
    cout<<b<<""<<a<<""<<endl;

    return 0;
}

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, s, n) for (ll i = s; i < (ll)(n); i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define pb push_back
#define COUT(x) cout << (x) << "\n"
#define COUTF(x) cout << setprecision(15) << (x) << "\n"
#define ENDL cout << "\n"
#define DF(x) x.erase(x.begin())
#define ALL(x) x.begin(), x.end()
#define SORT(x) sort(ALL(x))
#define RSORT(x) sort(x.rbegin(), x.rend())
#define REVERSE(x) reverse(ALL(x))
#define MAX(x) *max_element(ALL(x))
#define MAXI(x) max_element(ALL(x)) - x.begin()
#define SUM(x) accumulate(ALL(x), 0)
#define COUNT(x, y) count(ALL(x), y);
#define ANS cout << ans << "\n"
#define YES cout << "YES\n";
#define NO cout << "NO\n";
#define Yes cout << "Yes\n";
#define No cout << "No\n";
#define YN YES else NO
#define yn Yes else No
#define init() \
  cin.tie(0);  \
  ios::sync_with_stdio(false)
#define LINE cerr << "[debug] line: " << __LINE__ << "\n";
#define debug(x) cerr << "[debug] " << #x << ": " << x << "\n";
#define debugV(v)                       \
  cerr << "[debugV] " << #v << ":";     \
  rep(z, v.size()) cerr << " " << v[z]; \
  cerr << "\n";
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using mll = map<ll, ll>;
using qll = queue<ll>;
using P = pair<ll, ll>;
using vp = vector<P>;
using vs = vector<string>;

template <typename T>
inline istream& operator>>(istream& i, vector<T>& v) {
  rep(j, v.size()) i >> v[j];
  return i;
}

template <typename T1, typename T2>
inline istream& operator>>(istream& i, pair<T1, T2>& v) {
  return i >> v.first >> v.second;
}

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

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

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

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

signed main() {
  init();
  string S;
  ll w;
  cin >> S >> w;

  string ans = "";
  rep(i, S.size()) if (i % w == 0) ans += S[i];
  ANS;
  return 0;
}


#include<bits/stdc++.h>
#define rep(i,n) for (long long i = 0; i < (n); ++i)
//g++ -std=c++11

using namespace std;

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

long long prime_factor(long long num){
	long long bak = num;
	set<long long> nums;
	for(long long i = 2; i*i <= num; i++){
		while(bak % i == 0){
			nums.insert(i);
			bak /= i;
		}
	}
	if(bak > 1){
		nums.insert(bak);
	}
	nums.insert(1);

	return (long long) nums.size();
}

long long A, B;
int main(){
	cin >> A >> B;
	long long num = gcd(A, B);
	cout << prime_factor(num) << endl;
}

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

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

long long gcd(long long x, long long y){
  if(y == 0) return x;
  else return gcd(y, x%y);
}

bool IsPrime(long long n){
  if(n == 1) return false;
  for(long long i = 2; i * i < n; i++){
    if(n%i == 0) return false;
  }
  return true;
}

vector<long long> EnumDivisors(long long n){
  vector<long long> res;

  for(long long i = 1; i * i <= n; i++){
    if(n % i == 0){
      res.push_back(i);
      if(n/i != i) res.push_back(n/i);
    }
  }
  sort(res.begin(), res.end());
  return res;
}

vector<pair<long long, long long>> PrimeFactorize(long long n){
  vector<pair<long long, long long>> res;

  long long a = 0;
  long long b = 0;

  for(long long i = 2; i * i <= n; i++){
    a = 0;
    if(n%i == 0){
      if(IsPrime(i)){
        while(n % i == 0){
          n /= i;
          a++;
        }
        res.push_back(make_pair(i, a));
      }
    }
  }

  if(n!= 1) res.push_back(make_pair(n, 1));

  return res;

}

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

  vector<pair<long long, long long>> a_fac = PrimeFactorize(a);
  vector<pair<long long, long long>> b_fac = PrimeFactorize(b);

  // for(int i = 0; i < a_fac.size(); i++) cout << a_fac[i].first << ", " << a_fac[i].second << endl;
  // for(int i = 0; i < b_fac.size(); i++) cout << b_fac[i].first << ", " << b_fac[i].second << endl;

  long long ans = 0;
  for(int i = 0; i < a_fac.size(); i++){
    for(int j = 0; j < b_fac.size(); j++){
      if(a_fac[i].first == b_fac[j].first) {
        // cout << "i = " << i << ", j = " << j << endl;
        ans ++;
      }
    }
  }
  cout << ans + 1 << endl;

  return 0;
}


#include <bits/stdc++.h>

using namespace std;
const int nax = 105;
const int mod = 1e9 + 7;

string s;
int d;

void add_self(int &a, int b)
{
    a += b;

    if(a >= mod)
        a -= mod;
}

int main()
{
    cin >> s;
    cin >> d;

    vector<vector<int>>dp(nax, vector<int>(2));
    int n = s.length();
    dp[0][0] = 1;

    for(int where = 0; where < n; ++where)
    {
        vector<vector<int>>new_dp(nax, vector<int>(2));

        for(int sum = 0; sum < d; ++sum)
            for(int digit = 0; digit < 10; ++digit)
                for(bool is_smaller : {false, true})
                {
                    if(!is_smaller && digit > s[where] - '0')
                        continue;

                    add_self(new_dp[(sum + digit) % d][is_smaller || digit < s[where] - '0'], dp[sum][is_smaller]);
                }

        dp = new_dp;
    }

    int answer = (dp[0][false] + dp[0][true]) % mod;
    --answer;

    if(answer < 0)
        answer += mod;

    cout << answer;

    return 0;
}

//product of pairs
#include<bits/stdc++.h>
using namespace std;
long long int mod = 1e9 + 7;
int main()
{
    long long int tt;
    tt = 1;
    while (tt--)
    {
        long long int n, i, count = 0;
        cin >> n;
        long long int a[n + 1], p[n + 1];
        for (i = 1; i <= n; i++)
        {
            cin >> a[i];
        }
        p[n] = a[n];
        for (i = n - 1; i >= 1; i--)
        {
            p[i] = (p[i + 1] + a[i]) % mod;
        }
        for (i = 1; i <= n - 1; i++)
        {
            count = (count + (a[i] * p[i + 1]) % mod) % mod;
        }
        cout << count << " " << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int mat[4][N], val[N], n, num[2], v[N];
void add(int x, int val) {
	for (; x <= n; x += ((x) & (-(x))))
		v[x] += val;
}
int query(int x) {
	int ret = 0;
	for (; x; x -= ((x) & (-(x))))
		ret += v[x];
	return ret;
}
void fail() {
	puts("No");
	exit(0);
}
int main() {
	scanf("%d", &n);
	for (int i = 1; i <= 3; ++i)
		for (int j = 1; j <= n; ++j)
			scanf("%d", &mat[i][j]);
	for (int i = 1; i <= n; ++i) {
		if (mat[1][i] % 3 == 0) {
			if (mat[2][i] == mat[1][i] - 1 && mat[3][i] == mat[1][i] - 2)
				val[i] = -mat[1][i] / 3;
			else fail();
		}
		else if (mat[3][i] % 3 == 0) {
			if (mat[2][i] == mat[3][i] - 1 && mat[1][i] == mat[3][i] - 2)
				val[i] = mat[3][i] / 3;
			else fail();
		}
		else fail();
	}
	for (int i = 1; i <= n; i += 2) {
		if (val[i] < 0) num[1] ^= 1, val[i] = -val[i];
		if (val[i] % 2 == 0) fail();
	}
	for (int i = 2; i <= n; i += 2) {
		if (val[i] < 0) num[0] ^= 1, val[i] = -val[i];
		if (val[i] & 1) fail();
	}
	for (int i = 1; i <= n; i += 2) {
		if ((query(n) - query(val[i])) & 1) num[0] ^= 1;
		add(val[i], 1);
	}
	memset(v, 0, sizeof v);
	for (int i = 2; i <= n; i += 2) {
		if ((query(n) - query(val[i])) & 1) num[1] ^= 1;
		add(val[i], 1);
	}
	if ((!num[0]) && (!num[1]))
		puts("Yes");
	else puts("No");
	return 0;
}


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

const int MAX = 30;
int dp[MAX + 1];

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

	dp[0] = 1;
	for (int i = 0; i <= MAX - 1; i++) {
		for (int j = 1; j <= 3; j++) {
			if (i + j <= MAX)
				dp[i + j] += dp[i];
		}
	}

	int n;
	while (cin >> n , n) {
		cout << ((dp[n] + 9) / 10 + 364) / 365 << endl;
	}
}

#include<iostream>
using namespace std;

int main()
{
    int n;
    long long int e[4000], p[4000], q[4000], sum[4000];
    bool ej[4000];

    while( cin >> n )
    {
        if( n == 0 )
            break;

        for( int i = 0; i < 4000; i++ )
        {
            sum[i] = 0;
            ej[i] = false;
        }

        for( int i = 0; i < n; i++ )
        {
            cin >> e[i] >> p[i] >> q[i];
            sum[e[i]] += p[i] * q[i];
        }

        bool judge = false;
        for( int i = 0; i < n; i++ )
        {
            if( sum[e[i]] >= 1000000 && ej[e[i]] == false )
            {
                cout << e[i] << endl;
                judge = true;
                ej[e[i]] = true;
            }
        }
        if( judge == false )
            cout << "NA" << endl;
    }
    return 0;
}


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

using ll = long long;
using pii = pair<int, int>;
template <class T>
using v = vector<T>;
template <class T>
using vv = v<v<T>>;
#define ALL(c) (c).begin(), (c).end()

int digit(ll i)
{
    ll r = i;
    int ret = 0;
    while (r != 0)
    {
        r /= 10;
        ret++;
    }
    return ret;
}

int main()
{
    ll N;
    cin >> N;
    int ans = INT32_MAX;
    for (ll i = 1; i <= sqrt(N); i++)
    {
        int f = INT32_MAX;
        if (N == N / i * i)
        {
            f = max(digit(i), digit(N / i));
        }
        ans = min(ans, f);
    }

    cout << ans << endl;

    return 0;
}

#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define F first
#define S second
#define Tsetso ios_base::sync_with_stdio(0) ; cin.tie(0) ;
using namespace std;
const ll N = 1e5+10, inf = 1e3+5, mod = 1e9+7, NN = 1e5+10;
int a ;
int main()
{
    Tsetso ;
    int n  , mn = 1e9 ;
    cin >> n ;
    for ( int i = 0 ; i  <n ; i++)
    {
        int cnt = 0 ;
        cin >> a ;
        while (a%2 == 0)
                a /= 2 , cnt++ ;
        mn = min(cnt,mn) ;
    }
    cout << mn ;
}


#include<bits/stdc++.h>
#define L long long
#define vi vector<int>
#define pb push_back
#define pi pair<int,int>
#define pii pair<pi,int>
#define aa first
#define bb second
#define xx aa.aa
#define yy aa.bb
#define zz bb
#define mp make_pair
#define mpp(a,b,c) mp(mp(a,b),c)
using namespace std;
int n,m,p;
L f[600000],g[600000],q,ans;
inline void modify(L f[],int i,int j,int k,int x,L p)
{
	f[i]=min(f[i],p);
	if(j<k)
	  if(x<=(j+k>>1))
	    modify(f,i<<1,j,j+k>>1,x,p);
	  else
	    modify(f,i<<1|1,(j+k>>1)+1,k,x,p);
}
inline L minn(L f[],int i,int j,int k,int l,int r)
{
	if(l<=j && k<=r)
	  return f[i];
	else
	  {
	   L p=1e18;;
	   if(l<=(j+k>>1))
	     p=min(p,minn(f,i<<1,j,j+k>>1,l,r));
	   if(r>(j+k>>1))
	     p=min(p,minn(f,i<<1|1,(j+k>>1)+1,k,l,r));
	   return p;
	  }
}
int main()
{
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	int i,j;
	L k;
	scanf("%d%d",&n,&m);
	for(p=1;p<n;p<<=1);
	for(i=1;i<2*p;i++)
	  f[i]=g[i]=1e18;
	scanf("%d%d",&i,&j);
	modify(f,1,1,p,i,-i);
	modify(g,1,1,p,i,i);
	while(m--)
	  {
	   scanf("%d",&i);
	   q+=abs(i-j);
	   ans+=abs(i-j);
	   k=min(minn(f,1,1,p,1,i)+i,minn(g,1,1,p,i,n)-i)-abs(i-j);
	   modify(f,1,1,p,j,k-j);
	   modify(g,1,1,p,j,k+j);
	   ans=min(ans,q+k);
	   j=i;
	  }
	printf("%lld\n",ans);
	return 0;
}

// Crt. 2019-12-11  21:17:53
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#define N 200005
using namespace std;
typedef long long ll;
const ll INF = 1e15;

struct Segt {
#define ls (p << 1)
#define rs (p << 1 | 1)
    ll sum[N << 2], tag[N << 2];
    Segt() {
        memset(sum, 0, sizeof sum);
        memset(tag, 0, sizeof tag);
    }
    inline void add(int p, ll w) {
        sum[p] += w, tag[p] += w;
    }
    inline void pushdown(int p) {
        if (!tag[p]) return ;
        add(ls, tag[p]);
        add(rs, tag[p]);
        tag[p] = 0;
    }
    void Modify(int l, int r, int L, int R, ll w, int p) {
        if (L <= l && r <= R) return add(p, w);
        int mid = (l + r) >> 1;
        pushdown(p);
        if (L <= mid) Modify(l, mid, L, R, w, ls);
        if (mid < R) Modify(mid + 1, r, L, R, w, rs);
        sum[p] = min(sum[ls], sum[rs]);
    }
    ll Query(int l, int r, int L, int R, int p) {
        if (L <= l && r <= R) return sum[p];
        int mid = (l + r) >> 1; ll res = 0x3f3f3f3f3f3f3f3fll;
        pushdown(p);
        if (L <= mid) res = Query(l, mid, L, R, ls);
        if (mid < R) res = min(res, Query(mid + 1, r, L, R, rs));
        return res;
    }
#undef ls
#undef rs
} x, y, z; // y + z -

inline int Abs(int t) {
    return t < 0 ? -t : t;
}

int n, q, a, b, pos[N];

int main() {
#ifdef LOCAL_JUDGE
    freopen("work.in", "r", stdin);
    freopen("work.out", "w", stdout);
#endif

    cin >> n >> q >> a >> b;

    for (int i = 1; i <= q; ++i)
        scanf("%d", pos + i);

    x.Modify(1, n, 1, n, INF, 1);
    y.Modify(1, n, 1, n, INF, 1);
    z.Modify(1, n, 1, n, INF, 1);

    x.Modify(1, n, a, a, -INF, 1);
    y.Modify(1, n, a, a, -INF + a, 1);
    z.Modify(1, n, a, a, -INF - a, 1);

    pos[0] = b;
    for (int i = 1; i <= q; ++i) {
        ll tmp = min(y.Query(1, n, pos[i], n, 1) - pos[i], z.Query(1, n, 1, pos[i], 1) + pos[i]);
        x.Modify(1, n, 1, n, Abs(pos[i] - pos[i - 1]), 1);
        y.Modify(1, n, 1, n, Abs(pos[i] - pos[i - 1]), 1);
        z.Modify(1, n, 1, n, Abs(pos[i] - pos[i - 1]), 1);
        ll tmp2 = x.Query(1, n, pos[i - 1], pos[i - 1], 1);
        //cout << tmp << " " << tmp2 << " #\n";
        if (tmp < tmp2) x.Modify(1, n, pos[i - 1], pos[i - 1], tmp - tmp2, 1), tmp2 = tmp;
        //cout << x.Query(1, n, pos[i - 1], pos[i - 1], 1) << endl;
        y.Modify(1, n, pos[i - 1], pos[i - 1], tmp2 + pos[i - 1] - y.Query(1, n, pos[i - 1], pos[i - 1], 1), 1);
        z.Modify(1, n, pos[i - 1], pos[i - 1], tmp2 - pos[i - 1] - z.Query(1, n, pos[i - 1], pos[i - 1], 1), 1);
    }

    cout << x.Query(1, n, 1, n, 1) << endl;

    return 0;
}


//Mahir Ratanpara (DA-IICT)
#include<bits/stdc++.h>
using namespace std;
#define Ff(i,a,n) for(i=a;i<n;i++)
#define Fr(i,a,n) for(i=a;i>n;i--)
#define ll long long
#define FAST ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define pb push_back
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
typedef pair<ll, ll>	pa;
typedef vector<ll>		vc;
typedef vector<pa>		vp;
typedef vector<vc>		vvc;
int mpow(int base, int exp);
void ipgraph(ll n,ll m);
void dfs(int u, int par);
const int mod = 1000000007;
const int N = 3e5, M = N;
//=======================

vc g[N];

int main() {
    FAST;
    ll t, i, j, k, p, q, r, x, y, u, v, n, m;
    cin>>n;
	vc a(n);
	Ff(i,0,n)
	cin>>a[i];

	ll ans=0;
	sortall(a);
	Ff(i,0,n)
	{
		Ff(j,i+1,n)
		{
			Ff(k,j+1,n)
			{
				if (a[i] != a[j] && a[j] != a[k] && a[k] < a[i] + a[j])
                    ans++;
			}
		}
	}
	cout<<ans;
    return 0;
}

int mpow(int base, int exp) {
  base %= mod;
  int result = 1;
  while (exp > 0) {
    if (exp & 1) result = ((ll)result * base) % mod;
    base = ((ll)base * base) % mod;
    exp >>= 1;
  }
  return result;
}

void ipgraph(ll n, ll m){
	int i, u, v;
	while(m--){
		cin>>u>>v;
		g[u-1].pb(v-1);
		g[v-1].pb(u-1);
	}
}

void dfs(int u, int par){
	for(int v:g[u]){
		if (v == par) continue;
		dfs(v, u);
	}
}

#include <iostream>
using namespace std;

int func(int x, int y, int z){
//  cout << x << " " << y << " " << z << endl;
  if (x==y||y==z||z==x) return false;
  if (x+y>z&&y+z>x&&z+x>y){
//    cout << "ok" << endl;
    return true;
  }
  return false;
}

int main(void){
  int n,ans;
  int len[200];

  cin >> n;
  for (int i=0; i<n; i++) cin >> len[i];
  ans = 0;
  for (int i=0; i<n; i++){
    for (int j=i+1; j<n; j++){
      for (int k=j+1; k<n; k++){
        ans += func(len[i], len[j], len[k]);
      }
    }
  }
  cout << ans << endl;
  return 0;
}


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

string a26(ll n) {
  string res = "";
  while (n) {
    --n;
    res += (char)('a' + (n % 26));
    n /= 26;
  }
  reverse(res.begin(), res.end());
  return res;
}

ll N;
void input() { cin >> N; }
void solve() { cout << a26(N) << endl; }

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

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

queue<char> que[3];

int rec(int i){
    if(que[i].empty()) return i;
    char q = que[i].front();
    que[i].pop();
    return rec(q - 'a');
}

int main(){
    for(int i = 0; i < 3; i++){
        string s;
        cin >> s;
        for(int j = 0; j < s.size(); j++){
            que[i].push(s[j]);
        }
    }
    cout << (char)(rec(0) + 'A') << endl;
    return 0;
}


#include <bits/stdc++.h>

// #undef DEBUG  // Uncomment this line to forcefully disable debug print.
#if DEBUG
template <typename T>
void debug(const T& value) {
  std::cerr << value;
}
template <typename T, typename... Ts>
void debug(const T& value, const Ts&... args) {
  std::cerr << value << ", ";
  debug(args...);
}
#define dbg(...)                        \
  do {                                  \
    cerr << #__VA_ARGS__ << ": ";       \
    debug(__VA_ARGS__);                 \
    cerr << " (L" << __LINE__ << ")\n"; \
  } while (0)
#else
#define dbg(...)
#endif

void read_from_cin() {}
template <typename T, typename... Ts>
void read_from_cin(T& value, Ts&... args) {
  std::cin >> value;
  read_from_cin(args...);
}
#define in(type, ...) \
  type __VA_ARGS__;   \
  read_from_cin(__VA_ARGS__);

template <typename T>
void write_to_cout(const T& value) {
  std::cout << value << '\n';
}
template <typename T, typename... Ts>
void write_to_cout(const T& value, const Ts&... args) {
  std::cout << value << ' ';
  write_to_cout(args...);
}
#define out(...) write_to_cout(__VA_ARGS__);

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

using ll = long long;

template <typename T>
using V = std::vector<T>;
template <typename T>
using VV = V<V<T>>;

using namespace std;

template <typename T>
class BIT {
 public:
  BIT(int n) : v_(n) {}
  T Sum(int i) const {
    T ret = 0;
    while (i >= 0) {
      ret += v_[i];
      i = (i & (i + 1)) - 1;
    }
    return ret;
  }
  T Get(int i) const { return Sum(i) - Sum(i - 1); }
  void Add(int i, T v) {
    while (i < v_.size()) {
      v_[i] += v;
      i |= i + 1;
    }
  }

 private:
  std::vector<T> v_;
};

using P = pair<int, int>;

struct Query {
  int i, l, r;
};

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

  in(int, N, M, Q);

  V<P> trains;
  rep(i, M) {
    in(int, L, R);
    trains.push_back({L - 1, R - 1});
  }
  sort(all(trains), [](const P& a, const P& b) { return a.second < b.second; });

  V<Query> queries;
  rep(i, Q) {
    in(int, p, q);
    queries.push_back({i, p - 1, q - 1});
  }
  sort(all(queries), [](const Query& a, const Query& b) { return a.r < b.r; });

  V<int> ans(Q);
  BIT<int> bit(N);
  auto ti = trains.begin();
  auto qi = queries.begin();
  rep(r, N) {
    for (; ti != trains.end() && ti->second <= r; ++ti) {
      bit.Add(ti->first, 1);
    }
    for (; qi != queries.end() && qi->r <= r; ++qi) {
      ans[qi->i] = bit.Sum(qi->r) - bit.Sum(qi->l - 1);
    }
  }
  rep(i, Q) out(ans[i]);
}

#include <bits/stdc++.h>

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

using namespace std;
using ll = long long;

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

int dp[505][505];
void solve()
{
  int n, m, q, l, r, ql, qr;
  cin >> n >> m >> q;
  for(int i = 0; i < m; ++i)
  {
    cin >> l >> r;
    dp[l][r]++;
  }

  for(int y = 1; y <= n; ++y)
  {
    for(int x = 1; x <= n; ++x)
    {
      dp[y][x] += dp[y][x - 1];
    }
  }

  for(int x = 1; x <= n; ++x)
  {
    for(int y = 1; y <= n; ++y)
    {
      dp[y][x] += dp[y - 1][x];
    }
  }

  for(int i = 0; i < q; ++i)
  {
    cin >> ql >> qr;
    ql--;
    cout << dp[qr][qr] - dp[ql][qr] - dp[qr][ql] + dp[ql][ql] << endl;
  }
}

int main()
{
  fastio;
  solve();

  return 0;
}


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

template<typename T>
struct Segtree {
    int n;
    T e;
    vector<T> dat;
    typedef function<T(T a, T b)> Func;
    Func f;

    Segtree(){}
    Segtree(int n_input, Func f_input, T e_input){
        initialize(n_input, f_input, e_input);
    }
    void initialize(int n_input, Func f_input, T e_input){
        f = f_input;
        e = e_input;
        n = 1;
        while(n < n_input) n <<= 1;
        dat.resize(2*n-1, e);
    }

    void update(int k, T a){
        k += n - 1;
        dat[k] = a;
        while(k > 0){
            k = (k - 1)/2;
            dat[k] = f(dat[2*k+1], dat[2*k+2]);
        }
    }

    T get(int k){
        return dat[k+n-1];
    }

    T between(int a, int b){
        return query(a, b+1, 0, 0, n);
    }

    T query(int a, int b, int k, int l, int r){
        if(r<=a || b<=l) return e;
        if(a<=l && r<=b) return dat[k];
        T vl = query(a, b, 2*k+1, l, (l+r)/2);
        T vr = query(a, b, 2*k+2, (l+r)/2, r);
        return f(vl, vr);
    }
};

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

struct UnionFind {
    vector<int> par;
    vector<int> sz;

    UnionFind(int n=0){
        if(n>0) initialize(n);
    }

    void initialize(int n){
        par.resize(n);
        sz.resize(n);
        for(int i=0; i<n; i++){
            par[i] = i;
            sz[i] = 1;
        }
    }

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

    void unite(int x, int y){
        x = find(x);
        y = find(y);
        if(x == y) return;
        if(sz[x] < sz[y]){
            par[x] = y;
            sz[y] += sz[x];
        }else{
            par[y] = x;
            sz[x] += sz[y];
        }
    }

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

int main(){
    int N;
    int64_t D, A[200000];
    cin >> N >> D;
    for(int i=0; i<N; i++) cin >> A[i];

    typedef pair<int64_t, int> P;
    const int64_t INF = 1e18;
    const P INFP = {INF, 0};
    Segtree<P> stL(N, [](P a, P b){ return min(a, b);}, INFP);
    Segtree<P> stR(N, [](P a, P b){ return min(a, b);}, INFP);

    vector<int> order;
    for(int i=0; i<N; i++) order.push_back(i);
    sort(order.begin(), order.end(), [&](int i, int j){ return A[i] < A[j];});
    vector<vector<int64_t>> edges;

    for(int i : order){
        auto p = stL.between(0, i);
        if(p != INFP){
            int64_t cost = p.first + A[i] - (N-i)*D;
            edges.push_back({cost, i, p.second});
        }
        p = stR.between(i, N-1);
        if(p != INFP){
            int64_t cost = p.first + A[i] - i*D;
            edges.push_back({cost, i, p.second});
        }
        stL.update(i, {A[i] + (N-i)*D, i});
        stR.update(i, {A[i] + i*D, i});
    }

    sort(edges.begin(), edges.end());
    UnionFind uf(N);
    int64_t ans = 0;
    for(auto& e : edges){
        if(!uf.same(e[1], e[2])){
            uf.unite(e[1], e[2]);
            ans += e[0];
        }
    }

    cout << ans << endl;
    return 0;
}

#include <iostream>
#include <algorithm>

using namespace std;

typedef long long ll;

int main(){
    ll N,D;
    ll A[200005];
    cin>>N>>D;
    ll ret = D*(N-1);
    for(int i = 0;i < N;i++){
        cin>>A[i];
        ret += A[i];
    }
    for(int i = 1;i < N;i++){
        A[i]=min(A[i-1]+D,A[i]);
    }
    for(int i = N-2;i >=0;i--){
        A[i]=min(A[i+1]+D,A[i]);
    }
    for(int i = 1;i < N-1;i++){
        ret += A[i];
    }
    cout<<ret<<endl;

}


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

int main() {
  string S;
  cin >> S;
  S = regex_replace(S, regex("hi"), "");
  cout << ((S.size() == 0) ? "Yes" : "No") << "\n";
}

#include <iostream>
using namespace std;

string s;

int main() {
    cin >> s;
    if (s.length() % 2 != 0) {
        cout << "No\n";
        return 0;
    }
    bool works = true;
    for (int i=0; i<s.length(); i+=2) {
        if (s[i] != 'h' || s[i + 1] != 'i')
            works = false;
    }
    if (works)
        cout << "Yes\n";
    else
        cout << "No\n";
}


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

int main(){
    string s;
    cin >> s;
    string ans = "";
    for(int i = 0; i+8 < s.length(); i++) ans += s[i];
    cout << ans << endl;
    return 0;
}

#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <vector>
#include <stack>
#include <queue>
#include <tuple>
#include <cassert>
#include <functional>
#include <set>
#include <deque>
#include <list>
using namespace std;
typedef long long ll;
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
#define LNF __LONG_LONG_MAX__
const ll MOD = 1000000007;
const int MAX = 200000;

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

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

// 二項係数計算
long long COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
// 法mでのaの逆元を計算
long long modinv(long long a, long long m) {
    long long b = m, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
}
// a^n mod を計算する
long long modpow(long long a, long long n, long long mod) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}
//最大公約数
long long gcd(long long x, long long y){
    if(y==0) return x;
    else return gcd(y,x%y);
}
//最小公倍数
long long lcm(long long x, long long y){
    return x / gcd(x, y) * y;
}
// nが大きく,kが10^7程度の時の二項係数
long long com[200005];
void combination(long long n,long long k){
    com[0] = 1;
    for(int i=1;i<=k;i++){
        com[i] = (com[i-1]*(n-i+1)%MOD)*modinv(i,MOD)%MOD;
    }
}

int main(){
    int A,B,C,D; cin >> A >> B >> C >> D;
    cout << min(A,B)+min(C,D) << endl;
    return 0;
}