#include <bits/stdc++.h>
using namespace std;
typedef pair<double, double> P;
int main() {
    int n;
    cin >> n;
	vector<queue<int>> q(n+1);
	for (int i=0;i<n; i++){
		for (int j=0;j<n-1;j++){
			int x;
			cin>>x;
			q[i].push(x-1);
		}
		q[i].push(n);
	}
	q[n].push(n);
	int ans=0;
	int cnt=n*(n-1);
	int f=1;
	vector<int>update;
	for (int i=0;i<n;i++){
		update.push_back(i);
	}
	while (cnt > 0){
		ans++;
		vector<int>update2;
		for (int i : update){
			if (q[q[i].front()].front()==i && q[i].front()>i){
				update2.push_back(i);
				update2.push_back(q[i].front());
			}
		}
		for (int i:update2){
			q[i].pop();
			cnt--;
		}
		if (update2.empty()){
			f=0;
			break;
		}
	}
	if(f==0) cout<<-1<<endl;
	else cout<<ans<<endl;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repl(i, l, r) for (ll i = (l); i < (r); i++)
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define CST(x) cout << fixed << setprecision(x)
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pl = pair<ll, ll>;
const ll MOD = 998244353;
const int inf = 1e9 + 10;
const ll INF = 4e18;
const int dx[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0}, dy[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
int main() {
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(false);

	int n;
	cin >> n;
	vector<ll> a(n + 5);
	rep(i, n) cin >> a[i];
	if (n == 2) {
		cout << max(a[0], a[1]) << endl;
		return 0;
	}
	vvl dp(n + 7, vl(3, -INF));
	rep(i, 3) dp[i][i] = 0;
	rep(i, n) {
		rep(j, 3) {
			if (dp[i][j] == -INF) continue;
			dp[i + 2][j] = max(dp[i + 2][j], dp[i][j] + a[i]);
			if (j < 2) dp[i + 3][j + 1] = max(dp[i + 3][j + 1], dp[i][j] + a[i]);
			if (j < 1) dp[i + 4][j + 2] = max(dp[i + 4][j + 2], dp[i][j] + a[i]);
		}
	}
	ll ans = -INF;
	if (n % 2)
		rep(i, 3) ans = max(ans, dp[n - 1 + i][i]);
	else
		rep(i, 2) ans = max(ans, dp[n + i][i]);
	cout << ans << endl;
	return 0;
}


#include<bits/stdc++.h>

using namespace std;
using ll = long long int;

int main(void) {
    constexpr ll MOD = 1e9 + 7;
    cout << fixed << setprecision(16);

    ll n, m;
    cin >> n >> m;
    vector<vector<ll>> G(n);
    while(m--) {
        ll a, b;
        cin >> a >> b;
        a--, b--;
        G[a].push_back(b);
    }

    vector<ll> idx(n, -1);
    set<ll> cyc;
    {
        ll x = 0;
        auto dfs = [&](auto dfs, ll cur, ll pre) -> ll {
            if(idx[cur] > -1) {
                cyc.insert(cur);
                return cur;
            }
            idx[cur] = x++;

            for(auto &v: G[cur]) {
                ll r = dfs(dfs, v, cur);
                if(r == cur) return -2;
                if(r == -2) return -2;
                if(r > -1) {
                    cyc.insert(cur);
                    return r;
                }
            }
            return idx[cur] = -1;
        };
        for(ll i=0; i<n; i++) {
            fill(idx.begin(), idx.end(), -1);
            if(dfs(dfs, i, -1) == -2) break;
        }
        if(cyc.size() == 0) {
            cout << -1 << endl;
            return 0;
        }
    }

    ll t = *cyc.begin();
    for(ll i=0; i<n; i++) {
        ll m = -1, mm = -1, s = t;
        for(auto &v: G[s]) {
            if(!cyc.count(v)) continue;
            if(idx[v] < idx[s] && mm < idx[v])
                mm = idx[v], t = v;
            if(mm == -1 && m < idx[v])
                m = idx[v], t = v;
        }
    }

    set<ll> res;
    for(ll i=0; i<n; i++) {
        ll m = -1, mm = -1, s = t;
        for(auto &v: G[s]) {
            if(!cyc.count(v)) continue;
            if(idx[v] < idx[s] && mm < idx[v])
                mm = idx[v], t = v;
            if(mm == -1 && m < idx[v])
                m = idx[v], t = v;
        }
        res.insert(t);
    }

    cout << res.size() << endl;
    for(auto &e: res) cout << e+1 << endl;

}

#include<bits/stdc++.h>
using namespace std;
int ac[100005];
char ch[100005];
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n,q;
	string s;
	cin>>n>>q>>s;
	for(int i=0; s[i]; i++) ch[i+1] = s[i];
	for(int i=1; i<=n; i++){
		if(ch[i]=='A' && ch[i+1]=='C') ac[i]++;
	}
	for(int i=1; i<=n; i++) ac[i]+=ac[i-1];
	while(q--){
		int a,b;
		cin>>a>>b;
		int ret = ac[b]-ac[a-1];
		if(ch[b]=='A' && ch[b+1]=='C') ret--;
		cout<<ret<<'\n';
	}
}


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

typedef long long ll;
typedef pair<int, int> ii;

const int N = 2e5 + 5;
int n, a[N], b[N];
int s[3 * N], p[3 * N];

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) scanf("%d", &a[i]);
  for (int i = 0; i < n; ++i) scanf("%d", &b[i]);

  for (int i = 0; i < n; ++i) s[i] = b[i] ^ b[(i + 1) % n];
  for (int i = 0; i < n; ++i) s[i + n + 1] = a[i] ^ a[(i + 1) % n];
  for (int i = 2 * n + 1; i <= 3 * n; ++i) s[i] = s[i - n];
  s[n] = -1;

  int now = p[0] = -1;
  for (int i = 1; i <= 3 * n; ++i) {
    while (now + 1 && s[now + 1] != s[i]) now = p[now];
    p[i] = s[now + 1] == s[i] ? ++now : (now = -1);
  }

  for (int i = 2 * n; i < 3 * n; ++i) {
    if (p[i] == n - 1) {
      int k = i - 2 * n;
      int x = a[k] ^ b[0];
      printf("%d %d\n", k, x);
    }
  }
}


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

int main() {
  string s;cin>>s;
  rep(bit,1<<3){
    int cnt=s[0]-'0';
    string t;
    t+=s[0];
    rep(i,3){
      if(bit&(1<<i)){
        cnt+=s[i+1]-'0';
        t+='+';
        t+=s[i+1];
      }
      else {
        cnt-=s[i+1]-'0';
        t+='-';
        t+=s[i+1];
      }
    }
    if(cnt==7){
      cout<<t<<"=7"<<endl;
      return 0;
    }
  }
}

#include <bits/stdc++.h>
using namespace std;
#include <math.h>
#include <iomanip>
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const int INF=1001001001;
vector<pair<int64_t,int64_t>>prime_factorize(int64_t x){
  vector<pair<int64_t,int64_t>>p;
  for(int64_t i=2;i*i<=x;i++){
    int cnt=0;
    if(x%i==0){
    while(x%i==0){cnt++;x/=i;}
    p.push_back(make_pair(i,cnt));
    }
  }
  if(x!=1){p.push_back(make_pair(x,1));}
  return p;
}

int main() {
  vector<char>s(4);
  for(int i=0;i<4;i++){
    cin>>s[i];
  }
  for(int p=0;p<(1<<3);p++){
    string t;int sum=s[0]-'0';
    t.push_back(s[0]);
    for(int i=0;i<3;i++){
      if((p>>i)&1){sum+=(s[i+1]-'0');t.push_back('+');}
      else{sum-=(s[i+1]-'0');t.push_back('-');}
      t.push_back(s[i+1]);
    }
    t+="=7";
    if(sum==7){cout<<t<<endl;return 0;}
  }
  return 0;
}


//what is going on
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
vl w;
int gcd(int a, int b) {
	if(a == 0) {return b;}
	if(b == 0) {return a;}
	return gcd(b,a%b);
}
int main() {
	int n;
	cin >> n;
	for(int i=0;i<n;i++) {
		ll t;
		cin >> t;
		w.push_back(t);
	}
	int lob = 0;
		for(int j=0;j<n;j++) {
			if(w[j]&(1)) {
				lob++;
			}
		}
	int poss = 0;
	if(lob != 1) {
		poss = (n-lob)&1;
	} else {
		if(n&1) {
			int ct = 0;
			int cob = 0;
			while(1) {
				int mo = 0;
				lob = 0;
				for(int i=0;i<n;i++) {
					if(w[i]&1) {lob++;
						mo = i;
					}
				}
				if(lob != 1) {
					cob = n-lob;
					break;
				}
				if(w[mo] == 1) {
					cob = 0;
					break;
				}
				ct++;
				w[mo]--;
				int g = w[0];
				for(int i=1;i<n;i++) {
					g = gcd(w[i],g);
				}
				for(int i=0;i<n;i++) {
					w[i] /= g;
				}
			}
			poss = (cob+ct)&1;
		} else {
			poss = (n-lob)&1;
		}
	}
	if(poss) {
		cout << "First\n";
	} else {
		cout << "Second\n";
	}
}

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#define ll long long
#define maxn 200010
inline ll read()
{
	ll x=0; char c=getchar(),f=1;
	for(;c<'0'||'9'<c;c=getchar())if(c=='-')f=-1;
	for(;'0'<=c&&c<='9';c=getchar())x=x*10+c-'0';
	return x*f;
}
inline void write(ll x)
{
	static int buf[20],len; len=0;
	if(x<0)x=-x,putchar('-');
	for(;x;x/=10)buf[len++]=x%10;
	if(!len)putchar('0');
	else while(len)putchar(buf[--len]+'0');
}
inline void writeln(ll x){write(x); putchar('\n');}
inline void writesp(ll x){write(x); putchar(' ');}
int p[10010],mn[10010];
ll a[510][510];
int n,tot;
void euler(int n)
{
	tot=0;
	for(int i=2;i<=n;i++){
		if(!mn[i])p[++tot]=i,mn[i]=tot;
		for(int j=1;j<=mn[i]&&i*p[j]<=n;j++)
			mn[i*p[j]]=j;
	}
}
int gcd(ll a,ll b){return b?gcd(b,a%b):a;}
int main()
{
	n=read();
	if(n==2){
		printf("4 7\n23 10\n");
		return 0;
	}
	euler(10000);
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			if((i+j)&1){
				int x=(i-j+n+1)/2,y=(i+j+1)/2;
				a[i][j]=p[x]*p[n+y];
			}
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			if(!((i+j)&1)){
				ll cur=1;
				if(i>1)cur=cur/gcd(cur,a[i-1][j])*a[i-1][j];
				if(j>1)cur=cur/gcd(cur,a[i][j-1])*a[i][j-1];
				if(i<n)cur=cur/gcd(cur,a[i+1][j])*a[i+1][j];
				if(j<n)cur=cur/gcd(cur,a[i][j+1])*a[i][j+1];
				a[i][j]=cur+1;
			}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++)
			writesp(a[i][j]);
		putchar('\n');
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define length size()
#define endl "\n"
signed main(void){
 float a,b,c;
  cin >> a >> b >> c;
  cout << (int)(c*(ceil(a/b)))+0 << endl;
}

#include <stdio.h>
#include <stdlib.h>

#define MAX_INPUT_SIZE  1024

int
main(int argc, char* argv[])
{
    char input[MAX_INPUT_SIZE];

    int n = atoi(fgets(input, sizeof(input), stdin));
    int ret = n * n * n;

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

    return 0;
}


#include <iostream>

template<typename X>
X input() {
	X value;
	std::cin >> value;
	return value;
}

int main() {
	int x = input<int>();
	int x3 = x * x * x;
	std::cout << x3 << std::endl;

	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) REP(i, 0, n)
#define rrep(i, n) for (int i = (int)(n-1); i >= 0; i--)
#define sz(x) int(x.size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define INF 2e9
#define LINF 1e18
#define mod 1000000007
template<class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; }
typedef long long ll;
typedef pair<int, int> P;
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};

int main() {
    int n; cin >> n;
    set<int> st;
    rep(i,n) {
        int a; cin >> a;
        st.insert(a);
    }
    if (sz(st) == n) cout << "YES" << endl;
    else cout << "NO" << endl;
}


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

int main(){
    int n;
    while(scanf("%d",&n) && n != 0){
        int arr[n];
        for(int i = 0; i < n; i++){
            scanf("%d",&arr[i]);
        }
        int max_e = arr[0], max_s = arr[0];
        for(int cur = 1; cur < n; cur++){
            max_e = max(arr[cur], max_e + arr[cur]);
            max_s = max(max_s, max_e);
        }
        printf("%d\n",max_s);
    }
}

#include<iostream>
#include<cstring>
using namespace std;
int n, a[10], x;
int main() {
	while (true) {
		cin >> n;
		if (n == 0) { break; }
		memset(a, 0, sizeof(a));
		for (int i = 0; i < n; i++) {
			cin >> x;
			a[x]++;
		}
		for (int i = 0; i < 10; i++) {
			if (a[i] == 0) { cout << '-' << endl; }
			else {
				for (int j = 0; j < a[i]; j++) { cout << '*'; }
				cout << endl;
			}
		}
	}
}


