#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
inline int read(){
	int x=0,f=1;
	char c=getchar();
	while(c<'0'||c>'9'){
		if(c=='-')f=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9'){
		x=(x<<1)+(x<<3)+c-'0';
		c=getchar();
	}
	return x*f;
}
int A,B;
const int c[5]={0,300000,200000,100000,0};
int main(){
	A=read(),B=read();
	if(A==1&&B==1){
		return !printf("1000000\n");
	}
	A=min(A,4),B=min(B,4);
	printf("%d\n",c[A]+c[B]);
	return 0;
}

#include <bits/stdc++.h>
#include <string>
#include <algorithm>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <float.h>
#include <iomanip>
#include <iostream>
using namespace std;

int main(){

    int A,B,ans=0;
    cin>>A>>B;

    for(int i=1;i<=3;i++){
        if(A==i)ans+=(4-i)*100000;
        if(B==i)ans+=(4-i)*100000;
    }

    if(A==1&&B==1)ans+=400000;

    cout <<ans<<endl;

    return 0;

}


#include <bits/stdc++.h>

using namespace std;

int main()
{
	int N;
	cin >> N;
	vector<int>D(N);
	for(size_t i = 0; i < N; i++)
	{
		cin >> D[i];
	}
	for(size_t i = 0; i < N; i++)
	{
		cout << D[N - i - 1] << ( i == N - 1 ? "\n" : " " );
	}
}


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

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

bool canmove(int x,int y){
  return (0 <= x && x < w && 0 <= y && y < h);
}

struct Map{
  int map[20][20];
  int s,g;
  int cnt;
  int cur;
  Map(int m[20][20]){
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	map[i][j]=m[i][j];
	if(m[i][j]==2) s=i*32+j;
	if(m[i][j]==3) g=i*32+j;
      }
    }
    cnt=0;
    cur=s;
  }
  Map(int m[20][20],int curling,int _cnt){
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	map[i][j]=m[i][j];
	if(m[i][j]==2) s=i*32+j;
	if(m[i][j]==3) g=i*32+j;
      }
    }
    cnt=_cnt;
    cur=curling;
  }
  Map move(int direc){
    Map m(map,cur,cnt+1);
    int y=cur/32,x=cur%32;
    if(canmove(x+dx[direc],y+dy[direc]) && map[y+dy[direc]][x+dx[direc]]==1){
      m.cnt=100;
      return m;
    }
    int k=1;
    while(true){
      bool b=canmove(x+k*dx[direc],y+k*dy[direc]);
      int num=map[y+k*dy[direc]][x+k*dx[direc]];
      if(b && num==3){
	m.cur=g;
	break;
      }
      else if(b && num!=1) k++;
      else if(b && num==1){
	m.map[y+k*dy[direc]][x+k*dx[direc]]=0;
	k--;
	m.cur=(y+k*dy[direc])*32+x+k*dx[direc];
	break;
      }else{
	m.cnt=100;
	break;
      }
    }
    // cout<<m.cnt<<endl;
    return m;
  }
};

void dfs(Map m){
  if(m.cnt > 10) return;
  if(m.cnt >= ans) return;
  if(m.cur == m.g){
    ans = min(m.cnt,ans);
    return;
  }
  for(int i=0;i<4;i++){
    dfs(m.move(i));
  }
}

int main(){
  while(cin>>w>>h,w+h){
    int map[20][20];
    ans=1000;
    for(int i=0;i<h;i++) for(int j=0;j<w;j++) cin>>map[i][j];
    Map m(map);
    dfs(m);
    if(ans == 1000) cout<<-1<<endl;
    else cout<<ans<<endl;
  }
  return 0;
}

