#include <bits/stdc++.h>
#define rep(i, to) for (int i = 0; i < (to); ++i)
#define repf(i, from, to) for (int i = (from); i < (to); ++i)
#define unless(cond) if (!(cond))
using namespace std;
using ll = long long;
template <typename T>
using V = vector<T>;
template <typename T, typename U>
using P = pair<T, U>;

int main() {
  string s;

  cin >> s;

  s[3] = '8';

  cout << s << endl;
}

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

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

    vector <pair <char, int>> ci;
    rep (i, s.size()) {
        if ((i == 0) || (i != 0 && s[i] != s[i - 1])) ci.emplace_back(s[i], 1);
        else ci.back().second ++;
    }

    int ans = 0;
    rep (i, (int)ci.size() - 2) {
        if (ci[i].first == 'J' && ci[i + 1].first == 'O' && ci[i + 2].first == 'I') {
            if (ci[i].second >= ci[i + 1].second && ci[i + 1].second <= ci[i + 2].second) {
                ans = max(ans, min({ci[i].second, ci[i + 1].second, ci[i + 2].second}));
            }
        }
    }
    cout << ans << endl;

    return 0;
}


#include <iostream>
using namespace std;

void solve(long long N){
    cout << N / 3 << '\n';
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    long long N;
    scanf("%lld",&N);
    solve(N);
    return 0;
}

#include<bits/stdc++.h>
#define fastio 	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define int long long int
using namespace std;
int mod = 1e9 + 7;

int32_t main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	fastio;

	string s;
	cin >> s;
	int count = 0, max_count = 0;
	for (int i = 0; i < s.length(); i++) {
		if (s[i] == 'R')
			count++;
		else {
			count = 0;
		}
		max_count = max(count, max_count);
	}
	cout << max_count;
	return 0;
}


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

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}

#define fo(i, n) for(int i=0; i<n; i++)
#define Fo(i, k, n) for(int i=k; i<n; i++)
#define w(x)  int x; cin>>x; while(x--)
#define watch(x) cout << (#x) << " is " << (x) << "\n"
#define watch2(x,y) cout <<(#x)<<" is "<<(x)<<" and "<<(#y)<<" is "<<(y)<<"\n"
#define watch3(x,y,z) cout <<(#x)<<" is "<<(x)<<" and "<<(#y)<<" is "<<(y)<<" and "<<(#z)<<" is "<<(z)<<"\n"
#define watch4(x,y,z,q) cout <<(#x)<<" is "<<(x)<<" and "<<(#y)<<" is "<<(y)<<" and "<<(#z)<<" is "<<(z)<<" and "<<(#q)<<" is "<<(q)<<"\n"
#define MOD 1000000007
#define INF 1000000007;
#define F first
#define S second
#define pb(x) push_back(x);

#define ll  long long
#define all(c) (c).begin(),(c).end()
#define endl "\n"

typedef vector<int> vl;
typedef vector< vl > vvl;
typedef pair< int, int> pll;
typedef map< int, int> mll;

#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

void c_p_c()
{
#ifndef ONLINE_JUDGE
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}
void parray(int a[],int n){
    for(int i=0;i<n;i++){
    cout<<a[i]<<" ";
    }
    cout<<endl;
}

int main() {
    int i,n,m,k,c=0,sum=0;
    c_p_c();
    // cout<<c<<endl;
    string s;
    cin>>s;
    n = (int)s.size();

    int ans =753;
    int mini=INF;
    for (int i = 0,j=2; i <=n-3; i++)
    {
    	/* code */
    	// j++;
    	string str=s.substr(i,3);

    	// watch(str-"0");
    	int x = stoi(str);
    	// debug(x);
    	mini=min(mini,abs(ans-x));

    }
    cout<<mini<<endl;
    return 0;
}

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {
    ll S;
    ll N = 1;
    cin >> S;
    while (S/(ll)pow(10,N)) {
        //cout << pow(10,N) << endl;
        //cout << S/pow(10,N) << endl;
        //cout << N << endl;
        N++;
    }
    //cout << N << endl;
    ll ans = 1000;

    for(int i = 0; i < N-2; i++) {
        ans = min(ans,abs(753 - (S/(int)pow(10,i))%1000));
    }
    cout << ans << endl;

    return 0;
}


#include<stdio.h>

int main()
{
    int N, count=0, min, i=0;

    scanf("%d", &N);

    int P[N+5];

    while(i != N)
    {
        scanf("%d", &P[i]);
        i++;
    }

    if(P[0] <= N)
        {
            count++;
            //printf("1st count = %d\n", count);
            min = P[0];
        }

    for(i=1; i<N; i++)
    {
        if(P[i] < min)
        {
            min = P[i];
            count++;
            //printf("count = %d\nmin = %d=%d\n", count, i, min);
        }
    }

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

    return 0;
}

#include <vector>
#include <iostream>
#include <iomanip>

using namespace std;

typedef long int ll;

double solve(vector<vector<vector<double>>>& dp,vector<vector<vector<bool>>>& memo,int a,int b,int c,int n)
{

    if (a<0 || b<0 || c<0) return 0;
    if (a==0 && b==0 && c==0) return 0;

    if (memo[a][b][c]) return dp[a][b][c];

    double den = double(a) + double(b) + double(c);

    double A = (double(a)/den) * solve(dp,memo,a-1,b,c,n);

    double B = (double(b)/den) * solve(dp,memo,a+1,b-1,c,n);

    double C = (double(c)/den) * solve(dp,memo,a,b+1,c-1,n);

    memo[a][b][c] = true;

    double ex = double(n)/den;
    dp[a][b][c] = A + B + C + ex;

    //cout<<dp[a][b][c]<<endl;

    return dp[a][b][c];
}

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

    int a = 0;
    int b = 0;
    int c = 0;

    for (int i=0;i<N;++i)
    {
        int x;
        cin>>x;

        if (x==1) ++a;
        if (x==2) ++b;
        if (x==3) ++c;
    }

    vector<vector<vector<double>>> dp(N+1,vector<vector<double>>(N+1,vector<double>(N+1)));
    vector<vector<vector<bool>>> memo(N+1,vector<vector<bool>>(N+1,vector<bool>(N+1)));

    cout<<setprecision(14)<<solve(dp,memo,a,b,c,N)<<endl;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define mp make_pair
#define PI pair<ll,ll>
#define poly vector<ll>
#define mem(a) memset((a),0,sizeof(a))
#define For(i,l,r) for(int i=(int)(l);i<=(int)(r);i++)
#define Rep(i,r,l) for(int i=(int)(r);i>=(int)(l);i--)
#define pb push_back
#define fi first
#define se second
#define SZ(x) ((int)(x.size()))
#define re resize
#define PB pop_back
inline char gc(){
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
#define gc getchar
inline ll read(){
	ll x = 0; char ch = gc(); bool positive = 1;
	for (; !isdigit(ch); ch = gc())	if (ch == '-')	positive = 0;
	for (; isdigit(ch); ch = gc())	x = x * 10 + ch - '0';
	return positive ? x : -x;
}
inline void write(ll a){
    if(a<0){
    	a=-a; putchar('-');
	}
    if(a>=10)write(a/10);
    putchar('0'+a%10);
}
inline void writeln(ll a){write(a); puts("");}
inline void wri(ll a){write(a); putchar(' ');}
ll rnd(){
	ull ans=0;
	For(i,0,4)ans=ans<<15^rand();
	return ans%((ull)1<<63);
}
const ll fm=1e9;
int n;
ll ans;
map<pair<PI,PI>,ll> M;
bool zc(PI a,PI b){
	return a.fi>=b.fi&&a.se>=b.se;
}
bool bao(pair<PI,PI> a,pair<PI,PI> b){
	return zc(a.fi,b.se)&&zc(b.fi,a.se);
}
PI get(ll t){
	PI a=mp(0,0);
	while(t%2==0){
		t/=2; a.fi++;
	}
	while(t%5==0){
		t/=5; a.se++;
	}
	return a;
}
int main(){
	#ifdef Brollan
		freopen("1.in","r",stdin);
	#endif
	n=read();
	For(i,1,n){
		ll a;
		cin>>a;
		a=a*fm;
		if(getchar()=='.'){
			ll b=0;
			string s;
			cin>>s;
			For(j,0,s.size()-1)b=b*10+s[j]-'0';
			For(j,1,9-s.size())b*=10;
			a+=b;
		}
		ll x=a,gc=__gcd(x,fm),y=fm/gc;
		x/=gc;
		PI X=get(x),Y=get(y);
		M[mp(X,Y)]++;
		if(y==1)ans--;
	}
	for(auto i:M){
		for(auto j:M)if(bao(i.fi,j.fi)){
			ans+=i.se*j.se;
		}
	}
	cout<<ans/2<<endl;
}

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

int main(){
  int n;
  string s;
  cin >> n >> s;
  vector<int> a(n);
  for(int i = 0; i < n; i++){
    if(s[i] == 'R') a[i] = 0;
    if(s[i] == 'G') a[i] = 1;
    if(s[i] == 'B') a[i] = 2;
  }
  vector<ll> cnt(3);
  for(int i = 0; i < n; i++){
    cnt[a[i]]++;
  }
  ll ans = 1;
  for(int i = 0; i < 3; i++){
    ans *= cnt[i];
  }
  for(int j = 0; j < n; j++){
    for(int i = 0; i < j; i++){
      int k = j + (j - i);
      if(k < n){
        if(a[i] == a[j]) continue;
       	if(a[i] == a[k]) continue;
        if(a[k] == a[j]) continue;
        ans--;
      }
    }
  }
  cout << ans << endl;
  return 0;
}


//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using db = double;
using ld = long double;
template<typename T> using V = vector<T>;
template<typename T> using VV = vector<vector<T>>;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(),(v).end()
#define siz(v) (ll)(v).size()
#define rep(i,a,n) for(ll i=a;i<(ll)(n);++i)
#define repr(i,a,n) for(ll i=n-1;(ll)a<=i;--i)
#define ENDL '\n'
typedef pair<int,int> Pi;
typedef pair<ll,ll> PL;
constexpr ll mod = 1000000007;
constexpr ll INF = 1000000099;
constexpr ll LINF = (ll)(1e18 +99);
const vector<ll> dx={-1,1,0,0},dy={0,0,-1,1};
template<typename T,typename U> inline bool chmin(T& t, const U& u){if(t>u){t=u;return 1;}return 0;}
template<typename T,typename U> inline bool chmax(T& t, const U& u){if(t<u){t=u;return 1;}return 0;}
template<typename T> inline T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<typename T,typename Y> inline T mpow(T a, Y n) {
  T res = 1;
  for(;n;n>>=1) {
    if (n & 1) res = res * a;
    a = a * a;
  }
  return res;
}

signed main(){
  cin.tie(0);ios::sync_with_stdio(false);
  cout<<fixed<<setprecision(20);
  ll n,m;cin>>n>>m;
  VV<int> e(n,V<int>(0));

  rep(i,0,m){
    int a,b;cin>>a>>b;
    a--;b--;
    e[a].pb(b);
    e[b].pb(a);
  }

  V<int> flag(n,-1);

  queue<int> q;
  q.push(0);
  flag[0]=0;
  q.push(0);
  q.push(-1);

  while(q.size()){
    int now=q.front();q.pop();
    int dis=q.front();q.pop();
    int pre=q.front();q.pop();

    for(auto&& i:e[now]){
      if(i==pre)continue;

      if(flag[i]==-1){
        q.push(i);
        flag[i]=dis^1;
        q.push(dis^1);
        q.push(now);
      }else if(flag[i]^(dis^1)){
        cout<<n*(n-1)/2 -m<<ENDL;
        return 0;
      }
    }
  }

  ll x,y;x=y=0;
  rep(i,0,n){
    if(flag[i]==0)x++;
    else y++;
  }

  cout<<x*y-m<<ENDL;

}
//! ( . _ . ) !
//CHECK overflow,vector_size,what to output?

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=5e5+5;
const int M=(1<<20)+5;
const int p=1e9+7;

int read()
{
	int s=0;
	char c=getchar(),lc='+';
	while (c<'0'||'9'<c) lc=c,c=getchar();
	while ('0'<=c&&c<='9') s=s*10+c-'0',c=getchar();
	return lc=='-'?-s:s;
}
void write(int x)
{
	if (x<0)
	{
		putchar('-');
		x=-x;
	}
	if (x<10) putchar(x+'0');
	else
	{
		write(x/10);
		putchar(x%10+'0');
	}
}
void print(int x,char c='\n')
{
	write(x);
	putchar(c);
}
void add(int &x,int y)
{
	x+=y;
	if (x>=p) x-=p;
}
int sum1[M],sum2[M],dp[N],s[N],x[N];

signed main()
{
	x[0]=s[0]=0;
	memset(sum1,0,sizeof(sum1));
	memset(sum2,0,sizeof(sum2));
	int n=read();
	dp[0]=1;
	sum1[0]=1;
	for (int i=1;i<=n;i++)
	{
		x[i]=x[i-1]^read();
		s[i]=s[i-1]+(x[i]==0);
		dp[i]=((s[i-1]*sum1[x[i]]-sum2[x[i]]+1)%p+p)%p;
		add(sum1[x[i]],dp[i]);
		add(sum2[x[i]],dp[i]*s[i]%p);
	}
	if (x[n]) print(dp[n]);
	else
	{
		int ans=dp[n];
		for (int i=1;i<=n;i++) if (x[i]) add(ans,dp[i]);
		print(ans);
	}

	return 0;
}


#include<iostream>
using namespace std;

int main(){
  int i,j,h,w,minh,minw;
  while(1){
    int result;
    int resultmin=100000000;

    cin >> h >> w;
    if(h==0 && w==0)break;

    int original = h*h+w*w;
    int original_minh = h;
    int flag = 0;

    for( i = 1;i < 150; i++ ){
      for( j = i+1; j <=150; j++ ){

	result = i*i+j*j;
      	if( result > original && result < resultmin ){
	    resultmin = result;
	    minh=i;
	    minw=j;
	}
	if( result == original && i > original_minh ){
	  minh=i;
	  minw=j;
	  flag = 1;
	  break;
	}

      }
      if( flag == 1 ) break;
    }
    cout << minh << " " << minw << endl;

  }

  return 0;
}

#include <stdio.h>

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

	if(a < 10) {
		a = 100 * (10 - a);
		b = b + a;
		printf("%d", b);
	} else {
		printf("%d", b);
	}

	return 0;
}