#include<iostream>
#include<string.h>
#include<string>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<bitset>
#include<math.h>
#include<stack>
#include<queue>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
typedef long double db;
typedef vector<int> vi;
typedef pair<int,int> pii;
const int N=200000+100;
const db pi=acos(-1.0);
#define lowbit(x) (x)&(-x)
#define sqr(x) (x)*(x)
#define rep(i,a,b) for (register int i=a;i<=b;i++)
#define per(i,a,b) for (register int i=a;i>=b;i--)
#define go(u,i) for (register int i=head[u],v=sq[i].to;i;i=sq[i].nxt,v=sq[i].to)
#define fir first
#define sec second
#define mkp make_pair
#define pb push_back
#define maxd 998244353
#define eps 1e-8
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;
}

namespace My_Math{
	#define N 100000

	int fac[N+100],invfac[N+100];

	int add(int x,int y) {return x+y>=maxd?x+y-maxd:x+y;}
	int dec(int x,int y) {return x<y?x-y+maxd:x-y;}
	int mul(int x,int y) {return 1ll*x*y%maxd;}
	ll qpow(ll x,int y)
	{
		ll ans=1;
		while (y)
		{
			if (y&1) ans=mul(ans,x);
			x=mul(x,x);y>>=1;
		}
		return ans;
	}
	int getinv(int x) {return qpow(x,maxd-2);}

	int C(int n,int m)
	{
		if ((n<m) || (n<0) || (m<0)) return 0;
		return mul(mul(fac[n],invfac[m]),invfac[n-m]);
	}

	void math_init()
	{
		fac[0]=invfac[0]=1;
		rep(i,1,N) fac[i]=mul(fac[i-1],i);
		invfac[N]=getinv(fac[N]);
		per(i,N-1,1) invfac[i]=mul(invfac[i+1],i+1);
	}
	#undef N
}
using namespace My_Math;

int n,a[N];
ll sum[N];

ll calc(int l,int r) {return sum[r]-sum[l-1];}

int main()
{
	n=read();
	rep(i,1,n) a[i]=read();
	rep(i,1,n) sum[i]=sum[i-1]+a[i];
	int l=1,r=3;
	ll ans=1e18;
	rep(i,2,n-2)
	{
		while ((l+2<=i) && (abs(calc(1,l)-calc(l+1,i))>abs(calc(1,l+1)-calc(l+2,i)))) l++;
		while ((r+2<=n) && (abs(calc(i+1,r)-calc(r+1,n))>abs(calc(i+1,r+1)-calc(r+2,n)))) r++;
		ll mx=0,mn=1e18,x;
		x=calc(1,l);mx=max(mx,x);mn=min(mn,x);
		x=calc(l+1,i);mx=max(mx,x);mn=min(mn,x);
		x=calc(i+1,r);mx=max(mx,x);mn=min(mn,x);
		x=calc(r+1,n);mx=max(mx,x);mn=min(mn,x);
		ans=min(ans,mx-mn);
	}
	printf("%lld\n",ans);
	return 0;
}

#include <bits/stdc++.h>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define rrep(i, a) for (int i = (int)a - 1; i >= 0; --i)
#define REP(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define RREP(i, a, b) for (int i = (int)a - 1; i >= b; --i)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define popcount __builtin_popcount
using ll = long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = 1LL << 60;

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

ll gcd(ll n, ll m)
{
	ll tmp;
	while (m != 0)
	{
		tmp = n % m;
		n = m;
		m = tmp;
	}
	return n;
}

ll lcm(ll n, ll m)
{
	return abs(n) / gcd(n, m) * abs(m); //gl=xy
}

using namespace std;

void solve()
{
	int n;
	cin>>n;
	vector<ll>a(n);
	rep(i,n)cin>>a[i];
	vector<ll>sum(n+1);
	rep(i,n){
		sum[i+1]=sum[i]+a[i];
	}
	ll ans=INF;
	for(int i=1;i<n-2;++i){
		vector<ll>left(4),right(4);
		int ok=1,ng=i+1;
		while(abs(ok-ng)>1){
			int mid=(ok+ng)/2;
			if(sum[i+1]-sum[mid]>sum[mid])ok=mid;
			else ng=mid;
		}
		left[0]=sum[ok],right[0]=sum[i+1]-sum[ok];
		// ok=1,ng=i+1;
		// while(abs(ok-ng)>1){
		// 	int mid=(ok+ng)/2;
		// 	if(sum[i+1]-sum[mid]<sum[mid])ok=mid;
		// 	else ng=mid;
		// }
		left[1]=sum[ng],right[1]=sum[i+1]-sum[ng];
		ok=i+2,ng=n;
		while(abs(ok-ng)>1){
			int mid=(ok+ng)/2;
			if(sum[n]-sum[mid]>sum[mid]-sum[i+1])ok=mid;
			else ng=mid;
		}
		left[2]=sum[ok]-sum[i+1],right[2]=sum[n]-sum[ok];
		// ok=i+2,ng=n;
		// while(abs(ok-ng)>1){
		// 	int mid=(ok+ng)/2;
		// 	if(sum[n]-sum[mid]<sum[mid]-sum[i+1])ok=mid;
		// 	else ng=mid;
		// }
		left[3]=sum[ng]-sum[i+1],right[3]=sum[n]-sum[ng];
		rep(j,2){
			REP(k,2,4){
				//cout<<left[j]<<" "<<right[j]<<" "<<left[k]<<" "<<right[k]<<"\n";
				chmin(ans,max({left[j],right[j],left[k],right[k]})-min({left[j],right[j],left[k],right[k]}));
			}
		}
	}
	cout<<ans<<"\n";
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(15);
	solve();
	return 0;
}


#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define fi first
#define se second
#define L(i,u) for (int i=head[u]; i!=0; i=nxt[i])
#define rep(i,a,b) for (register int i=a; i<=b; i++)
#define per(i,a,b) for (register int i=a; i>=b; i--)
typedef long long ll;
using namespace std;
typedef pair<int,int> Pii;
typedef vector<int> Vi;
inline void read(int &x) {
	x=0; char c=getchar(); int f=1;
	while (!isdigit(c)) {if (c=='-') f=-1; c=getchar();}
	while (isdigit(c)) {x=x*10+c-'0'; c=getchar();} x*=f;
}
const int N = 303;
char s[N];
int n,k,f[N][N][N];
inline void umax(int &a, int b) {a=a>b?a:b;}
int main() {
	scanf("%s",s+1); read(k); n=strlen(s+1);
	rep(i,1,n) f[i][i][0]=1; int ans=0;
	rep(l,-1,n-1) rep(i,1,n) {
		int j=i+l; if (j>n) break;
		rep(K,0,k) {
			int v=f[i][j][K]; umax(ans,v);
			umax(f[i][j+1][K],v); umax(f[i-1][j][K],v);
			umax(f[i-1][j+1][K+(s[i-1]!=s[j+1])],v+2);
		}
	}
	printf("%d",ans);
	return 0;
}

#include <bits/stdc++.h>

#define ll long long
#define pii pair <int, int>
#define x first
#define y second

using namespace std;

string s;
int dp[301][301][301];

inline int solve(int l, int r, int k) {
    if (k < 0)
        return INT_MIN;
    if (r - l < 1)
        return r - l + 1;
    if (dp[l][r][k] != -1)
        return dp[l][r][k];
    return dp[l][r][k] = max(solve(l + 1, r - 1, k - (s[l] != s[r])) + 2, max(solve(l + 1, r, k), solve(l, r - 1, k)));
}

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

	int k;
    cin >> s >> k;
    memset(dp, -1, sizeof(dp));
    return cout << solve(0, s.length() - 1, k), 0;
}


#include <bits/stdc++.h>
typedef long long ll;
#define pb push_back
using namespace std;
typedef pair<ll,ll> pll;

vector< pll > v;
ll n;
ll h[5005], p[5005];
ll dp[5005][5005];
const ll inf = 1e16;
int main()
{
        cin >> n;
        v.pb(make_pair(-2LL , -2LL));
        for(ll i=1;i<=n;i++) {
                cin >> h[i] >> p[i];
                v.pb(make_pair(h[i]+p[i], i));
        }
        sort(v.begin(), v.end());
        for(ll i=1;i<=n;i++) dp[0][i] = inf;
        dp[0][0] = 0;
        for(ll i=1;i<=n;i++) {
                ll id = v[i].second;
                for(ll j = 0; j <= n; j++) {
                        dp[i][j] = dp[i-1][j];
                        if(j && dp[i-1][j-1] <= h[id]) dp[i][j] = min(dp[i][j], p[id] + dp[i-1][j-1]);
                }
        }
        for(ll i=n;i>=0;i--){
                if(dp[n][i] != inf) {
                        cout << i << endl; return 0;
                }
        }
        return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair< ll, ll > Pi;
#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(Pi a, Pi b) {
    if(a.first != b.first){
        return a.first < b.first;

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

bool compareg(Pi a, Pi b) {
    if(a.second != b.second){
        return a.second > b.second;

    }else{
        return a.first > b.first;
    }
}

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<Pi>ab(N);
    vector<Pi>cd(N);
    rep(i,N){
        ll a,b;
        cin>>a>>b;
        ab[i] = mp(a,b);
    }

    rep(i,N){
        ll a,b;
        cin>>a>>b;
        cd[i] = mp(a,b);
    }

    sort(all(ab),compareg);
    sort(all(cd),compare);

    ll ans = 0;
    rep(i,N){
        rep(j,N){
            if(ab[i].first < cd[j].first && ab[i].second <cd[j].second){
                ab[i].first = INF;
                cd[j].first = -INF;
                //cout<<i<<" "<<j<<endl;
                ans++;
                break;
            }
        }
    }

    cout << ans<<endl;
    return 0;

}


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

// Bit Matrix
const int MAX_ROW = 110; // to be set appropriately
const int MAX_COL = 110000; // to be set appropriately
struct BitMatrix {
	int H, W;
	bitset<MAX_COL> val[MAX_ROW];
	BitMatrix(int m = 1, int n = 1) : H(m), W(n) {}
	inline bitset<MAX_COL>& operator [] (int i) { return val[i]; }
};

// 掃き出し法
void GaussJordan(BitMatrix &A, bool is_extended = false) {
	vector<bool> used(A.H, 0);
	for (int col = 0; col < A.W; ++col) {
		if (is_extended && col == A.W - 1) break;
		int pivot = -1;
		for (int row = 0; row < A.H; ++row) {
			if (used[row]) continue;
			if (A[row][col]) {
				pivot = row;
				break;
			}
		}
		if (pivot == -1) continue;
		for (int row = 0; row < A.H; ++row) {
			if (row != pivot && A[row][col]) A[row] ^= A[pivot];
		}
		used[pivot] = true;
	}
}

const int MD = 60;
int main() {
	int N; cin >> N;
	vector<long long> A(N);
	for (int i = 0; i < N; ++i) cin >> A[i];

	// 方程式を立てる
	long long res = 0;
	BitMatrix B(MD + 1, N + 1);
	vector<bool> cannot(MD + 1, false);
	for (long long d = MD; d >= 0; --d) {
		// d 桁目が 1 が何個か
		int num = 0;
		for (int i = 0; i < N; ++i) {
			if (A[i] & (1LL << d)) ++num;
		}
		if (num == 0) {
			cannot[d] = true;
			continue;
		}
		else if (1 == (num % 2)) {
			cannot[d] = true;
			res += (1LL << d);
			continue;
		}

		for (int i = 0; i < N; ++i) {
			if (A[i] & (1LL << d)) B[MD - d][i] = 1;
		}
		B[MD - d][N] = 1;
	}
	GaussJordan(B, true);

	// 集計
	for (int d = MD; d >= 0; --d) {
		if (cannot[d]) continue;

		// d 行目が (0 0 0 ... 0 | 1) だったらダメ
		bool ok = false;
		for (int i = 0; i < N; ++i) if (B[MD - d][i]) ok = true;
		if (!B[MD - d][N]) ok = true;

		// 結果に 2 × 2^d が寄与
		if (ok) res += (2LL << d);
	}
	cout << res << endl;
}

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

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

  vector<long long> v(n);

  for (int i = 0; i < n; i++)
    cin >> v.at(i);

  long long mod = 1000000007;
  long long sum = 0;

  for (int i = 0; i < n; i++) {
    sum += v.at(i);
    sum %= mod;
  }

  long long ans;

  ans = sum * sum % mod;

  for (int i = 0; i < n; i++) {
    ans -= (long long)v.at(i) * v.at(i) % mod;
  }

  while (ans < 0) ans += mod;

  ans *= (mod + 1) / 2;

  cout << ans % mod << endl;
}


#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < int(n); i++)
using namespace std;
using ll = long long;
const int INF = (1<<30)-1;
const long long LINF = (1LL<<62)-1;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

vector<vector<string>> s = {
  {
    "bcaa",
    "bcdd",
    "eegh",
    "ffgh"
  }, {
    "..abc",
    "..abc",
    "ddgee",
    "ffg.j",
    "hhiij"
  }, {
    "...abc",
    "...abc",
    "def...",
    "def...",
    "gghhii",
    "jjkkll"
  }, {
    "i..ll.a",
    "i..d..a",
    "..gd.nn",
    "..g..mj",
    "hh...mj",
    "effbb..",
    "ekk.cc."
  }
};

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  if (n < 3) {
    cout << -1 << endl;
    return 0;
  }
  if (n == 3) {
    cout << "..a\n..a\nbb.\n";
    return 0;
  }
  vector<vector<char>> grid(n, vector<char>(n, '.'));
  for (int i = 0; i+4 < n; i+=4) {
    rep(j, 4) rep(k, 4)
      grid[i+j][i+k] = s[0][j][k];
  }
  int rem = n%4;
  rep(i, rem+4) rep(j, rem+4)
    grid[n-rem-4+i][n-rem-4+j] = s[rem][i][j];
  rep(i, n) {
    rep(j, n)
      cout << grid[i][j];
    cout << '\n';
  }
  return 0;
}

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