#include <map>
#include <cmath>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 105;
const int mod = 1e9 + 7;
short dp[maxn][maxn][maxn][maxn];
short row[maxn][maxn], col[maxn][maxn];
char mp[maxn][maxn];
short max(short a, short b) {
    return a>b? a : b;
}
int main()
{
    int n, m, ex, ey;
    scanf("%d%d", &n, &m);
    for (int i=1; i<=n; i++) scanf("%s", mp[i]+1);
    for (int i=1; i<=n; i++) for (int j=1; j<=m; j++) {
            if (mp[i][j] == 'E'){
                ex = i, ey = j;
                break;
            }
    }
    for (int i=1; i<=n; i++) for (int j=1; j<=m; j++) {
            row[i][j] = row[i][j-1] + (mp[i][j] == 'o');
            col[i][j] = col[i-1][j] + (mp[i][j] == 'o');
    }
    int L = ey - 1, R = m - ey, U = ex - 1, D = n - ex;
    memset(dp, -1, sizeof dp);
    dp[0][0][0][0] = 0;
    short ans = 0;
    for (int l=0; l<=L; l++){
        for (int u=0; u<=U; u++){
            for (int r=0; r<=R; r++){
                for (int d=0; d<=D; d++) if(dp[l][u][r][d] != -1){
                    int up = max(ex - u, d + 1), down = min(ex + d, n - u);
                    int left = max(ey - l, r + 1), right = min(ey + r, m - l);
                    if (up > down || left > right) continue;
                    ans = max(ans, dp[l][u][r][d]);
                    short add;
                    //printf("\n=========%d %d %d %d -> %d %d %d %d=========\n",l,u,r,d,left,up,right,down);
                    if (l+r < L){
                        add = col[down][ey-l-1] - col[up-1][ey-l-1];
                        //printf("l: %d ",add);
                        dp[l+1][u][r][d] = max(dp[l+1][u][r][d], dp[l][u][r][d] + add);
                    }
                    if(u+d < U){
                        add = row[ex-u-1][right] - row[ex-u-1][left-1];
                        //printf("u: %d ",add);
                        dp[l][u+1][r][d] = max(dp[l][u+1][r][d], dp[l][u][r][d] + add);
                    }
                    if(l+r < R){
                        add = col[down][ey+r+1] - col[up-1][ey+r+1];
                        //printf("r: %d ",add);
                        dp[l][u][r+1][d] = max(dp[l][u][r+1][d], dp[l][u][r][d] + add);
                    }
                    if(u+d < D){
                        add = row[ex+d+1][right] - row[ex+d+1][left-1];
                        //printf("d: %d ",add);
                        dp[l][u][r][d+1] = max(dp[l][u][r][d + 1], dp[l][u][r][d] + add);
                    }
                }
            }
        }
    }
    printf("%d\n", ans);
    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 <cmath>
#include <iomanip>
#include <ctype.h>

using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi >;
using vl = vector<ll>;
using vvl = vector<vl >;
using pairi = pair<int, int>;
using pairl = pair<ll, ll>;

#define TR ","
#define TS " "
#define rep(i,N) for(ll i=0;i<(ll)N;++i)
#define rpt(i,a,b) for(ll i=a;i<(ll)b;++i)
#define all(v) v.begin(), v.end()
#define IO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)

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

ll lcm(ll x, ll y) {
    return x / gcd(x, y) * y;
}

void printVector(const vector<ll>& vec) {
    for (ll value : vec) {
        cout << value << " ";
    }
    cout << endl;
}

vector<int> IntegerToVector(int bit, int N) {
    vector<int> S;
    for (int i = 0; i < N; ++i) {
        if (bit & (1 << i)) {
            S.push_back(i);
        }
    }
    return S;
}

int main()
{
    IO;

    ll N; cin >> N;
    string str; cin >> str;
    vl wkosuu(N), ekosuu(N);

    ll x = 0, y = 0, z=0;

    rep(i, N) {
        if (str[i] == 'E') { ++y; }
        else if (str[i] == 'W') { ++z; }
    }

    wkosuu[0] = 0;
    wkosuu[N - 1] = z;
    ekosuu[0] = y;
    ekosuu[N - 1] = 0;

    for (ll i = 1; i < N-1; ++i) {
        if (str[i-1] == 'E') {
            wkosuu[i] = wkosuu[i - 1];
        }
        else if (str[i-1] == 'W') {
            ++x;
            wkosuu[i] = x;
        }
    }

    for (ll i = 0; i < N-1; ++i) {
        if (str[i] == 'W') {
            if (i > 0) {
                ekosuu[i] = ekosuu[i - 1];
            }
        }
        else if (str[i] == 'E') {
            --y;
            ekosuu[i] = y;
        }
    }

    ll ans = N;

    rep(i, N) {
        ans = min(ans, wkosuu[i] + ekosuu[i]);
    }

    cout << ans << endl;

}

#include <bits/stdc++.h>
using namespace std;
int main(){
  int N; cin>>N;
  string S; cin>>S;
  vector<int>E(N);
  vector<int>W(N);
  for(int i=0;i<N;i++){
    if(S.at(i)=='E')E[i]++;
    else W[i]++;
    if(i!=0){E[i]+=E[i-1];W[i]+=W[i-1];}
  }
  vector<int>ans(N);
  for(int i=0;i<N;i++){
    if(i!=0)ans[i]+=W[i-1];
    ans[i]+=E[N-1]-E[i];
  }
  sort(ans.begin(),ans.end());
  cout<<ans[0]<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int gcd(int a,int b){return b%a ? gcd(b%a,a) : a;}
int main(){
    int N;
    cin>>N;
    int a,b,c;
    cin>>a>>b;
    if(N==3) cin>>c;
    int tmp=gcd(a,b);
    if(N==3) tmp=gcd(tmp,c);
    for(int i=1;i<=tmp;i++){
        if(tmp%i==0) cout<<i<<endl;
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
#define MOD 1000000007
const int INF = 1LL<<30;

int main(){
    ll l,r;
    cin>>l>>r;

    ll ans=3000;
    for(int i=l;i<=min(r,l+3000);i++){
        for(int j=i+1;j<=min(r,l+3000);j++){
            ll tmp=((i%2019)*(j%2019))%2019;
            ans=min(ans,tmp);
        }
    }

    cout<<ans<<endl;
}


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

int main() {
    ll n, m;
    cin >> n >> m;
    vector<ll> a(n, -1), s(n), c(n);
    rep1(i, m) cin >> s[i] >> c[i];
    rep1(i, m){
        if (a[s[i]-1]!=-1 && a[s[i]-1]!=c[i]){
            cout << -1 << endl;
            return 0;
        }
        a[s[i]-1] = c[i];
    }
    if (a[0]==0 && n>1){
        cout << -1 << endl;
        return 0;
    }
    rep1(i, n){
        if (i==0 && n>1){
            if (a[i]==-1) cout << 1;
            else cout << a[i];
        }else{
            if (a[i]==-1) cout << 0;
            else cout << a[i];
        }
    }
}

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

int main(int argc, char **argv)
{
	string n;
  ll k;
  cin>>n;
  cin>>k;
  set <ll> arr;
  for(ll i=0;i<k;i++){
    ll x;
    cin>>x;
    arr.insert(x);
  }

  // checking for the case when the n is fine
  ll flag=1;
  for(ll i=0;i<n.size();i++){
    ll temp=n[i]-'0';
    if (arr.find(temp)!=arr.end()){
      flag=0;
      break;
    }
  }

  if (flag) {
    cout<<n<<"\n";
    return 0;
  }

  ll num=stoi(n);
  //cout<<"pika"<<"\n";
  while(num){
    ll ok=1;
    ll temp=num;
    while(temp!=0){
      ll digit=temp%10;
      temp=temp/10;
      //cout<<digit<<" "<<temp<<"\n";
      if (arr.find(digit)!=arr.end()){
        ok=0;
        break;
      }
    }
    if (ok){
      cout<<num<<"\n";
      return 0;
    }
    num++;
  }

	return 0;
}


#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iterator>
#include <bitset>
#include <unordered_set>
#include <unordered_map>
#include <fstream>
#include <iomanip>
#include <cassert>
//#include <utility>
//#include <memory>
//#include <functional>
//#include <deque>
//#include <cctype>
//#include <ctime>
//#include <numeric>
//#include <list>
//#include <iomanip>

//#if __cplusplus >= 201103L
//#include <array>
//#include <tuple>
//#include <initializer_list>
//#include <forward_list>
//
//#define cauto const auto&
//#else

//#endif

using namespace std;

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

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()
#define DEBUG
#ifdef DEBUG
#define dump(x)  cout << #x << " = " << (x) << endl;
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#else
#define dump(x)
#define debug(x)
#endif
#define LDcout(x,n) fixed<<setprecision(n)<<x

#define MOD 1000000007LL
#define EPS 1e-8
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
#define maxs(x,y) x=max(x,y)
#define mins(x,y) x=min(x,y)

void mainmain(){
	int n;
	while(cin>>n,n){
		rep(i,n){
			vint v(3);
			rep(j,3) cin>>v[j];
			vint w=v;
			sort(ALL(w));
			if(w[2]==100) cout<<"A"<<endl;
			else if(v[0]+v[1]>=180) cout<<"A"<<endl;
			else if(v[0]+v[1]+v[2]>=240) cout<<"A"<<endl;
			else if(v[0]+v[1]+v[2]>=210) cout<<"B"<<endl;
			else if(v[0]+v[1]+v[2]>=150&&max(v[0],v[1])>=80) cout<<"B"<<endl;
			else cout<<"C"<<endl;
		}
	}
}

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

#include<iostream>
#include<cstdio>
using namespace std;
#define N 200012
int n,a[N],las=0;bool vis[N];long long ans=0;
int main(){
	scanf("%d",&n);int i,l,r,lef;bool fl=true;
	for(i=1;i<=n;i++)scanf("%d",&a[i]);
	for(i=1;i<=n;i=r+1)
	{
		l=r=i;while((r<n)&&(a[r+1]==(a[r]+1)))++r;
		lef=l-a[l];if(lef<=las){printf("-1");fl=false;break;}
		ans+=r-lef;las=lef;
	}if(fl)printf("%lld",ans);return 0;
}


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

using namespace std;

struct edge{
	int u;	//?????????
	int v;	//??°??????
	int cost;	//??????
	//???????????????????????§?????§?°???¢????????????
	bool operator<(const edge& another) const {
			return cost < another.cost;
	}
};

const int MAX_N = 100000;
const int MAX_M = 100000;

edge es[MAX_M];
int par[MAX_N];
int rank[MAX_N];

void initial(int n){		//?????????
	for(int i=0;i<n;i++){
		par[i] = i;
		rank[i] = 0;
	}
}

int find(int x){		//??¨??????????±???????
	if(par[x] == x){
		return x;
	}else{
		return par[x] = find(par[x]);
	}
}

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

bool same(int x,int y){	//x,y???????????¨???????????§?????????
	return find(x) == find(y);
}

bool comp(const edge& e1,const edge& e2){
	return e1.cost < e2.cost;
}

int kruskal(int n,int m){
	initial(n);
	int res = 0;
	for(int i=0;i<m;i++){
		edge e = es[i];
		if(!same(e.u,e.v)){
			unite(e.u,e.v);
			res += e.cost;
		}
	}
	return res;
}

int main(){
	int n,m,j,k,l;
	cin >> n;	//????????°?????\???
	cin >> m;	//?????°?????\???
	for(int i=0;i<m;i++){
		cin >> j >> k >> l;
		es[i].u = j;	es[i].v = k;	es[i].cost = l;
	}
	sort(es,es+m);
	cout << kruskal(n,m) << "\n";
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using vll = vector<ll> ;
using vs = vector<string> ;
#define all(v) v.begin(),v.end()
const int MAX_V = 210;
const int INF = 1e5;

int V;
vll G[MAX_V];
int match[MAX_V];
bool used[MAX_V];

void add_edge(int u,int v){
  G[u].push_back(v);
  G[v].push_back(u);
}

bool dfs(int v){
  used[v]=true;
  rep(i,G[v].size()){
    int u = G[v][i], w = match[u];
    if(w<0 || !used[w] && dfs(w)){
      match[v] = u;
      match[u] = v;
      return true;
    }
  }
  return false;
}

int bipartite_matching(){
  int res = 0;
  memset(match, -1, sizeof(match));
  rep(v,V){
    if(match[v]<0){
      memset(used, 0, sizeof(used));
      if(dfs(v)) res++;
    }
  }
  return res;
}

int main(){
  cin>>V;
  vector<pair<int,int>> red,blue;
  rep(i,V){
    int a,b;cin>>a>>b;red.push_back(make_pair(a,b));
  }
  rep(i,V){
    int c,d;cin>>c>>d;blue.push_back(make_pair(c,d));
  }
  rep(i,V){
    rep(j,V){
      pair<int,int> r,b;
      r = red[i],b=blue[j];
      if(r.first<b.first && r.second<b.second) add_edge(i,j+V);
    }
  }
  //cout << "グラフ" << endl;
  //rep(i,V)rep(j,G[i].size()) cout<< i << " " << G[i][j]<<endl;
  cout << bipartite_matching() << endl;
}


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

#define lli long long int
#define REP(i,s,n) for(int i=s;i<n;i++)
#define MOD 1000000007
#define NUM 2520
#define INF (1LL<<50)
#define DEBUG 0
#define mp(a,b) make_pair(a,b)
#define SORT(V) sort(V.begin(),V.end())
#define PI (3.141592653589794)

const int MAX = 510000;

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

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

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

void func(long long N, long long M, long long K){
    COMinit();
    lli sum = 0;

    REP(i,0,N){
        REP(j,0,M){
            if(i!=0 && j!=0) sum += (2*( N-i)*(M-j)*(i+j))%MOD;
            else sum += ((N-i)*(M-j)*(i+j))%MOD;
            sum%= MOD;
        }
    }

    sum *= COM(N*M-2,K-2);
    sum %= MOD;
    cout<<sum<<endl;

}

int main(){
    // cout << fixed << setprecision(5);

    long long N;
    scanf("%lld",&N);
    long long M;
    scanf("%lld",&M);
    long long K;
    scanf("%lld",&K);
    func(N, M, K);
    return 0;
}

#include <bits/stdc++.h>
#define ll long long int
#define MOD 1000000007
#define INF 1000000000000000000

using namespace std;
ll power(ll n, ll k){
  ll res = 1;
  while(k > 0){
    if (k & 1)
      res = res * n % MOD;
    n = n * n % MOD;
    k >>= 1;
  }
  return res;
}

ll genfact(ll n, ll k){

  ll res = 1;
  k--;
  while (k >= 0){
    res = (res * (n - k)) % MOD;
    k--;
  }

  return res;
}

ll inv(ll n){
  return power(n, MOD - 2);
}

ll fact(ll n){
  return genfact(n, n - 1);
}

ll nck(ll n, ll k){
  if (k == 0)
    return 1;
  if (n <= 0)
    return 0;

  return ((genfact(n, k) % MOD) * inv(fact(k))) % MOD;
}

int main(void){
  ll n, m, k;
  cin >> n >> m >> k;

  ll ans = 0;

  ll t = nck(n*m-2,k-2);
  for (ll j = 1; j < m; j++){
    ans += j*(m-j) % MOD * n % MOD *n % MOD * t % MOD;
    ans = (ans % MOD);
  }
  for (ll i = 1; i < n; i++){
    ans += i*(n-i) % MOD * m % MOD *m % MOD * t % MOD;
    ans = (ans % MOD);
  }
  cout << ans << endl;

  return 0;
}


#include <bits/stdc++.h>
#define PI 3.1415926535897932
#define _GLIBCXX_DEBUG
using namespace std;
long long GCD(long long A,long long B){
  if(B==0){
    return A;
  }
  else{
    return GCD(B,A%B);
  }
}
int main() {

  string A,B,C;
  cin>>A>>B>>C;

  deque<char> X;
  for(int i=0;i<A.size();i++){
    X.push_back(A[i]);
  }

  deque<char> Y;
  for(int i=0;i<B.size();i++){
    Y.push_back(B[i]);
  }

  deque<char> Z;
  for(int i=0;i<C.size();i++){
    Z.push_back(C[i]);
  }

  int check=1;

  string ans;

  while(true){
    if(check==1){
      if(X.empty()){
       ans="A";
       break;
      }
      if(X.front()=='a'){
        check=1;
        X.pop_front();
      }
      else if(X.front()=='b'){
        check=2;
        X.pop_front();
      }
      else if(X.front()=='c'){
        check=3;
        X.pop_front();
      }
    }
    else if(check==2){
            if(Y.empty()){
        ans="B";
        break;
    }
      if(Y.front()=='a'){
        check=1;
        Y.pop_front();
      }
      else if(Y.front()=='b'){
        check=2;
        Y.pop_front();
      }
      else if(Y.front()=='c'){
        check=3;
        Y.pop_front();
      }
    }
    else if(check==3){
       if(Z.empty()){
        ans="C";
        break;
      }
      if(Z.front()=='a'){
        check=1;
        Z.pop_front();
      }
      else if(Z.front()=='b'){
        check=2;
        Z.pop_front();
      }
      else if(Z.front()=='c'){
        check=3;
        Z.pop_front();
      }
    }
    }
  cout<<ans<<endl;
}

#include <bits/stdc++.h>
template<class T> inline bool chmin(T&a, T b){if(a > b){a = b; return true;}else{return false;}}
template<class T> inline bool chmax(T&a, T b){if(a < b){a = b; return true;}else{return false;}}
#define ll long long
#define double long double
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,n) for(int i=1;i<=(n);i++)
#define mod (ll)(1e9+7)
#define inf (ll)(3e18+7)
#define pi (double) acos(-1.0)
#define P pair<int,int>
#define PiP pair<int,pair<int,int>>
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
using namespace std;

bool check(int w, int bit){
    bool ok = true;
    vector<int> a(w);
    rep(i, w){
        if(bit & (1<<i))a[i] = 1;
        else a[i] = 0;
    }
    REP(i, w-1)if(a[i] == a[i-1] && a[i] == 1)ok = false;
    return ok;
}

int main() {
    int h, w, k;
    cin >> h >> w >> k;
    vector<vector<ll>> dp(h+1, vector<ll>(w, 0));
    dp[0][0] = 1;
    rep(i, h){
        for(int bit = 0; bit < (1<<(w-1)); bit++){
            if(!check(w-1, bit))continue;
            vector<int> to(w);
            rep(j, w)to[j] = j;
            rep(j, w-1){
                if(bit & (1<<j)){
                    to[j] = j+1;
                    to[j+1] = j;
                }
            }
            rep(j, w){
                dp[i+1][to[j]] += dp[i][j];
                dp[i+1][to[j]] %= mod;
            }
        }
    }
    cout << dp[h][k-1] << endl;
}


#include <iostream>
#include <numeric>
#include <stdio.h>
#include <iomanip>
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
using namespace std;
using ll = long long;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
int Max(int(a), int(b), int(c)) {
	return max(max(a, b), c);
}
int Min(int(a), int(b), int(c)) {
	return min(min(a, b), c);
}

int main() {
	ll N,D=0;
	cin >> N ;
	vector<ll> A(N);
	vector<ll> B(N);
	vector<ll> W(N);
	for (int i = 0; i < N; i++) {
		cin >> A.at(i)>>B.at(i);
		W.at(i)= A.at(i) + B.at(i);
		D -= (B.at(i));
	}
	sort(W.begin(), W.end());
	reverse(W.begin(), W.end());
	rep(i, (N+1)/2) {
		D += W.at(i*2);
	}

	cout << D << endl;

}

// http://algorithms.blog55.fc2.com/blog-entry-66.html

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

#define M 100

int N,R[M+1],C[M][M],B[M][M];

void compute(){
	int i=0,j,k,cost;
	memset(C,99,sizeof(C));
	for(;i<N;i++)C[i][i]=0;
	for(j=1;j<N;j++)
		for(i=0;i<N - j; i++ )
			for (k = i; k < i + j; k++ ){
				cost = C[i][k] + C[k+1][i+j] + R[i]*R[k+1]*R[i+j+1];
				if(cost<C[i][i+j])C[i][i+j]=cost,B[i][i+j]=k;
			}
}

void order( int i, int j ){
	if ( i == j ) cout << "M" << i;
	else{
		cout << "(";
		order( i, B[i][j]-1 ); order(B[i][j], j);
		cout << ")";
	}
}

void input(){
	cin >> N;
	int z,i=0;
	for (; i < N; i++ ) cin >> R[i]>>z;
	R[i]=z;
	//SIZE++;
}

void output(){
	cout <<  C[0][N-1] << endl;
	//order(0, N);
	//cout << endl;
}

main(){
	input();
	compute();
	output();
}


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

int main() {
  int num;
  int tmpCnt1,tmpCnt2;
  int Max1 = 0, Max2 = 0;
  string str1, str2;
  cin >> num >> str1 >> str2;

  if(str1 == str2){
    cout << num;
    return 0;
  }

  for(int i = 0; i < num; i++){
    tmpCnt1 = 0;

    if(str1.at(i) == str2.at(0)){
      for(int j = 0; j < num - i; j++){
        if(str1.at(i+j) == str2.at(j)) tmpCnt1++;
        else break;
      }
      Max1 = max(Max1, tmpCnt1);
    }
  }

  for(int i = 0; i < num; i++){
    tmpCnt2 = 0;

    if(str2.at(i) == str1.at(0)){
      for(int j = 0; j < num - i; j++){
        if(str2.at(i+j) == str1.at(j)) tmpCnt2++;
        else break;
      }
      Max2 = max(Max2, tmpCnt2);
    }
  }

  if(Max1 == 0 && Max2 == 0){
    cout << num * 2;
  }else if(Max1 >= Max2){
    cout << num + (num - Max1);
  }else{
    cout << num + (num - Max2);
  }
}

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

int main(void){
        int N;
        string s, t;
        cin >> N >> s >> t;
        int ans = 2*N;
        rep(i,N) {
                if (s.substr(i,N-i) == t.substr(0,N-i) ){ ans = N+i; break;}
        }
        cout << ans << endl;

        return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int n,m,a[1000];
main()
{
	while(cin>>n>>m,n)
	{
		int ans=0;
		for(int i=0;i<n;i++)
		{
			cin>>a[i];
			ans+=a[i];
		}
		sort(a,a+n);
		for(int i=n-m;i>=0;i-=m)
		{
			ans-=a[i];
		}
		cout<<ans<<endl;
	}
}

#include<iostream>
using namespace std;

int main(){
  int n,m;
  while(1){
  int  ans=0,p[10001]={};
  cin>>n>>m;
  if(n+m==0)break;
  for(int i=1;i<=n;i++){
    cin>>p[i];
  }

  for(int j=0;j<n;j++)
    for(int i=1;i<=n;i++)
      if(p[i]<p[i+1])swap(p[i],p[i+1]);

  for(int i=1;i<=n;i++){
    if(i%m==0)p[i]=0;
    ans+=p[i];
  }

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


#include <bits/stdc++.h>
using namespace std;
int func(int x){
  string y=to_string(x);
  vector<char> a(y.size());
  vector<char> b(y.size());
  for(int i=0;i<y.size();i++){
    a.at(i)=y.at(i);
    b.at(i)=y.at(i);
  }
  reverse(b.begin(),b.end());
  int c=0;
  for(int i=0;i<y.size();i++){
    if(a.at(i)!=b.at(i))
      c=1;
  }
  if(c==0)
    return 1;
  return 0;

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

}

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

int main()
{
    int a, b;
    cin >> a >> b;
    int r = 0;
    for (int i = a; i <= b; i++)
    {
        int s = i % 10;
        int t = i / 10000 % 10;
        int u = i / 10 % 10;
        int v = i / 1000 % 10;
        if (s == t && u == v)
        {
            r++;
        }
    }
    cout << r << endl;
    /*
    5桁の回文数 9 * 9 * 10 = 810
    1桁 & 5桁目の取りうる範囲
    2桁 & 4桁目の取りうる範囲
    */
}


#include <bits/stdc++.h>
// #include "ane.cpp"

const int INF  = 1e9;
const long long INFLL = 1e18;
const int NMAX = 100005;
const int MMAX = 100005;
const int KMAX = 1005;
const int MOD  = 1e9 + 7;
using namespace std;

// comment to disable debug functions
#define DEBUG

// frequently used macros

#if __cplusplus >= 201103L
#define ALL(v) begin(v),end(v)
#define SORT(v) sort(begin(v), end(v))
#define FIND(v,x) find(begin(v), end(v), (x))
#else
#define ALL(v) (v).begin(),(v).end()
#define SORT(v) sort(v.begin(), v.end())
#define FIND(v,x) find(v.begin(), v.end(), (x))
#endif

#define MEMNEXT(from, to) do{ memmove((to), (from), sizeof(from)); \
memset((from), 0, sizeof(from)); } while(0)
#ifdef DEBUG
#define DUMP(x) do{ std::cerr << (#x) << ": " << x << std::endl; }while(0)
#else
#define DUMP(x) do{}while(0)
#endif

// frequent used aliases
typedef long long ll;
typedef pair<int, int> p;
typedef pair<ll, int> pl;
typedef pair<ll, ll> pll;
typedef vector<int> vec;
typedef vector<ll> vecll;
typedef vector<vec> mat;
typedef vector<vecll> matll;

// frequently used constants
static const int di[] = {-1, 0, 1, -1, 1, -1, 0, 1};
static const int dj[] = {-1, -1, -1, 0, 0, 1, 1, 1};

// frequently used structs
struct edge{
  int to,cost;
};

// printf for debug
#ifndef DEBUG
void debug(const char* format, ...){}
#else
void debug(const char* format, ...){
  va_list arg;
  va_start(arg, format);
  vprintf(format, arg);
  va_end(arg);
}
#endif

// dump vector
#ifdef DEBUG
#define DUMPV(v, c) do{       \
  printf("%s: ", #v);         \
  for (int i = 0; i < (c); ++i) \
  {                           \
  cout << (v)[i] << " ";      \
  }                           \
  cout << endl;               \
} while(0)
#else
#define DUMPV(v,c)
#endif

// std::fill of multi dimensions
template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
  std::fill( (T*)array, (T*)(array+N), val );
}

// binary search
ll BSearch(ll _begin, ll _end, bool (*f)(int)){
  ll mid;
  while(_end - _begin > 1LL) {
  mid = (_begin + _end) / 2LL;
  if(f(mid)) {
    debug("BSearch: f(%d) == true\n", mid);
    _end = mid;
  }
  else
  {
    debug("BSearch: f(%d) == false\n", mid);
    _begin = mid;
  }
  }
  return _end;
}

ll N,M,K,A,B,C,D;
int dp[NMAX] = {};
std::vector<edge> G[NMAX];
edge E[MMAX];

ll ans = 0;

void solve(){
  // main algorithm
  priority_queue<p, vector<p>, greater<p> > que;
  set<int> st;
  que.push(p{0, 0});
  while(!que.empty()) {
    ll cc = que.top().first;
    int i = que.top().second;
    que.pop();
    if(st.count(i)) continue;
    // cout << cc << " " << i << endl;
    ans += cc;
    st.insert(i);
    for(auto&& e : G[i]) {
      if(!st.count(e.to)) que.push(p(e.cost, e.to));
    }
  }
}
void debug(){
  // output debug information

}
void answer(){
  // output answer
  printf("%lld\n", ans);
}
int main(int argc, char const *argv[])
{
  // operate inputs

  // Fill(dp, -1);
  scanf("%lld%lld", &N,&M);
  for (int i = 0; i < M; ++i)
  {
    int f,t,c;
    scanf("%d%d%d", &f, &t, &c);
    G[f].push_back(edge{t, c});
    G[t].push_back(edge{f, c});
  }
  solve();
  #ifdef DEBUG
  debug();
  #endif
  answer();

  return 0;
}

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

struct UF{
    int N,par[10000],sz[10000];

    void init(int _N){
        N=_N;
        for(int i=0;i<N;i++){
            par[i]=i;
            sz[i]=1;
        }
    }

    int find(int x){
        return x==par[x]?x: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])swap(x,y);
        par[y]=x;
        sz[x]+=sz[y];
    }

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

typedef pair<int,int>P;
typedef pair<int,P>PP;
int main(){
    int N,M;
    cin>>N>>M;

    vector<PP>es;

    for(int i=0;i<M;i++){
        int s,t,w;
        cin>>s>>t>>w;
        es.push_back(PP(w,P(s,t)));
    }

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

    UF uf;
    uf.init(N);
    int ans=0;
    for(int i=0;i<M;i++){
        PP p=es[i];
        if(uf.isSame(p.second.first,p.second.second))continue;
        ans+=p.first;
        uf.unite(p.second.first,p.second.second);
    }

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


#include<bits/stdc++.h>
using namespace std;
int main(){
  string s;
  cin>>s;
  if(s.size()%2==1){
    cout<<"No"<<endl;
    return 0;
  }
  for(int i=0;i<s.size();i++){
    if(s.at(i)!=(i%2==0?'h':'i')){
      cout<<"No"<<endl;
      return 0;
    }
  }
  cout<<"Yes"<<endl;
}

#include <bits/stdc++.h>
#define rep(i,l,n) for(int i=l;i<n;i++)
#define rer(i,l,n) for(int i=l;i<=n;i++)
#define all(a) a.begin(),a.end()
#define o(a) cout<<a<<endl
#define pb(a) push_back(a)
#define mk(a,b) make_pair(a,b)
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

string change(string s,int n){
	string ret(s);
	rep(i,0,ret.size()){
		if(ret[i]>='a' && ret[i]<='z'){
			if(ret[i]+n>'z') ret[i]+=n-26;
			else ret[i]+=n;
		}
	}
	return ret;
}

int main(){
	string s;
	string str[3]={"this","that","the"};
	string stu[2]={" ","."};
	while(getline(cin,s)){
		bool f=0;
		rep(i,0,26){
			string t;
			t=change(s,i);
			rep(j,0,3){
				rep(k,0,2){
					if(t.find(str[j]+stu[k])!=string::npos){
						o(t); f=1; break;
					}
				}
				if(f) break;
			}if(f) break;
		}
	}
}


#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n;

struct node
{
    int key;
    node *parent,*left,*right;
    node():key(-1),parent(NULL),left(NULL),right(NULL) {}
};
node *root,*nil;

node* newnode()
{
    return new node();
}

void insert(int num)
{
    node* x=root;
    node* y=nil;
    node* z=newnode();
    z->key=num;
    while(x!=nil)
    {
        y=x;
        if(num>=x->key)
            x=x->right;
        else
            x=x->left;
    }
    if(y==nil) root=z;
    else
    {
        z->parent=y;
        if(num<y->key)
            y->left=z;
        else
            y->right=z;
    }
}

node* find(node* u,int num)
{
    while(u!=nil&&num!=u->key)
    {
        if(num< u->key) u=u->left;
        else u=u->right;
    }
    return u;
}

node* minele(node* u)
{
    while(u->left!=nil)
        u=u->left;
    return u;
}

node* nextpos(node* u)
{
//	node* y=nil;
//	if(u->right!=nil) y=minele(u->right);
//	else 														//?¬¬????????\???????????????????????°?????¶???????°±???u??????????????????
//	{
//		y=u->parent;
//		while(y!=nil&&y->right==u)
//		{
//			u=y;
//			y=y->parent;
//		}
//	}
//	return y;
    return minele(u->right);
}

void Delete(node* u)
{
    node* y;
    node* x;
    if(u->left==nil||u->right==nil) y=u;
    else y=nextpos(u);
    if(y->left!=nil) x=y->left;
    else x=y->right;

    if(x!=nil) x->parent=y->parent;

    if(y->parent==nil) root=x;
    else
    {
        if(y==y->parent->left) y->parent->left=x;
        else y->parent->right=x;
    }
    if(y!=u)
        u->key=y->key;
    delete y;
}

void inorder(node* u)
{
    if(u->left!=nil) inorder(u->left);
    printf(" %d",u->key);
    if(u->right!=nil) inorder(u->right);
    return;
}

void preorder(node* u)
{
    printf(" %d",u->key);
    if(u->left!=nil) preorder(u->left);
    if(u->right!=nil) preorder(u->right);
    return;
}

int main()
{
    scanf("%d",&n);
    for(int i=1; i<=n; ++i)
    {
        char s[11];
        scanf("%s",s);
        if(s[0]=='i')
        {
            int x;
            scanf("%d",&x);
            insert(x);
        }
        else if(s[0]=='f')
        {
            int x;
            scanf("%d",&x);
            node* u=(find(root,x));
            if(u!=nil)
                printf("yes\n");
            else
                printf("no\n");
        }
        else if(s[0]=='d')
        {
            int x;
            scanf("%d",&x);
            Delete(find(root,x));
        }
        else if(s[0]=='p')
        {
            inorder(root);
            printf("\n");
            preorder(root);
            printf("\n");
        }
    }
    return 0;
}

#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define rep(i,n) for(ll i=0;i<(n);i++)
#define repr(i,n) for(ll i=(n-1);i>=0;i--)
#define all(x) x.begin(),x.end()
#define br cout << "\n";
using namespace std;
const long long INF = 1e10;
const long long MOD = 1e9+7;
using Graph = vector<vector<ll>>;
using pll = pair<ll, ll>;
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;}

// 0 false, 1 true
// string to int : -48
// a to A : -32
// ceil(a)  1.2->2.0
// c++17	g++ -std=c++17 a.cpp

vector<ll> tate; vector<ll> yoko;

void f(vector<vector<string>> chess, ll count){

    if(count == 8){
        bool flag = true;
        rep(i, tate.size()){
            if(chess[tate[i]][yoko[i]] != "Q") flag = false;
        }

        if(flag){
            rep(i, 8){
                rep(j, 8){
                    cout << chess[i][j];
                }
                br;
            }
        }
    }else{
        rep(j, 8){
            //縦
            bool tate_flag = true;
            for(ll k = count-1; k >= 0; k--){
                if(chess[k][j] == "Q") tate_flag = false;
            }

            bool naname_flag = true;
            for(ll k = count, l = j; k >= 0 && l >= 0; k--, l--){
                if(chess[k][l] == "Q") naname_flag = false;
            }
            for(ll k = count, l = j; k >= 0 && l < 8; k--, l++){
                if(chess[k][l] == "Q") naname_flag = false;
            }
            for(ll k = count, l = j; k < 8 && l >= 0; k++, l--){
                if(chess[k][l] == "Q") naname_flag = false;
            }
            for(ll k = count, l = j; k < 0 && l < 0; k++, l++){
                if(chess[k][l] == "Q") naname_flag = false;
            }

            if(tate_flag && naname_flag){
                chess[count][j] = "Q";
                f(chess, count+1);
                chess[count][j] = ".";
            }
        }
    }

}

int main() {
    std::cout << std::fixed << std::setprecision(15);
    ll k; cin >> k;

    rep(i, k){
        ll a, b;
        cin >> a >> b;
        tate.push_back(a); yoko.push_back(b);
    }

    vector<vector<string>> chess(8, vector<string>(8));
    rep(i, 8)rep(j, 8){
        chess[i][j] = ".";
    }

    rep(i, 8){
        chess[0][i] = "Q";
        f(chess, 1);
        chess[0][i] = ".";
    }

}


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

using ll = long long;
using ull = unsigned long long;
using ld = long double;

#define MOD 1000000007

#define rep(i, n) for(ll (i) = 0LL;(i) < (ll)(n);(i)++)
#define rep2(i, s, e) for(ll (i) = (ll)(s);(i) < (ll)(e);(i)++)
#define repi(i, n) for(ll (i) = 0LL;(i) <= (ll)(n);(i)++)
#define repi2(i, s, e) for(ll (i) = (ll)(s);(i) <= (ll)(e);(i)++)
#define per(i, n) for(ll (i) = (ll)(n) - 1LL;(i) >= 0LL;(i)--)
#define per2(i, s, e) for(ll (i) = (ll)(s) - 1LL;(i) >= (ll)(e);(i)--)
#define peri(i, n) for(ll (i) = (ll)(n);(i) >= 0LL;(i)--)
#define peri2(i, s, e) for(ll (i) = (ll)(s);(i) >= (ll)(e);(i)--)
#define iter(i, it) for(auto &(i): (it))

template<typename T, typename U> ostream& operator<<(ostream &s, const pair<T, U> m) {
    cout << "(" << m.first << ", " << m.second << ")";
    return s;
}
template<typename T, typename U> ostream& operator<<(ostream &s, const map<T, U> m) {
    ll c = 0;
    cout << "{ ";
    iter(i, m) cout << i << (c++ == m.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}
template<typename T> ostream& operator<<(ostream &s, const vector<T> &v) {
    cout << "{ ";
    rep(i, v.size()) cout << v[i] << (i == v.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}
template<typename T> ostream& operator<<(ostream &s, const list<T> &v) {
    ll c = 0;
    cout << "{ ";
    iter(i, v) cout << i << (c++ == v.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}

int main(void) {
    ll N, M;
    ll prev = 0;
    map<ll, ll> B;
    ll count = 0;
    cin >> N >> M;
    B[0]++;
    rep(i, N) {
        ll temp;
        cin >> temp;
        temp += prev;
        B[temp % M]++;
        prev = temp;
    }
    iter(i, B) count += i.second * (i.second - 1) / 2;
    cout << count << endl;
    return 0;
}

#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,b,a) for(int i=b;i>=a;i--)
#define fori(a) for(auto i : a )
#define all(a) begin(a), end(a)
#define set(a,b) memset(a,b,sizeof(a))
#define sz(a) a.size()
#define pi 3.14159
#define ll long long
#define ull unsigned long long
#define pb push_back
#define PF push_front //deque
#define mp make_pair
#define pq priority_queue
#define mod 1000000007
#define f first
#define s second
#define pii pair< int, int >
#define vi vector<int>
#define vpii vector<pii>
#define debug(v) for(auto i:v) cout<<i<<" ";
#define tc int t; cin >> t; while(t--)
using namespace std;
void optimizeIO(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}
void  solve(){
  ll n,m;
  cin>>n>>m;
  ll a[n];
  rep(i,0,n) cin>>a[i];
  vector<ll> pre(n,0);
  map<ll,int> m1;
  pre[0]=a[0]%m;
  m1[pre[0]]++;
  rep(i,1,n) pre[i]=(pre[i-1]+a[i])%m,m1[pre[i]]++;
  ll ans=0;
  ll modsum=0;
  rep(i,0,n){
    if(i!=0) ans+= m1[pre[i-1]];
    else ans+=m1[0];
    m1[pre[i]]--;
  }
  cout<<ans<<endl;
}

int main(){
    optimizeIO();
    solve();
    // solve1();
}


#include <bits/stdc++.h>
#define all(vec) vec.begin(),vec.end()
using namespace std;
using ll=long long;
using P=pair<int,int>;
const ll INF=1LL<<30;
const ll LINF=1LL<<61;
const double eps=1e-9;
const ll MOD=1000000007LL;
vector<ll> f,fi;
ll mpow(ll x,ll n){
    ll res=1;
    while(n>0){
        if(n&1){
            res=res*x%MOD;
        }
        x=x*x%MOD;
        n>>=1;
    }
    return res;
}
ll ncr(ll n,ll r){
    if(n<r)return 0;
    if(r==0)return 1;
    return f[n]*fi[r]%MOD*fi[n-r]%MOD;
}
int main(){
    int n;cin>>n;
    f.resize(n+10,0);
    fi.resize(n+10,0);
    f[0]=1;
    for(ll i=1;i<=n;i++)f[i]=f[i-1]*i%MOD;
    fi[n]=mpow(f[n],MOD-2LL);
    for(ll i=n-1;i>=0;i--)fi[i]=fi[i+1]*(i+1)%MOD;
    vector<vector<ll>> dp(n+10,vector<ll>(n+10,0));
    int a,b,c,d;cin>>a>>b>>c>>d;
    dp[a-1][0]=1;
    for(int i=a;i<=b;i++){
        for(int j=0;j<=n;j++){
            dp[i][j]+=dp[i-1][j];
            dp[i][j]%=MOD;
            ll s=1;
            for(int k=1;k<c;k++)s=s*ncr(n-j+i*k,i)%MOD;
            for(int k=c;k<=d;k++){
                if(j-i*k<0)break;
                s=s*ncr(n-j+i*k,i)%MOD;
                dp[i][j]+=dp[i-1][j-i*k]*s%MOD*fi[k]%MOD;
                dp[i][j]%=MOD;
            }
        }
    }
    cout<<dp[b][n]<<endl;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define i_7 (ll)(1E9+7)
#define i_5 (ll)(1E9+5)
ll mod(ll a){
    ll c=a%i_7;
    if(c>=0)return c;
    else return c+i_7;
}
typedef pair<int,int> i_i;
typedef pair<ll,ll> l_l;
ll inf=(ll)1E12;
#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[i][j],dp[i-1][j]);
void Min(ll * pos,ll val){*pos=min(*pos,val);}
void Add(ll * pos,ll val){*pos=mod(*pos+val);}
const long double EPS=1E-8;
////////////////////////////////////////
#define N 100004

ll kai[N];
ll kai2[N];
ll po(ll i,ll p){
    if(p==0)return 1;
    else if(p==1)return mod(i);
    else if(p%2==0)return mod(po(mod(mod(i)*mod(i)),p/2));
    else  return mod(mod(i)*mod(po(i,p-1)));
}
void calc(){
    kai[0]=1;
    kai2[0]=1;
    for(int i=1;i<N;i++){
        kai[i]=mod(kai[i-1]*i);
        kai2[i]=mod(po(kai[i],i_5));
    }
}
ll comb(ll n,ll k){
    if(n==0){
        if(k==0)return 1;
        else return 0;
    }
    else if(n<k)return 0;
    else return mod(mod(mod(kai[n])*mod(kai2[n-k]))*mod(kai2[k]));
}

//#include <time.h>

int main(){
   // clock_t start = clock();    // スタート時間
    calc();
    ll n,a,b,c,d;cin>>n>>a>>b>>c>>d;
    ll dp[n+5][n+5];
    memset(dp,0,sizeof(dp));
    dp[a][0]=1;

    rep(k,a,b){
        rep(j,0,n){
            for(ll p=0;j+p*k<=n&&p<=d;p++){
                ll add=comb(n-j,p*k);
                add=mod(add*dp[k][j]);
                add=mod(add*kai[p*k]);
                add=mod(add*kai2[p]);
                ll w=(ll)po(kai2[k],p);
                add=mod(add*w);
                Add(&dp[k+1][j+p*k],add);
                if(p==0)p=c-1;
            }
        }
    }

    /*rep(i,0,b+1){
        rep(j,0,n){
            cout<<dp[i][j]<<"  ";
        }cout<<endl;
    }*/

    cout<<mod(dp[b+1][n])<<endl;

    //clock_t end = clock();     // 終了時間
   // std::cout << "duration = " << (double)(end - start) / CLOCKS_PER_SEC << "sec.\n";
    return 0;
}


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

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

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

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

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

int main()
{
    ll M; cin >> M;
    ll c[M], d[M];
    rep(i,M) cin >> d[i] >> c[i];
    ll res = 0;
    vector<int> N;
    rep(i,M){
        vector<ll> out;
        while(c[i] != 1){
            if(c[i]%2 == 1){
                out.push_back(d[i]);
                --c[i];
            }
            int d_tmp = d[i]+d[i];
            res += c[i]/2;
            if(d_tmp >= 10){
                d_tmp = 1 + (d_tmp%10);
                res += c[i]/2;
            }
            d[i] = d_tmp;
            c[i] /= 2;
        }
        for(int o: out){
            res++;
            int d_tmp = d[i]+o;
            if(d_tmp >= 10){
                res++;
                d_tmp = 1 + d_tmp % 10;
            }
            d[i] = d_tmp;
        }
        N.push_back(d[i]);
    }
    int m_tmp = -1;
    for(int m : N){
        if(m_tmp < 0){
            m_tmp = m;
        }else{
            res++;
            m_tmp = m_tmp + m;
            if(m_tmp >= 10){
                res++;
                m_tmp = 1 + (m_tmp % 10);
            }
        }
    }
    cout << res << endl;
}

#include <bits/stdc++.h>
using namespace std;
int main(){
  int M;
  cin >> M;
  vector<int> d(M);
  vector<long long> c(M);
  for (int i = 0; i < M; i++){
    cin >> d[i] >> c[i];
  }
  long long cnt = 0;
  long long sum = 0;
  for (int i = 0; i < M; i++){
    cnt += c[i];
    sum += c[i] * d[i];
  }
  cout << cnt + (sum - 1) / 9 - 1 << endl;
}


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

#define ll long long
const int N = 1e3 + 5;
const ll mod = 1e9 + 7;

ll mul(ll a, ll b){
	return (a * b) % mod;
}
ll power(ll x, ll y){
	if(y == 1){
		return x;
	} else {
		ll tmp = power(x, y/2);
		if(y & 1){
			return mul(mul(tmp, tmp), x);
		} else {
			return mul(tmp, tmp);
		}
	}
}

ll n, a, b, c, d, dp[N][N];
ll fac[N], rev[N];

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n >> a >> b >> c >> d;
	fac[0] = 1;
	rev[0] = power(fac[0], mod - 2);
	for(int i = 1; i <= n; i++){
		fac[i] = (fac[i - 1] * i) % mod;
		rev[i] = power(fac[i], mod - 2);
	}
	for(int i = a - 1; i <= b; i++){
		dp[0][i] = 1;
	}
	for(int i = 1; i <= n; i++){
		for(int j = a; j <= b; j++){
			dp[i][j] = dp[i][j - 1];
			for(int k = c; k <= d && k * j <= i; k++){
				ll tmp = mul(mul(mul(fac[n - i + j * k], rev[n - i]), power(rev[j], k)), rev[k]);
				dp[i][j] = (dp[i][j] + mul(dp[i - j * k][j - 1], tmp)) % mod;
			}
		}
	}
	cout << dp[n][b];
}

#include <iostream>
#include <vector>
#define pb push_back
#define mp make_pair
using namespace std;

const long long INF = 2e18 + 18;

vector<long long> dp, cost;
vector<vector<long long> > c;

void solve() {
	int n; cin >> n;
	c.resize(n, vector<long long>(n));
	dp.resize((1 << n), -INF);
	cost.resize((1 << n));
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cin >> c[i][j];
		}
	}
	for (int mask = 0; mask < (1 << n); mask++) {
		long long res = 0;
		for (int i = 0; i < n; i++) {
			if (mask & (1 << i)) {
				for (int j = i + 1; j < n; j++) {
					if (mask & (1 << j)) {
						res += c[i][j];
					}
				}
			}
		}
		cost[mask] = res;
	}
	dp[(1 << n) - 1] = 0;
	for (int mask = (1 << n) - 1; mask >= 0; mask--) {
		for (int submask = mask; ; submask = (submask - 1) & mask) {
			dp[mask - submask] = max(dp[mask - submask], dp[mask] + cost[submask]);
			if (submask == 0) break;
		}
	}
	cout << dp[0] << endl;
}

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


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <stdio.h>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <utility>
#include <tuple>
#include <math.h>
#include <set>
#include <map>
using namespace std ;
using ll = long long ;
using ld = long double ;
using vll = vector<ll> ;
using vvll = vector<vll> ;
using vc = vector<char> ;
using vvc = vector<vc> ;
using vb = vector<bool> ;
using vvb = vector<vb> ;
using pll = pair<ll,ll> ;
#define all(v) v.begin(),v.end()
const ll INF = 1e18 ;
const ll mod = 1e9+7 ;
const double pie = acos(-1);
vll dx4 = {-1,0,1,0} ;
vll dy4 = {0,-1,0,1} ;
vll dx8 = {-1,0,1,1,1,0,-1,-1} ;
vll dy8 = {-1,-1,-1,0,1,1,1,0} ;

void fix_cout(){cout << fixed << setprecision(20) ; }
ll gcd(ll a,ll b){if(b==0) return a ; return gcd(b,a%b) ; }
ll lcm(ll a,ll b){return a/gcd(a,b)*b ; }
void chmax(ll &a,ll b){if(a<b) a = b ; }
void chmin(ll &a,ll b){if(a>b) a = b ; }

int main(){
	ll h,w ;
	cin >> h >> w ;
	map<ll,ll> gs,dis ;
	for(int i=1;i<=w;i++) gs[i] = i ;
	gs[w+10] = -1 ;
	dis[0] = w ;
	for(int i=1;i<=h;i++){
		ll a,b ;
		cin >> a >> b ;
		ll mx = -1 ;
		for(auto it=gs.lower_bound(a);it->first<=b+1;it=gs.erase(it)){
			ll g = it->first ;
			ll s = it->second ;
			chmax(mx,s) ;
			if(dis.count(g-s)){
				dis[g-s]-- ;
				if(dis[g-s]==0) dis.erase(g-s) ;
			}
		}
		if(mx>=0&&b<w){
			gs[b+1] = mx ;
			dis[b+1-mx]++ ;
		}
		ll ans = -1 ;
		if(!dis.empty()){
			ans = i+dis.begin()->first ;
		}
		cout << ans << endl ;
	}
}

/**
 *   @FileName	f.cpp
 *   @Author	kanpurin
 *   @Created	2020.08.31 16:46:32
**/

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

int main() {
    int h,w;cin >> h >> w;
    set<pair<int,int>> st1; // (現在地,スタート位置)
    multiset<int> st2; // 移動回数
    for (int i = 0; i < w; i++) {
        st1.insert({i,i});
        st2.insert(0);
    }
    for (int i = 0; i < h; i++) {
        int a,b;cin >> a >> b;
        // 現在地が[a,b+1]にあるものをb+1に移動させる.
        // b=wなら∞に移動.
        // 移動させるものの中で一番右のものだけ残す.
        a--;b--;
        auto l = st1.lower_bound({a,-1});
        auto r = st1.upper_bound({b+1,w});
        if (l != r) { // ある
            r--;
            while(l != r) {
                st2.erase(st2.find(l->first-l->second));
                l = st1.erase(l);
            }
            st2.erase(st2.find(r->first-r->second));
            st1.erase(r);
            // b+1への移動
            if (b+1 != w) {
                st1.insert({b+1,r->second});
                st2.insert(b+1-r->second);
            }
        }
        if (st1.empty()) cout << -1 << endl;
        else cout << *st2.begin() + i + 1 << endl;
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <stack>
#include <queue>
#include <set>
#include <bitset>
#include <map>
#include <unordered_map>
#include <ctime>
#include <list>
#include <numeric>
#include <utility>
#include <ctime>
#define INF 1000000000
#define LINF 9000000000000000000
#define mod 1000000007

#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 REP(i,a,b) for(int i=(a);i<int(b);i++)
#define all(x) (x).begin(),x.end()
#define pb push_back
#define mp make_pair
#define MOD(x) (x%(mod))

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int,int> pi;

int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int ddx[8]={-1,-1,0,1,1,1,0,-1};
int ddy[8]={0,1,1,1,0,-1,-1,-1};
bool debug=false;

/*---------------------------------------------------*/

char island[13][13];

void bfs(int x,int y){
  island[y][x]='0';
  queue<pi> que;
  que.push(mp(x,y));
  while(que.size()){
    pi d=que.front();que.pop();
    rep(i,4){
      int xx=d.first+dx[i],yy=d.second+dy[i];
      if(0<=xx&&xx<=12&&0<=yy&&yy<=12&&island[yy][xx]=='1'){
	que.push(mp(xx,yy));
	island[yy][xx]='0';
      }
    }
  }
}

int solve(){
  int ret=0;
  rep(i,12){
    rep(j,12){
      if(island[i][j]=='1'){
	ret++;
	bfs(j, i);
      }
    }
  }
  return ret;
}

int main(){
  char a;
  while(cin>>a){
    island[0][0]=a;
    // cout<<"in"<<endl;
    rep(i,12){
      rep(j,12){
	if(i==0&&j==0)continue;
	// cout<<i<<" "<<j<<endl;
	cin>>island[i][j];
      }
    }
    cout<<solve()<<endl;
  }
}

#include<iostream>
#include<algorithm>
#include<vector>
#include<cstdio>
#include<cstring>
#include<climits>
#include<cmath>
#include<map>

#define f first
#define s second
#define mp make_pair

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define ALL(c) (c).begin(), (c).end()

typedef unsigned int uint;
typedef long long ll;

using namespace std;

char buff[12][13];
int  cl[12][12];
int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};

void check(int x, int y, int c){
  if(buff[y][x] == '0') return;
  if(cl[y][x] != 0) return;
  if(y < 0) return;
  if(x < 0) return;
  if(y > 11) return;
  if(x > 11) return;
  cl[y][x] = c;
  REP(i,4)
    check(x+dx[i],y+dy[i],c);
}

int main(){
  while(cin>>buff[0]){
    REP(i,11)
      cin>>buff[i+1];

    memset(cl,0,sizeof(cl));
    int cnt = 1;
    REP(i,12) REP(j,12){
      if(cl[i][j] == 0 && buff[i][j] == '1')
        check(j,i,cnt++);
    }

    /*
    REP(i,12){ REP(j,12)
        printf("%2d ",cl[i][j]); puts("");}
    */
    cout << cnt-1 << endl;
  }
  return 0;
}


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

template<typename Monoid>
struct SegmentTree {
    using F = function<Monoid(Monoid, Monoid)>;
    const F f;
    const Monoid M1;
    int sz;
    vector<Monoid> dat;
    SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1), sz(1) {
        while (sz < n) sz <<= 1;
        dat.assign(sz * 2, M1);
    }
    void set(int k, const Monoid &x) { dat[k + sz] = x; }
    void build() {
        for (int k = sz - 1; k > 0; k--) {
            dat[k] = f(dat[2 * k], dat[2 * k + 1]);
        }
    }
    void update(int k, const Monoid &x) {
        dat[k += sz] = x;
        while (k >>= 1) dat[k] = f(dat[2 * k], dat[2 * k + 1]);
    }
    Monoid get(int a, int b) {  // [a, b)
        Monoid L = M1, R = M1;
        for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
            if (a & 1) L = f(L, dat[a++]);
            if (b & 1) R = f(dat[--b], R);
        }
        return f(L, R);
    }
    Monoid operator[](const int &k) const { return dat[k + sz]; }
    friend ostream& operator<<(ostream& os, SegmentTree<Monoid> &seg) {
        for (int i = 0; i < seg.sz; i++) os << seg[i] << " ";
        return os;
    }
};

int main() {
    int n; cin >> n;
    int Q; cin >> Q;
    int a, b; cin >> a >> b; a--, b--;
    vector<int> x(Q + 1);
    x[0] = a;
    for (int q = 1; q <= Q; q++) cin >> x[q], x[q]--;
    const long long INF = 1LL << 60;    // 1.15x10^18
    using ll = long long;
    SegmentTree<ll> dpL(n, [](ll a, ll b) { return min(a, b); }, INF);
    SegmentTree<ll> dpR(n, [](ll a, ll b) { return min(a, b); }, INF);
    dpL.set(b, 0 - b); dpL.build();
    dpR.set(b, 0 + b); dpR.build();
    ll add = 0;
    for (int q = 1; q <= Q; q++) {
        int diff = abs(x[q] - x[q - 1]);
        add += diff;
        ll val = min(dpL.get(0, x[q]) + x[q], dpR.get(x[q], n) - x[q]);
        val -= diff;
        dpL.update(x[q - 1], val - x[q - 1]);
        dpR.update(x[q - 1], val + x[q - 1]);
    }
    ll mi = INF;
    for (int i = 0; i < n; i++) mi = min(mi, dpL[i] + i);
    cout << mi + add << endl;
    return 0;
}

#if 0
2019.12.30

朴素 dp 设 f[i][p][q] 表示前 i 次操作后当前棋子为 (p, q) 的最小代价。
事实上由于第 i 次操作后一定有一个棋子是 x[i] ，因此可以优化一维：
f[i][j] 表示前 i 次操作后当前棋子为 (j, x[i]) 的最小代价。

转移有两种：
1) f[i - 1][j] + |x[i - 1] - x[i]| -> f[i][j]
2) min(f[i - 1][k] + |x[i] - k|) -> f[i][x[i - 1]]
用线段树维护 f[~][j] ，第一种转移就是全局加，第二种是单点取 min 。
第二种转移可以把左边的绝对值拆开：
f[i - 1][k] - k + x[i] (k < x[i])
f[i - 1][k] (k = x[i])
f[i - 1][k] + k - x[i] (k > x[i])
线段树分别维护 f[~][j] - j, f[~][j], f[~][j] + j 即可。
#endif
#include <cstdio>
#include <algorithm>
#define debug(...) fprintf(stderr, __VA_ARGS__)

typedef long long ll;

struct {
	inline operator int () { int x; return scanf("%d", &x), x; }
	inline operator ll () { ll x; return scanf("%lld", &x), x; }
	template<class T> inline void operator () (T &x) { x = *this; }
	template<class T, class ...A> inline void operator () (T &x, A &...a)
	{ x = *this; this -> operator ()(a...); }
} read;

const int maxn = 200005;
ll f[maxn << 2], fa[maxn << 2], fb[maxn << 2], tag;

void modify(int now, int L, int R, int p, ll x) {
	f[now] = std::min(f[now], x);
	fa[now] = std::min(fa[now], x + p);
	fb[now] = std::min(fb[now], x - p);
	if(L == R) return;
	int M = (L + R) >> 1;
	if(p <= M) modify(now << 1, L, M, p, x);
	else modify(now << 1 | 1, M + 1, R, p, x);
}

void build(int now, int L, int R) {
	f[now] = fa[now] = fb[now] = 1000000000000000000;
	if(L == R) return;
	int M = (L + R) >> 1;
	build(now << 1, L, M);
	build(now << 1 | 1, M + 1, R);
}

ll query(int now, int L, int R, int p) {
	if(R < p) return p + fb[now];
	if(L > p) return fa[now] - p;
	if(L == R) return f[now];
	int M = (L + R) >> 1;
	return std::min(query(now << 1, L, M, p), query(now << 1 | 1, M + 1, R, p));
}

int main() {
	int n = read, q = read, a = read, b = read;

	build(1, 1, n);
	modify(1, 1, n, a, 0);
	/* modify(1, 1, n, b, 0); */

	int last = b;
	while(q --) {
		int x = read;
		ll qu = query(1, 1, n, x) + tag;
		/* debug("qu = %lld\n", qu); */
		tag += std::abs(x - last);
		modify(1, 1, n, last, qu - tag);
		last = x;
		/* debug("tag=%lld\n", tag); */
	}

	printf("%lld\n", f[1] + tag);
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <cmath>
#include <iomanip>
#include <cassert>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) (int)(x).size()
using namespace std;
using ll = long long;
constexpr ll INF = 3000000000000000000;

template <typename Operator>
struct LazySegmentTree {
    Operator OP;
    using NodeType = decltype(OP.NodeE);
    using FuncType = decltype(OP.FuncE);
    int length, height;
    vector<NodeType> node;
    vector<FuncType> lazy;

    LazySegmentTree(int n) {
        length = 1;
        height = 0;
        while (length < n) {
            length <<= 1;
            ++height;
        }
        node.resize(length << 1, OP.NodeE);
        lazy.resize(length << 1, OP.FuncE);
    }

    void eval(int n) {
        node[n] = OP.NodeFunc(node[n], lazy[n]);
        if (n < length) {
            lazy[(n << 1) | 0] = OP.MergeFunc(lazy[(n << 1) | 0], lazy[n]);
            lazy[(n << 1) | 1] = OP.MergeFunc(lazy[(n << 1) | 1], lazy[n]);
        }
        lazy[n] = OP.FuncE;
    }

    void update(int a, int b, FuncType x) {
        int l = a + length, r = b + length - 1;
        for (int i = height; i > 0; --i) {
            eval(l >> i);
            eval(r >> i);
        }
        ++r;
        while (r > l) {
            if (l & 1) {
                lazy[l] = OP.MergeFunc(lazy[l], x);
                ++l;
            }
            if (r & 1) {
                --r;
                lazy[r] = OP.MergeFunc(lazy[r], x);
            }
            l >>= 1; r >>= 1;
        }
        l = a + length; r = b + length - 1;
        while (l >>= 1) node[l] = OP.MergeNode(OP.NodeFunc(node[(l << 1) | 0], lazy[(l << 1) | 0]), OP.NodeFunc(node[(l << 1) | 1], lazy[(l << 1) | 1]));
        while (r >>= 1) node[r] = OP.MergeNode(OP.NodeFunc(node[(r << 1) | 0], lazy[(r << 1) | 0]), OP.NodeFunc(node[(r << 1) | 1], lazy[(r << 1) | 1]));
    }

    NodeType get(int a, int b) {
        int l = a + length, r = b + length - 1;
        for (int i = height; i >= 0; --i) {
            eval(l >> i);
            eval(r >> i);
        }
        ++r;
        NodeType vl = OP.NodeE, vr = OP.NodeE;
        while (l < r) {
            if (l & 1) vl = OP.MergeNode(vl, node[l++]);
            if (r & 1) vr = OP.MergeNode(node[--r], vr);
            l >>= 1; r >>= 1;
        }
        return OP.MergeNode(vl, vr);
    }
};

struct hoge {
    ll NodeE = 0;
    ll FuncE = INF;
    ll MergeNode(ll x, ll y) {
        return x + y;
    }
    ll MergeFunc(ll x, ll y) {
        if (y == INF) return x;
        else return y;
    }
    ll NodeFunc(ll x, ll y) {
        if (y == INF) return x;
        else return y;
    }
};

int main() {
    int n, q;
    cin >> n >> q;
    LazySegmentTree<hoge> LST(n);
    LST.update(0, n, (1LL << 31) - 1);
    REP(_, q) {
        int num;
        cin >> num;
        if (!num) {
            int s, t, x;
            cin >> s >> t >> x;
            ++t;
            LST.update(s, t, x);
        } else {
            int i;
            cin >> i;
            cout << LST.get(i, i + 1) << "\n";
        }
    }

    return 0;
}


#include <bits/stdc++.h>

#define _overload(_1,_2,_3,name,...) name
#define _rep(i,n) _range(i,0,n)
#define _range(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)

#define _rrep(i,n) _rrange(i,n,0)
#define _rrange(i,a,b) for(int i=(int)(a)-1;i>=(int)(b);--i)
#define rrep(...) _overload(__VA_ARGS__,_rrange,_rrep,)(__VA_ARGS__)

#define _all(arg) begin(arg),end(arg)
#define uniq(arg) sort(_all(arg)),(arg).erase(unique(_all(arg)),end(arg))
#define bit(n) (1LL<<(n))

// #define DEBUG

#ifdef DEBUG
    #define dump(...) fprintf(stderr, __VA_ARGS__)
#else
    #define dump(...)
#endif

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

using namespace std;
using ll=long long;
using vi=vector<int>;
using vll=vector<ll>;

const double EPS = 1e-10;
const double PI = acos(-1.0);
const int inf = INT_MAX;
const ll INF = LLONG_MAX;
const ll MOD = 1000000007LL;
const int dx[4]={1,0,-1,0};
const int dy[4]={0,1,0,-1};

ll extgcd(ll a,ll b,ll& x,ll& y){x=1,y=0;ll g=a;if(b!=0) g=extgcd(b,a%b,y,x),y-=a/b*x;return g;}
ll ADD(const ll &a, const ll &b, const ll mod=MOD) { return (a+b)%mod;}
ll SUB(const ll &a, const ll &b, const ll mod=MOD) { return (a-b+mod)%mod;}
ll MUL(const ll &a, const ll &b, const ll mod=MOD) { return (1LL*a*b)%mod;}
ll DIV(const ll &a, const ll &b, const ll mod=MOD) {ll x,y; extgcd(b,mod,x,y);return MUL(a,(x+mod)%mod,mod);}

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

    int R; cin >> R;

    string res = "AGC";
    if(R < 1200){
        res = "ABC";
    }
    else if(R < 2800){
        res = "ARC";
    }

    cout << res << endl;

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define endl "\n"
#define REP(i,a,b) for(ll i=a;i<b;i++)
#define PER(i,b,a) for(ll i=b;i>a;i--)
#define forit(it,x) for(auto it=(x).begin();it!=(x).end();++it)
#define ull unsigned long long int
#define ld long double
#define prec(n) fixed<<setprecision(n)
#define test ll t; cin>>t; while(t--)
#define re return 0
#define pb push_back
#define PI 3.1415926535897932384626
#define MOD (ll)1000000007
#define fi first
#define se second
#define eb emplace_back
#define vll vector<ll>
#define vi  vector<int>
#define mpll map<ll,ll>
#define pll pair<ll,ll>
#define mp(x,y) make_pair(x,y)
const ll MAXI = (ll)1e9+7;
int main(){
    IOS;
//    test{
        int a;
        cin>>a;
/*        int a[n];
        REP(i,n){
           cin>>a[i];
        }
        */
        cout<<a*(1+a+a*a)<<"\n";
//    }
re;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ii = pair<ll, ll>;

const int MAX { 200010 };
const ll MOD { 998244353 }, oo { 1LL << 62 };

ll fs[MAX];

ll st[MAX][2];

void precomp()
{
    fs[0] = fs[1] = 1;

    for (ll i = 2; i < MAX; ++i)
        fs[i] = (fs[i - 1] * i) % MOD;
}

ll mod_pow(ll a, ll n)
{
    ll res = 1, base = a;

    while (n)
    {
        if (n & 1)
            res = (res * base) % MOD;

        base = (base * base) % MOD;
        n >>= 1;
    }

    return res;
}

ll inv(ll N)
{
    return mod_pow(N, MOD - 2);
}

ll C(ll N, ll M)
{
    auto res = (fs[N] * inv(fs[N - M])) % MOD;
    res = (res * inv(fs[M]) % MOD);

    return res;
}

ll solve(ll N, ll M, ll K)
{
    auto ans = mod_pow(M - 1, N - 1);
    ans = (ans * M) % MOD;

#ifdef DEBUG
    cout << "--- ans = " << ans << endl;
#endif
    for (ll i = 1; i <= K; ++i)
    {
        ll res = (C(N - 1, i) * mod_pow(M - 1, N - 1 - i)) % MOD;
        res = (M * res) % MOD;
        ans = (ans + res) % MOD;
    }

    return ans;
}

int main()
{
    ios::sync_with_stdio(false);
    precomp();

    ll N, M, K;
    cin >> N >> M >> K;

    auto ans = solve(N, M, K);

    cout << ans << '\n';

    return 0;
}

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

int main() {
  ll n,a,b,c,d,e;
  cin >> n >> a >> b >> c >> d >> e;
  ll mn= min(min(min(a,b),c),min(d,e));
  ll ans = (n-0.5)/mn +5;
  cout << ans << endl;
  return 0;
}


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

using ll=long long;
#define int ll

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

int read(){
	int i;
	scanf("%lld",&i);
	return i;
}

signed main(){
	while(true){
		int e=read();
		int ans=100000;
		int m;
		if(e==0) break;
		int z,y,x;
		for(z=1;z*z*z<e;z++){
		}
		for(y=1;y*y<e;y++){
		}
		for(int i=0;i<=z;i++){
			for(int j=0;j<=y;j++){
				if(e-i*i*i-j*j >= 0){
					x = e-i*i*i-j*j;
					ans = min(ans,x+j+i);
				}
			}
		}

		cout << ans << endl;

	}

}

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

int main(){
    Init();
    ll h, w, a, b;
    cin >> h >> w >> a >> b;
    string zo = string(a, '0') + string(w-a, '1');
    string oz = string(a, '1') + string(w-a, '0');
    rep(i, h) cout << (i<b ? zo : oz) << endl;
}


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

int main() {
    int N;
    cin >> N;
    vector<int> x(N), y(N);
    rep(i,N) cin >> x[i] >> y[i];
    double d=0;
    rep(i,N) rep(j,N)
        d += hypot(x[i]-x[j], y[i]-y[j]);
    printf("%.8f\n", d/N);
}

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(v) v.begin(),v.end()
#define len(x) (ll)(x).length()
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int INF=1e9;
const int di[] = {-1,0,1,0};
const int dj[] = {0,-1,0,1};

int main(){
  double n;
  cin>>n;
  vector<vector<double>> d(n,vector<double>(2));
  rep(i,n)rep(j,2) cin>>d[i][j];
  vector<int> t(n);
  rep(i,n) t[i]=i;
  double sum=0;
  int cnt=0;
  do{
    for(int i=1;i<n;i++){
      sum+=sqrt((d[t[i]][0]-d[t[i-1]][0])*(d[t[i]][0]-d[t[i-1]][0])+(d[t[i]][1]-d[t[i-1]][1])*(d[t[i]][1]-d[t[i-1]][1]));
    }
    cnt++;
  }while(next_permutation(all(t)));
  printf("%.10f\n",sum/cnt);
}


#include<iostream>
#include<algorithm>
#include<ctime>
#include<vector>
#include<string>
#include<cmath>
#include<map>
#include<iomanip>
#include<numeric>
#include<queue>
#include<deque>
#include<cfloat>
#include<functional>
#include<tuple>
#include<math.h>
#include<bitset>
#include<stack>
#include<set>
#include<random>
#include<stdlib.h>
#define rip(i,n) for(int i=0;i<n;i++)
#define Rip(i,n) for(int i=1;i<=n;i++)
#define RIP(i,a,b) for(int i=a;i<b;i++)
#define all(V) V.begin(),V.end()
#define sec  setprecision;
#define _CRT_SECURE_NO_WARNINGS
#pragma target("avx")
#pragma optimize("O3")
#pragma optimize("unroll-loops");
constexpr double eps = 1e-9;
constexpr double pi = 3.141592653589793238462643383279;
using namespace std;
using ll = long long;
using ld = long double;
using Pa = pair<int, int>;
ll mod = 1000000007;
ll MOD = 998244353;
ll gcd(ll a, ll b)
{
	if (a % b == 0)	return(b);
	else return(gcd(b, a % b));
}
ll lcm(ll a, ll b)
{
	return a * b / gcd(a, b);
}
ll pow(ll x, ll n) {
	x %= mod;
	if (n == 0)
		return 1;
	if (n % 2 == 0)
		return (pow((x * x) % mod, n / 2) % mod);
	else
		return (x * pow(x, n - 1)) % mod;
}
bool prime(int n) {
	int i = 2;
	if (n == 2)return true;
	else if(n <= 1)return false;
	if (n % i == 0)return false;
	i = 3;
	while (i <= sqrt(n)) {
		if (n % i == 0)return false;
		i += 2;
	}
	return true;
}
signed main() {
	int h, w; cin >> h >> w;
	vector<vector<bool>>s(h+2,vector<bool>(w+2,false));
	rip(i, h){
      rip(j, w) {
		char a; cin >> a;
		if (a == '#')s[i+1][j+1] = true;
		}
    }
	rip(i, h) {
		rip(j, w) {
			bool p = true;
          int k=i+1;int l=j+1;
			p = s[k-1][l]||s[k][l-1]||s[k+1][l]||s[k][l+1];
			if(p == false&&s[k][l]==true) {
				cout << "No" << endl;
				return 0;
			}
		}
	}
	cout << "Yes" << endl;
}

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

void solve(long long N, long long K, std::vector<long long> D){
    long long ans = 0;
    for(long long i = N; i < 100000; ++i){
        //cout << i << endl;
        string S = to_string(i);
        bool check = true;
        for(auto s : S){
            for(auto d : D){
                string st{s};
                string ds = to_string(d);
                //cout << st << " " << ds << endl;
                if(st == ds){
                    //cout << "check!" << endl;
                    check = false;
                }
            }
        }
        if(check){
            //cout << i << endl;
            ans = i;
            break;
        }
    }
    cout << ans << endl;
    return;
}

int main(){
    long long N;
    scanf("%lld",&N);
    long long K;
    scanf("%lld",&K);
    std::vector<long long> D(K);
    for(int i = 0 ; i < K ; i++){
        scanf("%lld",&D[i]);
    }
    solve(N, K, std::move(D));
    return 0;
}


#include<iostream>
using namespace std;

int main()
{
	int n;
	int i, j, k, l, m;
	int tmp;
	int count=0;

	cin >> n;
	int Dice[n][6];

	for(i=0;i<n;i++){
		for(j=0;j<6;j++){
			cin >> Dice[i][j];
		}
	}

	for(i=0;i<n;i++){
		for(j=i+1;j<n;j++){
			for(k=0;k<4;k++){
				tmp=Dice[j][0];
				Dice[j][0]=Dice[j][1];
				Dice[j][1]=Dice[j][5];
				Dice[j][5]=Dice[j][4];
				Dice[j][4]=tmp;
				if(Dice[j][1]==Dice[i][1])
					goto Break;
				else{
					for(l=0;l<4;l++){
						tmp=Dice[j][0];
						Dice[j][0]=Dice[j][3];
						Dice[j][3]=Dice[j][5];
						Dice[j][5]=Dice[j][2];
						Dice[j][2]=tmp;
						if(Dice[j][0]==Dice[i][1]){
							tmp=Dice[j][0];
							Dice[j][0]=Dice[j][4];
							Dice[j][4]=Dice[j][5];
							Dice[j][5]=Dice[j][1];
							Dice[j][1]=tmp;
							goto Break;
						}
					}
				}
			}
			Return:;
		}
	}
	goto Exit;

	Break:;
	for(m=0;m<4;m++){
		tmp=Dice[j][0];
		Dice[j][0]=Dice[j][3];
		Dice[j][3]=Dice[j][5];
		Dice[j][5]=Dice[j][2];
		Dice[j][2]=tmp;
		if(Dice[j][0]==Dice[i][0]){
			if(Dice[i][0]==Dice[j][0]&&Dice[i][1]==Dice[j][1]&&Dice[i][2]==Dice[j][2]&&Dice[i][3]==Dice[j][3]&&Dice[i][4]==Dice[j][4]&&Dice[i][5]==Dice[j][5]){
				cout << "No" << endl;
				return 0;
			}
			else{
				goto Return;
			}
		}
	}
	goto Return;

	Exit:;
	cout << "Yes" << endl;
	return 0;
}

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

vector<long> dp;
vector<long> pre;

void rec(int i, vector<int>& nottaken, long scoresofar, int mask, int group)
{
  if(i==nottaken.size())
  {
    dp[mask]=max(dp[mask],scoresofar+pre[group]);
    return;
  }
  rec(i+1,nottaken,scoresofar,mask,group);
  rec(i+1,nottaken,scoresofar,mask^(1<<nottaken[i]),group^(1<<nottaken[i]));

}

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

  pre.resize(1<<n);
  for(int mask=0;mask<(1<<n);mask++)
  {
    for(int i=0;i<n;i++)
    {
      if(mask&(1<<i))
      {
        for(int j=i+1;j<n;j++)
        {
          if(mask&(1<<j))
            pre[mask]+=a[i][j];
        }
      }
    }
  }

  dp.resize(1<<n,-99999999);
  dp[0]=0;
  for(int mask=0;mask<(1<<n);mask++)
  {
    vector<int> nottaken;
    for(int i=0;i<n;i++)
    {
      if(!(mask&(1<<i)))
         nottaken.push_back(i);
    }
    rec(0,nottaken,dp[mask],mask,0);
  }
  cout<<dp[(1<<n)-1];

}


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

using namespace std;
using namespace __gnu_pbds;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key

typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef long double ld;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
typedef set<int>::iterator sit;
typedef map<int,int>::iterator mit;
typedef vector<int>::iterator vit;

int h,w;
int ex,ey;
short dp[101][101][101][101];
char a[101][101];
short S[101][101];

short calc(int r1, int r2, int c1, int c2)
{
	if(r1>r2||c1>c2) return 0;
	int ans = S[r2][c2];
	if(r1>0&&c1>0) ans+=S[r1-1][c1-1];
	if(r1>0) ans-=S[r1-1][c2];
	if(c1>0) ans-=S[r2][c1-1];
	return ans;
}

bool valid(int u, int d, int l, int r)
{
	if(u>ex||d>h-1-ex||l>ey||r>w-1-ey) return false;
	return true;
}

void amax(short &x, short y)
{
	if(y>x) x=y;
}

void update(int u, int d, int l, int r)
{
	int val = dp[u][d][l][r];
	//cerr<<u<<' '<<d<<' '<<l<<' '<<r<<' '<<val<<'\n';
	if(valid(u+1,d,l,r))
	{
		int L = r; int R = w - l - 1;
		int U = d; int D = h - (u + 1) - 1;
		amax(dp[u+1][d][l][r], val + calc(max(U,ex-u-1),min(D,ex-u-1),max(L,ey-l),min(R,ey+r)));
	}
	if(valid(u,d+1,l,r))
	{
		int L = r; int R = w - l - 1;
		int U = d + 1; int D = h - (u) - 1;
		amax(dp[u][d+1][l][r], val + calc(max(U,ex+d+1),min(D,ex+d+1),max(L,ey-l),min(R,ey+r)));
	}
	if(valid(u,d,l+1,r))
	{
		int L = r; int R = w - (l+1) - 1;
		int U = d; int D = h - u - 1;
		amax(dp[u][d][l+1][r], val + calc(max(U,ex-u),min(D,ex+d),max(L,ey-l-1),min(R,ey-l-1)));
	}
	if(valid(u,d,l,r+1))
	{
		int L = r+1; int R = w - l - 1;
		int U = d; int D = h - u - 1;
		amax(dp[u][d][l][r+1], val + calc(max(U,ex-u),min(D,ex+d),max(L,ey+r+1),min(R,ey+r+1)));
	}
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin>>h>>w;
	for(int i=0;i<h;i++)
	{
		for(int j=0;j<w;j++)
		{
			cin>>a[i][j];
			if(a[i][j]=='o') S[i][j]=1;
			if(i>0) S[i][j]+=S[i-1][j];
			if(j>0) S[i][j]+=S[i][j-1];
			if(i>0&&j>0) S[i][j]-=S[i-1][j-1];
			if(a[i][j]=='E') ex=i,ey=j;
		}
	}
	dp[0][0][0][0]=0;
	for(int sum = 0; sum <= h+w; sum++)
	{
		for(int u = 0; u <= min(ex,sum); u++)
		{
			for(int d = 0; d+u<=sum&&d<=h-1-ex; d++)
			{
				for(int l = 0; l<=ey&&d+u+l<=sum; l++)
				{
					int r = sum-u-d-l;
					if(r>=0&&r<=w-1-ey)
					{
						update(u,d,l,r);
					}
				}
			}
		}
	}
	cout<<dp[ex][h-1-ex][ey][w-1-ey]<<'\n';
}

#include <iostream>
#include <vector>

using namespace std;

void solve()
{
	int N, M;
	cin >> N >> M;
	vector<int> A(N);
	vector<int> B(M);
	for(int i = 0; i < N; ++i)
	{
		cin >> A[i];
	}
	for(int i = 0; i < M; ++i)
	{
		cin >> B[i];
	}
	vector<int> point(N);
	int max = 0;
	for(int i = 0; i < M; ++i)
	{
		for(int j = 0; j < N; ++j)
		{
			if(B[i] >= A[j])
			{
				++point[j];
				if(max < point[j])
				{
					max = point[j];
				}
				break;
			}
		}
	}
	for(int i = 0; i < M; ++i)
	{
		if(max == point[i])
		{
			cout << i + 1 << endl;
		}
	}
}

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


#include <bits/stdc++.h>

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

using namespace std;

int main(){
	string s; cin>>s;
	if(s.length()<26){
		char c='a';
		rep(i,s.length()) if(s[i]==c) c++, i=-1;
		s+=c;
		cout<<s<<'\n';
	}
	else{
		string t=s;
		if(next_permutation(t.begin(),t.end())){
			while(t.substr(0,t.length()-1)>s) t=t.substr(0,t.length()-1);
			cout<<t<<'\n';
		}
		else{
			cout<<"-1\n";
		}
	}
	return 0;
}


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

class Base {
public:
	Base() {
		score = 0;
		outNum = 0;
		first = false;
		second = false;
		third = false;
	}
	void hit() {
		if (third) {
			score++;
		}
		third = second;
		second = first;
		first = true;
	}
	void homerun() {
		if (first) {
			first = false;
			score++;
		}
		if (second) {
			second = false;
			score++;
		}
		if (third) {
			third = false;
			score++;
		}
		score++;
	}
	void out() {
		outNum++;
		if (outNum > 2) {
			cout << score << endl;
			score = 0;
			outNum = 0;
			first = false;
			second = false;
			third = false;
		}
	}

private:
	int score;
	int outNum;
	bool first;
	bool second;
	bool third;
};

int main() {
	Base base;
	string str;
	int inning;
	int count = 0;

	cin >> inning;

	while (count < inning * 3) {
		char data[10];
		cin >> str;
		if (str == "HIT") {
			base.hit();
		}else if (str == "HOMERUN") {
			base.homerun();
		}
		else if(str == "OUT"){
			base.out();
			count++;
		}
	}
	return 0;
}

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

int main(){
	int n, x;
	vector<int> a;

	cin>>n;
	for(int i=0;i<n;i++){
		cin>>x;
		a.push_back(x);
	}

	for(int i=a.size()-1;i>=0;i--){
		if(i != a.size()-1) cout<<" ";
		cout<<a[i];
	}
	cout<<endl;

	return 0;
}


#include <bits/stdc++.h>
//#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
//using namespace boost::multiprecision;
typedef long long int ll;
typedef long double ld;
#define MOD 1000000007
#define ALL(obj) (obj).begin(),(obj).end()
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 long long INF = 1LL << 60;
bool pairCompare(const pair<double,ll>& firstElof, const pair<double,ll>& secondElof){
    return firstElof.first < secondElof.first;
}
bool pairCompareSecond(const pair<double,ll>& firstElof, const pair<double,ll>& secondElof){
    return firstElof.second < secondElof.second;
}
//四方向への移動ベクトル
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

struct edge{//グラフに使うヤツ
    ll from,to,cost;
};

typedef vector<vector<edge> > G;
ll gcd(ll a,ll b){
    if (a%b==0)return(b);
    else return(gcd(b,a%b));
}

int main(){
    bool abc[26];
    fill(abc,abc+26,true);
    string s;
    cin >> s;
    for (ll i=0;i<s.size();i++){
        abc[s[i]-'a']=false;
    }
    for (ll i=0;i<26;i++){
        if (abc[i]){
            cout << s;
            cout << (char)('a'+i) << endl;
            return 0;
        }
    }
    if (s.size()==26){
        for (ll i=s.size()-1;i>=0;i--){
            for (ll j=(s[i]-'a');j<26;j++){
                if (abc[j]){
                     for (ll k=0;k<i;k++){
                         cout << s[k];
                     }
                     cout << (char)('a'+j) << endl;
                     return 0;
                }
            }
            abc[s[i]-'a']=true;
        }
    }
    cout << -1 << endl;
    return 0;
}

#include <iostream>
#include <set>

using namespace std;

int main() {
  long long N, L[2]{};
  cin >> N;
  set<int> S;
  while (cin >> N) {
    if (!S.insert(N).second) {
      S.erase(N);
      if (N > L[0]) L[1] = L[0], L[0] = N;
      else if (N > L[1]) L[1] = N;
    }
  }
  cout << L[0] * L[1] << endl;
}


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

struct Node{
  int key;
  Node *right,*left,*parent;
};

Node *root,*NIL;

Node * Min(Node *p){
  while(p->left!=NIL) p=p->left;
  return p;
}

Node * find(Node *p,int a){
  while(p!=NIL && a!=p->key){
    if(a<p->key) p=p->left;
    else p=p->right;
  }
  return p;
}

Node * Successor(Node *p){
  if(p->right!=NIL) return Min(p->right);
  Node *q=p->parent;
  while(q!=NIL && p==q->right){
    p=q;
    q=q->parent;
  }
  return q;
}

Node * Delete(Node *p){
  Node *a,*b;

  if(p->left==NIL || p->right==NIL) b=p;
  else b=Successor(p);

  if(b->left!=NIL) a=b->left;
  else a=b->right;

  if(a!=NIL) a->parent=b->parent;
  if(b->parent==NIL) root=a;
  else if(b==b->parent->left) b->parent->left=a;
  else b->parent->right=a;

  if(b!=p) p->key=b->key;

  free(b);
}

void insert(int a){
  Node *y=NIL, *x=root, *z;
  z = (Node *)malloc(sizeof(Node));
  z->key=a;
  z->left=NIL;
  z->right=NIL;

  while(x!=NIL){
    y=x;
    if(z->key<x->key) x=x->left;
    else x=x->right;
  }
  z->parent=y;
  if(y==NIL) root=z;
  else if(z->key<y->key) y->left=z;
  else y->right=z;
}

void inorder(Node *p){
  if(p==NIL) return;
  inorder(p->left);
  cout << " " << p->key;
  inorder(p->right);
}

void preorder(Node *p){
  if(p==NIL) return;
  cout << " " << p->key;
  preorder(p->left);
  preorder(p->right);
}

int main(){
  int n,x;
  string str;
  cin >> n;

  for(int i=0;i<n;i++){
    cin >> str;
    if(str=="find"){
      cin >> x;
      Node *p=find(root,x);
      if(p!=NIL) cout << "yes" << endl;
      else cout << "no" << endl;
    }
    else if(str=="insert"){
      cin >> x;
      insert(x);
    }
    else if(str=="print"){
      inorder(root);
      cout << endl;
      preorder(root);
      cout << endl;
    }
    else if(str=="delete"){
      cin >> x;
      Delete(find(root,x));
    }
  }
  return 0;
}

#include <bits/stdc++.h>
#define endl    '\n'
using namespace std;

typedef long long ll;

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

    int n, c;

    cin >> n;

    while (n != 0) {
        int hist[10] = {};

        for (int i = 0; i < n; ++i) {
            cin >> c;

            hist[c]++;
        }

        for (int i = 0; i < 10; i++) {
            if (hist[i] == 0) {
                cout << "-" << endl;
            } else {
                for (int j = 0; j < hist[i]; ++j) {
                    cout << "*";
                }
                cout << endl;
            }
        }

        cin >> n;
    }

    return 0;
}


//
//AtCoder Beginner Contest 122
//C
//https://atcoder.jp/contests/abc122/tasks/abc122_c
//

#include <iostream>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <queue>
#include <map>
#include <set>
#include <tuple>
#include <functional>
#include <cassert>
#include <bitset>
#include <numeric>

using namespace std;

#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=a;i<=(ll)(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=(ll)(b);i--)
#define ALL(x) (x).begin(),(x).end()
#define RALL(x) (x).rbegin(),(x).rend()
#define SIZE(x) (int)(x).size()
#define SIZEL(x) (ll)(x).size()
#define MSG(a) cout << #a << " " << a << endl;

using ll = long long;
using ld = long double;
using Graph = vector<vector<int>>;

const double PI = 3.14159265358979323846;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

ll gcd(ll x, ll y) { return (x % y)? gcd(y, x % y): y; }    //最大公約数
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }            //最小公倍数

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

    string s;
    cin >> s;

    vector<pair<int,int>> lr(n);
    REP(i, q) {
        cin >> lr[i].first >> lr[i].second;
        lr[i].first--;
        lr[i].second--;
    }
    vector<int> sAC(n+1, 0);
    int acCnt=0;
    REP(i,n){
        if ((s[i]=='A') && (s[i+1]=='C')) acCnt++;
        sAC[i+1]=acCnt;
    }
    REP(i,q){
        int ans = sAC[lr[i].second] - sAC[lr[i].first];
        cout << ans << endl;
    }
    return 0;
}