#include <bits/stdc++.h>
#define rep(i,n) for (int (i) = 0; (i) < (n); i++)
#define ll long long
using namespace std;
int main() {
  ll N = 0, A,B;;
  cin >> N >> A >> B;
  if(N == 1){
    cout << "Hello World";
  }
  else{
    cout << A + B;
  }
}

/*
ID: anonymo14
TASK: wormhole
LANG: C++
*/
#include<bits/stdc++.h>

using namespace std;

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

#define F first
#define S second
#define PU push
#define PUF push_front
#define PUB push_back
#define PO pop
#define POF pop_front
#define POB pop_back

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

#define MOD 1000000007

void solve(int test_case) {//227
	int n;
	cin>>n;
	if(n==1)cout<<"Hello World";
	else {
		int a,b;
		cin>>a>>b;
		cout<<a+b;
	}
}

int main() {

	////// FILE BASED IO////
	//freopen("wormhole.in", "r", stdin);
	//freopen("wormhole.out", "w", stdout);
	///////////////

	ios::sync_with_stdio(0);
	cin.tie(0);
	int t=1;
	//cin>>t;
	REP(i,1,t) {
		solve(i);
	}
	return 0;
}


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

int dp[34];
int main(){
int N,x;

  cin >> x;
  dp[0] = 1;
  for(int i = 0; i < 31; i++){

	  dp[i+1] += dp[i];
	  dp[i+2] += dp[i];
	  dp[i+3] += dp[i];

  }

  while(x != 0){

	  cout << dp[x]/3650 + 1 << endl;;

	  cin >> x;
  }

	return 0;
}

#include<iostream>

using namespace std;

int main(){

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

        int dp[31];
        fill(dp, dp+31, 0);
        dp[1]=1; dp[2]=2; dp[3]=4;
        for(int i=4; i<=n; i++){
            dp[i]=dp[i-1]+dp[i-2]+dp[i-3];
        }

        cout<< (dp[n]+3650-1)/3650<< endl;
    }

    return 0;
}


#define _GLIBCXX_DEBUG
#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
typedef unsigned long long int ulli;
#define vec(s) vector<s>
#define vvec(s) vector<vector<s>>
typedef vector<lli> vi;
typedef vector<vi> vvi;
typedef pair<lli,lli> pii;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define Yes(x) cout<<(x?"Yes":"No")<<endl;
#define YES(x) cout<<(x?"YES":"NO")<<endl;
#define out(s) cout<<s<<endl;
#define pb(s) push_back(s);
#define sp " "
#define INF 10000000000
#define LINF 9000000000000000000
#define all(s) s.begin(),s.end()
void vout(vi v){
    for(lli i=0;i<v.size();i++)
    cout<<v.at(i)<<endl;
}
int main(){
    map<string, lli > ans;
    ans["SUN"]=7;
    ans["MON"]=6;
    ans["TUE"]=5;
    ans["WED"]=4;
    ans["THU"]=3;
    ans["FRI"]=2;
    ans["SAT"]=1;
    string s;
    cin>>s;
    out(ans[s]);
}

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

void solve(std::string S){
    vector<string> yobi = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};
    for(int i = 0; i < yobi.size(); ++i){
        if(yobi[i] == S){
            cout << 7 - i << endl;
            break;
        }
    }
}

int main(){
    std::string S;
    std::cin >> S;
    solve(S);
    return 0;
}


#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<cstdio>
#include<sstream>
#include<iomanip>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
using namespace std;
//kaewasuretyuui
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int,pii> pip;
typedef vector<pip>vip	;
const double PI=acos(-1);
const double EPS=1e-8;
const int inf=1e8;
int n,m;
vvi in;
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
int out;
void f(int x,int y,int c){
	if(c==10)return;
	rep(i,4){
		int nx=x,ny=y;
//		cout<<nx<<" "<<ny<<endl;
		while(1){
			nx+=dx[i];ny+=dy[i];
			if(nx<0||ny<0||nx>=n||ny>=m)break;
			if(in[x+dx[i]][y+dy[i]]==1)break;
			if(in[nx][ny]==3){
				out=min(out,c+1);
				return;
			}
			if(in[nx][ny]==1){
				in[nx][ny]=0;
				f(nx-dx[i],ny-dy[i],c+1);
				in[nx][ny]=1;
				break;
			}
		}
	}
}
int main(){
	while(cin>>m>>n,n+m){
		out=inf;
		in=vvi(n,vi(m));
		rep(i,n)rep(j,m)cin>>in[i][j];
		rep(i,n)rep(j,m)if(in[i][j]==2){
			f(i,j,0);
			if(out!=inf)cout<<out<<endl;
			else cout<<-1<<endl;
			goto end;
		}
		end:;
	}
}

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

#define rep(i, n) for(int i = 0; i < n; i ++)
#define COUNT(i, n) for(int i = 1; i <= n; i ++)
#define ITER(c) __typeof((c).begin())
#define each(c, it) for(ITER(c) it =(c).begin(); it!=(c).end(); it++)
#define ALL(c) c.begin(), c.end()
#define mp make_pair
#define pb push_back
#define MEMSET(v, h) memset((v), h, sizeof(v))

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef vector<int> vi;
typedef vector<string> vs;

const int INF = 1 << 24;

int main(void){
    for(int n, m, s; s = 0, cin >> n >> m, n; cout << s << endl){
        priority_queue<int> p;
        rep(i, n){
            int in; cin >> in;
            p.push(in);
        }
        COUNT(i, n){
            if(i % m) s += p.top();
            p.pop();
        }
    }

    return 0;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#define MN 1000000
using namespace std;
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
char s[MN+5],t[MN+5];
int last[MN+5][26],a[MN+5],n,ans=0;
int main()
{
	n=read();
	scanf("%s%s",s+1,t+1);
	for(int i=1;i<=n;++i)
	{
		memcpy(last[i],last[i-1],sizeof(last[0]));
		last[i][s[i]-'a']=i;
	}
	for(int i=n,j=n,r=0,k=0;i;--i)
	{
		if(j>i) j=i;
		if(s[j]!=t[i])
		{
			if(!last[j][t[i]-'a']) return 0*puts("-1");
			j=last[j][t[i]-'a'];
			++r;++k;ans=max(ans,k);
			++a[j+r];
		}
		k-=a[i+r];a[i+r]=0;
	}
	cout<<ans;
	return 0;
}

#include <cstdio>
using namespace std;
#define REP(a,b,c) for(int a=b;a<=c;++a)
#define RREP(a,b,c) for(int a=b;a>=c;--a)

char s[1000005],t[1000005];
int n,x[1000005],l,r,ans,q,j;

int main() {
	scanf("%d%s%s",&n,s+1,t+1);
	j=n; l=1; r=0; ans=0; q=0;
	RREP(i,n,1) {
		if(j>i) --j;
		if(s[j]==t[i]) continue;
		while(j&&s[j]!=t[i]) --j;
		if(!j) {puts("-1"); return 0;}
		--q; x[++r]=j-q; while(l<=r&&x[l]+q>=i) ++l;
		if(r-l>=ans) ans=r-l+1;
	} printf("%d\n",ans); return 0;
}


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

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

  if(N == 1) cout << "Hello World" << endl;
  else {
    cin >> A >> B;
    cout << A+B << endl;
  }

}

#define _GLIBCXX_DEBUG
#include<bits/stdc++.h>
#define rep(i,n) for (int i = 0;i < (n);i++)
#define all(v) v.begin(),v.end()
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
using vl = vector<ll>;
using vvl = vector<vl>;

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

ll mod(ll a){
  return (a % 1000000007);
}

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

int main(){
  ll n; cin >> n; ll a,b; cin >> a >> b;
  if(n==1) cout << "Hello World" << endl;
  else cout << a+b << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
const ll MOD = 1000000007;
const string V = "Vacant";
int main() {
  ll N;
  cin >> N;
  string l, r;
  cout << 0 << endl << flush;
  cin >> l;
  cout << endl;
  if (l == V) return 0;
  cout << N - 1 << endl << flush;
  cin >> r;
  cout << endl;
  if (r == V) return 0;
  int l_num = 0, r_num = N - 1;
  while (true) {
    int mid_num = (l_num + r_num) / 2;
    string mid;
    cout << mid_num << endl << flush;
    cin >> mid;
    cout << endl;
    if (mid == V) return 0;
    int num = mid_num - l_num;
    if (num % 2 == 1 && mid == l) {
      r_num = mid_num;
      r = mid;
      continue;
    } else if (num % 2 == 0 && mid != l) {
      r_num = mid_num;
      r = mid;
      continue;
    }
    l = mid;
    l_num = mid_num;
  }
}

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

vector<string> v;

void init() { rep(i, 0, 7) cin >> v[i]; }
string test(int i) { return v[i]; }

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

  int maxs = 19, mid = 0, head = 0, tail = n - 1;
  string old_input, input;

  cout << 0 << endl;
  cin >> input;
  old_input = input;
  // v.resize(n);
  // init();
  // old_input = input = test(0);
  if (input == "Vacant")
    return 0;

  while (maxs--) {
    mid = head + ((tail + 1) - head) / 2;
    cout << mid << endl;
    cin >> input;
    // input = test(mid);

    if (input == "Vacant")
      return 0;
    if ((mid - head) & 1) {
      if (old_input == input) {
        tail = mid;
      } else {
        head = mid;
        old_input = input;
      }
    } else {
      if (old_input == input) {
        head = mid;
        old_input = input;
      } else {
        tail = mid;
      }
    }
  }
}


#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for(int i = 0; i < n; ++i)
using namespace std;
template <typename T>
bool PN(T x){ if (x <= 1) return false; if (x == 2) return true; for (int i = 2; i < sqrt(x) + 1; i++) if (x % i == 0) return false; return true;}
const ll MOD = 1e9+7;
long long Comb(int n, int i){long long ans = 1; if(i>n || i < 0) return 0; if(i == 0 || i == n) return 1; else {for(int j = 1; j <= i; ++j){
      ans *=(n+1-j);
      ans /= j;
      ans %= MOD;} }return ans;}