vector<vector<char>> grid;
int n;

bool check3() {
  rep(i, n) {
    int num = 0;
    char prev = '.';
    rep(j, n) {
      if (grid[i][j] != prev) {
        if (grid[i][j] != '.')
          num++;
        prev = grid[i][j];
      }
    }
    if (num != 3)
      return false;
  }
  rep(j, n) {
    int num = 0;
    char prev = '.';
    rep(i, n) {
      if (grid[i][j] != prev) {
        if (grid[i][j] != '.')
          num++;
        prev = grid[i][j];
      }
    }
    if (num != 3)
      return false;
  }
  return true;
}

void dfs(int now, int c) {
  if (now == n*n) {
    if (!check3())
      return;
    rep(i, n) {
      rep(j, n)
        cout << grid[i][j];
      cout << '\n';
    }
    exit(0);
  }
  int x = now/n, y = now%n;
  dfs(now+1, c);
  if (x > 0 && grid[x-1][y] == '.') {
    grid[x-1][y] = grid[x][y] = 'a'+c;
    dfs(now+1, (c+1)%26);
    grid[x-1][y] = grid[x][y] = '.';
  }
  if (y > 0 && grid[x][y-1] == '.') {
    grid[x][y-1] = grid[x][y] = 'a'+c;
    dfs(now+1, (c+1)%26);
    grid[x][y-1] = grid[x][y] = '.';
  }
}

int main() {
  cin >> n;
  grid.resize(n, vector<char>(n, '.'));
  if (n < 7)
    dfs(0, 0);
  else {
    random_device rnd;
    mt19937 mt(rnd());
    while (true) {
      rep(i, n) rep(j, n) grid[i][j] = '.';
      int cnt = 0, c = 0;
      int lim = 3*n*2/3; //1つのドミノは行と列合わせて3箇所を+1する->クオリティの総和/3=ドミノの数
      while (cnt < lim) {
        int x = mt()%n, y = mt()%n, d = mt()%2;
        if (grid[x][y] != '.')
          continue;
        if (d && x+1 < n && grid[x+1][y] == '.') {
          grid[x][y] = grid[x+1][y] = 'a'+c;
          c = (c+1)%26;
          cnt++;
        } else if (y+1 < n && grid[x][y+1] == '.') {
          grid[x][y] = grid[x][y+1] = 'a'+c;
          c = (c+1)%26;
          cnt++;
        }
      }
      if (check3()) {
        rep(i, n) {
          rep(j, n)
            cout << grid[i][j];
          cout << '\n';
        }
        break;
      }
    }
  }
  return 0;
}
*/

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define maxn 100005
const int mod=1e9+7;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
#define mp make_pair
#define pb push_back
#define ff first
#define ss second

int n,m;
char s[maxn<<1];

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

int main(){
	n=read(); scanf("%s",s+1);
	int cnt=0; int res=1;
	for (int i=1;i<=(n<<1);i++){
		if (s[i]=='B'){if (cnt&1) res=1ll*res*cnt%mod,--cnt; else ++cnt;}
		else if (s[i]=='W'){if (cnt&1) ++cnt; else res=1ll*res*cnt%mod,--cnt;}
		if (cnt<0){puts("0"); return 0;}
	}
	if (cnt){puts("0"); return 0;}
	for (int i=1;i<=n;i++) res=1ll*res*i%mod;
	printf("%d\n",res);
    return 0;
}


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

#define ll long long

int main() {
  int n;
  ll a, min, max, sum;
  min = 100000000;
  max = -min;
  sum = 0;
  cin >> n;
  for (int i = 0; i < n; i ++) {
    cin >> a;
    if (a < min) min = a;
    if (a > max) max = a;
    sum += a;
  }
  cout << min << " " << max << " " << sum << endl;

  return 0;
}

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

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

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

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

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

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


#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
/********************************************************************/
#define M1 1000000007
#define M2 998244353
const double pi = 3.14159265;

#define ll long long
#define pll pair<ll,ll>

#define forr(i, n) for(ll i=0;i<n;i++)

#define pb push_back
#define ppb pop_back
#define mp make_pair
#define MT make_tuple
#define V(a) vector<a>
#define vi vector<ll>

#define CASE(t) ll t; cin>>t; while(t--)

#define endl '\n'
#define space ' '
#define enter cout<<endl
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl

#define all(x) x.begin(), x.end()
#define sortall(x) sort(all(x))
#define collect(v, n) for(ll i = 0; i < n;i++){cin>>v[i];}

#define deci(n) fixed << setprecision(n)		//sets no of digits to show after decimal
#define setcount(n) __builtin_popcount(n)       //the number of ones in the int
#define setcountll(n) __builtin_popcountll(n)   //the number of ones in the ll
#define parity(n) __builtin_parity(n)			//the parity (even or odd) of the number of ones
#define clz(n) __builtin_clz(n)					//the number of zeros at the beginning of the number
#define clzll(n) __builtin_clzll(n)				//the number of zeros at the beginning of the ll
#define ctz(n) __builtin_ctz(n)					//the number of zeros at the end of the number

void deb(ll x) { cout << x << endl;}
void deb(int x) { cout << x << endl;}
void deb(unsigned int x) { cout << x << endl;}
void deb(unsigned ll x) { cout << x << endl;}
void deb(float x) { cout << x << endl;}
void deb(double x) { cout << x << endl;}
void deb(long double x) { cout << x << endl;}
void deb(char x) {cout << '\'' << x << '\'' << endl;}
void deb(string s) { cout << '\"' << s << '\"' << endl;}
void deb(vector<ll> v) { for (auto x : v) cout << x << ' '; cout << endl;}
void deb(bool bb) {cout << (bb ? "True" : "False") << endl;}
void deb(map<ll, ll> mpp) {for (auto x : mpp) cout << x.first << ' ' << x.second << endl;}
void deb(pair<ll, ll> pp) { cout << "{" << pp.first << "," << pp.second << "}" << endl;}

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

template<typename T>
#define o_set(T) tree<T, null_type,less_equal<T>, rb_tree_tag,tree_order_statistics_node_update>
//member functions :
//1. order_of_key(k) : number of elements strictly lesser than k
//2. find_by_order(k) : k-th element in the set
// MULTISET ==> less_equal
// SET ==> less

ll powM(ll x, ll y, ll m) {
	ll ans = 1, r = 1;
	x %= m;
	while (r > 0 && r <= y) {
		if (r & y) {
			ans *= x;
			ans %= m;
		}
		r <<= 1;
		x *= x;
		x %= m;
	}
	return ans;
}

map<long long, long long> factorize(long long n) {
	map<long long, long long> ans;
	for (long long i = 2; i * i <= n; i++) {
		while (n % i == 0) {
			ans[i]++;
			n /= i;
		}
	}
	if (n > 1) {
		ans[n]++;
		n = 1;
	}
	return ans;
}

ll countBits(ll n) {
	ll count = 0;
	while (n)
	{
		count++;
		n >>= 1;
	}
	return count;
}

ll isP(ll n) {
	if (n == 0 || n == 1)
		return 0;
	for (ll i = 2; i <= sqrt(n); i++) {
		if (n % i == 0)
			return 0;
	}
	return 1;
}

/* **********END OF TEMPLATE*********** */

ll check(ll ar[], ll mid, ll k, ll n) {
	ll tmp = 0;
	for (ll i = 0; i < n; i++) {
		tmp += (ar[i] / mid + (ar[i] % mid != 0) - 1);

	}
	if (tmp <= k)
		return 1;

	return 0;
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	ll n;
	ll k;
	cin >> n >> k;
	double ar[n];
	for (ll i = 0; i < n; i++) {
		cin >> ar[i];
		ar[i] = (double)(ar[i] + 1) / (double)2;
	}
	double prefs[n + 1];
	prefs[0] = 0.0000;
	for (ll i = 0; i < n; i++) {
		prefs[i + 1] = prefs[i] + ar[i];
	}
	double mx = -1;
	for (ll i = k; i <= n; i++) {
		if (prefs[i] - prefs[i - k] > mx)
			mx = (prefs[i] - prefs[i - k]);
	}
	cout << deci(10) << mx << endl;

	return 0;
}

#include<bits/stdc++.h>
#define REP(i,n) for(int i=0;i<n;i++)
#define RREP(i,n) for(int i=n;i>=0;i--)
#define FOR(i,m,n) for(int i=m;i<n;i++)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
using namespace std;
typedef long long ll;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
//const ll INF = 1LL << 60;
//const int INF = 1001001001;

int main(void){
    int n,k;
    cin >> n >> k;
    int p[n];
    REP(i,n) cin >> p[i];
    int sumf=0,suml=0,maxs=0;
    REP(i,n){
        sumf+=p[i];
        if(i>=k){
            suml+=p[i-k];
            chmax(maxs,sumf-suml);
        }
    }
    if(n==k) maxs=sumf;
    double ans=(double)k/2 + (double)maxs/2;
    printf("%.7f\n",ans);

    return 0;
}


#include <bits/stdc++.h>
#define REP(i, n) for(long long i=0; i<n; i++)
#define REPR(i, n) for(long long i=n-1; i>=0; i--)
#define FOR(i, m, n) for(long long i=m; i<=n; i++)
#define FORR(i, m, n) for(long long i=m; i>=n; i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define VSORTR(v) sort(v.rbegin(), v.rend());
#define ALL(v) (v).begin(),(v).end()
#define FIN ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll, ll>;
const ll mod = 1e9+7;
const ll inf = 1e15;

int main(){FIN

    ll n,m;cin>>n>>m;
    vll a(n);
    REP(i,n) cin>>a[i];
    vll rui(n+1,0);
    ll sum=0;
    REP(i,n){
        sum += a[i];
        rui[i+1] = sum % m;
    }
    map<ll,ll> mp;
    REP(i,n+1) mp[rui[i]]++;
    ll ans=0;
    for(auto p:mp){
        // cout<<p.second<<endl;
        ans += p.second * (p.second-1) /2;
    }
    cout<<ans<<endl;
    return 0;
}


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

using namespace std;

typedef long long ll;

const int MOD = 1e9 + 7; // change if needed

struct Modint {
  ll val;

  Modint (ll _val = 0)
    : val(_val % MOD) {}

  Modint operator+ (Modint other) const {
    return Modint(val + other.val);
  }

  void operator+= (Modint other) {
    val += other.val;
    val %= MOD;
  }

  Modint operator- () const {
    return Modint(MOD - val);
  }

  Modint operator- (Modint other) const {
    return Modint(val + MOD - other.val);
  }

  void operator-= (Modint other) {
    val += MOD - other.val;
    val %= MOD;
  }

  Modint operator* (Modint other) const {
    return Modint(val * other.val);
  }

  void operator*= (Modint other) {
    val *= other.val;
    val %= MOD;
  }

  bool operator== (Modint other) const {
    return val == other.val;
  }

  bool operator!= (Modint other) const {
    return val != other.val;
  }
};

Modint exp (Modint a, int k) {
  if (k == 0) {
    return Modint(1);
  } else if (k % 2 == 0) {
    Modint half = exp(a, k / 2);
    return half * half;
  } else {
    return a * exp(a, k - 1);
  }
}

Modint inv (Modint a) {
  return exp(a, MOD - 2);
}

ostream& operator<< (ostream& out, Modint p) {
  out << p.val;
  return out;
}

const int MAX_N = 3e5 + 5;

struct State {
  int pc, sl, sv;

  State (int _pc, int _sl, int _sv) :
    pc(_pc), sl(_sl), sv(_sv) {}
};

ostream& operator<< (ostream &out, State s) {
  out << "(" << s.pc << " " << s.sl << " " << s.sv << ")";
  return out;
}

State nxt (State s, int c) {
  if (s.pc == 2) return s;

  if (s.sl == 0) {
    if (c == 0) return State(s.pc, s.sl + 1, 0);
    if (c == 1) return State(s.pc + 1, 0, 0);
  }

  if (s.sl == 1) {
    return State(s.pc, s.sl + 1, 2 * s.sv + c);
  }

  if (c == 0) {
    if (s.sv == 0) {
      return State(s.pc, 1, 0);
    } else {
      return State(s.pc, 1, 0);
    }
  } else {
    if (s.sv == 0) {
      return State(s.pc, 1, 0);
    } else {
      return State(s.pc + 1, 0, 0);
    }
  }
};

Modint _dp [MAX_N][3][3][4];

Modint& dp (int i, State s) {
  return _dp[i][s.pc][s.sl][s.sv];
}

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

  int n = s.size();

  vector<State> alls;
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      for (int k = 0; k < 4; k++) {
        alls.push_back(State(i, j, k));
      }
    }
  }

  dp(0, State(0, 0, 0)) = Modint(1);
  for (int i = 0; i < n; i++) {
    for (State st : alls) {
      if (s[i] != '1') {
        dp(i + 1, nxt(st, 0)) += dp(i, st);
      }
      if (s[i] != '0') {
        dp(i + 1, nxt(st, 1)) += dp(i, st);
      }
    }
  }

  Modint ans (0);
  ans += dp(n, State(2, 0, 0));
  ans += dp(n, State(1, 2, 1));
  ans += dp(n, State(1, 0, 0));
  cout << ans << endl;
}

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

#define mp make_pair
#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define all(v) v.begin(),v.end()

const int maxn = 3e5 + 100;
const ll inf = 2e18, mod = 1e9 + 7;