#pragma GCC optimize(2)
#pragma GCC optimize("inline")
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#define mp make_pair
#define pb push_back
using namespace std;

typedef long long li;
template<class T>inline void read(T &x) {
  x = 0;
  T tmp = 1;
  char c = getchar();
  while ((c < '0' || c > '9') && c != '-') c = getchar();
  if (c == '-') tmp = -1, c = getchar();
  while (c >= '0' && c <= '9')
    x = (x << 1) + (x << 3) + c - '0', c = getchar();
  x *= tmp;
}
template<class T>inline void Max(T &x, T y) {
  if (y > x) x = y;
}
template<class T>inline void Min(T &x, T y) {
  if (y < x) x = y;
}

const int N = 600;
int n, d1, d2, a[N][N], b[N][N];

const int dx[] = {1, 1, -1, -1};
const int dy[] = {1, -1, 1, -1};
inline bool out(int x, int y) {
  return x < 0 || y < 0 || x >= (n << 1) || y >= (n << 1);
}

vector<pair<int, int> > g, h;

struct node {
  int x, y, ty;
  node() {}
  node(int _x, int _y, int _ty) : x(_x), y(_y), ty(_ty) {}
};
queue<node> q;
void Fill_a(int Ax, int Ay) {
  q.push(node(Ax, Ay, 0));
  while (!q.empty()) {
    int x = q.front().x, y = q.front().y;
    int cur = q.front().ty;
    q.pop();
    if (a[x][y] != -1) continue;
    a[x][y] = cur;
    for (int i = 0; i < (int)g.size(); ++i) {
      int nowx = x + g[i].first, nowy = y + g[i].second;
      if (out(nowx, nowy) || a[nowx][nowy] != -1) continue;
      q.push(node(nowx, nowy, cur ^ 1));
    }
  }
}
void Fill_b(int Ax, int Ay) {
  q.push(node(Ax, Ay, 0));
  while (!q.empty()) {
    int x = q.front().x, y = q.front().y;
    int cur = q.front().ty;
    q.pop();
    if (b[x][y] != -1) continue;
    b[x][y] = cur;
    for (int i = 0; i < (int)h.size(); ++i) {
      int nowx = x + h[i].first, nowy = y + h[i].second;
      if (out(nowx, nowy) || b[nowx][nowy] != -1) continue;
      q.push(node(nowx, nowy, cur ^ 1));
    }
  }
}