template<typename T> T gcd(T a, T b){if(b == 0) return a;else return gcd(b, a%b);}
template<typename T> T lcm(T a, T b){if(b > a) swap(a, b); T g = gcd(a, b);return a / g * b;
}

void solve() {
    int n; cin >> n;
    map<ll,ll, greater<ll>> mp;
    rep(i, n) {
        ll tmp;
        cin >> tmp;
        mp[tmp]++;
    }
    int cnt = 0;
    ll ans = 1;
    for(auto p: mp){
        if(p.second >= 4 && cnt == 0) {
            ans = p.first * p.first;
            cnt = 2;
            break;
        } else if (p.second >= 2 && cnt == 1) {
            ans *= p.first;
            cnt++;
            break;
        } else if(p.second >= 2 && cnt == 0) {
            ans *= p.first;
            cnt++;
        }
    }
    if(cnt <= 1) cout << 0 << endl;
    else cout << ans << endl;
}

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

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

int main() {
    ll N, a, H = 0, W = 0;
    cin >> N;
    vector<int> A(N);
    for(int i=0; i<N; i++) cin >> A[i];
    sort(A.begin(), A.end());

    for(int i=N-1; i>=1; i--) {
        if(A[i] == A[i-1]) {
            if(H == 0) H = A[i];
            else if(W == 0) W = A[i];
            else break;
            i--;
        }
    }
    cout << H * W << endl;
}


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

template<typename T>
ostream& operator<<(ostream &os, vector<T> &v){
	string sep = " ";
	if(v.size()) os << v[0];
	for(int i=1; i<v.size(); i++) os << sep << v[i];
	return os;
}

template<typename T>
istream& operator>>(istream &is, vector<T> &v){
	for(int i=0; i<v.size(); i++) is >> v[i];
	return is;
}

#ifdef DBG
void debug_(){ cout << endl; }
template<typename T, typename... Args>
void debug_(T&& x, Args&&... xs){
	cout << x << " "; debug_(forward<Args>(xs)...);
}
#define dbg(...) debug_(__VA_ARGS__)
#else
#define dbg(...)
#endif

void solve(){
	int n; cin >> n;
	vector<ll> a(n); cin >> a;
	string s; cin >> s;

	vector<ll> b;
	for(int i=n-1; i>=0; i--){
		ll x = a[i];
		for(ll y: b){
			x = min(x, y^x);
		}
		if(s[i]=='0'){
			if(x) b.push_back(x);
		} else {
			if(x){
				cout << 1 << endl;
				return;
			}
		}
	}
	cout << 0 << endl;
}

int main() {
	ios_base::sync_with_stdio(false);
	cout << setprecision(20) << fixed;

	int t; cin >> t;
	for(int i=0; i<t; i++) solve();
	return 0;
}

#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <functional>
#include <cmath>
#include <set>
#include <queue>
#include <deque>
#include <vector>
#include <climits>
#include <sstream>
#include <iomanip>
#include <map>
#include <stack>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;

template <typename T>
struct RollingHash
{
	int size;
	int n;
	ll factor = 1;
	ll b = 0;
	ll mod = 0;
	ll hash = 0;
	vector<T> list;
	int num = 0;
	int head = 0;

	void init(int _n, ll _b = (ll)1e8 + 7, ll _mod = (ll)1e9 + 7)
	{
		n = _n;
		b = _b;
		list.reserve(n);
		mod = _mod;
		auto temp = 1ll;
		for (auto i = 0; i < n; ++i)
		{
			factor = temp;
			temp = temp * b % mod;
		}
	}

	void add(ll val)
	{
		if ((int)list.size() >= n)
		{
			hash = (((hash - list[head] * factor) % mod) + mod) % mod;
			list[head] = val;
			head = (head + 1) % n;
		}
		else
		{
			list.push_back(val);
		}
		hash = hash * b % mod;
		hash = (hash + val) % mod;
	}

	ll getHash() { return hash; }
};

int main()
{
	int N;
	cin >> N;
	vector<ll> a(N);
	vector<ll> b(N);
	for (auto i = 0; i < N; ++i)
	{
		ll temp;
		cin >> temp;
		a[i] = temp;
		a.push_back(temp);
	}
	for (auto& temp : b)
	{
		cin >> temp;
	}

	vector<ll> x;
	for (auto i = 0; i < N; ++i)
	{
		x.push_back(b[0] ^ a[i]);
	}

	auto bitNum = 31;
	auto mod = (ll)1e9 + 7;
	vector<ll> rollingHashFactor = { (ll)1e8 + 7 };
	vector<vector<vector<RollingHash<ll>>>> hashA
		(bitNum, vector<vector<RollingHash<ll>>>(2, vector<RollingHash<ll>>((int)rollingHashFactor.size())));
	vector<vector<RollingHash<ll>>> hashB
		(bitNum, vector<RollingHash<ll>>((int)rollingHashFactor.size()));

	for (auto i = 0; i < bitNum; ++i)
	{
		for (auto k = 0; k < N; ++k)
		{
			auto val = (b[k] >> i) & 1;
			for (auto l = 0; l < (int)rollingHashFactor.size(); ++l)
			{
				if (k == 0)
				{
					hashB[i][l].init(N, rollingHashFactor[l], mod);
				}
				hashB[i][l].add(val);
			}
		}
	}

	for (auto i = 0; i < bitNum; ++i)
	{
		for (auto j = 0; j < 2; ++j)
		{
			for (auto k = 0; k < N; ++k)
			{
				auto val = ((a[k] >> i) ^ j) & 1;
				for (auto l = 0; l < (int)rollingHashFactor.size(); ++l)
				{
					if (k == 0)
					{
						hashA[i][j][l].init(N, rollingHashFactor[l]);
					}
					hashA[i][j][l].add(val);
				}
			}
		}
	}

	vector<pair<int, ll>> ans;
	for (auto k = 0; k < N; ++k)
	{
		auto match = true;
		for (auto i = 0; i < bitNum; ++i)
		{
			int j = (x[k] >> i) & 1;
			for (auto l = 0; l < (int)rollingHashFactor.size(); ++l)
			{
				if (hashB[i][l].getHash() != hashA[i][j][l].getHash())
				{
					match = false;
					break;
				}
			}

			if (match == false)
			{
				break;
			}
		}

		if (match != false)
		{
			ans.push_back({ k, x[k] });
		}

		for (auto i = 0; i < bitNum; ++i)
		{
			for (auto j = 0; j < 2; ++j)
			{
				auto val = ((a[k + N] >> i) ^ j) & 1;
				for (auto l = 0; l < (int)rollingHashFactor.size(); ++l)
				{
					hashA[i][j][l].add(val);
				}
			}
		}
	}

	for (auto& pair : ans)
	{
		cout << pair.first << " " << pair.second << endl;
	}

	return 0;
}


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

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef pair<int, int> P;

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

ll mod = 1000000007;
int main(){
  int n; cin >> n;
  vector <ll> a(n);
  rep(i, n){
    cin >> a[i];
    a[i]++;
  }
  vector <ll> num(n+1);
  num[0] = 3;
  ll ans = 1;
  rep(i, n){
    if (num[a[i]-1] < 0){
      cout << 0 << endl;
      return 0;
    }
    ans *= num[a[i]-1];
    ans %= mod;
    num[a[i]-1]--;
    num[a[i]]++;
  }
  cout << ans <<endl;
}

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

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

template <typename T> void s_max(T& a, T b) {
	if (a < b) {
		a = b;
	}
}

class TaskU {
public:
	int N;
	vector<vector<int>> a;
	vector<ll> dp, scores;
	vector<int> rem;

	void solve(std::istream& in, std::ostream& out) {
		in >> N;
		a.resize(N, vector<int> (N));
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < N; j++) {
				in >> a[i][j];
			}
		}
		scores.resize(1 << N, 0);
		dp.resize(1 << N, 0);
		pre_calc();
		for (int mask = 0; mask < (1 << N); mask++) {
			rem.clear();
			for (int i = 0; i < N; i++) {
				if (!(mask & (1 << i))) rem.push_back(i);
			}
			rec(0, dp[mask], mask, 0);
		}
		out << dp[(1 << N) - 1];
	}

	void pre_calc() {
		for (int mask = 0; mask < (1 << N); mask++) {
			for (int i = 0; i < N; i++) {
				for (int j = i + 1; j < N; j++) {
					if ((mask & (1 << i)) && (mask & (1 << j)))
						scores[mask] += a[i][j];
				}
			}
		}
	}

	void rec(int pos, ll score, int mask, int group) {
		if (pos == (int) rem.size()) {
			s_max(dp[mask], score + scores[group]);
			return;
		}
		rec(pos + 1, score, mask ^ (1 << rem[pos]), group ^ (1 << rem[pos]));
		rec(pos + 1, score, mask, group);
	}
};

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	TaskU solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
	return 0;
}


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

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

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;

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

  if (R < 1200)
    cout << "ABC" << endl;
  else if (R < 2800)
    cout << "ARC" << endl;
  else
    cout << "AGC" << endl;

  return 0;
}

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

bool include_3(int);

int main(){
  int n,i;

  cin>>n;

  for(i=1;i<=n;i++){
    if(i%3==0||include_3(i)){
      cout<<" "<<i;
    }
  }

  cout<<endl;

  return 0;
}

bool include_3(int n){
  while(n){
    if(n%10==3){
      return 1;
    }
    n=n/10;
  }
  return 0;
}


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

const int MAXN = 1e6 + 10;

class no_solution: std::exception
{
public:
	const char * what() const noexcept
	{
		return "There is no solution under such constrains";
	}
};

int len;
char a[MAXN], b[MAXN];

auto findSolution() -> std::deque<int>::size_type
{
	std::deque<int>::size_type answer = 0;
	std::deque<int> DQ;
	DQ.push_back(len + 1);

	int k = len + 1, offset = 0;
	for (int i = len; i > 0; i--)
	{
		int t = k;
		k = std::min(i, k);
		while (k && a[k] != b[i])
			k--;
		if (k <= 0)
			throw no_solution();

		if (k == t)
		{
			while (!DQ.empty() && DQ.back() + offset >= i)
				DQ.pop_back();
			DQ.push_back(i - offset);
		}
		else
		{
			offset--;
			if (i != k)
				answer = std::max(answer, DQ.size()), DQ.push_front(k - offset);
		}
	}
	return answer;
}

int main()
{
	std::cin >> len >> (a+1) >> (b+1) ;

	try
	{
		std::cout << findSolution() << std::endl ;
	}
	catch(no_solution & x)
	{
		std::cout << "-1" << std::endl ;
		std::cerr << x.what() << std::endl ;
	}

	return 0;
}


#include <bits/stdc++.h>

#define MAXN 100005

using namespace std;

int n, k, a[MAXN], dep[MAXN], sum[MAXN], adv[MAXN], fa[MAXN], ans;
vector<int> g[MAXN];

int read() {
    char c = getchar();
    int x = 0;
    while (!isdigit(c))
        c = getchar();
    while (isdigit(c)) {
        x = (x << 3) + (x << 1) + c - '0';
        c = getchar();
    }
    return x;
}

int DFS(int u) {
    int maxi = dep[u];
    for (int i = 0; i < (int)g[u].size(); ++i) {
        int v = g[u][i];
        if (v == 1)
            continue;
        dep[v] = dep[u] + 1;
        maxi = max(DFS(v), maxi);
    }
    if (maxi - dep[u] == k - 1) {
        if (maxi > k)
            ++ans;
        return dep[u] - 1;
    }
    return maxi;
}

int main() {
    n = read();
    k = read();
    for (int i = 1; i <= n; ++i) {
        fa[i] = read();
        g[fa[i]].push_back(i);
    }
    DFS(1);
    ans += fa[1] != 1;
    printf("%d\n", ans);
}

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