int h, w, a, b;
ll fact[maxn], invfact[maxn], dp[maxn];

ll comb(int n, int r)
{
	return (fact[n] * (invfact[r] * invfact[n - r] % mod) % mod);
}

ll powmod(ll a, ll b)
{
	if (!b)
		return 1;
	ll tmp = powmod(a, b / 2);
	tmp = tmp * tmp % mod;
	if (b & 1)
		tmp = tmp * a % mod;
	return tmp;
}

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

	fact[0] = invfact[0] = 1;
	for (int i = 1; i < maxn; i++)
		fact[i] = fact[i - 1] * i % mod, invfact[i] = powmod(fact[i], mod - 2);

	scanf("%d %d %d %d", &h, &w, &a, &b);
	for (int i = 0; i < h - a; i++)
		dp[i] = comb(i + b - 1, i);

	ll ans = 0;
	for (int i = 0; i < h - a; i++)
		ans = (ans + dp[i] * comb(w - b - 1 + h - i - 1, w - b - 1) % mod) % mod;
	printf("%lld\n", ans);
}


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

int main(){
    int n,q;
    int cnt=0;
    cin>>n;
    vector<int>s(n);
    for(int i=0;i<n;i++){
        cin>>s[i];
    }
    cin>>q;
    vector<int>t(q);
    for(int i=0;i<q;i++){
        cin>>t[i];
    }
    for(int i=0;i<q;i++){
        for(int j=0;j<n;j++){
            if(s[j]==t[i]){
                cnt++;
                break;
            }
        }
    }
    cout<<cnt<<endl;
    return 0;
}

#include <iostream>
#include <algorithm>

using namespace std;

bool Bsearch(int key, int A[], int len) {
	int l = 0, r = len - 1;
	while (l <= r) {
		int mid = l + (r - l) / 2;
		if (A[mid] > key)
			r = mid - 1;
		else if (A[mid] < key)
			l = mid + 1;
		else
			return true;
	}
	return false;
}

int main() {
	int n, *S;
	int q, *T;
	int ans = 0;
	cin >> n;
	S = new int[n];
	for (int i = 0; i < n; i++)
		cin >> S[i];
	cin >> q;
	T = new int[q];
	for (int i = 0; i < q; i++)
		cin >> T[i];

	sort(S, S + n);
	for (int i = 0; i < q; i++)
		for (int j = 0; j < n; j++)
			if (Bsearch(T[i], S, n) == true) {
				ans++;
				break;
			}

	cout << ans << endl;
	delete[] S;
	delete[] T;
	return 0;
}


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

#define rep(i,n) for(ll i = 0; i < n; i++)
#define Rep(i,n) for(ll i = 1; i < n; i++)
#define sz(x) int(x.size())
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define YesorNo(a) printf(a ? "Yes\n" : "No\n")
#define endl '\n'
#define fi first
#define se second
using ll = long long;
using P = pair<int,int>;
using Pl = pair<ll,ll>;
template<class T> using V = vector<T>;
const int dx[] = {0,1,0,-1,1,1,-1,-1};
const int dy[] = {1,0,-1,0,1,-1,-1,1};
const int inf = (1<<30)-1;
const ll infll = (1LL<<62)-1;
ll ceil(const ll &a, const ll &b){return ((a)+(b)-1)/b;}
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(){

 	ll n, a, b;
 	cin >> n >> a >> b;
 	V<ll> x(n);
 	rep(i,n) cin >> x[i];

 	V<ll> dist(n-1);
 	rep(i,n) {
 		dist[i] = x[i+1] - x[i];
 		dist[i] *= a;
 	}

 	sort(all(dist));
 	int idx = 0;
 	ll ans = 0;
 	while(dist[idx] < b && idx < n-1) {
 		ans += dist[idx];
 		idx++;
 	}

 	ans += 1LL * (n-1 - idx) * b;
 	cout << ans << endl;
}

#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define IO(i, o) freopen(i, "r", stdin), freopen(o, "w", stdout)

using namespace std;
using namespace __gnu_pbds;
typedef tree<long long, null_type, less<long long>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());

int n, a, b, x[100000];

int main(){
	//IO("input.txt", "output.txt");
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n >> a >> b >> x[0];
	long long cnt = 0;
	for(int i = 1; i < n; i++){
		cin >> x[i];
		cnt += min((long long)b, a * (long long)(x[i] - x[i - 1]));
	}
	cout << cnt << "\n";
	return 0;
}


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

#define ll long long
const int N = 2005;
const int mod = 1e9+7;
int fac[N],combi[N][N];

int add(int x,int y) {
   int ret = ((x+y)%mod + mod)%mod;
   return ret;
}
int mul(int x,int y) {
   ll ret = (ll)x*y;
   ret%=mod;
   return ret;
}

int num[2];
int group[2];
int dp[N][N];
int inv[N];

int invFac[N];

int solveDp(int x,int rem) {
   if(rem==0) {
      return 1;
   }
   if(x>num[1]) {
      return 0;
   }
   int &ret = dp[x][rem];
   if(ret != -1) {
      return ret;
   }
   ret = solveDp(x+1,rem);
   int cur = 1;
   for(int j = 0;j<group[0];++j) {
      cur = mul(cur, invFac[x]);
   }
   for(int f = group[0];f<=group[1] && f*x<=rem;++f) {
      int tot = f*x;
      int m = fac[tot];
      int t = cur;
      t = mul(t, invFac[f]);
      m = mul(t,m);
      m = mul(combi[rem][tot],m);
      ret = add(ret, mul(m,solveDp(x+1,rem-tot)));
      cur = mul(cur,invFac[x]);
   }
   return ret;
}

int myPow(int x,int c) {
   int ret = 1;
   while(c>0) {
      if(c&1) {
         ret = mul(ret,x);
      }
      x = mul(x,x);
      c/=2;
   }
   return ret;
}
void solve() {
   int n;
   memset(dp,-1,sizeof(dp));
   scanf("%d",&n);
   for(int i=0;i<2;++i) {
      scanf("%d",&num[i]);
   }
   for(int i=0;i<2;++i) {
      scanf("%d",&group[i]);
   }
   fac[0] = 1;
   inv[0] = 1;
   invFac[0] = 1;
   for(int i=1;i<N;++i) {
      fac[i] = mul(fac[i-1],i);
      inv[i] = myPow(i,mod-2);
      invFac[i] = myPow(fac[i],mod-2);
   }
   for(int i=0;i<N;++i) {
      combi[i][i] = combi[i][0] = 1;
      for(int j = 1;j<i;++j) {
         combi[i][j] = add(combi[i-1][j],combi[i-1][j-1]);
      }
   }
   printf("%d\n", solveDp(num[0],n));
}

int main()
{
   //freopen("input.txt","r",stdin);
   solve();
}

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
const int N = 1e3 + 5;
const int mod = 1e9 + 7;
using namespace std;

int n, a, b, c, d, fac[N], inv[N], g[N][N], f[N][N], ans;

template < typename T >
inline T read()
{
	T x = 0, w = 1; char c = getchar();
	while(c < '0' || c > '9') { if(c == '-') w = -1; c = getchar(); }
	while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
	return x * w;
}

int fpow(int x, int y)
{
	int res = 1;
	for( ; y; y >>= 1, x = 1ll * x * x % mod)
		if(y & 1) res = 1ll * res * x % mod;
	return res;
}

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

int main()
{
#ifndef ONLINE_JUDGE
	freopen("cpp.in", "r", stdin);
#endif
	n = read <int> (), a = read <int> (), b = read <int> (), c = read <int> (), d = read <int> ();
	for(int i = (fac[0] = 1); i <= n; i++) fac[i] = 1ll * fac[i - 1] * i % mod;
	inv[n] = fpow(fac[n], mod - 2);
	for(int i = n - 1; i >= 0; i--) inv[i] = 1ll * inv[i + 1] * (i + 1) % mod;
	g[0][0] = 1;
	for(int res = 1, i = a; i <= b; i++, res = 1)
		for(int j = 1; j <= n && i * j <= n; j++)
			g[i * j][j] = 1ll * g[i * (j - 1)][j - 1] * C(i * j, i) % mod * inv[j] % mod * fac[j - 1] % mod;
	f[a - 1][0] = 1;
	for(int i = a; i <= b; i++)
		for(int j = 0; j <= n; j++)
		{
			f[i][j] = f[i - 1][j];
			for(int k = c; k <= d; k++)
			{
				if(i * k > j) break;
				f[i][j] = (1ll * f[i - 1][j - i * k] * C(n - (j - i * k), i * k) % mod * g[i * k][k] + f[i][j]) % mod;
			}
		}
	printf("%d\n", f[b][n]);
	return 0;
}


#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define repo(i,n) for(int i = 1; i < (int)(n); i++)
#define pb push_back
#define np next_permutation
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define mod 1000000007
#define pi acos(-1.0)
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
//cout << fixed << setprecision (20);   小数点以下2０桁まで
//intの最大値2147483647 ≒ 2×10^9
//long longの最大値9223372036854775807 ≒ 9×10^18
//'0'+=16;        で大文字に
//'大文字'+=32;    で小文字に
//s[i]-'0'でchar文字→int数に;
//string s = to_string(int数);
//int n = stoi(string文字列)
//実行時間制約2秒では２×10^8回くらいまで計算できる

int main() {
  ll a,b,k;
  cin >> a>>b>>k;

  rep(i,k){
    if(i%2==0){
      if(a%2==1) a--;
    b+=a/2;
    a/=2;
    }
    if(i%2==1){
      if(b%2==1) b--;
    a+=b/2;
    b/=2;
    }
  }

  cout << a << " " << b << endl;

}

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

int main() {
  long double ans=0,n,m,d;
  cin >> n >> m >> d;

  if(d==0) {
    ans=(m-1)/n;
  } else {
    ans=(n-d)*2*(m-1)/n/n;

  }
  cout << fixed << setprecision(10);
  cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repi(i, s, n) for (int i = (s); i < (n); i++)
#ifdef LOCAL
#define INPUT_FILE                              \
  ifstream in("atcoder-problems/abc045_b.txt"); \
  cin.rdbuf(in.rdbuf());
#define print_vec(v)                        \
  rep(l, v.size()) { cout << v[l] << " "; } \
  cout << endl;
#else
#define INPUT_FILE
#define print_vec(v)
#endif
#define CIN_OPTIMIZE \
  cin.tie(0);        \
  ios::sync_with_stdio(false);
typedef pair<int, int> P;
typedef long long ll;
typedef pair<ll, ll> pl;
const int INF = 100100100;
const ll LINF = 1e18 + 100;
const int MOD = 1e9 + 7;

int main() {
  INPUT_FILE CIN_OPTIMIZE;

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

  int a = 0, b = -1, c = -1;
  char next = 'a';
  while (true) {
    if (next == 'a') {
      a++;
      if (a == A.size()) {
        cout << "A" << endl;
        return 0;
      }
      next = A[a];
    } else if (next == 'b') {
      b++;
      if (b == B.size()) {
        cout << "B" << endl;
        return 0;
      }
      next = B[b];
    } else {
      c++;
      if (c == C.size()) {
        cout << "C" << endl;
        return 0;
      }
      next = C[c];
    }
  }
}

#include <bits/stdc++.h>
using namespace std;
#define END '\n'
#define int long long
#define pb push_back
#define pii pair<int, int>
#define ff first
#define ss second
#define trace(x) cerr<<x<<END;
#define loop(i, a, b) for(int i = (a); i < (b); i++)
#define loopb(i, b, a) for(int i = (b); i > (a); --i)
const int mod = 1e9+7;
const int mod1 = 998244353;
const int inf = 1e18;
set<int> s;
int n,x;
void solve()
 {
  cin>>n;
  loop(i,0,n)
   {
	   cin>>x;
	   s.insert(x);
	}
 cout<<((int)s.size() == n ? "YES" : "NO");

 }
signed main()
 {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
   /*int t;
   cin>>t;
   loop(i,1,t+1)*/
      solve();

  //cerr<<END<<1.0*clock();
  return 0;
}


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

vector<char>stack;
// stack.back() will bring you to the last index

void checkForT(string s , char character, int index){
	if(!stack.empty() && stack.back()!= 'S'){
		stack.push_back(character);
	}
	else if(!stack.empty()){
		stack.pop_back();
	}
	else if(stack.empty()){
		stack.push_back(character);
	}
}

void checkForS(string s , char character, int index){
	stack.push_back(character);
}

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

	for(int i = 0; i < s.size(); i++){
		if(s[i] == 'S'){
			checkForS(s , s[i] , i);
		}
		else{ // else if stack[i]=='T'
			checkForT(s, s[i] , i);
		}
	}
	cout << stack.size() << endl; // Amount of elements left in stack
}

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

int main()
{
    string x;
    cin >> x;
    int stack = 0, puyo = 0;
    rep(i, x.length())
    {
        if (x[i] == 'S')
            stack++;
        else if (stack)
        {
            stack--;
            puyo++;
        }
    }
    cout << x.length() - 2 * puyo << endl;
}