int main(void) {
  read(n), read(d1), read(d2);
  for (int i = 0; i < 450; ++i)
    for (int j = 0; j < 450; ++j) {
      int now = i * i + j * j;
      if (now == d1) {
	for (int t = 0; t < 4; ++t)
	  g.pb(mp(i * dx[t], j * dy[t]));
      }
      if (now == d2) {
	for (int t = 0; t < 4; ++t)
	  h.pb(mp(i * dx[t], j * dy[t]));
      }
    }
  for (int i = 0; i < (n << 1); ++i)
    for (int j = 0; j < (n << 1); ++j)
      a[i][j] = b[i][j] = -1;
  for (int i = 0; i < (n << 1); ++i)
    for (int j = 0; j < (n << 1); ++j)
      if (a[i][j] == -1) Fill_a(i, j);
  for (int i = 0; i < (n << 1); ++i)
    for (int j = 0; j < (n << 1); ++j)
      if (b[i][j] == -1) Fill_b(i, j);

  for (int x = 0; x < 2; ++x)
    for (int y = 0; y < 2; ++y) {
      int cnt = 0;
      for (int i = 0; i < (n << 1); ++i)
	for (int j = 0; j < (n << 1); ++j)
	  cnt += (a[i][j] == x && b[i][j] == y);
      if (cnt >= n * n) {
	int m = n * n;
	for (int i = 0; i < (n << 1) && m; ++i)
	  for (int j = 0; j < (n << 1) && m; ++j)
	    if (a[i][j] == x && b[i][j] == y)
	      printf("%d %d\n", i, j), --m;
	return 0;
      }
    }
}