#include<algorithm>
#include<iostream>
#include<vector>
#include<string>
#define rep(i, start, end) for (int i = (int)start; i < (int)end; ++i)
#define rrep(i, start, end) for (int i = (int)start - 1; i >= (int)end; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
template<typename T> inline bool chmax(T& a, T b) {if (a < b) {a = b; return true;} return 0;}
template<typename T> inline bool chmin(T& a, T b) {if (a > b) {a = b; return true;} return 0;}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    vector<int> A(N), B(N);
    for (auto& a : A) {
        cin >> a;
    }
    for (auto& b : B) {
        cin >> b;
    }
    vector<int> cnt(N + 1, 0);
    for (auto& a : A) {
        ++cnt[a];
    }
    for (auto& b : B) {
        ++cnt[b];
    }
    rep(i, 0, N) {
        if (cnt[i + 1] > N) {
            cout << "No" << endl;
            return 0;
        }
    }
    reverse(all(B));
    int same_val = 0;
    rep(i, 0, N) {
        if (A[i] == B[i]) {
            same_val = A[i];
            break;
        }
    }
    if (same_val) {
        vector<int> idx_buf;
        rep(i, 0, N) {
            if (B[i] != same_val && A[i] != same_val) {
                idx_buf.push_back(i);
            }
        }
        rep(i, 0, N) {
            if (A[i] == B[i]) {
                int target = idx_buf.back();
                idx_buf.pop_back();
                swap(B[i], B[target]);
            }
        }
    }
    cout << "Yes" << endl;
    rep(i, 0, N) {
        cout << B[i] << " \n"[i + 1 == N];
    }
    return 0;
}


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

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned __int128 HASH;
typedef pair<int,int> pii;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pullull;
typedef pair<ll,int> plli;
typedef pair<double, int> pdbi;
typedef pair<int,pii> pipii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;
typedef vector<vector<int>> mat;

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n);i>0;i--)
#define rrep2(i,a,b) for (int i=(a);i>b;i--)
#define pb push_back
#define fi first
#define se second
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()

const ll hmod1 = 999999937;
const ll hmod2 = 1000000000 + 9;
const ll INF = 1<<30;
const ll mod = 1000000000 + 7;
const int dx4[4] = {1, 0, -1, 0};
const int dy4[4] = {0, 1, 0, -1};
const int dx8[8] = {1, 1, 1, 0, 0, -1, -1, -1};
const int dy8[8] = {0, 1, -1, 1, -1, 0, 1, -1};
const double pi = 3.141592653589793;

//#define int long long
#define addm(X, Y) (X) = ((X) + ((Y) % mod) + mod) % mod

signed main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    int cnt[n] = {};
    rep(i, n * (n - 1) / 2) {
        int a, b, c, d;
        cin >> a >> b >> c >> d;
        a--; b--;
        if (c > d) cnt[a] += 3;
        else if (c == d) {
            cnt[a]++;
            cnt[b]++;
        }
        else cnt[b] += 3;
    }
    int score[305] = {};
    rep(i, n) score[cnt[i]]++;
    int total[305] = {};
    rrep2(i, 303, 0) {
        total[i] = total[i + 1] + score[i];
    }
    rep(i, n) {
        cout << total[cnt[i]] - score[cnt[i]] + 1 << endl;
    }
}

#include <iostream>
#include <string>
#include <cstring>
#include <sstream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <chrono>
#include <numeric>

using namespace std;

class cLSearch
{
	int mn, mq;
	string mS, mT;
	vector<int> vs, vt;
	int ans;

	void decode();
public:
	cLSearch(const int &n, const int &q, const string &s, const string &t)
	{
		mn = n;
		mq = q;
		mS = s;
		mT = t;

		decode();
	}

	void search();
	void output();
};

void cLSearch::search()
{
	sort(vs.begin(), vs.end());
	sort(vt.begin(), vt.end());
	int count = 0;
	vector<int>::iterator vis = vs.begin();
	vector<int>::iterator vit = vt.begin();

	int ts, tt;
	while (vis != vs.end() && vit != vt.end())
	{
		if (*vis == *vit)
		{
			++count;
			ts = *vis;
			tt = *vit;
			while (vis != vs.end() && ts == *vis)
				++vis;
			while (vit != vt.end() && tt == *vit)
				++vit;
		}
		else
		{
			if (vis != vs.end() && vit != vt.end() && *vis > *vit)
				++vit;
			else
				++vis;
		}
	}

	ans = count;
}

void cLSearch::output()
{
	cout << ans << endl;
}
void cLSearch::decode()
{
	istringstream iss;
	iss.str(mS);
	int ts;
	for (int i = 0; i < mn; ++i)
	{
		iss >> ts;
		vs.push_back(ts);
	}
	iss.str(mT);
	iss.seekg(0, iss.beg);
	for (int i = 0; i < mq; ++i)
	{
		iss >> ts;
		vt.push_back(ts);
	}
}

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

	int n, q;
	string s, t;
	cin >> n;
	cin.ignore();
	getline(cin, s);
	cin >> q;
	cin.ignore();
	getline(cin, t);

	cLSearch cls(n, q, s, t);
	cls.search();
	cls.output();

	return 0;
}


#include <bits/stdc++.h>
#include<iostream>
#include<vector>
#include<map>
#include<math.h>

#define irep(i,n) for(int i = 0; i < (int)(n); i++)
#define irep2(i,a,n) for(int i = (int)(a); i <= (int)(n); i++)
#define irrep(i,n) for(int i = (int)(n-1); i > -1; i--)
#define irrep2(i,n,a) for(int i = (int)(n); i >= (int)(a); i--)

#define fi first
#define se second

using namespace std;

using ll = long long;
using v_int = vector<int>;
using v2_int = vector<v_int>;
using v_ll = vector<ll>;
using v2_ll = vector<v_ll>;
using v_string = vector<string>;
using v_bool = vector<bool>;
using v2_bool = vector<v_bool>;
using pii = pair<int, int>;
using mii = map<int, int>;

const double PI = 3.1415926535897932;
const int INF = (int)2e9;
const ll LINF = (ll)2e18;
const ll MOD = 1000000007;
const int dc[8] = {1, -1, 0,  0, 1, -1,  1, -1};
const int dr[8] = {0,  0, 1, -1, 1,  1, -1, -1};

template<typename T, typename U> inline
ostream& operator<<(ostream &o, pair<T,U> &p) {
    return o << "{" << p.first << "," << p.second << "}";
}
template<typename T> inline
istream& operator>>(istream &is, vector<T> &vec) {
    for(auto &v : vec) { is >> v; }
    return is;
}
template<typename T> inline
ostream& operator<<(ostream &os, vector<T> &vec) {
    for(auto &v : vec) { os << v << ",";}
    return os;
}
template<typename T> inline
ostream& operator<<(ostream &os, vector<vector<T> > &mat) {
    for(auto &row : mat) { os << row << endl; }
    return os;
}

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

int main(void)
{
    int n, l;
    cin >> n >> l;

    int sum = n * l + n*(n+1)/2 - n;
    int ans = INF;
    int apple = 0;

    irep2(i, 1, n) {
        int eat = sum - (l + i - 1);
        if(ans > abs(eat-sum)) {
            apple = i;
            ans = abs(eat-sum);
        }
    }
    cout << sum - (l + apple -1) << endl;
    return 0;
} /*atcoder*/


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

int main() {
  vector<int> X(3);
  rep(i,3) cin >> X.at(i);

  swap(X.at(0), X.at(1));
  swap(X.at(0), X.at(2));

  for(auto e:X) cout << e << " ";
  cout << endl;
  return 0;
}

//#pragma GCC optimize "trapv"
#include<bits/stdc++.h>
using namespace std;
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//find_by_order(k)  returns iterator to kth element starting from 0;
//order_of_key(k) returns count of elements strictly smaller than k;
//erase,insert same as normal set

//#define      ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define        fast ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define        f first
#define        s second
#define        pb push_back
#define        eb emplace_back
#define        mp  make_pair
#define        forn(i,n) for(int i=0;i<n;++i)
#define        show(b) for(auto const&x: b) cout<<x<<" "
#define        sshow(b) for(auto x:b) cout<<x<<" "
#define        fix(b,val)   memset(b,val,sizeof(b))
#define        pill(b,n,v)  fill(b,b+n,v)
#define        itn          int
#define        w(t)     int t;cin>>t;while(t--)
#define        mx(a,b,c) max(a,max(b,c))
#define        mn(a,b,c) min(a,min(b,c))
#define        intmax    INT_MAX
#define        intmin    INT_MIN
#define        nl        "\n"
#define        prt(val)  cout<<val<<"\n"
#define        all(v)    v.begin(),v.end()
#define        trace1(x)                cerr<<#x<<": "<<x<<endl
#define        trace2(x, y)             cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define        trace3(x, y, z)          cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define        trace4(a, b, c, d)       cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define        trace5(a, b, c, d, e)    cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
#define        trace6(a, b, c, d, e, f) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl

typedef       long long int ll;
typedef       long double LD;
typedef       unsigned long long int LL;
typedef       pair<int,int> pii;
typedef       pair<long long int,int> pli;
typedef       pair<int,long long int> pil;
typedef       pair<long long int,long long int> pll;
const int N=2*1e6+10;
const int mod=1e9+7;
//mt19937 rnd(time(NULL));

/*------Global--------*/

/*-----Functions------*/
template<typename T>
void show2d(const vector<vector<T>>& adj)
{
    for(auto x:adj)
    {
        for(auto y:x)
        {
           cout<<y<<" ";
        }
    cout<<"\n";
    }
}

ll modexp(ll a,ll b,ll mod)
{
    ll x=1;
    while(b)
    {
        if(b&1)
        x = ((x%mod) * (a%mod))%mod;
        a = ((a%mod) * (a%mod))%mod;
        b = b / 2;
    }
    return x;
}

void rf()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
}

int32_t main()
{
    fast;
  //  rf();
    int h1,m1,h2,m2,k;
    cin>>h1>>m1>>h2>>m2>>k;
    int tot = (h2-h1)*60 + (m2-m1);
    tot -= k;
    cout<<tot<<nl;
}


#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define fi first
#define se second
int n,ans=0,cnt[5007],id[5007];
long long h[5007],p[5007],dp[5007];
bool cmp(int x,int y){
	return (h[x]+p[x]<h[y]+p[y]);
}
int main(){
	ios_base::sync_with_stdio(0);
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>h[i]>>p[i];
		id[i]=i;
	}
	sort(id+1,id+1+n,cmp);
	for(int i=1;i<=n;i++) dp[i]=1e16;
	dp[0]=0;
	for(int i=1;i<=n;i++){
		for(int j=i-1;j>=0;j--){
			int t=id[i];
			if(dp[j]<=h[t]) dp[j+1]=min(dp[j+1],dp[j]+p[t]);
		}
	}
	for(int i=n;i>=0;i--){
		if(dp[i]!=1e16){
			cout<<i;
			return 0;
		}
	}
}

#include <iostream>
using namespace std;

void direction(char d, int dice[])
{
    int temp;
    switch(d)
    {
        case 'E': temp    = dice[1];
                  dice[1] = dice[4];
                  dice[4] = dice[6];
                  dice[6] = dice[3];
                  dice[3] = temp;
                  break;
        case 'S': temp    = dice[1];
                  dice[1] = dice[5];
                  dice[5] = dice[6];
                  dice[6] = dice[2];
                  dice[2] = temp;
                  break;
        case 'W': temp    = dice[1];
                  dice[1] = dice[3];
                  dice[3] = dice[6];
                  dice[6] = dice[4];
                  dice[4] = temp;
                  break;
        case 'N': temp    = dice[1];
                  dice[1] = dice[2];
                  dice[2] = dice[6];
                  dice[6] = dice[5];
                  dice[5] = temp;
                  break;
        case 'R': temp    = dice[2];
                  dice[2] = dice[3];
                  dice[3] = dice[5];
                  dice[5] = dice[4];
                  dice[4] = temp;
                  break;
    }
    return;
}

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

    int dice[n][7], same[n];
    for (int i = 0;i < n;i++) same[i] = true;

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

    for (int i = 1;i < n;i++)
    {
        for(int j = 1;j <= 4;j++)
        {
            if (dice[i][1] != dice[0][1])
            {
                direction('N', dice[i]);
            }
            else break;
        }
        if (dice[i][1] != dice[0][1])
        {
            if (dice[i][3] == dice[0][1]) direction('W', dice[i]);
            else if (dice[i][4] == dice[0][1]) direction('E', dice[i]);
            else same[i] = false;
        }
        if (same[i])
        {
            for(int j = 1;j <= 4;j++)
            {
                if (dice[i][2] != dice[0][2])
                {
                    direction('R', dice[i]);
                }
                else break;
            }
            for(int j = 1;j <= 6;j++)
            {
                if (dice[i][j] != dice[0][j])
                {
                    same[i] = false;
                    break;
                }
            }
        }
    }

    int alldiff = true;
    for (int i = 1;i < n;i++)
    {
        if (same[i])
        {
            alldiff = false;
            break;
        }
    }

    if (alldiff) cout << "Yes" << endl;
    else cout << "No" << endl;

    return 0;
}


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