#include<bits/stdc++.h>
#include<string.h>
#define ll long long
#define pf printf
#define sf scanf
#define ff first
#define ss second
#define clr clear()
#define sz size()
#define pb push_back
#define mk make_pair
#define pi acos(-1)
#define inf 20000000000000000
#define mod 998244353
#define ull unsigned long long int
#define f(i,k,n) for(i=k;i<n;i++)
#define fr(i,n,k) for(i=n;i>=k;i--)
#define ent(a) scanf("%lld",&a)
#define ent2(a,b) scanf("%lld%lld",&a,&b)
#define ent3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define mem(a) memset(a,0,sizeof(a))
#define vec(v,s) vector<ll>v[s]
#define arr(a,s) ll a[s];
/*#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")*/
//knight and king//
int dr[]= {2,2,-2,-2,1,-1,1,-1};
int dc[]= {1,-1,1,-1,2,2,-2,-2};
int dr1[]= {0,0,1,1,1,-1,-1,-1};
int dc1[]= {1,-1,1,0,-1,0,1,-1};
int dr2[]= {0,0,1,-1};
int dc2[]= {1,-1,0,0};
////////////////////////////
using namespace std;
#define ma 200
ll a[4][4];
ll mark[ma];

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ll i,j,x,y,n,m;
    while(cin>>n)
    {
        memset(mark,-1,sizeof(mark));
        cin>>m;
        vector<pair<ll,ll> >v;
        ll max1=n;
        for(i=0;i<m;i++)
        {
            cin>>x>>y;
            v.pb({x,y});
            mark[x]=y;
            max1=max(max1,x);
        }
        for(i=1;i<=max1;i++)
        {
            if(mark[i]==-1)
            {
                if(i==1&&max1>1)mark[i]=1;
                else mark[i]=0;
            }
        }
        bool f=0;

        for(i=0;i<v.sz;i++)
        {
            if(mark[v[i].ff]!=v[i].ss)f=1;
        }
        if(mark[1]==0&&max1>1)f=1;
        if(f)cout<<"-1"<<endl;
        else
        {
            for(i=1;i<=max1;i++)cout<<mark[i];
            cout<<endl;
        }

    }

    return 0;
}