#include <bits/stdc++.h>
#define REP(i, n) for(int i = 0; i < (n); i++)
#define REP1(i, n) for(int i = 1; i <= (n); i++)
#define REPD(i,a,b) for (int i=(a);i<(b);i++)
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef set<int> si;
typedef vector<si> vsi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<int, int> pi;
typedef queue<int> qi;
typedef queue<pi> qpi;
typedef pair<ll, ll> pll;
typedef queue<pll> qpll;
typedef vector<pi> vpi;
typedef vector<pll> vpll;
const int mod = 1000000007;
const int INF = 1001001001;
// 小数点　<< fixed << setprecision(0) <<
// sort降順　sort(ALL(),greater<int>());
// 円周率　M_PI
// 文字判定　isupper islower
// 文字変換
// 順列　do {} while(next_permutation(ALL(X)));
// 最大値　LLONG_MAX
// a内でx以上　auto iter = lower_bound(ALL(a), x);
// a内でxより大きい　auto iter = upper_bound(ALL(a), x);
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
struct edge {
    int from; //出発点
    int to;   //到達点
    int cost; //移動コスト
};
typedef struct edge se;
typedef vector<edge> ve;
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;
}

vpll PrimeFactorization(ll n) {
  vpll res;
  for (ll i=2; i*i<=n; i++) {
    if(n%i!=0) continue;
    ll ex=0;
    while(n%i==0) {
      ex++;
      n/=i;
    }
    res.push_back(pll(i,ex));
  }
  if(n!=1) res.push_back(pll(n,1));
  return res;
}