const ll INF = 1LL << 62;
const double PI = acos(-1);
const double eps = 1e-10;

int main() {
  ll n, x, y;
  cin >> n;
  vector<P> red, blue;
  vector<bool> is_pair_red(n, false), is_pair_blue(n, false);
  rep(i, n) {
    cin >> x >> y;
    red.emplace_back(y, x);
  }
  rep(i, n) {
    cin >> x >> y;
    blue.emplace_back(x, y);
  }
  sort(red.rbegin(), red.rend());
  sort(blue.begin(), blue.end());

  ll ans = 0;
  rep(i, n) {
    rep(j, n) {
      if (is_pair_red[j] || is_pair_blue[i]) continue;
      int bx = blue[i].first, by = blue[i].second;
      int rx = red[j].second, ry = red[j].first;
      if (rx < bx && ry < by) {
        is_pair_red[j] = true;
        is_pair_blue[i] = true;
        ans++;
        break;
      }
    }
  }
  cout << ans << endl;
  return 0;
}

//
// Created by tac on 2016/10/16.
//

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

namespace {

void print(const std::string &str,
           std::string::size_type a,
           std::string::size_type b) {
    std::cout << str.substr(a, b - a + 1) << std::endl;
    return;
}

const std::string reverse(const std::string &str,
                          std::string::size_type a,
                          std::string::size_type b) {
    std::string ret = str;
    std::reverse(ret.begin() + a, ret.begin() + b + 1);
    return ret;
}

const std::string replace(const std::string &str,
                          std::string::size_type a,
                          std::string::size_type b,
                          const std::string &rep) {
    std::string ret = str;
    ret.replace(ret.begin() + a, ret.begin() + (b + 1), rep);
    return ret;
}

}