#include <algorithm>
#include <bitset>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <cctype>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <limits>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using ld = long double;
#define endl "\n"
//#define int long long
#define ALL(x) begin(x),end(x)
#define RALL(a) (a).rbegin(), (a).rend()
#define _CRT_SECURE_NO_WARNINGS
#define rep(i, n) for (int i = 0; i < n; i++)
const int MOD = 1e9 + 7;
const ll INF = 1LL << 60;
const int inf = 1 << 25;
constexpr long double pi = 3.141592653589793238462643383279;
int dx[] = { 1,0,-1,0 };
int dy[] = { 0,1,0,-1 };
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
ll fact(ll i) {
    if (i == 0) return 1;
    return ((fact(i - 1)) * i) % MOD;
}
ll gcd(ll a, ll b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
    return a * b / gcd(a, b);
}
ll keta(ll n) {
    if (n == 0) return 1;
    ll count = 0;
    while (n != 0) {
        n /= 10;
        count++;
    }
    return count;
}
ll ketasum(ll n) {
    ll sum = 0;
    while (n != 0) {
        sum += n % 10;
        n /= 10;
    }
    return sum;
}
ll mch(ll n) {
    if (n == 1) return 1;
    else return n * mch(n - 1);
}
ll modpow(ll a, ll n, ll mod) {
    ll hi = 1;
    while (n > 0) {
        if (n & 1) hi = hi * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return hi;
}
bool is_prime(const unsigned n) {
    switch (n) {
    case 0: // fall-through
    case 1: return false;
    case 2: // fall-through
    case 3: return true;
    } // n > 3 が保証された
    if (n % 2 == 0 || n % 3 == 0) return false;
    for (unsigned i = 5; i * i <= n; i += 6) {
        if (n % i == 0) return false; // (6の倍数)-1
        if (n % (i + 2) == 0) return false; // (6の倍数)+1
    }
    return true;
}
/*signed*/int main(void) {
    int n, m;
    cin >> n >> m;
    vector<pair<int, int>> p(m);
    rep(i, m) {
        int s, c;
        cin >> s >> c;
        s--;
        p[i] = make_pair(s, c);
    }
    rep(i, 1000) {
        string s = to_string(i);
        if (s.size() == n) {
            bool ans = true;
            rep(j, m) {
                if ((s[p[j].first] - '0') != p[j].second)ans = false;
            }
            if (ans) {
                cout << i << endl;
                return 0;
            }
        }
    }
    cout << -1 << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
string s;
int w;
int main(void){
    cin>>s>>w;
    for(int i=0;i<s.size();i=i+w){
        cout<<s[i];
    }
    cout<<endl;

}

#include<bits/stdc++.h>
//g++ -std=c++11

using namespace std;
typedef long long ll;

#define rep(i,n) for (long long i = 0; i < (n); ++i)
#define DIV 1000000007 //10^9+7

int main(){
	ll K, X;
	cin >> K >> X;

	K--;
	for(ll i = X - K; i <= X + K; i++) {
		cout << i;
		if(i == X + K) {
			cout << endl;
		} else {
			cout << " ";
		}

	}

}


#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>

#define rep(i,a,b) for(ll i=a;i<b;i++)
#define rrep(i,b,a) for(int i=b;i>=a;i--)
#define fori(a) for(auto i : a )
#define all(a) begin(a), end(a)
#define set(a,b) memset(a,b,sizeof(a))
#define sz(a) a.size()
double pi=acos(-1);
#define ll long long
#define ull unsigned long long
#define pb push_back
#define PF push_front //deque
// #define mp make_pair
#define pq priority_queue
const ll mod=1000000007;
// #define f first
// #define s second
#define pii pair< ll, ll >
#define vi vector<int>
#define vpii vector<pii>
#define debug(v) for(auto i:v) cout<<i<<" ";
#define tc int t; cin >> t; while(t--)

using namespace boost::multiprecision;
using namespace std;
void optimizeIO(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

const int N=1000005;
ll fact[N],invfact[N];
ll power(ll x,ll y){
  if(y<=0) return 1LL;
  ll z=power(x,y/2);
  if(y%2) return (((z*z)%mod)*x)%mod;
  return (z*z)%mod;
}

void pre(){
  fact[0]=invfact[0]=invfact[1]=fact[1]=1;
  rep(i,2,N) fact[i]=(i*fact[i-1])%mod;
  rep(i,2,N) invfact[i]=(invfact[i-1]*power(i,mod-2))%mod;
}
ll nCr(ll n,ll k){ return (((fact[n]*invfact[n-k])%mod)*invfact[k])%mod; }

const int N1=1e6+1;
vector<int> isprime(N1,1),prime;
void seive(){
  rep(i,2,sqrt(N1)+1){
      if(isprime[i]){
        for(int j=i*i;j<N1;j+=i) isprime[j]=0;
        prime.pb(i);
      }
  }
  rep(i,sqrt(N1)+1,N1) if(isprime[i]) prime.pb(i);
}

struct dsu {
    vector<int> par, rank;
    dsu(int n): par(n+1), rank(n+1) {
        for (int i = 0; i <= n; i++) {
            par[i] = i;
            rank[i]= 1;
        }
    }
    int root(int a) {
        if (a == par[a]) return a;
        return par[a] = root(par[a]);
    }
    void merge(int a, int b) {
        a = root(a);
        b = root(b);
        if (a == b) return;
        if (rank[a] > rank[b]) swap(a, b);
        par[b] = a;
    }
    set<int> parent(int n){
        set<int> s;
        for(int i=1;i<=n;i++){
            s.insert(root(i));
        }
        return s;
    }
};
void solve(){
  string s;
  cin>>s;
  int a=s[0]-'0',b=s[1]-'0',c=s[2]-'0',d=s[3]-'0';
  vector<string> v1={"+--","---","--+","-+-","++-","-++","+-+","+++"};
  rep(i,0,8){
    int rem=7-a;
    if(v1[i][0]=='+') rem-=b;
    else rem+=b;
    if(v1[i][1]=='+') rem-=c;
    else rem+=c;
    if(v1[i][2]=='+') rem-=d;
    else rem+=d;
    if(rem==0){
      cout<<a<<v1[i][0]<<b<<v1[i][1]<<c<<v1[i][2]<<d<<"=7"<<endl;
      return;
    }
  }
}

int main(){
    optimizeIO();
    int r=1;
    {solve();}
}

#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <fstream>
#include <cassert>
#include <cstring>
#include <unordered_set>
#include <unordered_map>
#include <numeric>
#include <ctime>
#include <bitset>
#include <complex>
#include <random>

using namespace std;

#define int long long

vector<vector<int>> solivoli =
{
    {1, 2, -1, -1, 3, 3},
    {1, 2, -1, -1, 4, 4},
    {5, 5, 7, 8, -1, -1},
    {6, 6, 7, 8, -1, -1},
    {-1, -1, 9, 9, 11, 12},
    {-1, -1, 10, 10, 11, 12}
};

vector<vector<int>> soli = {
    {1, 1, 3, 4},
    {2, 2, 3, 4},
    {5, 6, 7, 7},
    {5, 6, 8, 8}
};

vector<vector<int>> voli = {
    {1, 1, -1},
    {-1, -1, 2},
    {-1, -1, 2}
};

vector<vector<int>> volians5 {
    {1, 2, 3, -1, -1},
    {1, 2, 3, -1, -1},
    {4, 5, 5, 6, 6},
    {4, -1, 8, 9, 9},
    {7, 7, 8, 10, 10}
};

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

    int n;
    cin >> n;
    vector<vector<int>> ans(n, vector<int> (n, -1));
    if (n == 7) {
        cout << "aabbcc." << endl;
        cout << "dd.ee.f" << endl;
        cout << "..a..cf" << endl;
        cout << "..a..cb" << endl;
        cout << "ll.jj.b" << endl;
        cout << "..a..ch" << endl;
        cout << "..a..ch" << endl;
        return 0;
    }
    if (n % 3 == 0) {
        for (int i = 0; i < n; i += 3) {
            for (int di = 0; di < 3; di++) {
                for (int dj = 0; dj < 3; dj++) {
                    ans[i + di][i + dj] = voli[di][dj];
                }
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (ans[i][j] == -1) {
                    cout << '.';
                } else {
                    cout << (char)(ans[i][j] + 'a');
                }
            }
            cout << endl;
        }
        return 0;
    }
    int wn = n;
    if (n % 2 == 1 && n >= 5) {
        for (int i = n - 5; i < n; i += 5) {
            for (int di = 0; di < 5; di++) {
                for (int dj = 0; dj < 5; dj++) {
                    ans[i + di][i + dj] = volians5[di][dj];
                }
            }
        }
        n -= 5;
    }
    if (n % 2 == 0 && n != 2) {
        int c1 = 0;
        int c2 = 0;
        int ln = n;
        while (ln % 4) {
            ln -= 6;
            c2++;
        }
        c1 = ln / 4;
        for (int i = 0; i < 4 * c1; i += 4) {
            for (int di = 0; di < 4; di++) {
                for (int dj = 0; dj < 4; dj++) {
                    ans[i + di][i + dj] = soli[di][dj];
                }
            }
        }
        for (int i = 4 * c1; i < n; i += 6) {
            for (int di = 0; di < 6; di++) {
                for (int dj = 0; dj < 6; dj++) {
                    ans[i + di][i + dj] = solivoli[di][dj];
                }
            }
        }
       for (int i = 0; i < wn; i++) {
            for (int j = 0; j < wn; j++) {
                if (ans[i][j] == -1) {
                    cout << '.';
                } else {
                    cout << (char)(ans[i][j] + 'a');
                }
            }
            cout << endl;
        }
        return 0;
    }
    cout << -1 << endl;
}


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

int main(void){
  char buf[128] = { 0 };
  short grid[64][64] = { {0} };
  int H,W;

  fgets(buf, sizeof(buf), stdin);
  sscanf(buf, "%d %d", &H, &W);

  for(int i = 0; i < H; i++){
    memset(buf, 0, sizeof(buf));
    fgets(buf, sizeof(buf), stdin);
    for(int j = 0; j < W; j++)
      if(buf[j] == '#') grid[i+1][j+1] = 1;
  }

  int flag = 0;
  for(int i = 1; i < H+1; i++){
    for(int j = 1; j < W+1; j++){
      if(grid[i][j] == 0) continue;
      else{
        if(grid[i-1][j] != 1 && grid[i][j-1] != 1
           && grid[i+1][j] != 1 && grid[i][j+1] != 1){
          flag = 1;
          break;
        }
      }
    }
    if(flag) break;
  }
  if(flag == 0) printf("Yes\n");
  else printf("No\n");
  return 0;
}

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

/*
 * union-find???????£??????\???????????????
 */
#define MAX_V 10001

typedef struct UF{
    int parent;
}UF;

UF uf[MAX_V + 1];

/*
 * ?????????idx???parent??¨???????????????????¨??????????
 */
void initialize(int num){
    int i ;

    for (i=1; i <= num; i++) {
        uf[i].parent = i;
    }
}
/*
 * ??????????±?????????°????????????????????????
 */
int find(int a){

    if (a == uf[a].parent) {
        return a;
    }

    return uf[a].parent = find(uf[a].parent);
}

int isSame(int a, int b){

    if (find(a) == find(b)) {
        return 1;
    }
    else{
        return 0;
    }
}

void unite(int a, int b){
    a = find(a);
    b = find(b);

    if (a == b) {
        return;
    }

    uf[b].parent = a;

}

/*
 * ??\??????Kruskal??????????????°??????(??????????????\????????¨??????????£?)
 * ?¢???????????????????X?????????????????¢???????????????X???????????????????????¨??§????°???¨?????¨????±???????
 */

#define MAX_V 10001
#define MAX_E 100001
#define INF INT_MAX

typedef struct edge{
    int from;
    int to;
    int cost;
}edge;

edge es[MAX_E];

// cost?????????????????¢??°
bool cmp(const edge& e1, const edge& e2){
    return e1.cost < e2.cost;
}

/*
 * e:???????????°
 * v:?????????????????°
 */
int Kruskal(int e, int v){

    int res = 0;

    //?????????????????????????????????
    sort(es, es + e, cmp);

    //union-find????????????
    initialize(v);

    for (int i = 0; i< e; i++) {
        edge e = es[i];
        if (!isSame(e.from, e.to)) {
            unite(e.from, e.to);
            res += e.cost;
        }
    }

    return res;
}

int main( ){

    int i, V, E, u, v, c, ans;
    cin >> V >> E;

    for (i = 0; i < E; i++) {
        cin >> u >> v >> c;
        es[i].from = u;
        es[i].to = v;
        es[i].cost = c;
    }

    ans = Kruskal(E, V);

    cout << ans << endl;

    return 0;
}


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

int e,minimum;

int solve(){
    minimum = e;
    for(int z=0;z*z*z<=e;z++){
        for(int y=0;y <= sqrt(e);y++){
            int x = e - z*z*z - y*y;
            if(x>=0)
                minimum = min(x+y+z,minimum);
        }
    }
    return minimum;
}

int main(){
    while(cin >> e && e!=0){
        cout << solve() << endl;
    }
}


#include <iostream>
using namespace std;

int main() {
    while (true) {
        int n;
        cin >> n;
        if (n == 0) {
            break;
        }
        for (int i = 0; i < n; i++) {
            int pm, pe, pj;
            cin >> pm >> pe >> pj;
            char output;
            if ((pm == 100 || pe == 100 || pj == 100)
                || (180 <= pm + pe)
                || (240 <= pm + pe + pj)) {
                output = 'A';
            } else if ((210 <= pm + pe + pj)
                       || (150 <= pm + pe + pj && (80 <= pm || 80 <= pe))) {
                output = 'B';
            } else {
                output = 'C';
            }
            cout << output << endl;
        }
    }
}

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

#define iota(i,n,b,s) for(int i=int(b);i!=int((b)+(s)*(n));i+=(s))
#define range(i,n,m) iota(i,(((n)>(m))?((n)-(m)):((m)-(n))),(n),((n)>(m)?-1:1))
#define rep(i,n) iota(i,(n),0,1)
#define loop for(;;)

#define INF (1e9)
#define EPS (1e-9)
#define cons(a,b) (make_pair(a,b))
#define car(a) (a.first)
#define cdr(a) (a.second)
#define cadr(a) (car(cdr(a)))
#define cddr(a) (cdr(cdr(a)))
#define all(a) a.begin(), a.end()
#define trace(var) cerr<<">>> "<<#var<<" = "<<var<<endl;

typedef long long Integer;
typedef double Real;
typedef vector<int> vi;
typedef vector<string> vs;
typedef map<string,int> Dictionary;
const Real PI = acos(-1);

template<class S, class T>
ostream& operator<<(ostream& os, pair<S,T> p) {
  os << '(' << car(p) << ", " << cdr(p) << ')';
  return os;
}

template<class T>
ostream& operator<<(ostream& os, vector<T> v) {
  os << v[0];
  for (int i=1, len=v.size(); i<len; ++i) os << ' ' << v[i];
  return os;
}

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

string rot(string s) {
  string r = "";
  rep (i, s.size()) {
    char c = s[i] < 'a' ? s[i]
           : s[i] == 'z' ? 'a' : char(s[i]+1);
    r += c;
  }
  return r;
}

bool ok(string s) {
  int a = s.find("the");
  int b = s.find("this");
  int c = s.find("that");
  return a != string::npos || b != string::npos || c != string::npos;
}

int main() {

  string s;
  while (getline(cin, s)) {
    while (!ok(s)) s = rot(s);
    cout << (s) << endl;
  }

  return 0;
}


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

ll GCD(ll x,ll y){
    if(y == 0) return x;
    else return GCD(y,x%y);
}

ll LCM(ll a,ll b){
    return a / GCD(a,b) * b;
}

const int MOD = 1000000007;

int main() {
    int n,m,x,y; cin >> n >> m >> x >> y;
    int mx = -101, ym = 101;
    vector<int> vec;
    for(int i = 0;i < n;i++){
        int a; cin >> a;
        mx = max(mx,a);
        if(x < a && a <= y) vec.push_back(a);
    }
    for(int i = 0;i < m;i++){
        int b; cin >> b;
        ym = min(ym,b);
        if(x < b && b <= y) vec.push_back(b);
    }
    string ans = "War";
    for(int i = 0;i < vec.size();i++){
        if(mx < vec[i] && vec[i] <= ym){
            ans = "No War";
            break;
        }
    }
    cout << ans;
}

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

int main(){
  int n; cin >>n;
  string s,t;
  cin >>s >>t;
  for(int i = n; 0<=i; i--){
    if(s.substr(n-i)==t.substr(0,i)) {
      cout << 2*n-i << endl;
      return 0;
    }
  }
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

long long D[201][100001], MOD=1000000007;

int main()
{
    int n, x, a;
    cin >> n >> x;
    vector <int> A;
    for (int i = 0; i < n; i++)
    {
        cin >> a;
        A.push_back(a);
    }
    sort(A.begin(),A.end());
    for (int i = 0; i <= x; i++)
    {
        for (int y = 1; y <= n; y++)
        {
            D[y][i] = 0;
        }
        D[0][i] = i;
    }
    for (int i = 0; i < n; i++)
    {
        for (int y = 0; y <= x; y++)
        {
            D[i+1][y] += D[i][y] * i;
            D[i+1][y] %= MOD;
            D[i+1][y] += D[i][y % A[i]];
            D[i+1][y] %= MOD;
        }
    }
    cout << D[n][x];
    return 0;
}

//Solution by Tima
#include <bits/stdc++.h>

#define f first
#define s second
#define ll long long
#define ull unsigned long long
#define mp make_pair
#define pb push_back
#define vi vector <int>
#define ld long double
#define pii pair<int, int>
#define y1 sda
#define all(x) x.begin(), x.end()

using namespace std;
const int N = int(1e5) + 12, mod = int(1e9)  + 7;

int n,x,a[N],dp[202][N], obr[N];

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

int calc(int i,int x){
	if(i > n) return x;
	if(dp[i][x] != -1) return dp[i][x];
	int res = 1ll * calc(i + 1, x) * (n - i) % mod;
	res += calc(i + 1, x % a[i]);
	if(res >= mod) res -= mod;
	return dp[i][x] = 1ll * res * obr[n - i + 1] % mod;
}

void solve(){
	scanf("%d%d", &n, &x);

	int f = 1;

	memset(dp, -1, sizeof(dp));

	for(int i = 1; i <= n; i++){
		scanf("%d", &a[i]);
		f = 1ll * f * i % mod;
		obr[i] = modpow(i, mod - 2);
	}
	sort(a + 1, a + n + 1);
	reverse(a + 1, a + n + 1);
	printf("%d\n", 1ll * calc(1,x) * f % mod);

}

int main () {
	int T = 1;
	//scanf("%d", &T);

	while(T--){
		solve();
	}

return 0;
}


#include <cstdio>

#define F(i, a, b) for (LL i = a; i <= b; i ++)
#define G(i, a, b) for (LL i = a; i >= b; i --)
#define pf printf

typedef long long LL;

const LL N = 1e7 + 10;
const LL M = 20;

using namespace std;

LL D, L, Ans, Sum, total, w[M];
LL ten[M], v[M], tot[M], TOT[M];

void Dfs(LL k, LL s, LL S) {
	if (s < 0) return;
	if (s + 10 * v[k] < D) return;
	if (s - 10 * v[k] > D) return;
	if (k > L) {
		if (s == D) {
			Sum += S;
//			printf("Yes\n");
		}
		return;
	}
	F(i, -9, 9)
		w[k] = i, Dfs(k + 1, s + v[k] * i, S * (k == 1 ? TOT[i + 9] : tot[i + 9]));
}

int main() {
	scanf("%lld", &D);
//	F(i, 1, N - 10) {
//		LL rev = 0;
//		for (LL x = i; x; rev = rev * 10 + (x % 10), x /= 10);
//		if (rev - i == D)
//			printf("%lld\n", i), total ++;
//	}

//	puts("");

	F(i, 0, 9)
		F(j, 0, 9)
			tot[i - j + 9] ++;
	F(i, 0, 9)
		F(j, 1, 9)
			TOT[i - j + 9] ++;

	ten[0] = 1;
	F(i, 1, 18)
		ten[i] = ten[i - 1] * 10LL;
	F(i, 2, 18) {
		L = i / 2, Sum = 0;
		F(j, 1, L)
			v[j] = ten[i - j] - ten[j - 1];
		Dfs(1, 0, 1);
		Ans += Sum * ((i & 1) ? 10 : 1);
//		pf("%d\n", i);
	}
	pf("%lld\n", Ans);
}

#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define MN 21001
#define int long long
using namespace std;

int read_p,read_ca;
inline int read(){
    read_p=0;read_ca=getchar();
    while(read_ca<'0'||read_ca>'9') read_ca=getchar();
    while(read_ca>='0'&&read_ca<='9') read_p=read_p*10+read_ca-48,read_ca=getchar();
    return read_p;
}
const int MOD=1e9+7;
int n,a[MN],B[MN],x,mmh=0,mm[20][20][2][2];
inline int M(int x){return x<0?x+10:x;}
inline void work(int p,int n,int a,int b){
    if (mm[p][n][a][b]!=-1) {mmh+=mm[p][n][a][b];return;}
    int u=mmh;
    if (p+p-1==n){
        if (a==0&&b==0) mmh+=10*(B[p]==0);else
        if (a==1&&b==1) mmh+=10*(B[p]==9);
        return;
    }
    if (p+p-1>n){
        mmh+=a==b;
        return;
    }
    for (int i=p==1;i<=9;i++)
    for (int j=0;j<=9;j++)
    if (M(i-j-b)==B[p]&&(a==(j<i)||j==i)&&(M(j-i)==B[n+1-p]||M(j-1-i)==B[n+1-p])){
        if (a){
            if (j==i&&B[n+1-p]==0) continue;
            work(p+1,n,M(j-1-i)==B[n+1-p],i-b-j<0);
        }else{
            if (j==i&&B[n+1-p]==9) continue;
            work(p+1,n,M(j-1-i)==B[n+1-p],i-b-j<0);
        }
    }
    mm[p][n][a][b]=mmh-u;
}
signed main(){
    int i,j,k;
    x=n=read();
    if (n%9) return puts("0"),0;
    for (k=1;k<=10&&x;k++) B[k]=x%10,x/=10;k--;
    for (int i=k;i<=18;i++) memset(mm,-1,sizeof(mm)),work(1,i,0,0);
    printf("%lld\n",mmh);
}


#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define REP(i,n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define debug(var) do{cout << #var << " : "; view(var);}while(0)
template<class T> bool chmin(T &a, T b) {if(a>b) {a=b;return 1;}return 0;}
template<class T> bool chmax(T &a, T b) {if(a<b) {a=b;return 1;}return 0;}
using namespace std;
template<class T> void view(T e) {cout << e << endl;}
template<class T> void view(const vector<T> &v) {for(const auto &e : v){cout << e << " ";} cout << endl;}
template<class T> void view(const vector<vector<T>> &vv) {for(const auto &v : vv){view(v);}}
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int,int>;
const int inf = 1e9;
const ll inf_l = 1e18;
const int MAX = 1e5;

int main() {
    int n; cin >> n;
    int l = 0, r = n;
    cout << l << endl;
    string sex; cin >> sex;
    if (sex == "Vacant") return 0;
    else {
        while (true) {
            int mid = (l+r)/2;
            cout << mid << endl;
            string sex_ ; cin >> sex_;
            if (sex_ == "Vacant") return 0;
            if (mid&1) {
                if (sex_ == sex) r = mid;
                if (sex_ != sex) l = mid;
            } else {
                if (sex_ == sex) l = mid;
                if (sex_ != sex) r = mid;
            }
        }
    }
}

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

typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
#define rep(i, n) for(ll i = 0; i < n; i++)
#define REP(i, a, b) for(ll i = a; i <= b; i++)
#define out(x) std::cout << x << '\n'
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define sz(x) ((int)(x).size())
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { ll g = gcd(a, b); return a / g * b; }
const int dx[4] = { 1, 0, -1,  0 };
const int dy[4] = { 0, 1,  0, -1 };
const int INF = (int)1e9;
const ll  inf = (ll)1e18;
const int mod = 1000000007;
const int MAX_N = 100010;
const double PI = acos(-1);

int N;
string a0;

string ask(int i) {
    out(i);
    fflush(stdout);

    string res;
    cin >> res;
    return res;
}

void f(int l, int r) {
    int md = (l + r) / 2;

    string res = ask(md);
    if (res == "Vacant") return;

    if ((md % 2 == 0 && a0 == res)
        || (md % 2 == 1 && a0 != res)) {
        f(md + 1, r);
    }
    else {
        f(l, md);
    }
}

int main() {
    cin >> N;

    a0 = ask(0);
    if (a0 == "Vacant") return 0;

    f(1, N);

    return 0;
}


#include <iostream>
using namespace std;
int main(void)
{
	int a,b,c[101]={0},d,e=0,f=0,g,h,i;
	for (a=0;a<4;a++)
	{
		cin>>b;
		c[b]=1;
	}
	for (a=100;a>0;a--)
	{
		if (c[a]==1&&e!=3)
		{
			f=f+a;
			e++;
		}
	}
	cin>>d>>e;
	if (d>=e)
	{
		g=d+f;
	}
	else if (d<=e)
	{
		g=e+f;
	}
	cout<<g<<endl;
	return 0;
}

// clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
// #define main int main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
#define stlice(from, to) substr(from, (to) - (from) + 1)
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
using namespace std;
using pii = pair<int, int>;
using vi = vector<int>;
using vd = vector<double>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vpii = vector<pii>;
using vvi = vector<vi>;
using vvb = vector<vb>;
using vvpii = vector<vpii>;
template<typename A> using fn = function<A>;
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = acos(-1);
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
constexpr bool odd(const int &n) { return n & 1; }
constexpr bool even(const int &n) { return !odd(n); }
void solve();
main { solve(); return 0; }
// clang-format on

void solve() {
  vi a(4), b(2);
  rep(i, 4) cin >> a[i];
  rep(i, 2) cin >> b[i];
  sort(rall(a)), sort(rall(b));
  cout << a[0] + a[1] + a[2] + b[0] << endl;
}


#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <vector>

using namespace std;

#define rep(i, n) for (int64_t i = 0; i < (n); i++)
#define irep(i, n) for (int64_t i = 0; i <= (n); i++)
#define rrep(i, n) for (int64_t i = (n)-1; i >= 0; i--)
#define rirep(i, n) for (int64_t i = n; i >= 0; i--)

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

  const int64_t INF = 1'000'000'000L * 200'000 + 1;
  vector<vector<int64_t>> dp(N + 2, vector<int64_t>(3, -INF));
  dp[0][0] = 0;
  rep(i, N) {
    int a;
    cin >> a;
    irep(j, 2) {
      dp[i + 2][j] = dp[i][j] + a;
      rep(k, j) {
        if (i - (j - k) >= 0) {
          dp[i + 2][j] = max(dp[i + 2][j], dp[i - (j - k)][k] + a);
        }
      }
      dp[i + 2][j] = max(dp[i + 2][j], -INF);
    }
  }

  /*rep(i, N + 2) {
    rep(j, 3) { cerr << " " << dp[i][j]; }
    cerr << "\n";
  }*/

  if (N % 2 == 0) {
    cout << max(dp[N + 1][1], dp[N][0]) << endl;
  } else {
    cout << max(dp[N + 1][2], max(dp[N][1], dp[N - 1][0])) << endl;
  }
  return 0;
}

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

#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define all(x) (x).begin(),(x).end()
using ll = long long;
string char_to_string(char val) {
  return string(1, val);
}
int char_to_int(char val) {
  return val - '0';
}
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 vector_finder(std::vector<ll> vec, int number) {
  auto itr = std::find(vec.begin(), vec.end(), number);
  size_t index = std::distance( vec.begin(), itr );
  if (index != vec.size()) { // 発見できたとき
    return 1;
  }
  else { // 発見できなかったとき
    return 0;
  }
}

int main() {
  double N; cin >> N;
  vector<double> a(N); REP(i, N) cin >> a[i];
  double avg = accumulate(all(a), 0) / N;
  double ans = 0;
  double tmp = pow(10, 8);
  REP(i, N) {
    if(abs(avg - a[i]) < tmp) {
      ans = i;
      tmp = abs(avg - a[i]);
    }
  }
  cout << ans << endl;
}


#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define FOR(i,k,n) for (int (i)=(k); (i)<(n); ++(i))
#define rep(i,n) FOR(i,0,n)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fst first
#define snd second
#define all(v) begin(v), end(v)
#define debug(x) cerr<< #x <<": "<<x<<endl
#define debug2(x,y) cerr<< #x <<": "<< x <<", "<< #y <<": "<< y <<endl

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<ll> vll;
typedef vector<vector<ll> > vvll;
template<class T> using vv=vector<vector< T > >;

struct UF {
  vector<int> par;
  vector<int> sizes;
  UF(int n) : par(n), sizes(n, 1) {
    rep(i,n) par[i] = i;
  }
  int root(int x) {
    if (x == par[x]) return x;
    return par[x] = root(par[x]);
  }
  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) {
      return;
    }
    if (sizes[x] < sizes[y]) {
      swap(x, y);
    }
    par[y] = x;
    sizes[x] += sizes[y];
    sizes[y] = 0;
  }
  bool same(int x, int y) {
    return root(x) == root(y);
  }
  int size(int x) {
    return sizes[root(x)];
  }
}; // UF