int main() {
  int k;
  vi a(3);
  cin >> a[0] >> a[1] >> a[2] >> k;
  sort(ALL(a));
  int ans=a[0]+a[1]+a[2]*pow(2,k);
  cout << ans << endl;
  return 0;
}

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

const ll LINF = 1e18;
const int INF = 1e9;
const ll MOD = 1000000007;

int main(){

    int a, b, c, k;
    cin >> a >> b >> c >> k;

    for(int i = 0; i < k; i++){
        if(a < b){
            if(b < c) c *= 2;
            else b *= 2;
        }
        else{
            if(a < c) c *= 2;
            else a *= 2;
        }
    }
    cout << a + b + c << endl;

    return 0;
}


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

using namespace std;

int main()
{
	string S;

	cin >> S;

	for (string::size_type i = 0; i < S.size() / 2; i++)
	{
		swap(S[i], S[S.size() - i - 1]);
	}

	for (string::size_type i = 0; i < S.size(); i++)
	{
		cout << S[i];
	}

	cout << endl;

	return 0;
}

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

/*int main(int argc, char **argv)
{
	int a, b, c, d, e;
	std::cin >> a >> b >> c >> d >> e;
	std::cout << std::min(a, std::min(b, c)) + std::min(d, e) - 50 << std::endl;
	return 0;
}*/
/*int main()
{
	int l, a, b, c, d;
	std::cin >> l >> a >> b >> c >> d;
	int japa, math;
	japa = a / c;
	if(a % c != 0){
		japa++;
	}
	math = b / d;
	if(b % d != 0){
		math++;
	}
	std::cout << l - std::max(japa, math) << std::endl;
}*/
/*int main()
{
	bool sub[31];
	int input;
	for(int i = 0; i < 31; i++){
		sub[i] = false;
	}
	for(int i = 0; i < 28; i++){
		std::cin >> input;
		sub[input] = true;
	}
	for(int i = 1; i < 31; i++){
		if(!sub[i]) std::cout << i << std::endl;
	}
}*/
int main()
{
	std::vector<int> w, k;
	int input;
	for(int i = 0; i < 10; i++){
		std::cin >> input;
		w.push_back(input);
	}
	for(int i = 0; i < 10; i++){
		std::cin >> input;
		k.push_back(input);
	}
	std::sort(w.begin(),w.end(),std::greater<int>());
	std::sort(k.begin(),k.end(),std::greater<int>());
	std::cout << w[0] + w[1] + w[2] << " " << k[0] + k[1] + k[2] << std::endl;
}