int main() {
    std::string str;
    int n;
    std::string op, rep;
    int a, b;
    std::cin >> str;
    std::cin >> n;
    for (int i = 0; i < n; ++i) {
        std::cin >> op;
        std::cin >> a >> b;
        if (op == "print") {
            print(str, a, b);
        } else if (op == "reverse") {
            str = reverse(str, a, b);
        } else if (op == "replace") {
            std::cin >> rep;
            str = replace(str, a, b, rep);
        } else {
            throw std::runtime_error("");
        }
    }
    return EXIT_SUCCESS;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <limits>
#include <iomanip>
#include <vector>
#include <cstring>
#include <queue>
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using uld = unsigned long long;
using P = pair<int,int>;
const ll mod = 1e9+7;
const int dx[4] = {0,1,0,-1};
const int dy[4] = {1,0,-1,0};
template<class T> inline bool chmax(T &a,T& b){if(a < b){a = b; return true;} else return false;}
template<class T> inline bool chmin(T &a,T& b){if(a > b){a = b; return true;} else return false;}
int main(){
    int n,x;
    cin >> n >> x;
    int num[n],cum[n+1]={0};
    rep(i,n){
        cin >> num[i];
    }
    sort(num,num+n);
    rep1(i,n){
        cum[i] = cum[i-1] + num[i-1];
    }
    int ans = n;
    rep(i,n+1){
        if(i == n){
            if(x != cum[n]){ ans--;}
            break;
        }
        if(cum[i] <= x && x < cum[i+1]){
            ans = i;
            break;
        }
    }
    cout << ans << endl;
}

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

signed main() {
  int n, x;
  cin >> n >> x;

  vector<int> a(n);
  rep(i,n) cin >> a[i];

  sort(a.begin(),a.end());
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if(i == n - 1) {
      if(x == a[i]) ans++;
      break;
    }

    if(a[i] <= x) {
      x -= a[i];
      ans++;
    } else {
      break;
    }

  }
  cout << ans << endl;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> Vi;
typedef tuple<int, int, int> T;
#define FOR(i,s,x) for(int i=s;i<(int)(x);i++)
#define REP(i,x) FOR(i,0,x)
#define ALL(c) c.begin(), c.end()
#define DUMP( x ) cerr << #x << " = " << ( x ) << endl

const int dr[4] = {-1, 0, 1, 0};
const int dc[4] = {0, 1, 0, -1};

int mat[110][2], dp[110][110];
const int INF = 1<<29;

int main() {
  // use scanf in CodeForces!
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N;
  cin >> N;
  REP(i, N) REP(j, 2) cin >> mat[i][j];
  REP(i, N) REP(j, N) dp[i][j] = INF;
  REP(i, N) dp[i][i] = 0;
  for (int w = 1; w < N; w++) {
    for (int L = 0; L + w < N; L++) {
      int R = L + w;
      for (int i = L; i < R; i++)
	dp[L][R] = min(dp[L][R], dp[L][i] + dp[i+1][R] + mat[L][0] * mat[i][1] * mat[R][1]);
    }
  }
  cout << dp[0][N-1] << endl;
  return 0;
}


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

#include <iostream>
#include <fstream>

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 9e18
#define ceill(a, b) (a+b-1)/b
#define ok cout << "ok" << endl;
#define sp << " " <<
ll mod = 1e9+7;
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(b<a){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

class debug
{
public:
    template<typename T> void vdeb(T vec);
    template<typename T1, typename T2> void adeb(T1 *arr, T2 n);
    template<typename T> void mdeb(T mp);
};

#endif //INCLUDE_DEBUG_HPP

template<typename T> void debug::vdeb(T vec)
{
    cout << "#vector set debug" << endl;
    for(auto vv : vec) cout << vv << " ";
    cout << endl;
}

template<typename T1, typename T2> void debug::adeb(T1 *arr, T2 n)
{
    cout << "#adebug" << endl;
    for(int i=0; i<=n; i++) cout << arr[i] << " ";
    cout << endl;
}

template<typename T> void debug::mdeb(T mp)
{
    cout << "#map pair debug" << endl;
    for(auto const& m : mp) cout << m.first sp m.second << endl;
}
debug dbg;

class AThumbnail {
public:
	void solve(std::istream& in, std::ostream& out)
	{
	    int n;
	    in >> n;
	    double sum = 0.0;
	    vector<int> a(n);
	    for(int i=0; i<n; i++){
	        in >> a[i];
	        sum += (double)a[i];
	    }

	    double ave = sum / (double)n;

	    int res = 0;
	    double mi = inf;
	    for(int i=0; i<n; i++){
            double diff = abs(ave - (double)a[i]);
            if(diff < mi){
                mi = diff;
                res = i;
            }
	    }

	    out << res << endl;
	}
};

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const long long INF = 1LL << 60;
const int MOD = 1000000007;
using pint = pair<ll, ll>;

int main(){
    ll N;
    cin >> N;
    vector<ll> A(N);
    vector<ll> out;
    for (int i = 0; i < N; ++i) cin >> A[i];
    sort(A.begin(), A.end());
    ll cnt = 1;
    for (int i = 0; i < N-1; i++) {
      if (A[i] == A[i + 1]) {
        cnt++;
      }
      else{
          if(cnt/2==1){
            out.push_back(A[i]);
          }
          else if(cnt/2>=2){
            out.push_back(A[i]);
            out.push_back(A[i]);
          }
          cnt = 1;
      }
    }
    if (cnt / 2 == 1) {
      out.push_back(A[A.size()-1]);
    } else if (cnt / 2 >= 2) {
      out.push_back(A[A.size()-1]);
      out.push_back(A[A.size()-1]);
    }
    if (out.size() < 2)
      cout << 0 << endl;
    else
      cout << out[out.size() - 1] * out[out.size() - 2] << endl;
    return 0;
}


#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 <iostream>
#include <iomanip>
#include <sstream>
#include <stdio.h>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <math.h>
#include <utility>
#include <string>
#include <ctype.h>
#include <cstring>
#include <cstdio>
#include <sstream>
#include <functional>

using namespace std;

#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define INF 1145141919
#define ll long long
//#define scanf scanf_s

int v, e;
struct edge{ int from, to, cost; };
edge ed[100001];
int par[10001];

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

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

void unite(int x, int y)
{
	x = find(x);
	y = find(y);
	par[x] = y;
}

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

int main()
{
	cin >> v >> e;
	int a, b, c;
	REP(i, e)
	{
		cin >> a >> b >> c;
		ed[i] = { a, b, c };
	}
	sort(ed, ed + e, comp);
	int sum = 0;
	REP(i, 10001) par[i] = i;
	REP(i, e)
	{
		if (!same(ed[i].from,ed[i].to))
		{
			unite(ed[i].from, ed[i].to);
			sum += ed[i].cost;
		}
	}
	cout << sum << endl;
	return 0;
}


#include <iostream>

using namespace std;

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

int main() {
  int n, m;
  cin >> n >> m;
  int l = (m+1)/2, r = m/2;
  rep(i,l) cout << i+1 << " " << 2*l - i << endl;
  rep(i,r) cout << 2*l + i+1 << " " << 2*(l+r) - i + 1 << endl;
  return 0;
}

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

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

  int target = 0;
  for (int i=0 ; i<n ; i++)
    target += l + i;

  int ans;
  vector<int> pie(n);
  for (int i=0 ; i<n ; i++){
    for (int j=0 ; j<n ; j++){
      if (i == j)
        continue;
      pie.at(i) += l + j;
    }
    if (i == 0)
      ans = pie.at(i);
    else{
      if (abs(pie.at(i)-target) < abs(ans-target))
        ans = pie.at(i);
    }
  }

  cout << ans << endl;
}


#include<iostream>
#include<string>
#include<array>
#include<algorithm>
#define MAX 50
using namespace std;

typedef struct {
  string name;
  double rate;
}hurvest;

bool cmp(const hurvest &a,const hurvest &b){
  return a.rate > b.rate;
}

int main(){
  array<hurvest,MAX> data;
  int n,seed_cost,time_sum,time,value,F,S,M,D,E;

  while(1){
  cin >> n;
  if(n==0){
    break;
  }
  for(int i=0;i<n;i++){
    cin >> data[i].name>>seed_cost;
    time_sum=0;
    for(int j=0;j<5;j++){
      cin >> time;
      if(j==3){
	D=time;
      }
      if(j==4){
	E=time;
      }
      time_sum+=time;
    }

    cin >> F >> S >> M;
    value=F*S;
    if(M!=1){
      value*=M;
      time_sum+=(E+D)*(M-1);
    }
    data[i].rate=(double)(value-seed_cost)/(double)time_sum;
  }

  sort(data.begin(),data.end()-(50-n),cmp);

  bool change=true;
  hurvest temp;
  while(change){
    change=false;
    for(int i=0;i<n-1;i++){
      if(data[i].rate==data[i+1].rate){
	if(data[i].name>data[i+1].name){
	  temp=data[i];
	  data[i]=data[i+1];
	  data[i+1]=temp;
	  change=true;
	}
      }
    }
  }

  for(int i=0;i<n;i++){
    cout << data[i].name<<endl;
  }
  cout << '#' <<endl;
  }

  return 0;
}

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <bitset>
#include <map>
#include <unordered_map>
#include <list>
#include <numeric>
#include <utility>
#include <iterator>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <ctime>
#include <cassert>

#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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<long long>vll;
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;

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

typedef pair<double,string> PDS;

bool my_greater(PDS a, PDS b){
  if(a.first==b.first){
    return (a.second > b.second);
  }else{
    return a.first < b.first;
  }
}

void solve(int n){
  vector<PDS> vp(n);

  string L;
  double P,A,B,C,D,E,F,S,M;
  for(int i=0;i<n;i++){
    cin>>L>>P>>A>>B>>C>>D>>E>>F>>S>>M;
    double res=(F*S*M-P)/(A+B+C+(D+E)*M);
    vp[i]=mp(res,L);
  }

  sort(all(vp),my_greater);
  reverse(all(vp));
  for(int i=0;i<n;i++){
    cout<<vp[i].second<<endl;
  }
}

int main(){
  int n;
  while(cin>>n,n){
    solve(n);
    cout<<"#"<<endl;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
ll a[110];
int main(){
	ll n,k;
	cin>>n>>k;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	ll ans=0;
	for(int i=0;i<=min(n,k);i++)for(int j=0;j<=min(n,k)-i;j++){
		vector<ll>v;
		ll sum=0;
		for(int l=1;l<=n;l++){
			if((1<=l&&l<=i)||(n+1-j<=l&&l<=n)){
				sum+=a[l];
				v.push_back(a[l]);
			}
		}
		ll num=k-i-j;
		//cout<<sum<<' '<<v.size()<<endl;
		sort(v.begin(),v.end());
		for(int l=0;l<min(num,(long long)v.size());l++){
			if(v[l]>=0)break;
			sum-=v[l];
		}
		//cout<<num<<' '<<sum<<endl;
		ans=max(ans,sum);
	}
	cout<<ans;
	return 0;
}

#include <bits/stdc++.h>//Carefully Crafted by hetp111
using namespace std;
#define int long long
#define double long double
#define all(v) (v).begin(),(v).end()
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int,int>
#define vii vector<pii>
#define MOD 1000000007
#define MOD2 998244353
#define MOD3 1000000009
#define PI acos(-1)
#define eps (1e-8)
#define INF (1e18)
#define FASTER ios_base::sync_with_stdio(0);cin.tie(0)
template<class A,class B>ostream&operator<<(ostream&out,const pair<A,B>&a){return out<<"("<<a.first<<","<<a.second<<")";}
template<class A>ostream&operator<<(ostream&out,const vector<A>&a){for(const A &it:a)out<<it<<" ";return out;}
template<class A,class B>istream&operator>>(istream&in,pair<A,B>&a){return in>>a.first>>a.second;}
template<class A>istream&operator>>(istream&in,vector<A>&a){for(A &i:a)in>>i;return in;}
//ifstream cinn("in.in");ofstream coutt("out.out");
int poww(const int &a,int b,const int &m=MOD){if(b==0)return 1;int x=poww(a,b/2,m);x=x*x%m;if(b&1)x=x*a%m;return x;}
int ceil(const int &a,const int &b){return (a+b-1)/b;}
////Read:
//Check corner cases, n==1;
//
////Some function:
//__builtin_popcountll(),

int n,c,dp[55][55][55][55];
vi v;

int f(int i,int cnt,int mxgap,int lst){
    if(i==n){
        mxgap=max(mxgap,i-lst-1);
        if(cnt+(n-cnt-mxgap)*2<=c)return 0;
        return -INF;
    }
    int &ans=dp[i][cnt][mxgap][lst+1];
    if(ans!=-INF)return ans;
    ans=v[i]+f(i+1,cnt+1,max(mxgap,i-lst-1),i);
    ans=max(ans,f(i+1,cnt,mxgap,lst));
    return ans;
}

signed main(){
    FASTER;
    cin>>n>>c;
    v=vi(n);cin>>v;
    for(int i=0;i<55;i++){
        for(int j=0;j<55;j++){
            for(int k=0;k<55;k++){
                for(int l=0;l<55;l++){
                    dp[i][j][k][l]=-INF;
                }
            }
        }
    }
    cout<<f(0,0,0,-1);
}


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

using namespace std;

void addWordCount(const vector<string>& cards, unordered_map<string, int>& wordCount);
void subWordCount(const vector<string>& cards, unordered_map<string, int>& wordCount);

int main() {
    int numOfBlueCard = 0;
    cin >> numOfBlueCard;
    vector<string> blueCards;
    for (int i = 0; i < numOfBlueCard; ++i) {
        string blueCard;
        cin >> blueCard;
        blueCards.push_back(blueCard);
    }

    int numOfRedCard = 0;
    cin >> numOfRedCard;
    vector<string> redCards;
    for (int i = 0; i < numOfRedCard; ++i) {
        string redCard;
        cin >> redCard;
        redCards.push_back(redCard);
    }

    unordered_map<string, int> wordCount;
    addWordCount(blueCards, wordCount);
    subWordCount(redCards, wordCount);

    unordered_map<string, int>::iterator maxValueWord = std::max_element(wordCount.begin(), wordCount.end(),
    [](const pair<string, int>& p1, const pair<string, int>& p2) {
        return p1.second < p2.second; });

    int maxValue = maxValueWord->second;
    if (maxValueWord->second < 0) {
        maxValue = 0;
    }
    cout << maxValue << endl;
}

void addWordCount(const vector<string>& cards, unordered_map<string, int>& wordCount) {
    for (string card : cards) {
        ++wordCount[card];
    }
}

void subWordCount(const vector<string>& cards, unordered_map<string, int>& wordCount) {
    for (string card : cards) {
        --wordCount[card];
    }
}

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

int main()
{
    int N, M, point = 0, max = 0;
    cin >> N;
    vector<string> blue(N);
    set<string> ch;
    for (int i = 0; i < N; ++i)
    {
        cin >> blue.at(i);
        ch.insert(blue.at(i));
    }
    cin >> M;
    vector<string> red(M);
    for (int i = 0; i < M; ++i)
    {
        cin >> red.at(i);
        ch.insert(red.at(i));
    }

    for (auto x : ch)
    {
        point = 0; //重要
        for (int i = 0; i < N; ++i)
        {
            if (blue.at(i) == x)
            {
                ++point;
            }
        }
        for (int i = 0; i < M; ++i)
        {
            if (red.at(i) == x)
            {
                --point;
            }
        }
        max = max < point ? point : max;
    }
    cout << max << endl;
}


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

int main() {
    int n; cin >> n;
    int N[n], n_min = INT_MAX;
    for (int i = 0; i < n; i++) {
        cin >> N[i];
        if (N[i] < n_min) n_min = N[i];
    }
    for (int j = 1; j <= n_min; j++) {
        bool div_all = true;
        for (int i = 0; i < n; i++) if (N[i] % j != 0) div_all = false;
        if (div_all == true) cout << j << endl;
    }
}

#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 watch(x) cout<<(#x)<<"="<<(x)<<'\n'
#define mset(d,val) memset(d,val,sizeof(d))
#define setp(x) cout<<fixed<<setprecision(x)
#define forn(i,a,b) for(int i=(a);i<(b);i++)
#define fore(i,a,b) for(int i=(a);i<=(b);i++)
#define pb push_back
#define F first
#define S second
#define pqueue priority_queue
#define fbo find_by_order
#define ook order_of_key
typedef long long ll;
typedef pair<ll,ll> ii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef long double ld;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;
void amin(ll &a, ll b){ a=min(a,b); }
void amax(ll &a, ll b){ a=max(a,b); }
void SD(int t=0){ cout<<"PASSED "<<t<<endl; }
const ll INF = ll(1e18);
const int MOD = 1e9+7;

ll add(ll a,ll b)
{
	a%=MOD; b%=MOD;
	a+=b;a%=MOD;
	if(a<0) a+=MOD;
	return a;
}
ll mult(ll a, ll b)
{
	a%=MOD; b%=MOD;
	ll ans=(a*b)%MOD;
	if(ans<0) ans+=MOD;
	return ans;
}
ll pw(ll a, ll b)
{
	ll r=1;
	while(b){
		if(b&1) r=mult(r,a);
		a=mult(a,a);
		b>>=1;
	}
	return r;
}
ll inverse(ll a)
{
	return pw(a,MOD-2);
}
const int MAXN = 100005;

void NO(){ cout<<0<<'\n'; exit(0); }

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

	int n; cin>>n;
	int a[n];
	forn(i,0,n) cin>>a[i];

	int c[3]{};
	ll ans=1;

	forn(i,0,n){
		int cnt=0;
		forn(j,0,3){
			if(a[i]==c[j]) cnt++;
		}
		ans=mult(ans,cnt);
		forn(j,0,3){
			if(a[i]==c[j]){ c[j]++; break; }
		}
	}

	cout<<ans<<'\n';

	return 0;
}


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

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

#define MOD 1000000007LL

#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 A, B, C, D;
    cin >> A >> B >> C >> D;
    cout << min(A, B) + min(C, D) << endl;
    return 0;
}

#include <bits/stdc++.h>
#define repr(i,from,to) for(int (i)=(from);(i)<(to);(i)++)
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
// const bool debug=true;
const bool debug=false;
#define DEBUG if(debug==true)
#define all(x) begin((x)), end((x))
#define vprint(x) for(auto a:(x)) cout << x << endl;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1000000007;
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;
}
int main(void) {
    cin.tie(0);
    ios::sync_with_stdio(false);
    long double n,m,d;
    cin>>n>>m>>d;
    long double ans = (n-d) * 2 * (m-1) / (n*n);
    if (d==0) {
        ans = (m-1) / n;
    }
    printf("%.10Lf\n", ans);
    return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#define all(c) (c).begin(),(c).end()

using namespace std;

int mergecount(vector<int> &a) {
  int count=0,n=a.size();
  if (n>1){
    vector<int>b(a.begin(),a.begin()+n/2);
    vector<int>c(a.begin()+n/2,a.end());
    count+=mergecount(b);
    count+=mergecount(c);
    for (int i=0,j=0,k=0;i<n;i++)
      if (k == c.size())a[i]=b[j++];
      else if (j == b.size())a[i]=c[k++];
      else if (b[j]<=c[k])a[i]=b[j++];
      else a[i]=c[k++],count+=n/2-j;
  }
  return count;
}

int main(void){
  int n;
  cin >> n;
  vector<int>v(n);
  for(int i=0;i<n;i++)cin >> v[i];
  int cnt=mergecount(v);
  sort(all(v));
  for(int i=0;i<n;i++)cout << v[i] << (i<n-1?" ":"\n");
  cout << cnt << endl;
  return 0;
}

#include <cstdio>
#include <iostream>

using namespace std;

namespace {

// BubbleSort sorts the array |a| in ascending order, and then returns
// the number of swap operations.
int BubbleSort(int* a, int n) {
  int nswap = 0;
  bool flag = true;
  while (flag) {
    flag = false;
    for (int j = n - 1; j >= 1; --j) {
      if (a[j] < a[j - 1]) {
        const int t = a[j - 1];
        a[j - 1] = a[j];
        a[j] = t;
        ++nswap;
        flag = true;
      }
    }
  }
  return nswap;
}

void Print(const int* a, int n) {
  for (int i = 0; i < n; ++i) {
    if (i > 0) printf(" ");
    printf("%d", a[i]);
  }
  printf("\n");
}

} // namespace

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

  const int r = BubbleSort(a, n);
  Print(a, n);
  cout << r << endl;
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

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

typedef long long ll;
typedef vector<int> vi;

int main() {
	int n;
	while (cin >> n, n) {
		ll data[4001] = {};
		bool uni[4001] = {};
		vi order;

		REP(i, n) {
			ll a, b, c;
			scanf("%lld %lld %lld", &a, &b, &c);
			if (!uni[a]) {
				uni[a] = true;
				order.push_back(a);
			}
			data[a] += b * c;
		}

		bool f = false;
		REP(i, order.size()) {
			int num = order[i];
			if (data[num] >= 1000000) {
				printf("%d\n", num);
				f = true;
			}
		}
		if (!f) puts("NA");
	}
	return 0;
}

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

#define REP(i,a,n) for(i=a; i<n; i++)
#define rep(i,n) REP(i,0,n)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define foreach(it,x) for(typeof(x.begin()) it=x.begin(); it!=x.end(); it++)

int matrix[16][16];

int main(){
    int n,i,j;

    while( cin >> n, n ){
        // input
        rep(i,n) rep(j,n) cin >> matrix[i][j];

        // yoko
        rep(i,n){
            int yoko = 0;
            rep(j,n) yoko += matrix[i][j];
            matrix[i][n] = yoko;
        }

        // tate
        rep(j,n){
            int tate = 0;
            rep(i,n) tate += matrix[i][j];
            matrix[n][j] = tate;
        }

        int sum = 0;
        rep(i,n) sum += matrix[i][n];
        matrix[n][n] = sum;

        // output
        rep(i,n+1){
            rep(j,n+1){
                printf("%5d",matrix[i][j]);
            }
            puts("");
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
typedef long long ll;
ll a[N],n,m,v,p;
int ans;
bool cmp(ll x,ll y) {
	return x>y;
}
int main() {
	scanf("%lld%lld%lld%lld",&n,&m,&v,&p);
	for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
	sort(a+1,a+n+1,cmp);
	ll d=a[p];
	ans=p;
	for(ll i=p+1;i<=n;i++) {
		if(a[i]+m>=a[p]) {
			ll cnt=max(0ll,v-(n-i+p)); //p~i-1 放的点数
			if(cnt==0) ans++;
			else {
				if(d+cnt*m<=(a[i]+m)*(i-p))
					ans++;
				else break;
			}
		}
		else break;
		d+=a[i];
	}
	printf("%d\n",ans);
	return 0;
}

#include <iostream>
using namespace std;

long long numOfInvs(int a[], int from, int to, int t[]) {
	if (from == to) {
		return 0LL;
	}
	int mid = (from + to) / 2;
	long long n = numOfInvs(a, from, mid, t) + numOfInvs(a, mid + 1, to, t);
	int i = from, j = mid + 1, k = 0;
	while (i <= mid && j <= to) {
		if (a[i] <= a[j]) {
			t[k++] = a[i++];
		}
		else {
			n += mid - i + 1;
			t[k++] = a[j++];
		}
	}
	if (i == mid + 1) {
		while (j <= to) {
			t[k++] = a[j++];
		}
	}
	else {
		while (i <= mid) {
			t[k++] = a[i++];
		}
	}
	k = 0;
	for (int i = from; i <= to; ++i) {
		a[i] = t[k++];
	}
	return n;
}

long long numOfInvs(int a[], int n) {
	int *t = new int[n];
	int from = 0;
	int to = n - 1;
	long long ret = numOfInvs(a, from, to, t);
	delete[] t;
	return ret;
}

int main() {
	int n;
	cin >> n;
	int *a = new int[n];
	for (int i = 0; i < n; ++i) {
		cin >> a[i];
	}
	cout << numOfInvs(a, n) << endl;
	delete[] a;
}


#include <bits/stdc++.h>
#define ENDL '\n'
#define io ios_base::sync_with_stdio(false);cin.tie(0);
#define sayy cout<<"YES"<<ENDL;
#define sayn cout<<"NO"<<ENDL;

using namespace std;

int main(){
    io

    string s;
    cin >> s;

    int smaller = INT_MAX;
    for(int i(0); i<s.length()-2; i++){
        char temp[3];
        temp[0] = s[i];
        temp[1] = s[i+1];
        temp[2] = s[i+2];
        int n;
        sscanf(temp, "%d", &n);
        int dif = abs(753 - n);
        if(dif < smaller) smaller = dif;
    }
    cout << smaller << ENDL;

    return 0;
}

// include
// ------------------------------------------------
#include <bits/stdc++.h>
#include <algorithm>
#include <math.h>

using namespace std;

// func
// ------------------------------------------------
int CalcSumOfDigit(int n);      // 各桁の和を計算する。
int getDigit(int n);            // 数字の桁数を取得する。
string upper(string str);       // 英字を大文字に変換する。
string lower(string str);       // 英字を小文字に変換する。

// class
// ------------------------------------------------
class Combi
{
  public:
    Combi();
    long long Combination(long long n, long long k);
  private:
    vector<vector<long long>> memo;
    long long n_num;
    long long k_num;
    void Resize(long long n, long long k);
};

// define
// ------------------------------------------------
#define all(a)        (a).begin(),(a).end()
#define rall(a)       (a).rbegin(),(a).rend()
#define sz(a)         int((a).size())
#define rep(i,n)      for(int(i)=0;(i)<(n);(i)++)
#define repe(i,n)     for(int(i)=0;(i)<=(n);(i)++)
#define vsort(v)      sort((v).begin(),(v).end())
#define rvsort(v)     sort(rall((v)))
#define vi            vector<int>
#define GCD(a,b)      __gcd((a),(b))
#define LCM(a,b)      (a)/GCD((a),(b))*(b)
#define kiriage(a,b)  ((a)+(b)-1)/(b)

const int INF = 1e9;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<long> vll;
// code
// ------------------------------------------------
int main() {

  string s;
  cin >> s;

  bool ans = true;
  rep(i,sz(s))
  {
    if(i % 2 == 0)
    {
      if(s[i] != 'h')
        ans = false;
    }
    else
    {
      if(s[i] != 'i')
        ans = false;
    }
  }

  if(sz(s) % 2) ans = false;

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

  return 0;
}
// funcの実体
// ------------------------------------------------
int getDigit(int n)
{
  int i = 1;
  while(1)
  {
    n = n / 10;
    if(n == 1)
      break;
    i++;
  }
  return i;
}
int CalcSumOfDigit(int n)
{
  int s = 0;
  while(n)
  {
    s += n % 10;
    n = n / 10;
  }
  return s;
}

string upper(string str)
{
  for(auto itr = str.begin();itr != str.end() ; itr++)
  {
    if(97 <= *itr && *itr <= 122)
    {
      *itr = *itr - 32;
    }
  }
  return str;
}

string lower(string str)
{
  for(auto itr = str.begin();itr != str.end() ; itr++)
  {
    if(65 <= *itr && *itr <= 90)
    {
      *itr = *itr + 32;
    }
  }
  return str;
}

Combi::Combi(){
  n_num = -1;
  k_num = -1;
};
ll Combi::Combination(ll n, ll k)
{
  Resize(n,k);

  ll ret;
  if(memo[n][k] != 0)
  {
    ret = memo[n][k];
  }
  else if(n == k || k == 0)
  {
    memo[n][k] = 1;
    ret = 1;
  }
  else
  {
    ret = Combination(n - 1, k - 1) + Combination(n - 1, k);
    memo[n][k] = ret;
  }
  return ret;
}

void Combi::Resize(ll n, ll k)
{
  if(n_num <= n || k_num <= k)
  {
    n_num = (n + 1) * 2;
    k_num = (k + 1) * 2;
    memo.resize(n_num);
    for(auto itr = memo.begin(); itr != memo.end(); ++itr)
    {
      itr->resize(k_num);
    }
  }
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define F first
#define S second
const int mod = 1e9 + 7, A = 5e5 + 2;

int main() {
    ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int n, x;
    cin >> n >> x;
    ll a[n];
    for (int i = 0; i < n; i++) cin >> a[i];
    ll sum = 0;
    for (int i = 0; i <= x; i++) {
        for (int j = 0; i + j <= min(n, x); j++) {
            priority_queue<ll, vector<ll>, greater<ll>> pq;
            for (int k = 0; k < i; k++)
                pq.push(a[k]);
            for (int k = n - 1, c = 0; c < j; c++, k--)
                pq.push(a[k]);
            for (int k = i + j; k < x; k++) {
                if (!pq.empty() && pq.top() < 0) pq.pop();
            }
            ll val = 0;
            while(!pq.empty()) val += pq.top(), pq.pop();
            sum = max(sum, val);
        }
    }
    cout << sum;
    return 0;
}

#include "bits/stdc++.h"
using namespace std;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
const long long INF = 100000000;
int dp[300100][2];

int main()
{

	int N; cin >> N;
	string S; cin >> S;
	dp[0][0] = 0;
	dp[0][1] = 0;
	for (int i = 1; i < N; i++) if (S.at(i) == 'E') dp[0][1]++;
	for (int i = 0; i < N-1; i++) {
		if (S.at(i) == 'E') dp[i + 1][0] = dp[i][0];
		else dp[i + 1][0] = dp[i][0] + 1;
		if (S.at(i+1) == 'E') dp[i + 1][1] = dp[i][1] - 1;
		else dp[i + 1][1] = dp[i][1];
	}

	int ans = INF;
	for (int i = 0; i < N; i++) chmin(ans, dp[i][0] + dp[i][1]);

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


#include<iostream>

int main() {
	int N,X,T;
	std::cin >> N >> X >> T;
	int tmp;
	tmp = N / X;
	if (N%X != 0) { tmp += 1; }
	std::cout << tmp * T << std::endl;
	return 0;

}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;

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

const ll mod=1000000007;

ll countBits(ll in){
  int res=0;
  for(;in>0;in>>=1){
    if((in&0x01)!=0){
      res++;
    }
  }
  return res;
}

template<typename T>
void show2dVector(vector<vector<T>>& v){
  for(int i=0;i<v.size();i++){
    int m=v[i].size();
    cout<<i<<" : ";
    for(int j=0;i<m;i++){
      cout<<v[i][j]<<" ";
    }
    cout<<endl;
  }
}

void bfs(const vector<vector<int>>&g, int v, vector<bool>& seen){
  seen[v]=true;
  cout<<v<<" ";
  for(auto next: g[v]){
    if(seen[next]){
      continue;
    }
    bfs(g,next,seen);
  }
}

bool dfs(vector<vector<int>> &g,int start,int goal,vector<bool>&seen){
  bool res=false;
  seen[start]=true;
  if(start==goal){
    return true;
  }
  for(auto next: g[start]){
    if(seen[next]){
      continue;
    }
    res=dfs(g,next,goal,seen);
    if(res){
      break;
    }
  }
  return res;
}

ll gcd(ll a,ll b) {
  return b? gcd(b,a%b): a;
}
ll lcm(ll a,ll b) {
  return a*b/gcd(a,b);
}
bool isLowerCase(char c){
  return (c>='a'&&c<='z');
}

char toLowerCase(char c){
  if(isLowerCase(c)){
    return c;
  }else{
    return c+'a'-'A';
  }
}
char toUpperCase(char c){
  if(isLowerCase(c)){
    return c-('a'-'A');
  }else{
    return c;
  }
}
ll powm(ll a,ll n, ll m){
  ll ret=1;
  while(n>0){
    if(n%2==1){
      ret=(ret*a)%m;
    }
    n>>=1;
    a=(a*a)%m;
  }
  return ret;
}

const string yesno(bool ans){
  return (ans?"Yes":"No");
}
int main() {
  int n,h,w;cin>>n>>h>>w;
  int ans=(n-h+1)*(n-w+1);
  cout<<ans<<endl;
  return 0;
}


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

#define REP(i,n) for(long long i = 0; i < (n); i++)
#define FOR(i, m, n) for(long long i = (m);i < (n); ++i)
#define ALL(obj) (obj).begin(),(obj).end()

template<class T> using V = vector<T>;
template<class T, class U> using P = pair<T, U>;

const ll MOD = (ll)1e9 + 7;
const ll MOD2 = 998244353;
const ll LLINF = (ll)1e18;
const ll INTINF = (ll)1e9;
const long double PI = 3.1415926535897932384626433;

template <class T> void corner(bool flg, T hoge) {if (flg) {cout << hoge << endl; exit(0);}}
template <class T, class U>ostream &operator<<(ostream &o, const map<T, U>&obj) {o << "{"; for (auto &x : obj) o << " {" << x.first << " : " << x.second << "}" << ","; o << " }"; return o;}
template <class T>ostream &operator<<(ostream &o, const set<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const vector<T>&obj) {o << "{"; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "}"; return o;}
template <class T, class U>ostream &operator<<(ostream &o, const pair<T, U>&obj) {o << "{" << obj.first << ", " << obj.second << "}"; return o;}
template <template <class tmp>  class T, class U> ostream &operator<<(ostream &o, const T<U> &obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr)o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
void print(void) {cout << endl;}
template <class Head> void print(Head&& head) {cout << head;print();}
template <class Head, class... Tail> void print(Head&& head, Tail&&... tail) {cout << head << " ";print(forward<Tail>(tail)...);}
template <class T> void chmax(T& a, const T b){a=max<T>(a,b);}
template <class T> void chmin(T& a, const T b){a=min<T>(a,b);}
void YN(bool flg) {cout << ((flg) ? "YES" : "NO") << endl;}
void Yn(bool flg) {cout << ((flg) ? "Yes" : "No") << endl;}
void yn(bool flg) {cout << ((flg) ? "yes" : "no") << endl;}

long long Maximum_Rectangle(vector<long long>& ar){
	ar.push_back(0);
	stack<pair<long long,long long>> st;
	long long res = 0;
	for(long long r = 0; r < ar.size(); ++r){
		long long vr = ar[r];
		if(st.empty()){
			st.push({vr,r});
			continue;
		}

		long long vl = st.top().first, l = st.top().second;
		if(vl < vr) st.push({vr,r});
		if(vl < vr || vl == vr)	continue;

		while(vl > vr) {
			res = max(res,vl*(r - l));
			st.pop();

			if(st.size() && st.top().first > vr) vl = st.top().first, l = st.top().second;
			else break;
		}
		st.push({vr,l});
	}
	ar.pop_back();
	return res;
}

int main() {

	ll H,W; cin >> H >> W;
	string S1,S2;
	cin >> S1;
	V<V<ll>> flg(H,V<ll>(W,0)),cnt(H,V<ll>(W+1,0));
	for(int i = 0; i < H - 1; ++i){
		cin >> S2;
		for(int j = 0; j < W - 1; ++j){
			int a = (S1[j] == '#');
			int b = (S1[j + 1] == '#');
			int c = (S2[j] == '#');
			int d = (S2[j + 1] == '#');
			int tmp = a+b+c+d;
			if(tmp == 1 || tmp == 3) flg[i][j] = 1;
		}
		S1 = S2;
	}

	for(int j = 0; j < W; ++j){
		if(!flg[0][j]) cnt[0][j] = 1;
		for(int i = 1; i < H; ++i){
			if(flg[i][j]) continue;
			cnt[i][j] = cnt[i-1][j] + 1;
		}
	}
	ll ans = max(H,W);
	for(int i = 1; i < H; ++i){
		stack<pair<ll,ll>> st;
		st.push({cnt[i-1][0]+1,0});
		for(ll j = 1; j <= W; ++j){

			ll vj = cnt[i-1][j]+1;
			if(st.empty()){
				st.push({vj,j});
				continue;
			}

			ll vk = st.top().first, k = st.top().second;

			if(vk < vj) st.push({vj,j});
			if(vk < vj || vk == vj)	continue;

			while(1){
				chmax(ans,vk*(j - k + (j!=W?1:0)));
				st.pop();
				if(st.size() && st.top().first > vj) vk = st.top().first, k = st.top().second;
				else break;
			}
			st.push({vj,k});
		}
	}
	//for(int i = 0; i < H; ++i) print(i,cnt[i]);
	cout << ans << endl;

	return 0;
}

#include<iostream>
#include<set>
#include <bitset>
#include<queue>
#include<vector>
#include<map>
#include<stack>
#include <cstdio>
#include<algorithm>
#include <sstream>
#include<string>
#include<string.h>
#include <cmath>
#include <iomanip>
#include <string>
#include<list>
#include <limits>
#include <numeric>
#include <type_traits>
#define int long long
#define ll long long
#define mod  1000000007
#define inf 1e17
#define rep(i,j,n) for(int i=j;i<n;i++)
#define P pair<int,int>
#define ps push_back
double pi = 3.141592653589793;
using namespace std;
template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    fill( (T*)array, (T*)(array+N), val );
}
//ここから始めよう
int p[214514],q[214514],c[214151];
vector<P> G[614514];
int lastid=0;
map<P,int> sid;
int getid(int v,int c){
    P p(v,c);
    if(sid.find(p)!=sid.end())return sid[p];
    return sid[p]=lastid++;
}
void addedge(int p,int q,int c){
    int v=getid(p,c);int u=getid(q,c);
    G[v].ps(P(u,0));
    G[u].ps(P(v,0));
}
signed main(){
    int n,m;cin>>n>>m;
    rep(i,0,m){
        cin>>p[i]>>q[i]>>c[i];
        p[i]--;q[i]--;
        addedge(p[i],q[i],c[i]);
    }
    rep(i,0,m){
        G[getid(p[i],c[i])].ps(P(lastid+p[i],1));
        G[getid(q[i],c[i])].ps(P(lastid+q[i],1));
        G[lastid+p[i]].ps(P(getid(p[i],c[i]),1));
        G[lastid+q[i]].ps(P(getid(q[i],c[i]),1));
    }
    vector<int> d(lastid+n,mod);d[lastid]=0;
    priority_queue<P> que;
    que.push(P(0,lastid));
    while(!que.empty()){
        auto p=que.top();que.pop();
        int u=p.second,dist=-p.first;
        if(dist>d[u])continue;
        for(auto e:G[u]){
            int v=e.first,c=e.second;
            if(d[v]>d[u]+c){
                d[v]=d[u]+c;
                que.push(P(-d[v],v));
            }
        }
    }int ans=d[lastid+n-1];
    if(ans==mod)ans=-1;
    else ans/=2;
    cout<<ans<<endl;return 0;
}


#include <bits/stdc++.h>

#define loop(s, e, i) for (int i = s; i < e; ++i)
#define print(s) cout << s << endl;
using namespace std;
using ll = long long;
using lld = long long;

/*
浮動小数点の入力
cout << fixed << setprecision(9) << endl;
*/

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

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

  ll mn;
  cin >> mn;
  loop(0, 4, i) {
    ll tmp;
    cin >> tmp;
    mn = min(mn, tmp);
  }

  ll bn;
  if (N % mn == 0) {
    bn = N/mn;
  } else {
    bn = N/mn + 1;
  }

  print(bn + 5 - 1);
}

#include <iostream>

using namespace std;
int main(){
	int n;
	int a[100001];
	bool b[100001];
	cin >> n;
	for(int i = 0; i < n; i++){
		cin >> a[i];
	}
	int x = a[n - 1];
	int i = -1;
	for(int j = 0; j < n - 1; j++){
		if(a[j] <= x){
			i++;
			int tmp = a[i];
			a[i] = a[j];
			a[j] = tmp;
		}
	}
	int tmp = a[i + 1];
	a[i + 1] = a[n - 1];
	a[n - 1] = tmp;
	for(int i = 0; i < n - 1; i++){
		if(a[i] == x){
			for(int j = 0; j < i; j++){
				b[j] = false;
			}
			b[i] = true;
		}else{
			b[i] = false;
		}
	}
	for(int i = 0; i < n - 1; i++){
		if(b[i]){
			cout << "[" << a[i] <<"] ";
		}else{
			cout << a[i] <<" ";
		}
	}
	cout << a[n - 1] << endl;
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
#define SZ 1234567
int n,h_[SZ+SZ],*h=h_,dt=0; char s[SZ],t[SZ];
int main()
{
	scanf("%d%s%s",&n,s+1,t+1);
	if(strcmp(s+1,t+1)==0)
	{
		puts("0");
		return 0;
	}
	int cr=n,cl=n+1,ans=0;
	for(int i=n;i>=1;--i)
	{
		if(t[i-1]==t[i]) continue;
		int x=min(cl-1,i);
		while(x&&s[x]!=t[i]) --x;
		if(!x)
		{
			puts("-1");
			return 0;
		}
		int ir=i;
		while(t[ir+1]==t[i]) ++ir;
		while(cl>x)
			h[--cl]=-dt;
		while(cr>=ir)
			ans=max(ans,h[cr--]+dt);
		++h; ++dt;
	}
	while(cl>1)
		h[--cl]=-dt;
	while(cr>=1)
		ans=max(ans,h[cr--]+dt);
	printf("%d\n",ans);
}

#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <stdlib.h>
#include <fstream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <deque>
#include <iterator>
#include <iomanip>
using namespace std;

int n;
vector < vector<int> > g;
vector<int> gi;
vector<int> cl;
vector<int> p;
vector<long long> cycle;

bool dfs (int v) {
	if(cl[v] == 2) return false;
  if(cl[v] == 1){
    while(cycle.back() != v){
      p.push_back(cycle.back());
      cycle.pop_back();
    }
    p.push_back(v);
    reverse(p.begin(), p.end());
    return true;
  }
  cl[v] = 1;
  cycle.push_back(v);
  long long u = -1;
  for(int i = 0; i < g[v].size(); ++i){
    long long to = g[v][i];
    if(cl[to] == 1){
      if(u == -1 || gi[to] > gi[u]) u = to;
    }
  }
  if(u != -1){
    dfs(u);
    return true;
  }

  for (int i = 0; i < g[v].size(); i++) {
      int y = g[v][i];
      if (!cl[y]) {
          gi[y] = gi[v] + 1;
          if (dfs(y)) return true;
      }
  }
  cycle.pop_back();
	cl[v] = 2;
	return false;
}

int main() {
	long long m, in, ot;
  cin >> n >> m;
  g.resize(n);
  gi.resize(n);
  for(int i = 0; i < m; ++i){
    cin >> in >> ot;
    in--, ot--;
    g[in].push_back(ot);
  }
	cl.resize(n);
	for (int i = 0; i < n; ++i)
		if (dfs (i))
			break;

	if (p.size() == 0){
		cout << -1;
    return 0;
  }

  vector<long long> q(n, -1);
  for(int i = 0; i < p.size(); ++i)
      q[p[i]] = i;

  long long v = p[0];
  vector<long long> res;
  while(true){
    res.push_back(v);
    long long vv = v;
    for(long long a: g[v])
        if(q[a] > q[vv]) vv = a;
    if(vv == v) break;
    v = vv;
  }
  cout << res.size() << endl;
  for(int i = 0; i < res.size(); ++i) cout << res[i] + 1 << endl;
}


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

int main(){
  double x1, x2, y1, y2, dis;

  cin >> x1 >> y1 >> x2 >> y2;
  dis = sqrt(pow(x2 - x1,2.0) + pow(y2 - y1,2.0));
  cout << fixed << dis <<endl;
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep2(i,n) for(int i=1;i<=(n);i++)
#define rep3(i,i0,n) for(int i=i0;i<(n);i++)
#define pb push_back
#define mod 1000000007
const ll INF = 1LL << 60;
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; }
ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) {return a/gcd(a,b)*b;}
#define all(x) x.begin(), x.end()
#define mp make_pair
bool compare(pair<int, int> a, pair<int, int> b) {
    if(a.first != b.first){
        return a.first < b.first;

    }else{
        return a.second < b.second;
    }
}

bool In_map(ll y,ll x,ll h,ll w){
    if(y<0 || x<0 || y>=h || x>=w){
        return 0;
    }else{
        return 1;
    }
}
const vector<ll> dx{1,0,-1,0};
const vector<ll> dy{0,1,0,-1};

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

    vector<ll> dp(N+1,INF);
    vector<ll> A(N);
    rep(i,N){
        cin >> A[i];
    }
    reverse(all(A));
    rep(i,N){
        ll inx;
        inx = upper_bound(all(dp),A[i]) -dp.begin();
        dp[inx] = A[i];
    }
    ll ans;
    rep(i,N+1){
        if(dp[i] == INF){
            ans = i;
            break;
        }
    }

    cout << ans << endl;
    return 0;

}


#include <bits/stdc++.h>
#define rep(i,n) for (int (i) = 0; (i) < (n); i++)
#define ll long long
using namespace std;
int main() {
  ll a,b; cin >> a >> b;
  if(a<9&&b<9){
    cout << "Yay!";
  }
  else{
    cout << ":(";
  }
}

#include <bits/stdc++.h>
#define ll long long
#define MODV 1000000007
#define INF INT_MAX // 2147483647
#define INFLL LLONG_MAX // 9223372036854775807
#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 per(i, n) for(ll i=((ll)(n))-1; i>=0; i--)
#define perf(i, n) for(ll i=((ll)(n)); i>0; 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 r; cin >> r;
    if(r<1200) cout << "ABC" << endl;
    else if(r<2800) cout << "ARC" << endl;
    else cout << "AGC" << endl;
}