int V, E;
vvi edge;
ll cost;

void kruskal() {
  cost = 0;
  UF uf(V);
  rep (i, E) {
    auto e = edge[i];
    if (uf.same(e[1], e[2])) {
      continue;
    }
    uf.unite(e[1], e[2]);
    cost += e[0];
    if (uf.size(e[1]) == V) {
      break;
    }
  }
  return;
}

int main() {
  scanf("%d %d", &V, &E);
  edge.assign(E, vi(3));
  rep (i, E) {
    int s, t, w;
    scanf("%d %d %d", &s, &t, &w);
    edge[i][0] = w;
    edge[i][1] = s;
    edge[i][2] = t;
  }
  sort(all(edge));
  kruskal();
  printf("%lld\n", cost);

  return 0;
}

#include <iostream>
#include <vector>
#include <cmath>
#include <iomanip>
#include <queue>
#include <algorithm>
#define shosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef pair<int,P> pip;
typedef vector<pip> vip;
const int inf=1<<30;
const double pi=acos(-1);

class Graph{
	private:
	vvi Gpro,Gd;
	vvp Glist;
	int GV,GE;
	public:
	Graph(int x,int y,bool z){
		GV=x;
		GE=y;
//		Gd=vvi(x,vi(x,inf));
		if(z) Gpro=vvi(x,vi(x,inf));
		else Glist=vvp(x);
	}
	void Gpro_add(int x,int y,int z){
		Gpro[x][y]=z;
		Gpro[y][x]=z;
	}
	void Glist_add(int x,int y,int z){
		Glist[x].push_back(P(y,z));
		Glist[y].push_back(P(x,z));
	}
	void Gd_add(int x,int y,int z){
		Gd[x][y]=z;
	}
	int open(int x,int y){
		return Gd[x][y];
	}
	void DIJplus(int x){
		priority_queue<P> que;
		Gd[x][x]=0;
		que.push(P(0,x));
		while(!que.empty()){
			P p=que.top();
			p.first*=-1;
			que.pop();
			int v=p.second;
			if(Gd[x][v]<p.first) continue;
			for(int i=0;i<Glist[v].size();i++){
				P e=Glist[v][i];
				if(Gd[x][e.first]>Gd[x][v]+e.second){
					Gd[x][e.first]=Gd[x][v]+e.second;
					que.push(P(-1*Gd[x][e.first],e.first));
				}
			}
		}
	}
	void BELLMAN(int x){
		Gd[x][x]=0;
		while(1){
			bool update=1;
			for(int i=0;i<GV;i++){
				for(int j=0;j<Glist[i].size();j++){
					P e=P(Glist[i][j].first,Glist[i][j].second);
					if(Gd[x][i]!=inf&&Gd[x][e.first]>Gd[x][i]+e.second){
						Gd[x][e.first]=Gd[x][i]+e.second;
						update=0;
					}
				}
			}
			if(update) break;
		}
	}
	bool FNL(int x){
		Gd[x][x]=0;
		for(int i=0;i<GV;i++){
			for(int j=0;j<GV;j++){
				for(int k=0;k<Glist[j].size();k++){
					P e=P(Glist[j][k].first,Glist[j][k].second);
					if(Gd[x][j]!=inf&&Gd[x][e.first]>Gd[x][j]+e.second){
						Gd[x][e.first]=Gd[x][j]+e.second;
						if(i==GV-1) return 1;
					}
				}
			}
		}
		Gd=vvi(GV,vi(GV,inf));
		return 0;
	}
	void WARSHALL(){
		for(int i=0;i<GV;i++){
			for(int j=0;j<GV;j++){
				for(int k=0;k<GV;k++) Gd[i][j]=min(Gd[i][j],Gd[i][k]+Gd[k][j]);
			}
		}
	}
	int PRIM(){
		int Gans=0;
		vi mincost(GV,inf);
		vector<bool> used(GV,1);
		mincost[0]=0;
		while(1){
			int v=-1;
			for(int i=0;i<GV;i++){
				if(used[i]&&(v==-1||mincost[i]<mincost[v])) v=i;
			}
			if(v==-1) break;
			used[v]=0;
			Gans+=mincost[v];
			for(int i=0;i<Glist[v].size();i++){
				mincost[Glist[v][i].first]=min(mincost[Glist[v][i].first],Glist[v][i].second);
			}
		}
		return Gans;
	}
};

int V,E;

int main(){
	cin>>V>>E;
	Graph gra(V,E,0);
	for(int i=0;i<E;i++){
		int s,t,d;
		cin>>s>>t>>d;
		gra.Glist_add(s,t,d);
	}
	cout<<gra.PRIM()<<endl;
}


#include <stdio.h>

int main(){

 char X[3];

    scanf("%s",&X);

    if(X[0]=='7'){

        printf("Yes\n");

    }

    else if(X[1]=='7'){

        printf("Yes\n");

    }
    else if(X[2]=='7'){

        printf("Yes\n");

    }

    else{

       printf("No\n");

    }

return 0;

}

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

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

#define rep(i,cc,n) for(int i=cc;i<n;++i)
#define lrep(i,cc,n) for(long long i=cc;i<n;++i)
#define sqrep(i,cc,n) for(long long i=cc;i*i<=n;++i)
#define rrep(i,cc,n) for(long i=cc;i>n;--i)
#define pii pair<int, int>
#define pll pair<long long, long long>

using ll = long long;

const vector<int> dx = {1, 0, -1, 0};
const vector<int> dy = {0, 1, 0, -1};
const ll inf = 1001001001;
const ll e9  = 1000000000;
const ll mod = 1000000007;
const double PI = 3.1415926535;

int main() {
   string s;
   cin >> s;
   int n = s.size();
   vector<int>cnt(26,0);
   if(n!=26){
       rep(i, 0, n){
           cnt[s[i]-'a']++;
       }
       //cout << "ok" << endl;
       rep(i, 0, 26){
           if(cnt[i]==0){
               char t = i + 'a';
               cout << s << t << endl;
               return 0;
           }
       }
   }else if (s!="zyxwvutsrqponmlkjihgfedcba"){
       string s2 = s;
       string p;
       next_permutation(s.begin(),s.end());
       while(s>s2){
           p = s[s.size()-1];
           s.erase(s.size()-1);
       }
       cout << s << p << endl;
   }else{
       cout << -1 << endl;
   }
}


#include<iostream>
#include <bits/stdc++.h>
#include<algorithm>
#include <cstring>
#define endl "\n"
#define SRK ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define EPS 1e-6
#define MAX 100000
typedef long long ll;
const ll MOD = 1e9 + 7;
using namespace std;

int N = 1000000;
int NOD[1000000];
void generateNOD()
{
    for (int i = 0; i <= N; i++)
    {
        for (int j = i; j <= N; j += i)
        {
            NOD[j]++;
        }
    }
}
int main()
{
    int n;
    cin >> n;
    int yen500 = 1000 * (n / 500);
    int left = n % 500;
    int yen5 = 5 * (left / 5);
    cout << yen500 + yen5 << endl;

}

#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define endl "\n"
#define F first
#define S second
#define mod 1000000007
#define pb push_back
#define FOR(i,a,n) for(int i=a;i<n;i++)
#define REV(i,a,n) for(int i=a;i>=n;i--)
#define all(a) a.begin(),a.end()
const int NUM = 2e5 + 5;

int32_t main(){
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int num_tests=1;
    //cin >> num_tests;
    while(num_tests-->0){
        int n; cin >> n;
        int ans = 0;
        while(n >= 500)
        {
            int x = n/500;
            ans += x*1000;
            n -= (x)*500;
        }
        while(n >= 5)
        {
            int y = n/5;
            ans += y*5;
            n -= y*5;
        }
        cout << ans;
    }
}