/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author champon
 */

#include <iostream>
#include <fstream>

#include <bits/stdc++.h>
#define se cout << "test ok" << endl;
#define sp << " " <<
#define enl "\n";
using namespace std;
using ll=long long;
using ld=long double;
ll mod = ll(1e9)+7;
ll inf = ll(1e18);
template<typename T1, typename T2> ll ceill(T1 a, T2 b){ return (a+b-1)/b; }
void ans(bool b, ostream& out=cout){ out << (b ? "Yes" : "No") << endl; }
void ans2(bool b, ostream& out=cout){ out << (b ? "YES" : "NO") << endl; }
template<typename T> inline bool chmax(T &a, T b){ if(a<b){a=b; return true;} return false; }
template<typename T> inline bool chmin(T &a, T b){ if(a>b){a=b; return true;} return false; }
template<typename A, size_t N, typename T> void Fill(A (&array)[N], const T &val){ fill((T*)array, (T*)(array+N), val); }
int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};

#ifndef INCLUDE_DEBUG_HPP
#define INCLUDE_DEBUG_HPP

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

namespace dbg {
    using ll = long long;

    class Debug {
    public:
        Debug(){}
        template<typename T>
        void vd(vector<T> vec);
        template<typename T>
        void vd2(vector<vector<T>> vec);
        template<typename T>
        void ad(T *arr, ll n);
        template<typename T, size_t N>
        void ad2(T (&arr)[N], ll n);
        template<typename T>
        void md(T mp);
    };

    template<typename T>
    void Debug::vd(vector<T> vec) {
        for (auto vv : vec) cout << vv << " ";
        cout << endl;
    }

    template<typename T>
    void Debug::vd2(vector<vector<T>> vec) {
        for (auto vv : vec) for (auto vvv : vv) cout << vvv << " ";
        cout << endl;
    }

    template<typename T>
    void Debug::ad(T *arr, ll n) {
        for (int i = 0; i <= n; i++) cout << arr[i] << " ";
        cout << endl;
    }

    template<typename T, size_t N>
    void Debug::ad2(T (&arr)[N], ll n) {
        for (int i = 0; i <= N; i++) for (int j = 0; j <= n; j++) cout << arr[i][j] << " ";
        cout << endl;
    }

    template<typename T>
    void Debug::md(T mp) {
        cout << "key : value" << endl;
        for (auto const &m : mp) cout << m.first << " : " << m.second << endl;
        cout << endl;
    }

}

#endif //INCLUDE_DEBUG_HPP

using dbg::Debug;
Debug debug;

class BTouitsu {
public:
	void solve(std::istream& in, std::ostream& out) {
	    int n;
	    in >> n;
	    string a, b, c;
	    in >> a >> b >> c;

	    int cnt = 0;
	    for(int i=0; i<n; i++){
	        if(a[i] == b[i] && b[i] == c[i]) continue;
	        else if(a[i] == b[i] || b[i] == c[i] || c[i] == a[i]) cnt++;
	        else cnt+=2;
	    }

	    out << cnt << enl;
	}
};

int main() {
	BTouitsu solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
	return 0;
}

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {
    ll N = 0;
    cin >> N;
    ll M = 0;
    cin >> M;
    vector<ll> p(M,0);
    vector<string> S(M);
    for(ll i = 0; i < M; i++) {
        cin >> p.at(i);
        cin >> S.at(i);
    }

    vector<int> A(N,0);
    vector<bool> B(N,0);
    ll sum = 0;
    ll ans = 0;
    for (size_t i = 0; i < M; i++) {
        if(S.at(i) == "AC" && !B.at(p.at(i) - 1)) {
            ans++;
            sum += A.at(p.at(i) - 1);
            B.at(p.at(i) - 1) = 1;
        } else if (S.at(i) == "WA" && !B.at(p.at(i) - 1)) {
            A.at(p.at(i) - 1)++;
        }
    }

    cout << ans << ' ' << sum << endl;

    return 0;
}


#include <bits/stdc++.h>

int solve() {
  int N, M; std::cin >> N >> M;
  std::vector<int> s(M);
  std::vector<int> c(M);
  for (int i = 0; i < M; ++i) std::cin >> s[i] >> c[i];

  /* 全探索 */
  for (int n = 0; n < std::pow(10,N); ++n) {
    int digit = 1;      // 0は1桁
    std::vector<int> each_num(1, n%10); // 一の位
    int x = n/10;

    while (x) {
      digit++;
      each_num.push_back(x%10);
      x /= 10;
    }

    if (digit != N) continue; // 桁数の条件を満たさない

    std::reverse(each_num.begin(), each_num.end());

    bool ok = true; // 条件を満たす整数が存在するかどうか

    for (int i = 0; i < M; ++i)
      if (each_num[s[i]-1] != c[i]) ok = false; // 一つでも条件を満たさないものがあれば

    if (ok) return n; // 桁数とそれぞれの桁の数字が条件を満たす
  }

  return -1;

}

int main() {
  std::cout << solve() << std::endl;

  return 0;
}

#include <bits/stdc++.h>
#include <set>
using namespace std;
using pii = pair<int,int>;
using pll = pair<long long, long long>;
const long long INF = 1<<29;

const int MOD = 1000000007;
long long ruizyou(long long m,long long n){
  if(m == 0)return 0;
  if(m == 1)return 1;
  long long ans = 1;
  long long tmp = m;
  for(int i=0;i<=30;i++){
    if(n & (1<<i)){
      ans *= tmp;
      ans %= MOD;
    }
    tmp *= tmp;
    tmp %= MOD;
  }
  return ans;
}
long long kaizyou(long long x){
  if(x == 0)return 1;
  return x * kaizyou(x-1) % MOD;
}
long long comb(long long x,long long y){
  long long bunsi = kaizyou(x);
  long long bunbo = kaizyou(x-y) * kaizyou(y) % MOD;
  return bunsi * ruizyou(bunbo,MOD-2) % MOD;
}
struct unionfind{
  vector<int> par;
  vector<vector<int>> children;
  vector<int> hukasa;
  vector<int> kazu;
  unionfind(int n){
    par = vector<int>(n);
    hukasa = vector<int>(n,0);
    kazu = vector<int>(n,1);
    for(int i=0;i<n;i++){
      par.at(i) = i;
    }
  }
  int root(int x){
    if(par.at(x) == x)return x;
    return root(par.at(x));
  }
  void unite(int x,int y){
    int rx = root(x);
    int ry = root(y);
    if(rx == ry)return;
    if(hukasa.at(rx) >= hukasa.at(ry)){
      par.at(ry) = rx;
      hukasa.at(rx) = max(hukasa.at(ry) + 1,hukasa.at(rx));
      kazu.at(rx) += kazu.at(ry);
    }
    else{
      par.at(rx) = ry;
      kazu.at(ry) += kazu.at(rx);
    }
  }
  void newkazu(){
    int n = kazu.size();
    for(int i=0;i<n;i++){
      kazu.at(i) = kazu.at(root(i));
    }
  }
  bool same(int x,int y){
    return root(x) == root(y);
  }
};
int ctoi(char a){
  return a - '0';
}
long long gcd(long long a,long long b){
  long long c = max(a,b);
  long long d = min(a,b);
  if(d % c == 0)return c;
  return gcd(c,d%c);
}
long long lcm(long long a,long long b){
  return a * b / gcd(a,b);
}
void hukasa(vector<vector<int>> s,vector<vector<int>> table,int &ans,pii frm,int n,int m){
  int x,y;
  tie(x,y) = frm;
  set<pii> se;
  if(x-1 >= 0)se.insert(pii(x-1,y));
  if(x+1 < n)se.insert(pii(x+1,y));
  if(y-1 >= 0)se.insert(pii(x,y-1));
  if(y+1 < m)se.insert(pii(x,y+1));
  for(auto t:se){
    int tx,ty;
    tie(tx,ty) = t;
    if(s.at(tx).at(ty) && table.at(tx).at(ty) == -1){
      table.at(tx).at(ty) = table.at(x).at(y) + 1;
      ans = max(ans,table.at(tx).at(ty));
      hukasa(s,table,ans,t,n,m);
      table.at(tx).at(ty) = -1;
    }
  }
}
void daiku(int frm,vector<vector<pii>> g,vector<int> &cost,int n){
  priority_queue<pii,vector<pii>,greater<pii>> q;
  q.push(pii(cost.at(frm),frm));
  vector<bool> kakutei(n,false);
  while(!q.empty()){
    pii now = q.top();
    q.pop();
    int n1,n2;
    tie(n1,n2) = now;
    if(kakutei.at(n2))continue;
    kakutei.at(n2) = true;
    for(auto t:g.at(n2)){
      int tx,ty;
      tie(tx,ty) = t;
      if(cost.at(tx) > n1 + ty){
        cost.at(tx) = n1 + ty;
        q.push(pii(cost.at(tx),tx));
      }
    }
  }
}

int main(){
  int n,m;
  cin >> n >> m;
  vector<int> s(m),c(m);
  for(int i=0;i<m;i++)cin >> s.at(i) >> c.at(i);
  int ans = -1;
  for(int i=ruizyou(10,n-1);i<ruizyou(10,n);i++){
    bool flag = true;
    for(int j=0;j<m;j++){
      if(i / ruizyou(10,n - s.at(j)) % 10 != c.at(j))flag = false;
    }
    if(flag){
      ans = i;
      break;
    }
  }
  if(n == 1){
    bool flag = true;
    for(int i=0;i<m;i++){
      if(c.at(i) != 0)flag = false;
    }
    if(flag)ans = 0;
  }
  cout << ans << endl;
}