#include <algorithm>
#include <bitset>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <cctype>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <limits>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using ld = long double;
#define endl "\n"
//#define int long long
#define ALL(x) begin(x),end(x)
#define RALL(a) (a).rbegin(), (a).rend()
#define _CRT_SECURE_NO_WARNINGS
#define rep(i, n) for (int i = 0; i < n; i++)
const int MOD = 1e9 + 7;
const ll INF = 1LL << 60;
const int inf = 1 << 25;
constexpr long double pi = 3.141592653589793238462643383279;
int dx[] = { 1,0,-1,0 };
int dy[] = { 0,1,0,-1 };
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
ll fact(ll i) {
    if (i == 0) return 1;
    return ((fact(i - 1)) * i) % MOD;
}
ll gcd(ll a, ll b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
    return a * b / gcd(a, b);
}
ll keta(ll n) {
    if (n == 0) return 1;
    ll count = 0;
    while (n != 0) {
        n /= 10;
        count++;
    }
    return count;
}
ll ketasum(ll n) {
    ll sum = 0;
    while (n != 0) {
        sum += n % 10;
        n /= 10;
    }
    return sum;
}
ll mch(ll n) {
    if (n == 1) return 1;
    else return n * mch(n - 1);
}
ll modpow(ll a, ll n, ll mod) {
    ll hi = 1;
    while (n > 0) {
        if (n & 1) hi = hi * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return hi;
}
bool is_prime(const unsigned n) {
    switch (n) {
    case 0: // fall-through
    case 1: return false;
    case 2: // fall-through
    case 3: return true;
    } // n > 3 が保証された
    if (n % 2 == 0 || n % 3 == 0) return false;
    for (unsigned i = 5; i * i <= n; i += 6) {
        if (n % i == 0) return false; // (6の倍数)-1
        if (n % (i + 2) == 0) return false; // (6の倍数)+1
    }
    return true;
}
/*signed*/int main(void) {
    int n, m;
    cin >> n >> m;
    vector<pair<int, int>> p(m);
    rep(i, m) {
        int s, c;
        cin >> s >> c;
        s--;
        p[i] = make_pair(s, c);
    }
    rep(i, 1000) {
        string s = to_string(i);
        if (s.size() == n) {
            bool ans = true;
            rep(j, m) {
                if ((s[p[j].first] - '0') != p[j].second)ans = false;
            }
            if (ans) {
                cout << i << endl;
                return 0;
            }
        }
    }
    cout << -1 << endl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define ll long long int
#define pb push_back
typedef pair<ll,ll> ipair;
#define F first
#define S second

/*
//functions from gfg

long long int power(long long int x,long long int y,long long  int p)
{
    long long int res = 1;     // Initialize result

    x = x % p;  // Update x if it is more than or
                // equal to p

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            {//d=(long double)x/p;
            res = (res*x) % p;
            }

        // y must be even now
        y = y>>1; // y = y/2
        x=(x*x)%p;

    }
    return res%p;
}

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

long long int binomialCoeff(long long int n, ll k)
{
    ll res = 1;

    // Since C(n, k) = C(n, n-k)
    if (k > n - k)
        k = n - k;

    // Calculate value of [n*(n-1)*---*(n-k+1)] / [k*(k-1)*---*1]
    for (ll i = 0; i < k; ++i)
    {
        res *= (n - i);
        res /= (i + 1);
    }

    return res;
}

void sieve(int n, vector<ll> &primes)
{
    vector<int> prime(n,1);
    ll p;
    for ( p=2; p*p<=n; p++)
    {
        // If prime[p] is not changed, then it is a prime
        if (prime[p] == 1)
        {

            for (int i=p*p; i<=n; i += p)
                prime[i] = 0;
        }
    }

    for(p=2;p<=n;p++)
    {
        if(prime[p])
        primes.pb(p);
    }

}

void dfs(vector<ll> adj[] ,ll src,vector<ll> &vis)
{
    vis[src]=1;
    for(ll i=0;i<adj[src].size();i++)
    {
        if(vis[adj[src][i]]==0)
        dfs(adj,adj[src][i],vis)
    }
}

*/

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

    ll n,i,j,p,k,m,t,ans=0;
    cin>>n>>t;
    ll dp[3005][3005];
    memset(dp, 0, sizeof(dp));
    vector<ll> a(n),b(n);
    vector<pair<ll,ll> > val(n);
    for(i=0;i<n;i++)
    {
        cin>>a[i]>>b[i];
        val[i].F=a[i];
        val[i].S=b[i];
    }
    sort(val.begin(),val.end());
    for(i=0;i<n;i++)
    {
        a[i]=val[i].F;
        b[i]=val[i].S;
    }
    for(i=0;i<t;i++)
    {
        for(j=0;j<n;j++)
        {
            if(i+a[j]<t)
            {
                dp[j+1][i+a[j]] = max(dp[j][i]+b[j],dp[j+1][i+a[j]]);
            }

            dp[j+1][i] = max(dp[j][i],dp[j+1][i]);

        }
    }
    for(i=0;i<n;i++)
    {
        ans=max(ans,dp[i][t-1]+b[i]);
    }
    cout<<ans<<endl;

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

    return 0;
}


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

typedef unsigned long ul;
typedef unsigned long long ull;
typedef long long ll;
typedef vector<ll> vint;
typedef vector< vector<ll> > vvint;
typedef vector< vector< vector<ll> > > vvvint;
typedef vector<string> vstring;
typedef vector< vector<string> > vvstring;
typedef vector<char> vchar;
typedef vector< vector<char> > vvchar;
typedef vector<long double> vdouble;
typedef vector< vector<long double> > vvdouble;
typedef vector< vector< vector<long double> > > vvvdouble;
typedef pair<ll,ll> pint;
typedef vector<pint> vpint;
typedef vector<bool> vbool;

#define rep(i,n) for(ll i=0;i<n;i++)
#define repf(i,f,n) for(ll i=f;i<n;i++)
#define repr(i,n) for(ll i=n-1;i>=0;i--)
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define ALL(obj) (obj).begin(), (obj).end()
// #define LLONG_MAX 9223372036854775806
#define vmax(vec) *max_element(vec.begin(), vec.end())
#define vmin(vec) *min_element(vec.begin(), vec.end())
#define vsort(vec) sort(vec.begin(), vec.end())
#define vsortgr(vec) sort(vec.begin(), vec.end(), greater<ll>())
#define MOD 1000000007
// #define MOD 998244353
// #define MOD LLONG_MAX
const double PI=3.14159265358979323846;
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; }

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

void printv(vint &v){
    for(auto e:v) cout<<e<<" ";
        cout<<endl;
    }

// 繰り返し二乗法
ll power(ll a, ll b){
    if(a==1)return a;
    // if(a==0)re
    ll res=1;
    while(b>0){
        if(b&1) res=res*a%MOD;
        a=a*a%MOD;
        b>>=1;
    }
    return res;
}

const int MAX=2100000;

ll fact[MAX], fact_inv[MAX];

void init_fact(ll n){
    fact[0]=1;
    // 階乗の計算
    for(ll i=0; i<n; i++) fact[i+1]=fact[i]*(i+1)%MOD;
    fact_inv[n]=power(fact[n], MOD-2);
    // 逆元の計算
    for(ll i=n-1;i>=0;i--) fact_inv[i]=fact_inv[i+1]*(i+1)%MOD;
}

ll comb(ll n, ll r){
    return (fact[n]*fact_inv[r])%MOD*fact_inv[n-r]%MOD;
}

ll perm(ll n, ll r){
    return (fact[n]*fact_inv[n-r])%MOD;
}

int main() {
    cout<<fixed<<setprecision(10);

    ll k; cin>>k;
    ll n = 50;

    ll x,y;
    x = k/n;
    y = k%n;
    // ll a_base = (x+1)*n - 1 - y;
    ll a_base = ( n - 1 ) + x - y;
    vint ans(n, a_base);
    rep(i,y){
        ans[i] += n - (i+1) + 2;
    }

    cout << n <<endl;
    for(auto e:ans) cout<<e<<" ";
    cout<<endl;

    return 0;

}

#include<bits/stdc++.h>
#define sqr(x) ((x)*(x))
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define ld long double
#define vi vector<int>
#define HEAP(...) priority_queue<__VA_ARGS__ >
#define heap(...) priority_queue<__VA_ARGS__,vector<__VA_ARGS__ >,greater<__VA_ARGS__ > >
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define debuge cerr<<"isok"<<endl
#define debug(x) cerr<<#x<<"="<<x<<endl
#define dprintf(...) fprintf(stderr,__VA_ARGS__)
#define SS second
#define FF first
#define ls (k<<1)
#define rs (k<<1|1)
#define clr(a,x) memset(a,x,sizeof(a))
#define cpy(a,x) memcpy(a,x,sizeof(a))
#define file(x) freopen(x".in","r",stdin),freopen(x".out","w",stdout)
#define SZ(x) ((int)x.size())
using namespace std;
template<class T> inline void gmin(T &x,const T &y){if(x>y) x=y;}
template<class T> inline void gmax(T &x,const T &y){if(x<y) x=y;}
const int BufferSize=1<<16;
char buffer[BufferSize],*Bufferhead,*Buffertail;
bool Terminal;
inline char Getchar(){
	if(Bufferhead==Buffertail){
		int l=fread(buffer,1,BufferSize,stdin);
		if(!l){Terminal=1;return 0;}
		Buffertail=(Bufferhead=buffer)+l;
	}
	return *Bufferhead++;
}
template<class T>inline bool read(T &x){
	x=0;char c=Getchar(),rev=0;
	while(c<'0'||c>'9'){c=Getchar();rev|=c=='-';if(Terminal)return 0;}
	while(c>='0'&&c<='9') x=x*10+c-'0',c=Getchar();
	if(c=='.'){
		c=Getchar();double t=0.1;
		while(c>='0'&&c<='9') x=x+(c-'0')*t,c=Getchar(),t=t/10;
	}
	x=rev?-x:x;
	return 1;
}
template<class T1,class T2> inline bool read(T1 &x,T2 &y){return read(x)&read(y);}
template<class T1,class T2,class T3> inline bool read(T1 &x,T2 &y,T3 &z){return read(x)&read(y)&read(z);}
template<class T1,class T2,class T3,class T4> inline bool read(T1 &x,T2 &y,T3 &z,T4 &w){return read(x)&read(y)&read(z)&read(w);}
inline bool reads(char *x){
	char c=Getchar();
	while(c<33||c>126){c=Getchar();if(Terminal)return 0;}
	while(c>=33&&c<=126) (*x++)=c,c=Getchar();
	*x=0;return 1;
}
template<class T>inline void print(T x,const char c='\n'){
	if(!x){putchar('0');putchar(c);return;}
	if(x<0) putchar('-'),x=-x;
	int m=0,a[20];
	while(x) a[m++]=x%10,x/=10;
	while(m--) putchar(a[m]+'0');
	putchar(c);
}
//--------------------------------------------------head--------------------------------------------------

const int inf=0x3f3f3f3f;
const int N=1000005,M=100005,mod=998244353;
template<class T,class S> inline void ch(T &x,const S y){x=(x+y)%mod;}
inline int exp(int x,int y,const int mod=::mod){
	int ans=1;
	while(y){
		if(y&1) ans=(ll)ans*x%mod;
		x=(ll)x*x%mod;y>>=1;
	}return ans;
}

int n,m,fac[N],ifac[N];

int main(){
#ifdef rqgao2014
	freopen("input.txt","r",stdin);
#endif
	n=1000000;fac[0]=1;
	for(int i=1;i<=n;i++) fac[i]=(ll)fac[i-1]*i%mod;
	ifac[n]=exp(fac[n],mod-2);
	for(int i=n;i;i--) ifac[i-1]=(ll)ifac[i]*i%mod;
	read(n,m);
	int ans=0;
	for(int i=1;i<=min(n,m);i++)
		ch(ans,(ll)ifac[i]*ifac[i]%mod*ifac[n-i]%mod*ifac[m-i]%mod*fac[i*2-1]%mod*fac[n+m-2*i]%mod*i);
	ans=(ll)ans*fac[n]%mod*fac[m]%mod*ifac[n+m]%mod;
	ch(ans,max(n,m));
	print(ans);
	return 0;
}


#include <bits/stdc++.h>
#include <ext/rope>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define ll long long
#define int long long
#define pb push_back
#define x first
#define ld long double
#define y second
#define mk(a,b) make_pair(a,b)
#define rr return 0
#define sqr(a) ((a)*(a))
#define all(a) a.begin(),a.end()

using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
template<class value, class cmp = less<value> >
using ordered_set = tree<value, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
template<class value, class cmp = less_equal<value> >
using ordered_multiset = tree<value, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
template<class key, class value, class cmp = less<key> >
using ordered_map = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;

/// find_by_order()
/// order_of_key()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int randll(int l = INT_MIN, int r = INT_MAX) {
    return uniform_int_distribution<int>(l, r)(rng);
}
vector <int> t;
inline void upd (int i, int x) {
    for (; i < (int)t.size(); i = i | (i + 1)) t[i] += x;
}
inline int sum (int r) {
    int ans = 0;
    for (; r >= 0; r = (r & (r + 1)) - 1) ans += t[r];
    return ans;
}
inline int sum (int l, int r) {
    return sum(r) - sum(l - 1);
}
main()
{
    ios::sync_with_stdio(0);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n, q;
    cin >> n >> q;
    vector <int> a(n);
    for (auto &i : a) cin >> i;
    vector <vector <pair<int, int> > > Q(n);
    int l, r;
    for (int i = 0; i < q; i++) {
        cin >> l >> r;
        --l, --r;
        Q[r].pb({l, i});
    }
    t.resize(n);
    map <int, int> have;
    vector <int> ans(q);
    for (int i = 0; i < n; i++) {
        if (have.count(a[i])) {
            upd(have[a[i]], -1);
        }
        have[a[i]] = i;
        upd(i, 1);
        for (auto &j : Q[i]) {
            ans[j.y] = sum(j.x, i);
        }
    }
    for (auto &i : ans) cout << i << '\n';
}

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

using namespace std;

int main() {

  while(1) {

    int n;
    cin >> n;
    int m;
    cin >> m;

    if(n==0 && m ==0) break;

    vector <int> re[502];

    queue <int > Q;
    int a, b;
    for(int i=0;i<m;i++) {
      cin >> a >> b;
      re[a].push_back(b);
      re[b].push_back(a);
    }

    int t;
    int count = -1;
    Q.push(1);

    int dis[502];
    for(int i=0;i<=n;i++) dis[i] = -1;

    while(Q.empty() != 1){

      t = Q.front();
      Q.pop();

      for(int i=0;i<re[t].size();i++) {
	if(dis[re[t][i]] == -1 && dis[t]+1 < 2) {
	  dis[re[t][i]] = dis[t] + 1;
	  count++;
	  //  printf("dis[i%d](%d) | dis[t%d](%d) | re[%d][%d](%d)\n",i,dis[re[t][i]],t,dis[t],t,i,re[t][i]);
	  Q.push(re[t][i]);
	  //cout << "Q.front == " << Q.front() << endl;
	}
      }
    }

    cout << count << endl;

  }

  return 0;
}