#include <iostream>
#include <iomanip>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <cstdio>
#include <utility>
#include <string>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <numeric>

using namespace std;

typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;

const double PI=3.14159265358979323846;

#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

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

#define CEIL(x, y) (((x) + (y) - 1) / (y))
#define MOD 1000000007ULL

#define IN(l, r, x) ((l) <= (x) && (x) < (r))

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

    s64 n, m;
    cin >> n >> m;
    vs64 x(n), y(m);
    rep (i, n)
    {
        cin >> x[i];
        if (x[i] < 0) x[i] += MOD;
    }
    rep (i, m)
    {
        cin >> y[i];
        if (y[i] < 0) y[i] += MOD;
    }

    s64 xadd, xsub, yadd, ysub;
    xadd = xsub = yadd = ysub = 0;
    rep (i, n)
    {
        xadd = (xadd + x[i] * i % MOD) % MOD;
        xsub = (xsub + x[n - i - 1] * i % MOD) % MOD;
    }
    xsub = MOD - xsub;
    rep (i, m)
    {
        yadd = (yadd + y[i] * i % MOD) % MOD;
        ysub = (ysub + y[m - i - 1] * i % MOD) % MOD;
    }
    ysub = MOD - ysub;

    cout << (((xadd + xsub) % MOD) * ((yadd + ysub) % MOD)) % MOD << "\n";
    return 0;
}

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

int read(){
	int x=0; char c=getchar(); int flag=1;
	while(!isdigit(c)) { if(c=='-') flag=-1; c=getchar(); }
	while(isdigit(c)) { x=((x+(x<<2))<<1)+(c^48); c=getchar(); }
	return x*flag;
}

const int mod=1e9+7;

int n,k;
int dp[2005][2005];//i个白球，j种颜色

int fac[4000005],Inv[4000005];

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

void exgcd(int a,int b,int &x,int &y){
    if(b==0) { x=1; y=0; return ; }
    exgcd(b,a%b,y,x); y-=a/b*x;
}

int inv(int a){
    int x=0,y=0;
    exgcd(a,mod,x,y);
    return (x%mod+mod)%mod;
}

signed main(){
	fac[0]=1;
	for(int i=1;i<=4000000;i++) fac[i]=1ll*fac[i-1]*i%mod;
	Inv[4000000]=inv(fac[4000000]);
	for(int i=3999999;i>=0;i--) Inv[i]=1ll*Inv[i+1]*(i+1)%mod;

    n=read(),k=read();
    if(k==1) { puts("1"); return 0; }
	dp[0][0]=1;

    for(int i=1;i<=n;i++){
	    dp[i][0]=1;
		for(int j=1;j<=i;j++){
			dp[i][j]=(dp[i-1][j]+1ll*(1ll*dp[i][j-1]*C(n*k-(j-1)*(k-1)-i-1,k-2))%mod*(n-j+1)%mod)%mod;
		}
	}

	printf("%d\n",dp[n][n]);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(){
  int n, k;
  cin >> n >> k;
  int l[n], ans = 0;
  for(int i=0; i<n; ++i) cin >> l[i];
  sort(l, l+n, greater<int>());
  for(int i=0; i<k; ++i) ans += l[i];
  cout << ans << endl;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define For(i,n,k) for(int i=(n);i<(k);i++)
#define ALL(a)  (a).begin(),(a).end()
vector<int> match = {-1,2,5,5,4,5,6,3,7,6};
void Main(){
    int n,m;
    cin >> n >> m;
    vector<pair<int,int>> approval(m);//first:本数　second:数
    For(i,0,m) {
        cin >> approval[i].second;
        approval[i].first = match[approval[i].second];
    }
    sort(ALL(approval), [](auto const& l, auto const& r) {//数の大きい順にソート
       if(l.second != r.second) return l.second > r.second;
       else return l.first > r.first;
    });
    vector<vector<string>> dp(m+1,vector<string> (n+1));
    For(j,0,n+1) dp[0][j]="";
    For(i,0,m+1) dp[i][0]="";
    For(i,0,m){
        For(j,1,n+1){
            if(j<approval[i].first) dp[i+1][j]=dp[i][j];
            else{
                string str1 = dp[i][j];
                string str2 = dp[i+1][j-approval[i].first];
                if(str2 == "" && j != approval[i].first) dp[i+1][j]=str1;
                else{
                    str2.push_back(char(approval[i].second + '0'));
                    if(str1.size()>str2.size()) dp[i+1][j]=str1;
                    else if(str1.size()<str2.size()) dp[i+1][j]=str2;
                    else if(str1>str2) dp[i+1][j]=str1;
                    else dp[i+1][j]=str2;
                }
            }
        }
    }
    cout << dp[m][n] << endl;
}
int main(){
    Main();
    /*
    東方風神録は神が出てくるので当然神ゲー
    */
    return 0;
}


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

int main() {
    int a,b;
    cin>>a>>b;
    int c=a+b,d=a-b,e=a*b;
    c=max(c,d);
    c=max(c,e);
    cout<<c<<endl;
}

#include<bits/stdc++.h>
using namespace std;
const int N=1000005,E=262144;
const int M=998244353;
int n,m,i,j,s,a[N],dp[6005][12005];
long long dfs(int i,int j)
{
	if(i<0)
		return 0;
	if(i==0)
		if(j>=0&&j%3==0)
			return 1;
		else
			return 0;
	if(dp[i][j+6000]!=-1)
		return dp[i][j+6000];
	long long s=dfs(i-1,j+1);
	s+=dfs(i-2,j-1)*(i-1);
	s+=dfs(i-3,j)*(i-1)*(i-2);
	return dp[i][j+6000]=s%m;
}
int main()
{
	scanf("%d %d",&n,&m);
	memset(dp,-1,sizeof(dp));
	cout<<dfs(n*3,0);
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N=5e5+50;
int gi(){
    char ch=getchar(); int x=0,q=0;
    while(ch<'0'||ch>'9') q=ch=='-'?1:q,ch=getchar();
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return q?-x:x;
}
int vis[N],d[N],head[N],nn[N][2],cnt=1,flag;
ll ans;
#define link(u,v) (nn[++cnt][1]=v,nn[cnt][0]=head[u],head[u]=cnt)
void dfs(int x,int op){
    if (vis[x]){
	if (op!=d[x]) flag=1;
	return;
    }
    vis[x]=1,d[x]=op;
    for (int i=head[x]; i; i=nn[i][0])
	dfs(nn[i][1],op^1);
    return;
}
int main(){
    int n=gi(),m=gi(),u,v;
    for (int i=1; i<=m; ++i)
	u=gi(),v=gi(),link(u,v),link(v,u);
    int a=0,b=0,c=0;
    for (int i=1;  i<=n; ++i)
	if (!head[i]) ++a;
    for (int i=1; i<=n; ++i)
	if (head[i]&&!vis[i]){
	    flag=0,dfs(i,0);
	    if (flag) ++b;
	    else ++c;
	}
    cout<<2ll*a*n-1ll*a*a+1ll*b*b+2ll*c*(b+c);
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>; // グラフ型
typedef long long ll;
#define _GLIBCXX_DEBUG

int main() {
  ll N, M;
  cin >> N >> M;
  vector<ll> save(N+1, -1);

  for(ll i=0; i<M; i++){
    ll a, b;
    cin >> a >> b;
    if(save[a] == -1){
        save[a] = b;
    }else if (save[a] == b)
    {
        save[a] = b;
    }else{
        cout  << "-1" << endl;
        return 0;
    }
  }
  if(save[1] == 0 && N!=1){
      cout  << "-1" << endl;
      return 0;
  }

  for(ll i=1; i<=N; i++){
      if(save[i]!=-1){
          cout << save[i];
      }else{
          if(i==1){
              if(N==1) cout << 0;
              else  cout << 1;
          }

          else cout << 0;
      }
  }

  cout << endl;
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int MAX_N = 2e5 + 5;
string s;
void solve(){
    stack<char> st;
    int i = 0, n = s.length();
    for(i = 0; i < n; i++){
        if(s[i] == 'S')
            st.push(s[i]);
        else{
            if(st.size() > 0 && st.top() == 'S')
                st.pop();
            else
                st.push(s[i]);
        }
    }
    cout << st.size() << "\n";
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> s;
    solve();
    return 0;
}


#include<bits/stdc++.h>
#define rint register int
using namespace std;
inline int read(){
	int s=0,f=1; char c=getchar();
	while(c<'0'||c>'9'){if(c=='-')f=0;c=getchar();}
	while(c>='0'&&c<='9') s=(s<<1)+(s<<3)+(c^48),c=getchar();
	return f?s:-s;
}
const int Mod=1e9+7;
int n,X,Y,Z,ed,ans=1;
int f[50][1<<18];
int main(){
	n=read(); X=read(); Y=read(); Z=read();
	ed=(1<<X+Y+Z-1)|(1<<Y+Z-1)|(1<<Z-1);
	f[0][0]=1;
	for(rint i=1;i<=n;++i,ans=1ll*ans*10%Mod)
		for(rint S=0;S<(1<<X+Y+Z);++S){
			if(!f[i-1][S]) continue;
			for(rint j=1;j<=10;++j){
				int now=(S<<j)|(1<<j-1);
				now=now&((1<<X+Y+Z)-1);
				if((now&ed)!=ed) f[i][now]=1ll*(f[i][now]+f[i-1][S])%Mod;
			}
		}
	for(rint S=0;S<(1<<X+Y+Z);++S)
		if((S&ed)!=ed) ans=1ll*(ans-f[n][S]+Mod)%Mod;
	printf("%d",ans);
	return 0;
}

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

int n,x,y,z;
typedef long long ll;
const int N = 41, MAXM = (1 << 18) + 50, mod = (int)1e9 + 7;

map<vector<int>, int> mp;

int cnt = 0;
int dp[N][MAXM];
int nxt[MAXM][11];
bool haiku[MAXM];

//vector<vector<int> > seqs;

int cumul(vector<int> &V, int cur, int sum) {
    while(cur < V.size() && sum > 0) sum -= V[cur], cur++;
    if(sum != 0) return -1;
    return cur;
}

bool is_haiku(vector<int> &V) {
    int sum = 0;
    for(int x : V) sum += x;
    if(sum != x + y + z) return false;
    int cur = 0;
    if((cur = cumul(V, cur, x)) == -1) return false;
    if((cur = cumul(V, cur, y)) == -1) return false;
    return cumul(V, cur, z) != -1;
}

int get(vector<int> &V) {
    if(!mp.count(V)) {
        haiku[cnt] = is_haiku(V);
//        seqs.push_back(V);
        mp[V] = cnt++;
    }
    return mp[V];
}

void add(int &to, int x) {
    to += x;
    if(to >= mod) to -= mod;
}

string tostr(vector<int> &V) {
    string s;
    for(int x : V) s.push_back((char)(x + '0'));
    return s;
}

int main() {
    cin >> n >> x >> y >> z;
    int mn = max(x, max(y, z));
    vector<int> V;
    int id0 = get(V);
    for(int i = 1; i <= mn; i++) {
        vector<int> nV(1, i);
        nxt[id0][i] = get(nV);
    }
    for(int len = 1; len <= x + y + z; len++) {
        for(int i = 0; i < (1 << (len-1)); i++) {
            vector<int> sV;
            int cur = 1;
            bool bad = false;
            for(int j = 0; j < len - 1; j++) {
                if((i >> j) & 1) {
                    if(cur > mn) {
                        bad = true;
                        break;
                    }
                    sV.push_back(cur), cur = 1;
                }
                else cur++;
            }
            if(cur > mn || bad) continue;
            sV.push_back(cur);

            int c_id = get(sV);
            for(int k = 1; k <= mn; k++) {
                vector<int> nV;
                nV.push_back(k);
                nV.insert(nV.end(), sV.begin(), sV.end());
                int sum = len + k;
                while(!nV.empty() && sum > x + y + z) {
                    sum -= nV.back();
                    nV.pop_back();
                }
                nxt[c_id][k] = get(nV);
            }
        }
    }

//    for(int i = 0; i < cnt; i++) {
//        for(int x : seqs[i]) cout << x << " ";
//        cout << endl;
//    }

//    cout << cnt << endl;
//    for(int i = 0; i < cnt; i++) {
//        cout << "nxts of " << tostr(seqs[i]) << endl;
//        if(haiku[i]) cout << "is haiku" << endl;
//        for(int k = 1; k <= 10; k++) cout << k << ": " << tostr(seqs[nxt[i][k]]) << endl;
//    }

    dp[0][0] = 1;
    for(int i = 1; i <= n; i++) {
        for(int j = 0; j < cnt; j++) {
            if(haiku[j]) continue;
            for(int k = 1; k <= 10; k++) add(dp[i][nxt[j][k]], dp[i-1][j]);
        }
    }
    ll res = 1;
    for(int i = 0; i < n; i++) res *= 10, res %= mod;
    int f_res = 0;
    for(int i = 0; i < cnt; i++) if(!haiku[i]) add(f_res, dp[n][i]);
    cout << (res - f_res + mod) % mod << endl;

}


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

int main() {
set<ll> S;
  for(ll i=0;i<3;i++) {
ll A;
    cin>>A;
    S.insert(A);
  }

  if(S.size()==2) {
cout<<"Yes"<<endl;
  }
  else {
cout<<"No"<<endl;
  }
}


#include <bits/stdc++.h>
#include <string>
#include <vector>
#include <algorithm>
#include <math.h>
#include <iostream>
#include <numeric>
#define rep(i,n) for (int i = 0;i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int,int>;
#define chmax(x,y) x = max(x,y)
int INF = 1e9;

int main(){
	int n;
	ll x;cin >> n >> x;
	vector<ll> a(n);
	rep(i,n) cin >> a[i];
	ll ans = 0;
	if(a[0]>x) {
		ans += a[0]-x;
		a[0] = x;
	}
	rep(i,n-1){
		//cout << " " << ans << endl;
		if(a[i]+a[i+1]>x){
			ans += a[i+1] - (x-a[i]);
			a[i+1] = x-a[i];
		}
	}
	//rep(i,n) cout << a[i] << endl;
	cout << ans << endl;
}

#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <cstdio>
#include <cmath>
#include <tuple>
#include <iomanip>
#include <numeric>
#include <unordered_map>
#include <sstream>
#include<limits.h>
#include<float.h>
#include<list>
#include <array>
#include <complex>
#include<stdio.h>
#include<string.h>
#include <bitset>
// #include<assert.h>
#include<random>
using namespace std;
#define int long long

#define I32_MAX 2147483647
#define I64_MAX 9223372036854775807LL
#define I64_MAX2 1223372036854775807LL
#define INF I64_MAX2
#define MOD 1000000007
// #define MOD 998244353
#define MEM_SIZE 10000
#define DEBUG_OUT true
#define ALL(x) (x).begin(), (x).end()

template<typename T> void DEBUG(T e){if(DEBUG_OUT == false)return; std::cout << e <<" ";}
template<typename T> void DEBUG(const std::vector<T>& v){if(DEBUG_OUT == false)return;for(const auto& e : v){std::cout<< e << " "; } std::cout << std::endl;}
template<typename T> void DEBUG(const std::vector<std::vector<T> >& vv){if(DEBUG_OUT == false)return;for(const auto& v : vv){ DEBUG(v); } }
template<class T,class... Ts> void DEBUG(T d, Ts... e){if(DEBUG_OUT == false)return;DEBUG(d);DEBUG(e...);}
template <class T> void corner(bool flg, T hoge) {if (flg) {cout << hoge << endl; abort();}}
template< typename T1, typename T2 > inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template< typename T1, typename T2 > inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

void solve(void)
{
  int N; cin>>N;
  vector<int> vec (N,0);
  for (int i = 0; i < N; i++)
  {
     cin>>vec[i];
  }
  vector<int> data;
  data.push_back(-1*vec[0]);
  for (int i = 1; i < N; i++)
  {
    int X = -1*vec[i];
    auto itr = upper_bound(ALL(data),X);
    if(itr == data.end())
    {
      data.push_back(X);
    }
    else
    {
      *itr =X;
    }

  }
  cout<<data.size()<<endl;

  return;
}
int32_t main(int32_t argc, const char *argv[])
{
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  std::cout << std::fixed;
  std::cout << std::setprecision(11);
  solve();

  return 0;
}


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

vector<int> num = {0,2,5,5,4,5,6,3,7,6};
vector<char> nchar = {'0','1','2','3','4','5','6','7','8','9'};

bool check(string s,string t){
    int n1 = s.size(), n2 = t.size();
    if(n1 > n2) return false;
    if(n1 < n2) return true;
    if(s > t) return false;
    return true;
}

int main(){
    int n,m; cin >> n >> m;
    vector<string> dp;
    vector<int> match(m);
    rep(i,m) cin >> match[i];
    dp.push_back("");
    for(int i = 1;i <= n;++i){
        string s = "";
        rep(j,m) {
            if(num[match[j]] <= i) {
                if(dp[i-num[match[j]]] == "" && i != num[match[j]]) continue;
                if(check(s,dp[i-num[match[j]]]+nchar[match[j]])) s = dp[i-num[match[j]]]+nchar[match[j]];
            }
        }
        dp.push_back(s);
    }
    cout << dp[n] << endl;
}

#include<bits/stdc++.h>
#define fi first
#define se second
#define lc (x<<1)
#define rc (x<<1|1)
#define gc getchar()//(p1==p2&&(p2=(p1=buf)+fread(buf,1,size,stdin),p1==p2)?EOF:*p1++)
#define mk make_pair
#define pi pair<int,int>
#define pb push_back
#define IT iterator
#define SZ(a) ((int)a.size())
#define all(a) a.begin(),a.end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int N=4e5+10,size=1<<20,mod=998244353;
const ll inf=1e18;

template<class o> void qr(o &x) {
	char c=gc; x=0; int f=1;
	while(!isdigit(c)){if(c=='-')f=-1; c=gc;}
	while(isdigit(c)) x=x*10+c-'0',c=gc;
	x*=f;
}
template<class o> void qw(o x) {
	if(x/10) qw(x/10);
	putchar(x%10+'0');
}
template<class o> void pr1(o x) {
	if(x<0)x=-x,putchar('-');
	qw(x); putchar(' ');
}
template<class o> void pr2(o x) {
	if(x<0)x=-x,putchar('-');
	qw(x); puts("");
}

int n,dic[150],f[N*2][2],g[N*2][2];
ll ans=inf;
vector<pi> x[N],y[N];

int main() {
	qr(n);
	dic['U']=0;
	dic['D']=1;
	dic['R']=2;
	dic['L']=3;
	for(int i=1;i<=n;i++) {
		int a,b,c;qr(a); qr(b);
		char str[6]; scanf("%s",str);
		a++; b++; c=dic[str[0]];
		x[a].pb(mk(b,c));
		y[b].pb(mk(a,c));
	}
	for(int i=1;i<N;i++) {
		sort(all(x[i]));
		sort(all(y[i]));
		ll a,b;
		a=b=inf;
		for(auto j:x[i]) {
			if(j.se==0) a=j.fi;
			else if(j.se==1) b=j.fi;
			if(a<b) ans=min(ans,(b-a)*5);
		}
		a=b=inf;
		for(auto j:y[i]) {
			if(j.se==2) a=j.fi;
			else if(j.se==3) b=j.fi;
			if(a<b) ans=min(ans,(b-a)*5);
		}
	}
	for(int i=1;i<N;i++)
		for(auto j:x[i]) {
			int t=j.fi;
			if(j.se==2) {
				f[j.fi-i+N][0]=i;
				f[j.fi+i][1]=i;
			}
			else if(j.se==3) {
				int k=g[j.fi+i][1];
				if(k) ans=min(ans,(i-k)*10LL);
				k=g[j.fi-i+N][0];
				if(k) ans=min(ans,(i-k)*10LL);
			}
			else if(j.se==1) {
				int k=f[j.fi-i+N][0];
				if(k) ans=min(ans,(i-k)*10LL);
				g[j.fi+i][1]=i;
			}
			else if(j.se==0) {
				int k=f[j.fi+i][1];
				if(k) ans=min(ans,(i-k)*10LL);
				g[j.fi-i+N][0]=i;
			}
		}
	if(ans>inf/2) puts("SAFE");
	else pr2(ans);
	return 0;
}


#include <bits/stdc++.h>
#define loop2(i, s, n, a) for (int i = int(s); i < int(n); i += a)
#define loop(i, s, n) loop2(i, s, n, 1)
#define rep(i, n) loop(i, 0, n)

#define pb push_back
#define all(in) in.begin(),in.end()

using ll = long long;
using ull = unsigned long long;
using namespace std;

int main(){
    int n; cin >> n;
    vector<int> data(n); rep(i,n) cin >> data[i];

    vector<int> ans(1,-data[0]);

    for(int i = 1; i < n; i++){
        int k = upper_bound(all(ans),-data[i]) - ans.begin();
        if(k == ans.size()) ans.push_back(-data[i]);
        else ans[k] = -data[i];
    }
    cout << ans.size() << endl;
}

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

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

    cin >> x1 >> y1 >> x2 >> y2;
    printf("%lf\n", sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2)));

    return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
//#include <stack>
//#include <queue>
//#include <cstdio>
#include <cmath>
#include <iterator>
#include <map>
//#include <list>
#include <iomanip>

using namespace std;

#define IOS ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define prec std::cout << std::fixed << std::setprecision(15);
#define endl "\n"
#define MOD 1000000007
#define Int int64_t
#define PI 3.14159265358979
#define ssort(z) sort(z.begin(), z.end())
#define rsort(z) sort(z.rbegin(), z.rend())
#define eerase(z) z.erase(unique(z.begin(), z.end()), z.end())
#define ccnt(z, w) count(z.begin(), z.end(), w)
#define rep(i,a,n) for(Int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(Int (i)=(a); (i)<=(n); (i)++)
const int MAX_N = 1000000;
const Int MAX_N_Int = 1000000000000;

template <typename T>
void printV(const std::vector<T>& v, const char * const separator = " ")
{
    if(!v.empty())
    {
        std::copy(v.begin(),
                  --v.end(),
                  std::ostream_iterator<T>(std::cout, separator));
        std::cout << v.back() << "\n";
    }
}

bool isPrime(int num)
{
    if (num < 2) return false;
    else if (num == 2) return true;
    else if (num % 2 == 0) return false; // 偶数はあらかじめ除く

    double sqrtNum = sqrt(num);
    for (int i = 3; i <= sqrtNum; i += 2)
    {
        if (num % i == 0)
        {
            // 素数ではない
            return false;
        }
    }

    // 素数である
    return true;
}
Int gcd(Int a, Int b) {
    return b != 0 ? gcd(b, a % b) : a;
}

Int lcm(Int a, Int b) {
    return a / gcd(a, b) * b;
    //a*bは64bit integer overflow
}

int Max(int a, int b, int c) {
    int temp = max(a, b);
    return max(temp, c);
}

int Min(int a, int b, int c) {
    int temp = min(a, b);
    return min(temp, c);
}

bool integer(double num) {
    return floor(num) == num;
}

Int fact(int num) {
    if (num == 0)
        return 1;
    else
        return num * fact(num - 1);
}

Int yakusu(int n) {
    int cnt = 0;
    for (int i = 1; i * i <= n; i++) {
        if (n % i == 0) {
            cnt++;
            if (i * i != n) cnt++;
        }
    }
    return cnt;
}

Int fact_mod(Int n, Int mod) {
    Int f = 1; repq(i,2,n) f = f * (i % MOD) % MOD;
    return f;
}

// 繰り返し二乗法 (modの世界での累乗)
Int mod_pow(Int x, Int n, Int mod) {
    Int res = 1;
    while(n > 0) {
        if(n & 1) res = (res * x) % mod;
        x = (x * x) % mod;
        n >>= 1;
    }
    return res;
}

// 組み合わせ nCr を求める
Int combination_mod(Int n, Int r, Int mod) {
    if(r > n-r) r = n-r;
    if(r == 0) return 1;
    Int a = 1;
    rep(i, 0, r) a = a * ((n-i) % mod) % mod;
    Int b = mod_pow(fact_mod(r, mod), mod-2, mod);
    return (a % mod) * (b % mod) % mod;
}

Int cntZero(string s){
    int cnt=0;
    reverse(s.begin(),s.end());
    for (int i = 0; i < s.length(); ++i) {
        if(s[i]=='0')cnt++;
        else break;
    }
    return cnt;
}

int main() {
    //COMinit();
    IOS;
    prec;
    Int a,b,c,d,n,m,k,x,y=0,ans=0,ans1=0;
    string str,s="",t,u;
    Int h,w;
    cin>>h>>w>>n;
    Int cnt=0;
    rep(i,0,min(h,w)){
        ans+=max(h,w);
        cnt++;
        if(ans>=n){
            cout<<cnt<<endl;
            return 0;
        }
    }
    rep(i,0,max(h,w)){
        ans+=min(h,w);
        cnt++;
        if(ans>=n){
            cout<<cnt<<endl;
            return 0;
        }
    }

    cout<<endl;
    return 0;
}

#include<stdio.h>

int main()
{
	int n, m, k;
	scanf("%d %d %d", &n, &m, &k);
	int sum1 = k/n;
	int sum2 = k/m;
	int ctr =  0;
	int temp;
	if(sum1 > sum2){
		for(int i = 1; i <= 100; i++){
			temp = m * i;
			++ctr;
			if(temp >= k){
				break;
			}
		}
	}else{
		for(int i = 1; i <= 100; i++){
			temp = n * i;
			++ctr;
			if(temp >= k){
				break;
			}
		}
	}
	printf("%d\n", ctr);
}


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

const int MaxM = 110;
int N, M, A[ MaxM ], Sum, Num;

int main() {
	scanf( "%d%d", &N, &M );
	for( int i = 1; i <= M; ++i ) scanf( "%d", &A[ i ] );
	for( int i = 1; i <= M; ++i ) Sum += A[ i ];
	if( Sum != N ) {
		printf( "Impossible" );
		return 0;
	}
	for( int i = 1; i <= M; ++i )
		if( A[ i ] % 2 == 1 )
			++Num;
	if( Num > 2 ) {
		printf( "Impossible" );
		return 0;
	}
	if( Num == 0 ) {
		for( int i = 1; i <= M; ++i ) printf( "%d ", A[ i ] ); printf( "\n" );
		printf( "%d\n", M + 1 );
		printf( "%d ", A[ 1 ] - 1 );
		for( int i = 2; i <= M; ++i ) printf( "%d ", A[ i ] );
		printf( "%d\n", 1 );
		return 0;
	}
	if( Num == 2 ) {
		int a, b;
		for( int i = 1; i <= M; ++i )
			if( A[ i ] % 2 == 1 ) {
				a = A[ i ];
				break;
			}
		for( int i = M; i >= 1; --i )
			if( A[ i ] % 2 == 1 ) {
				b = A[ i ];
				break;
			}
		printf( "%d ", a );
		for( int i = 1; i <= M; ++i )
			if( A[ i ] % 2 == 0 )
				printf( "%d ", A[ i ] );
		printf( "%d\n", b );
		if( a > 1 ) {
			printf( "%d\n", M );
			printf( "%d ", a - 1 );
			for( int i = 1; i <= M; ++i )
				if( A[ i ] % 2 == 0 )
					printf( "%d ", A[ i ] );
			printf( "%d\n", b + 1 );
			return 0;
		}
		printf( "%d\n", M - 1 );
		for( int i = 1; i <= M; ++i )
			if( A[ i ] % 2 == 0 )
				printf( "%d ", A[ i ] );
		printf( "%d\n", b + 1 );
		return 0;
	}
	int t = 0;
	for( int i = 1; i <= M; ++i )
		if( A[ i ] % 2 == 1 ) {
			t = A[ i ];
			break;
		}
	printf( "%d ", t );
	for( int i = 1; i <= M; ++i )
		if( A[ i ] % 2 == 0 )
			printf( "%d ", A[ i ] );
	printf( "\n" );
	if( t > 1 ) {
		printf( "%d\n", M + 1 );
		printf( "%d ", t - 1 );
		for( int i = 1; i <= M; ++i )
			if( A[ i ] % 2 == 0 )
				printf( "%d ", A[ i ] );
		printf( "%d\n", 1 );
	} else {
		printf( "%d\n", M );
		for( int i = 1; i <= M; ++i )
			if( A[ i ] % 2 == 0 )
				printf( "%d ", A[ i ] );
		printf( "%d\n", 1 );
	}
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
typedef pair<ll,ll> l_l;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
typedef pair<l_l,ll> lll;
typedef vector<string> vs;
const ll dx[4]={1,-1,0,0};
const ll dy[4]={0,0,1,-1};
#define pb push_back
#define sz(x) (int)(x).size()
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
const int INF=1001001000;
const int mINF=-1001001000;
const ll LINF=1010010010010010000;
template<class T> inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template<class T> inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
int main(){
    ll n,k;cin>>n>>k;
    vl a(n);
    rep(i,n)cin>>a[i];
    rep(t,k){
        vl imos(n+1);
        rep(i,n){
            imos[max(i-a[i],0ll)]++;
            imos[min(i+a[i]+1,n)]--;
        }
        rep(i,n)imos[i+1]+=imos[i];
        imos.pop_back();
        if(imos==a)break;
        a=imos;
    }
    rep(i,n)cout<<a[i]<<" ";cout<<endl;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <array>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <list>
#include <numeric>
#include <stack>
#include <iomanip>
#include <random>
#include <complex>
#include <functional>
#include <tuple>
#include <new>

//#include "All.h"

using namespace std;

#define EPS (1e-9)
#define INF (1e9)
#define MOD (ll)(1e9 + 7)
#define PI (acos(-1))
#define REP(i,a,n) for(int i=a;i<n;i++)
#define rep(i, n) REP(i,0,n)
#define allof(a) (a).begin(), (a).end()

#define Yes(q) ((q) ? "Yes" : "No")
#define YES(q) ((q) ? "YES" : "NO")
#define Possible(q) ((q) ? "Possible" : "Impossible")
#define POSSIBLE(q) ((q) ? "POSSIBLE" ; "IMPOSSIBLE")

using ll = long long int;

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

ll beki(ll a, ll b) {
    ll tmp = 1;
    rep(i, b) tmp *= a;
    return tmp;
}

ll modPow(ll x, ll a) {
    if (a == 1) return x;
    if (a % 2) return (x * modPow(x, a - 1)) % MOD;
    ll t = modPow(x, a / 2);
    return ((t % MOD) * (t % MOD)) % MOD;
}

ll modInv(ll x) {
    return modPow(x, MOD - 2);
}

int main() {
    map<string, int> day_to_sun;
    string S;

    day_to_sun["SUN"] = 7;
    day_to_sun["MON"] = 6;
    day_to_sun["TUE"] = 5;
    day_to_sun["WED"] = 4;
    day_to_sun["THU"] = 3;
    day_to_sun["FRI"] = 2;
    day_to_sun["SAT"] = 1;

    cin >> S;
    cout << day_to_sun[S] << endl;
}

#include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i=0; i<n; i++)
#define rep1(i, n) for(int i=1; i<=n; i++)
#define repr(i, n) for(int i=n-1; i>=0; i--)
#define repr1(i, n) for(int i=n; i>=1; i--)
#define all(v) v.begin(),v.end()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const int INF = 1e9;
const ll LLINF = 1e18;
const ll MOD = 1e9+7;
const double EPS = 1e-10;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

int main() {
    int a[2];
    cin >> a[0] >> a[1];

    int ans = 0;
    rep(i, 2) {
        if (a[i] <= 3) ans += (4 - a[i]) * 100000;
    }
    if (a[0] == 1 && a[1] == 1) ans += 400000;
    cout << ans << endl;
}


#include<iostream>
using namespace std;
#include<string>
#include<strstream>
#include<sstream>
#include<algorithm>
#include<math.h>
bool issqr(int n)
{
	double d = sqrt(n);
	double x = floor(d);
	if (d - x == 0)
		return true;
	else
		return false;
}
int main()
{
	int n; cin >> n;

	for (int i = n; i >= 0; --i)
	{
		if (issqr(i) == true)
		{
			cout << i;
			break;
		}
	}
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define ff first
#define ss second
#define inf 1000000000
#define IOS ios_base::sync_with_stdio(0);cin.tie(0)
#define meM(y,a) memset(y,a,sizeof y)
#define sC(a) scanf("%d",&a)
#define alL(a) a.begin(),a.end()
#define prinT(a,sz)  cout<<a[0];for(int i=1;i<sz;i++)cout<<" "<<a[i];cout<<endl
#define ranD srand(chrono::steady_clock::now().time_since_epoch().count());
typedef pair<int,int>pi;
typedef pair<ll,ll>pll;
//int fx[]={0,0,1,-1};
//int fy[]={1,-1,0,0};
//int gx[]={0,0,1,1,1,-1,-1,-1};
//int gy[]={1,-1,0,1,-1,0,1,-1};
const int N=100010;

int main()
{
    IOS;
    string s;cin>>s;
    int n=s.length();
    int cnt=0;
    int i=0;
    int ss=0,t=0;
    while(i<n)
    {
//        cout<<"i = "<<i<<endl;
        while(s[i]=='S'){
            ss++,i++;
            if(i==n)break;
        }
        while(s[i]=='T'){
            i++,t++;
            if(i==n)break;
        }
        cnt+=min(ss,t);
        ss=max(ss-t,0);
        t=0;
    }
    cout<<n-cnt*2<<endl;
}
/*
sttssttsst
SSSSTTSSTTTS
*/


#include <bits/stdc++.h>
#define INF 1e9
#define endl '\n'
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define FORE(i, a, b) for (int i = (a), e = (b); i < e; ++i)
#define RFORE(i, a, b) for (int i = (b)-1, e = (a); i >= e; --i)
#define ALL(x) (x).begin(), (x).end()
#define SORT(c) sort((c).begin(), (c).end())
#define SORTR(c) sort((c).begin(), (c).end(), greater<int>())
using namespace std;
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int N;
    cin >> N;
    vector<int> L(N);
    for (int i = 0; i < N; i++) {
        cin >> L.at(i);
    }
    int ans = 0;
    FOR(i, 0, N - 2) {
        FOR(j, i + 1, N - 1) {
            FOR(k, j + 1, N) {
                int Li = L.at(i);
                int Lj = L.at(j);
                int Lk = L.at(k);
                if ((Li != Lj) & (Li != Lk) & (Lj != Lk) & (Li + Lj > Lk) &
                    (Lj + Lk > Li) & (Lk + Li > Lj)) {
                    ++ans;
                }
            }
        }
    }
    cout << ans << endl;
}

#include <bits/stdc++.h>

#define PI 3.14159265359
#define rep(i,a,n) for(int i=a;i<(int)n;++i)
#define SZ(x) ((int)(x).size())	//size() unsigned -> int
#define descSort(a) sort(a.begin(),a.end(),std::greater<int>())
using namespace std;
typedef long long ll;
const ll INF = 1e9 + 7;

int main() {
    int n;
    cin>>n;
    vector<ll> l(n);
    rep(i,0,n)cin>>l[i];

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

    int ans=0;
    rep(i,0,n){
        rep(j,i+1,n){
            rep(k,j+1,n){
                if(l[i]+l[j]<=l[k])continue;
                if(l[i]<l[j]&&l[j]<l[k]){ans++;}
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}


/**
 *    author:  tourist
 *    created: 06.07.2020 09:38:22
**/
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int x, y;
  cin >> x >> y;
  int ans = 0;
  if (x <= 3) {
    ans += (4 - x) * 100000;
  }
  if (y <= 3) {
    ans += (4 - y) * 100000;
  }
  if (x == 1 && y == 1) {
    ans += 400000;
  }
  cout << ans << '\n';
  return 0;
}

#include <bits/stdc++.h>
#define mset(a, b) memset(a, b, sizeof(a))
#define mcpy(a, b) memcpy(a, b, sizeof(a))
#define rg register
using namespace std;
typedef long long LL;
const int MAXN = 200005;
const LL INF = 0x3f3f3f3f3f3f3f3f;

template <typename T> inline void read(T &AKNOI) {
	T x = 0, flag = 1;
	char ch = getchar();
	while (!isdigit(ch)) {
		if (ch == '-') flag = -1;
		ch = getchar();
	}
	while (isdigit(ch)) {
		x = x * 10 + ch - '0';
		ch = getchar();
	}
	AKNOI = flag * x;
}

template <typename T> inline void cmin(T &x, T y) {
	if (x > y) x = y;
}

template <typename T> inline void cmax(T &x, T y) {
	if (x < y) x = y;
}

int n, q, A, B, p[MAXN];
LL csum[MAXN], dp[MAXN];

struct BIT {
	LL tr[MAXN];
	BIT() {
		mset(tr, 0x3f);
	}
	inline int lowbit(int x) {
		return x & (-x);
	}
	inline void update(int x, LL val) {
		for (int i=x;i<=n;i+=lowbit(i)) {
			cmin(tr[i], val);
		}
	}
	inline LL query(int x) {
		LL ret = INF;
		for (int i=x;i;i-=lowbit(i)) {
			cmin(ret, tr[i]);
		}
		return ret;
	}
}t1, t2;

void init() {
	read(n); read(q);
	read(A); read(B);
	for (int i=1;i<=q;++i) {
		read(p[i]);
	}
	for (int i=2;i<=q;++i) {
		csum[i] = csum[i-1] + abs(p[i]-p[i-1]);
	}
}

void solve() {
	dp[0] = min(abs(A-p[1]), abs(B-p[1]));
	t1.update(A, abs(B-p[1]) - A);
	t2.update(n-A+1, abs(B-p[1]) + A);
	t1.update(B, abs(A-p[1]) - B);
	t2.update(n-B+1, abs(A-p[1]) + B);
	for (int i=1;i<q;++i) {
		dp[i] = min( t1.query(p[i+1]) + csum[i] + p[i+1],
					 t2.query(n-p[i+1]+1) + csum[i] - p[i+1] );
		t1.update(p[i], dp[i] - csum[i+1] - p[i]);
		t2.update(n-p[i]+1, dp[i] - csum[i+1] + p[i]);
	}
	LL ans = INF;
	for (int i=0;i<q;++i) {
		cmin(ans, dp[i] + csum[q] - csum[i+1]);
	}
	printf("%lld\n", ans);
}

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


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

int main(){
    int N;
    cin >> N;
    vector<pair<int,int> > red(N);
    vector<pair<int,int> > blue(N);
    rep(i,N){
        cin >> red[i].second >> red[i].first;
    }
    rep(i,N){
        cin >> blue[i].first >> blue[i].second;
    }
    sort(all(red));
    sort(all(blue));
    int ans = 0;
    rep(i,N){
        int len = red.size();
        for (int j = len - 1; j >= 0; j--){
            if (blue[i].first > red[j].second && blue[i].second > red[j].first){
                ans++;
                red.erase(red.begin() + j);
                break;
            }
        }
    }
    cout << ans << endl;
}

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

struct EdgeMF
{
	int to;
	ll capacity;
	int reverse;
};

void addEdgeForFlow(vector<vector<EdgeMF>>& G, int from, int to, ll capacity, ll reverseCapacity)
{
	G[from].push_back({ to, capacity, (int)G[to].size() });
	G[to].push_back({ from, reverseCapacity, (int)G[from].size() - 1 });
}

ll dfsForFlow(vector<vector<EdgeMF>>& G, vector<bool>& used, int u, int t, ll f)
{
	if (u == t)
	{
		return f;
	}

	used[u] = true;
	for (auto& e : G[u])
	{
		if (used[e.to] || e.capacity <= 0)
		{
			continue;
		}

		auto d = dfsForFlow(G, used, e.to, t, min(f, e.capacity));
		if (d > 0)
		{
			e.capacity -= d;
			G[e.to][e.reverse].capacity += d;
			return d;
		}
	}

	return 0;
}

ll calcMaxFlow(vector<vector<EdgeMF>>& G, int s, int t)
{
	auto f = 0ll;
	while (true)
	{
		vector<bool> used((int)G.size());
		auto d = dfsForFlow(G, used, s, t, LLONG_MAX);
		if (d <= 0)
		{
			break;
		}
		f += d;
	}

	return f;
}

int main()
{
	int N;
	cin >> N;
	vector<int> x(2 * N);
	vector<int> y(2 * N);
	for (auto i = 0; i < 2 * N; ++i)
	{
		cin >> x[i] >> y[i];
	}

	vector<vector<EdgeMF>> G(2 * N + 2);
	for (auto i = 0; i < N; ++i)
	{
		for (auto j = N; j < 2 * N; ++j)
		{
			if (x[i] < x[j] && y[i] < y[j])
			{
				addEdgeForFlow(G, i, j, 1, 0);
			}
		}
	}
	int s = 2 * N;
	int t = s + 1;
	for (auto i = 0; i < N; ++i)
	{
		addEdgeForFlow(G, s, i, 1, 0);
		addEdgeForFlow(G, i + N, t, 1, 0);
	}

	cout << calcMaxFlow(G, s, t) << endl;

	return 0;
}


#include <iostream>
#include <bitset>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <stack>
#include <cmath>
#include <regex>
#include <iomanip>
#include <climits>
#include <utility>
#include <queue>
using namespace std;
using ll = long long int;
using dd = long double;
const ll MOD = 1e9 + 7;
const dd PI = 3.1415926435;

class UnionFind
{
public:
    UnionFind(int N) : par(N)
    {
        for (ll i = 0; i < N; i++)
        {
            par[i] = i;
        }
    }

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

    void unite(int x, int y)
    {
        int rx = root(x);
        int ry = root(y);
        if (rx == ry)
            return;
        par[rx] = ry;
    }

    bool same(int x, int y)
    {
        int rx = root(x);
        int ry = root(y);
        return rx == ry;
    }

private:
    vector<int> par;
};

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

ll fact(ll n)
{
    if (n == 0)
    {
        return 1;
    }
    if (n == 1)
    {
        return 1;
    }
    return n * fact(n - 1) % MOD;
}

ll pow_fast(ll n, ll k)
{
    if (k == 0)
    {
        return 1;
    }
    if (k == 1)
    {
        return n;
    }

    if (k % 2 == 0)
    {
        ll tmp = pow_fast(n, k / 2LL);
        return tmp * tmp % MOD;
    }
    else
    {
        ll tmp = pow_fast(n, k / 2LL);
        tmp *= tmp;
        tmp %= MOD;
        return tmp * n % MOD;
    }
}

map<ll, ll> sosuu(ll n)
{
    map<ll, ll> res;
    for (ll i = 2; i * i <= n; i++)
    {
        while (n % i == 0)
        {
            res[i] += 1;
            n /= i;
        }
    }
    if (n != 1)
    {
        res[n] += 1;
    }
    return res;
}

int main()
{
    string S;
    cin >> S;
    regex re("A[a-z]+C[a-z]+");
    if (regex_match(S, re)) {
        cout << "AC" << endl;
    } else {
        cout << "WA" << endl;
    }
}

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

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

    string res = "AC";
    int C_cnt = 0;
    if (s.at(0) == 'A') {
        for (int i = 1; i < s.size(); i++) {
            if (s.at(i) == 'C') {
                if (2 <= i && i <= (s.size()-2)) {
                    C_cnt++;
                } else {
                    res = "WA";
                }
            } else {
                if (!('a' <= s.at(i) && s.at(i) <= 'z')) res = "WA";
            }
        }
        if (C_cnt != 1) res = "WA";
    } else {
        res = "WA";
    }

    cout << res << endl;

    return 0;
}


#include<bits/stdc++.h>
#define watch(x) cout << (#x) << " is " << (x) << endl
#define endl "\n"
typedef long long ll;
using namespace std;
int static fast = [](){
  ios::sync_with_stdio(false);
  cin.tie(0); cout.tie(0); return 0;
}();
// freopen("input.txt", "r", stdin);

int main() {
    int n;
    string s, t;
    cin >> n >> s >> t;
    int overlap = 0;
    for(int i = 1; i <= n; i++) {
        if (s.substr(n-i, i) == t.substr(0, i)) {
            overlap = i;
        }

    }
    cout << n*2 - overlap << endl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
int main() {
    long long int N;
    string s,t;
    cin>>N>>s>>t;
    long long len1,len2,count=0;
    len1=s.length();
    len2=t.length();
if(len1==len2&&len1==N&&N>=1&&N<=100){
 if (N>=len1+len2){
        count=N;
 }
else {
    count=len1+len2;
        for(int i=0;i<=len1;i++){
            for(int j=1;j<=len2;j++){
                if(s.substr(i,len1)==t.substr(0,j)&&len1+len2-j>=N){
                    count=min(len1+len2-j,count);
                }
            }
        }
}
cout<<count;
}
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <iomanip>
using ll = long long;
using uint = unsigned int;
using graph = std::vector<std::vector<ll>>;
using wGraph = std::vector<std::vector<std::pair<ll,ll>>>;
#define rep(i,n) for (int i=0; i < int(n); i++)

using namespace std;
ll MOD7 = 1000000007;
ll MOD9 = 1000000009;
vector<ll> InputVec(ll N){
    vector<ll> A(N);
    for (int i = 0; i < N; ++i) {
        cin >> A[i];
    }
    return A;
}
void OutputVec(vector<ll> A){
    for (int i = 0; i < A.size(); ++i) {
        cout << A[i] << ",";
    }
    cout << endl;
}
vector<vector<ll>> InputVec2d(ll H,ll W){
    vector<vector<ll>> A(H);
    for (int yi = 0; yi < H; ++yi) {
        A[yi] = vector<ll>(W);
    }
    for (int yi = 0; yi < H; ++yi) {
        for (int xi = 0; xi < W; ++xi) {
            cin >> A[yi][xi];
        }
    }
    return A;
}
vector<vector<char>> InputVec2dStr(ll H,ll W){
    vector<vector<char>> A(H);
    for (int yi = 0; yi < H; ++yi) {
        A[yi] = vector<char>(W);
    }
    for (int yi = 0; yi < H; ++yi) {
        string S;
        cin >> S;
        for (int xi = 0; xi < W; ++xi) {
            A[yi][xi] = S[xi];
        }
    }
    return A;
}
void OutputVec2d(vector<vector<ll>> A){
    for (int yi = 0; yi < A.size(); ++yi) {
        for (int xi = 0; xi < A[yi].size(); ++xi) {
            cout << A[yi][xi] << ",";
        }
        cout << endl;
    }
}
graph InputGraph(ll N,ll M){
    graph G(N);
    for (int i = 0; i < M; ++i) {
        ll a,b;
        cin >> a >> b;
        a--;b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    return G;
}
graph InputDGraph(ll N,ll M){
    graph G(N);
    for (int i = 0; i < M; ++i) {
        ll a,b;
        cin >> a >> b;
        a--;b--;
        G[a].push_back(b);
    }
    return G;
}
void OutputGraph(graph G){
    for (int i = 0; i < G.size(); ++i) {
        cout << i << ":";
        for (int j = 0; j < G[i].size(); ++j) {
            cout << G[i][j]<< ",";
        }
        cout << endl;
    }
}
void OutputAnswerBool(bool b,bool isUpperCase = false){
    if(b){
        if(isUpperCase){
            cout << "YES" << endl;
        }else{
            cout << "Yes" << endl;
        }
    }else{
        if(isUpperCase){
            cout << "NO" << endl;
        }else{
            cout << "No" << endl;
        }
    }
}

int main() {
    string S;
    cin >> S;
    ll w;
    cin >> w;
    for (int i = 0; i < S.size(); i += w) {
        cout << S[i];
    }
    cout << endl;
}

#include <iostream>
using namespace std;

void call(int n);
void judge1(int a);
void judge2(int b);
void judge3(int c);

int main() {
	int input;
	cin>>input;
	call(input);
	return 0;
}

void call(int n) {
	int i;
	for (i = 1; i <= n; i++) {
		judge1(i);
	}
	cout << endl;
}

void judge1(int a) {
	if (a % 3 == 0) cout << " " << a;
	else judge2(a);
}

void judge2(int b) {
	if (b % 10 == 3) cout << " " << b;
	else judge3(b);
}

void judge3(int c) {
	int temp = 0;
	while (1) {
		if (temp == 0) temp = c;
		temp /= 10;
		if (temp) {
			if (temp % 10 == 3) {
				cout << " " << c;
				break;
			}
		}
		else break;
	}
}


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

int main() {
  int n,m;
  int sum = 0; cin >> n;
  m = n;

  for(int i = 0; n > 0; ++i){
    sum += n%10;
    n /= 10;
  }
  if(m%sum==0) cout << "Yes" << endl;
  else cout << "No" << endl;
}

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
const int INF = 1000000009;
const ll LINF = 1e18;

int cal_digit(ll n) {
    int rtn = 0;
    while (n) {
        ++rtn;
        n /= 10;
    }
    return rtn;
}

int main() {
    ll n;
    cin >> n;
    int rtn = INF;
    for (ll a = 1; a * a <= n; ++a) {
        if (n % a == 0) {
            ll b = n / a;
            int t_dig = max(cal_digit(a), cal_digit(b));
            rtn = min(rtn, t_dig);
        }
    }
    cout << rtn << endl;
}


#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <iomanip>
#include <queue>
#include <deque>
#include <map>
#include <unordered_map>
#define rep(i,n) for(int i=0;i<n;i++)
#define repn(i,n) for(int i=1;i<=n;i++)
#define repr(e,x) for(auto& e:x)
using namespace std;
typedef long long ll;

map<string,int> m;
int memo[99999];
int N;
string s;

void bs(int start,int end){
    //cout<<"aaa1"<<endl;
    int v1,v2,vc;
    int c=(start+end)/2;

    //cout<<"aaa2"<<endl;
    if(memo[start]==-1){
        cout<<start<<'\n'<<flush;
        cin>>s;
        if(m[s]==2){
            return;
        }
        v1=memo[start]=m[s];
    }else{
        v1=memo[start];
    }

    //cout<<"aaa3"<<endl;
    if(memo[end]==-1){
        cout<<end<<'\n'<<flush;
        cin>>s;
        if(m[s]==2){
            return;
        }
        v2=memo[end]=m[s];
    }else{
        v2=memo[end];
    }

    //cout<<"aaa5"<<endl;
    if(memo[c]==-1){
        cout<<c<<'\n'<<flush;
        cin>>s;
        if(m[s]==2){
            return;
        }
        vc=memo[c]=m[s];
    }else{
        vc=memo[c];
    }

    //cout<<"aaa6"<<endl;
    if((c-start)%2==1 && v1==vc){
        bs(start,c);
    }else if((c-start)%2==0 && v1!=vc){
        bs(start,c);
    }else if((end-c)%2==1 && v2==vc){
        bs(c,end);
    }else if((end-c)%2==0 && v2!=vc){
        bs(c,end);
    }

}

int main(){
    m["Male"]=0;
    m["Female"]=1;
    m["Vacant"]=2;
    fill(memo,memo+99999,-1);
    cin>>N;

    bs(0,N-1);
}

#include<bits/stdc++.h>
//ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//clock_t start=clock();clock_t end=clock();cout<<(double)(end-start)/CLOCKS_PER_SEC<<endl;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef pair<int,int> pii;
typedef pair<pii,int> ppii;
typedef pair<int,pii> pipi;
typedef pair<ll,ll> pll;
typedef pair<pll,ll> ppll;
typedef pair<ll,pll> plpl;
typedef tuple<ll,ll,ll> tl;
typedef pair<double,double> pdd;
typedef vector<vector<ll>> mat;
ll mod=1000000007;
ll mod2=998244353;
ll mod3=1000003;
ll mod4=998244853;
ll mod5=1000000009;
ll inf=1LL<<60;
double pi=3.141592653589793238462643383279L;
double eps=1e-14;
#define rep(i,m,n) for(ll i=m;i<n;i++)
#define rrep(i,n,m) for(ll i=n;i>=m;i--)
#define srep(itr,st) for(auto itr=st.begin();itr!=st.end();itr++)
#define mrep(itr,mp) for(auto itr=mp.begin();itr!=mp.end();itr++)
#define Max(a,b) a=max(a,b)
#define Min(a,b) a=min(a,b)
int dh[4]={1,-1,0,0};
int dw[4]={0,0,1,-1};
int ddh[8]={-1,-1,-1,0,0,1,1,1};
int ddw[8]={-1,0,1,-1,1,-1,0,1};
ll gcd(ll a,ll b){
    if(a<b)swap(a,b);
    if(b==0)return a;
    if(a%b==0)return b;
    return gcd(b,a%b);
}
ll Pow(ll n,ll k){
    ll ret=1;
    ll now=n;
    while(k>0){
        if(k&1)ret*=now;
        now*=now;
        k/=2;
    }
    return ret;
}
ll beki(ll n,ll k,ll md){
  ll ret=1;
  ll now=n;
  while(k>0){
    if(k%2==1){
      ret*=now;
      ret%=md;
    }
    now*=now;
    now%=md;
    k/=2;
  }
  return ret;
}
ll gyaku(ll n,ll md){
  return beki(n,md-2,md);
}
ll popcount(ll n){
    ll ret=0;
    ll u=n;
    while(u>0){
        ret+=u%2;
        u/=2;

    }
    return ret;
}
int main(){
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int n;cin>>n;
    cout<<0<<endl;
    string s;cin>>s;
    if(s=="Vacant")return 0;
    int r=n,l=0;
    while(r-l>1){
        int mid=(l+r)/2;
        cout<<mid<<endl;
        string t;cin>>t;
        if(t[0]=='V')return 0;
        if(t==s){
            if(mid%2==0)l=mid;
            else r=mid;
        }
        else{
            if(mid%2==0)r=mid;
            else l=mid;
        }
    }

}


#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <string>
#include <bitset>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <sstream>
#include <stack>
#include <iomanip>
#include <assert.h>
using namespace std;
#define pb push_back
#define mp make_pair
typedef pair<int,int> pii;
typedef long long ll;
typedef double ld;
typedef vector<int> vi;
#define fi first
#define se second
#define fe first
#define FO(x) {freopen(#x".in","r",stdin);freopen(#x".out","w",stdout);}
#define Edg int M=0,fst[SZ],vb[SZ],nxt[SZ];void ad_de(int a,int b){++M;nxt[M]=fst[a];fst[a]=M;vb[M]=b;}void adde(int a,int b){ad_de(a,b);ad_de(b,a);}
#define Edgc int M=0,fst[SZ],vb[SZ],nxt[SZ],vc[SZ];void ad_de(int a,int b,int c){++M;nxt[M]=fst[a];fst[a]=M;vb[M]=b;vc[M]=c;}void adde(int a,int b,int c){ad_de(a,b,c);ad_de(b,a,c);}
#define es(x,e) (int e=fst[x];e;e=nxt[e])
#define esb(x,e,b) (int e=fst[x],b=vb[e];e;e=nxt[e],b=vb[e])
#define SZ 666666
int n,MOD;
ll fac[SZ],rfac[SZ];
ll qp(ll a,ll b)
{
	ll x=1; a%=MOD;
	while(b)
	{
		if(b&1) x=x*a%MOD;
		a=a*a%MOD; b>>=1;
	}
	return x;
}
ll rw[4];
ll C(int a,int b)
{
	return fac[a]*rfac[a-b]%MOD*rfac[b]%MOD;
}
ll f[6005][2005*6];
int main()
{
	cin>>n>>MOD;
	fac[0]=1;
	for(int i=1;i<SZ;++i)
		fac[i]=fac[i-1]*i%MOD;
	rfac[SZ-1]=qp(fac[SZ-1],MOD-2);
	for(int i=SZ-1;i;--i)
		rfac[i-1]=rfac[i]*i%MOD;
	int N=n*3;
	f[0][N]=1;
	for(int i=0;i<N;++i)
		for(int j=0;j<=N*2;++j) if(f[i][j])
			for(int s=1;s<=3&&i+s<=N;++s)
				(f[i+s][j+(s==1)-(s==2)]+=f[i][j]*fac[i+s-1]%MOD*rfac[i])%=MOD;
	ll aa=0;
	for(int j=N;j<=N*2;++j)
		(aa+=f[N][j])%=MOD;
	aa=(aa%MOD+MOD)%MOD;
	cout<<aa<<"\n";
}

#include<bits/stdc++.h>
using namespace std;
template<typename T>inline T read(){
	T f=0,x=0;char c=getchar();
	while(!isdigit(c)) f=c=='-',c=getchar();
	while(isdigit(c)) x=x*10+c-48,c=getchar();
	return f?-x:x;
}
namespace run{
	const int N=6009;int mod;
	inline int add(int x,int y){return x+y>=mod?x-mod+y:x+y;}
	int f[N][(N<<1)+3],n;
	int main(){
		n=read<int>()*3,mod=read<int>();
		f[0][N]=1;
		for(int i=0;i<n;i++)
			for(int j=-i;j<=i;j++){
				f[i+1][j+1+N]=add(f[i+1][j+1+N],f[i][j+N]);
				f[i+2][j-1+N]=add(f[i+2][j-1+N],1LL*f[i][j+N]*(i+1)%mod);
				f[i+3][j+N]=add(f[i+3][j+N],1LL*f[i][j+N]*(i+1)%mod*(i+2)%mod);
			}

		int ans=0;
		for(int j=0;j<=n;j++) ans=add(ans,f[n][j+N]);
		printf("%d\n",ans);
		return 0;
	}
}
int main(){
#ifdef my
	freopen(".in","r",stdin);
	freopen(".out","w",stdout);
#endif
	return run::main();
}


#include <bits/stdc++.h>
using namespace std;
#define ALL(x) (x).begin(),(x).end()
#define COUT(x) cout<<(x)<<"\n"
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define REP(i, n) for(int i=0;i<n;i++)
#define YES(x) cout<<(x?"YES":"NO")<<"\n"
#define Yes(x) cout<<(x?"Yes":"No")<<"\n"
#define dump(x) cout<<#x<<" = "<<(x)<<"\n"
#define endl "\n"
using G = vector<vector<int>>;
using M = map<int,int>;
using P = pair<int,int>;
using PQ = priority_queue<int>;
using PQG = priority_queue<int,vector<int>,greater<int>>;
using V = vector<int>;
using ll = long long;
using edge = struct { int to; int cost; };
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
const int INF = 1e9;
const int MOD = 1e9+7;
const ll LINF = 1e18;

string s;
int k, ans = INF;
vector<int> l;

void dfs(string t = "") {
  if (s.size() <= t.size()) {
    if (stoi(s) <= stoi(t)) chmin(ans, stoi(t));
    if (s.size() < t.size()) return;
  }
  for (int v: l) dfs(t + (char)('0' + v));
}

int main() {
  IOS;
  cin >> s >> k;
  M m;
  REP(i,k) {
    int d; cin >> d;
    m[d]++;
  }

  REP(i,10) if (m[i] == 0) l.push_back(i);
  dfs();
  COUT(ans);

  return 0;
}


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

#define IOS ios :: sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define AfterDecimal(n) cout << fixed << setprecision(n);
#define ll long long int
#define ld long double
#define all(a) a.begin(), a.end()
#define pii pair<int, int>
#define pll pair<ll, ll>
#define bits(x) __builtin_popcount(x)

using namespace __gnu_pbds;
using namespace std;

// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimization ("unroll-loops")

//template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

typedef tree<int, null_type, less<int>, rb_tree_tag,
            tree_order_statistics_node_update>
    ordered_set;

#ifdef LOCAL
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template<typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cout << "# "<< name << " = " << arg1 << '\n';
}
template<typename Arg1, typename... Args>
void __f(const char* names, Arg1 && arg1, Args &&... args){
        const char* comma = strchr(names + 1 ,',');
        cout << "# ";
        cout.write(names, comma - names)<< " = " << arg1 << " | ";
        __f(comma + 1, args...);
}
#else
#define debug(...) 36
#endif

std::mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int ri(int a, int b) {
    return uniform_int_distribution<int>(a, b)(rng);
}

ll power(ll x, ll y, ll p){
   ll res = 1;
   x = x % p;
   while (y > 0) {
      if (y & 1)
         res = (res * x) % p;
      y = y >> 1;
      x = (x * x) % p;
   }
   return res;
}

const ll INF = (ll)(8e18);
const ll MOD = (ll)(1e9 + 7);
const ll HMOD = (ll)4222234727;
const ll Hashprime = (ll)31;

const int maxn = 100100;

int32_t main(){
    IOS;
    #ifndef ONLINE_JUDGE
    //  freopen("input.txt", "r", stdin);
    //  freopen("output.txt", "w", stdout);
    #endif
    int t = 1; // cin >> t;
    while(t--){
      string s; cin >> s;
      string temp[] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};
      for(int i = 0; i < 14; i++){
         if(temp[i] == s){
            for(int j = i + 1; j < 14; j++){
               if(temp[j] == "SUN"){
                  cout << j - i << '\n';
                  return 0;
               }
            }
         }
      }
    }
    #ifdef LOCAL
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
    #endif
}
//g++ -DLOCAL -std=c++17 -Wshadow -Wall -o "c" "comp.cpp" -fsanitize=address -fsanitize=undefined -D_GLIBCXX_DEBUG -g

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

int main() {
	int R;
	cin >> R;
	string S;
	if (R < 1200) S = "ABC";
	else if (R < 2800) S = "ARC";
	else  S = "AGC";
	cout << S << endl;
	return 0;
}


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

// ----------- define --------------
#define int long long
#define vi vector<int>
#define vc vector<char>
#define ii pair<int,int>
#define fi first
#define sc second
#define all(x) (x).begin(),(x).end()
#define get_bit(x, k) ((x >> k) & 1)
// ---------------------------------

void MAIN() {
  string s;
  cin >> s;
  int ans = s.size(), cnt = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'S') cnt++;
    else {
      if (cnt) {
        ans -= 2;
        cnt--;
      }
    }
  }
  cout << ans << '\n';
}

signed main() {
  ios::sync_with_stdio(0); cin.tie(0);
  int T = 1;
  // cin >> T;
  while (T--) MAIN();
}

#ifdef __GNUC__
#pragma GCC target("avx2")
#pragma GCC optimize ("O30")
#pragma GCC optimize ("tree-vectorize")
#pragma GCC optimize("unroll-loops")
#define iss std::cin
#endif
#include "bits/stdc++.h"
#define rep(i, times) for(ll i = 0; i < (times); ++i)
#define p(x) (cout << (x) << "\n")
using ll = long long; using ld = long double; using namespace std;

class Festival
{
	ll N{0};
	vector<vector<ll>> A;
	void inp( ) {
		iss >> N; A.resize(3, vector<ll>(N));
		for (auto &abc : A) {
			for (auto &a : abc)  iss >> a;
		}
		sort(begin(A[0]), end(A[0]));
		sort(begin(A[2]), end(A[2]));
	}
	void biSearch( ) {
		ll cnt{0};
		for (auto &b : A[1]) {
			cnt += (end(A[2]) - upper_bound(begin(A[2]), end(A[2]), b))
				* (lower_bound(begin(A[0]), end(A[0]), b) - begin(A[0]));
		}
		p(cnt);
	}
public:
	void procedures( ) {
		inp( );	biSearch( );
	}
};
int main( ) {
	cin.tie(0);	ios::sync_with_stdio(0);
	class Festival a; a.procedures( );
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
#include <cstdio>
#include <map>
#include <set>
#define LL long long
#define pb push_back
#define mp make_pair
#define INF 0x3f3f3f3f
#define Inf 1000000000000000000LL
#define F first
#define S second
using namespace std;
typedef pair<int,int>pii;
int main(){
	int n,m;
	cin>>n>>m;
	map<int,int>mm;
	multiset<int>ss;
	for(int i=0;i<m;i++)mm[i]=i;
	for(int i=0;i<m;i++)ss.insert(0);
	for(int i=1,add=1;i<=n;i++){
		int a,b,c,d;
		cin>>a>>b;
		c=-1;
		for(auto j=mm.lower_bound(--a);j!=mm.end()&&j->F<=b;mm.erase(j++)){
			d=j->F;
			c=max(c,mm[d]);
			ss.erase(ss.find(d-mm[d]));
		}
		if(c+1&&b<m)mm[b]=c,ss.insert(b-c);
		if(ss.size())cout<<*ss.begin()+add<<endl,add++;
		else puts("-1");
	}
}

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5+2;
int pre1[MAXN],pre2[MAXN];
int finds(int x,int *pre)
{
    return pre[x] == x? pre[x]:pre[x] = finds(pre[x],pre);
}
void join(int x,int y,int *pre)
{
    int px = finds(x,pre);
    int py = finds(y,pre);
    if( px!=py)
    {
        pre[px] = py;
    }
}
int main()
{
    int n,k,l;
    cin>>n>>k>>l;
    for(int i = 1;i<=n;i++)
        pre1[i] = i,pre2[i] = i;
    for(int i = 1;i<=k;i++)
    {
        int x,y;
        cin>>x>>y;
        join(x,y,pre1);
    }
    for(int i = 1;i<=l;i++)
    {
        int x,y;
        cin>>x>>y;
        join(x,y,pre2);
    }
    for(int i = 1;i<=n;i++)
    {
        finds(i,pre1);
        finds(i,pre2);
    }
    map<pair<int,int>,int> mp;
    for(int i = 1;i<=n;i++)
    {
        mp[make_pair(pre1[i],pre2[i])] ++;
    }
    for(int i = 1;i<=n;i++)
    {
        cout<<mp[make_pair(pre1[i],pre2[i])]<<" ";
    }
    cout<<endl;
}


#include <iostream>
#include <algorithm>
#include <iomanip>
#include <string>
#include <vector>
#include <math.h>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
const int MOD = 1e9+7;
const int MOOD = 998244353;
#define PI 3.14159265359
typedef long long ll;
using namespace std;

int dp[200000][11];

int main(){
	int d, g;
	cin >> d >> g;
	vector<int> p(d+1), c(d+1);
	g /= 100;
	for (int i = 0; i < 200000; i++){
		for (int j = 0; j <= 10; j++){
			dp[i][j] = MOD;
		}
	}

	for (int i = 1; i <= d; i++){
		cin >> p[i] >> c[i];
		c[i] /= 100;
	}
	dp[0][0] = 0;
	for (int i = 0; i <= g; i++){
		for (int j = 1; j <= d; j++){
			if (dp[i][j-1] != MOD){
				for (int k = 0; k < p[j]; k++){
					dp[i+j*k][j] = min(dp[i][j-1] + k, dp[i+j*k][j]);
				}
				dp[i+j*p[j]+c[j]][j] = min(dp[i][j-1] + p[j], dp[i+j*p[j]+c[j]][j]);
			}
		}
	}
	int ans = MOD;
	for (int i = g; i <= g+20000; i++){
		for (int j = 1; j <= d; j++){
			ans = min(ans, dp[i][j]);
		}
	}
	cout << ans << endl;
	return 0;
}

#include <bits/stdc++.h>
#define REP(i, e) for(int (i) = 0; (i) < (e); ++(i))
#define FOR(i, b, e) for(int (i) = (b); (i) < (e); ++(i))
#define ALL(c) (c).begin(), (c).end()
#define PRINT(x) cout << (x) << "\n"
using namespace std;
using ll = long long; using pint = pair<int, int>; using pll = pair<ll, ll>;
const long long MOD = 1000000007;

ll D, G, p[20], c[20];

ll solve(ll st){
    ll sum = 0, cnt = 0;
    REP(i, D){
        if(st & (1 << i)){
            sum += p[i] * 100 * (i + 1) + c[i];
            cnt += p[i];
        }
    }
    while(sum < G){
        for(int i = D - 1; i >= 0; i--){
            if(st & (1 << i)) continue;
            ll d = (G - sum + 100 * (i + 1) - 1) / (100 * (i + 1));
            if(d <= p[i]){
                cnt += d;
                return cnt;
            }
            cnt += p[i];
            sum += p[i] * 100 * (i + 1);
        }
    }
    return cnt;
}

signed main(){
    cin >> D >> G;
    REP(i, D) cin >> p[i] >> c[i];
    ll ans = INT64_MAX;
    REP(i, 1 << D){
        ans = min(ans, solve(i));
    }
    PRINT(ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mxn= 5e5+5;
#define mod 1000000007
#define endl '\n'
void rishabh(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}
vector<array<ll,2>> adj[mxn];
bool vis[mxn];
ll ans=0,w,check;
ll dfs(int i, ll temp, int j){
    vis[i]=true;
    ll sum=0;
    for(auto x : adj[i]){
        if(!vis[x[0]]){
            if(temp+x[0]>=check*j){
                while(x[1]>=check){
                    ans++;
                    x[1]=x[1]/2;
                }
            }
            ll k=dfs(x[0],temp+x[1],j+1);
            sum=sum+k-x[1];
            if(k>=check*j){
               while(x[1]>=check){
                    ans++;
                    x[1]=x[1]/2;
                }
            }
            sum=sum+x[1];
        }
    }
    return sum;
}
int main(){
    int n,m,q,u,v;
    cin>>n>>m>>q;
    map<int,int> a,b;
    u=v=INT_MAX;
    for(int i=1;i<=n;i++){
        int c;
        cin>>c;
        u=min(u,c);
        a[i]=c;
    }
    for(int i=1;i<=m;i++){
        int c;
        cin>>c;
        b[i]=c;
        v=min(c,v);
    }
    int  ans=INT_MAX;
    while(q--){
        int x,y,z;
        cin>>x>>y>>z;
        int  temp=a[x]+b[y]-z;
        ans=min(ans,temp);
    }
    cout<<min(v+u,ans);
}


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

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int h1,m1,h2,m2,k;
	cin>>h1>>m1>>h2>>m2>>k;
	cout<<(h2*60+m2)-(h1*60+m1)-k<<endl;
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

using usize = ::std::size_t;
using u64 = ::std::int_least64_t;
static constexpr u64 Inf = ::std::numeric_limits<u64>::max() / 2;

u64 N;
vector<u64> a, parent, visited;

void findpath(u64 i){
        if (parent[i] != -1) {
            findpath(parent[i]);
        }
        visited[i] = 1;
        cout << a[i] << endl;
}

bool cmp(u64 a, u64 b){
    return a >= b;
}

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

    cin.tie(0);
    ios::sync_with_stdio(false);

    cin >> N;
    vector<u64> M, indexer;
    a.resize(N), parent.resize(N, -1), visited.resize(N, 0);
    for (u64 i = 0; i < N; i++) {
        cin >> a[i];
    }
    M.push_back(a[0]);
    indexer.push_back(0);
    for (auto it = a.begin() + 1; it != a.end(); it++) {
        if (*it <= M.back()) {
            M.push_back(*it);
            parent[it - a.begin()] = indexer.back();
            indexer.push_back(it - a.begin());
        } else {
            auto location = lower_bound(M.begin(), M.end(), *it, cmp);
            *location = *it;
            if (location != M.begin()) {
                parent[it - a.begin()] = indexer[location - M.begin() - 1];
            }
            indexer[location - M.begin()] = it - a.begin();
        }
    }

    cout << M.size() << endl;
    //findpath(indexer.back());

    return 0;
}


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

bool dfs(int i, int nowcolor, vector<vector<int>> &way, vector<int> &color) {
  color.at(i) = nowcolor;
  for(int j = 0; j < way.at(i).size(); j++) {
    if(color.at(way.at(i).at(j)) == nowcolor) {
      return false;
    }
    else if(color.at(way.at(i).at(j)) == -1) {
      color.at(way.at(i).at(j)) = 1 - nowcolor;
      if(!dfs(way.at(i).at(j), 1 - nowcolor, way, color)) {
        return false;
      }
    }
  }
  return true;
}

int main() {
  int64_t N, M;
  cin >> N >> M;
  vector<vector<int>> way(N);
  for(int64_t i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    way.at(a - 1).push_back(b - 1);
    way.at(b - 1).push_back(a - 1);
  }
  vector<int> color(N, -1);
  if(!dfs(0, 0, way, color)) {
    cout << N * (N - 1) / 2 - M << endl;
  }
  else {
    int count = 0;
    for(int i = 0; i < N; i++) {
      if(color.at(i) == 1) {
        count++;
      }
    }
    cout << count * (N - count) - M << endl;
  }
}

#include <bits/stdc++.h>
using namespace std;
int main() {
    int N,M; cin >> N >> M;
    int ans = -1;
    vector<int> s(M);
    vector<char> c(M);
    for (int i=0;i<M;i++) cin >> s[i] >> c[i];
    for (int i = (N==1) ? 0 : pow(10, N-1);i<pow(10,N);i++){
        string x = to_string(i);
        bool can = true;
        for (int j=0;j<M;j++){
            if (x[s[j]-1]!=c[j]) can = false;
        }
        if (can) {
            ans = i;
            break;
        }
    }
    cout << ans << endl;
    return 0;
}


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

#define ll  long long
#define PII pair<ll,ll>
#define VI  vector<ll>
#define VB  vector<bool>
#define VC vector<char>
#define VVI vector<vector<ll>>
#define VVC vector<vector<char>>
#define VS  vector<string>
#define VP  vector<PII>
#define lf(i,a,b) for(ll i=a;i<=b;i++)
#define lr(i,a,b) for(ll i=a;i>=b;i--)
#define lfl(i,v) for(ll i=0;i<v.size();i++)
#define lrl(i,v) for(ll i=(ll)(v.size())-1;i>=0;i--)
#define chk2(a,b) cout<<"check :  "<<a<<" "<<b<<endl
#define chk3(a,b,c) cout<<"check :  "<<a<<" "<<b<<" "<<c<<endl
#define chk4(a,b,c,d) cout<<"check :  "<<a<<" "<<b<<" "<<c<<" "<<d<<endl
#define chk5(a,b,c,d,e) cout<<"check :  "<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<e<<endl
#define l_b(v,k)  lower_bound(v.begin(),v.end(),k)-v.begin()
#define u_b(v,k)  upper_bound(v.begin(),v.end(),k)-v.begin()
#define mod 1000000007
#define sort(v) sort(v.begin(),v.end())
#define p_b push_back
#define p_f push_front
#define m_p make_pair
#define fir first
#define sec second
#define sz size()
#define maxn 1<<17
ll n;
ll dp[maxn];
ll dpsmm[maxn];
ll valor[maxn];
VVI sub(maxn);
VVI mat;

void calcula(){

    for(int bitmask = 1;bitmask < (1 << n);bitmask++){
        for(int i = 0;i<n;i++){
            if(!(bitmask & (1 << i))) continue;
            for(int j = i+1;j<n;j++){
                if(!(bitmask & (1 << j))) continue;
                valor[bitmask] += mat[i][j];
            }
        }
    }

}

void fill_sub(ll ind,ll bm,ll subm)
    {   if(ind==n)
        {   if(__builtin_popcount(subm) == 0) return;
            sub[bm].p_b(subm);return ;
        }
        fill_sub(ind+1,bm,subm);
        fill_sub(ind+1,bm^(1<<ind),subm^(1<<ind));
        fill_sub(ind+1,bm^(1<<ind),subm);
    }
ll smm(ll bm)
    {   VI v1;
        if(dpsmm[bm]!=-1)
            return dpsmm[bm];
        lf(i,0,n-1)
        {   if(bm&(1<<i))
            {   v1.p_b(i);
            }
        }
        ll len=v1.size();
        ll sum=0;
        lfl(i,v1)
            lf(j,i+1,len-1)
                sum+=mat[v1[i]][v1[j]];
        return dpsmm[bm]=sum;
    }
ll fun(ll bm)
    {   if(bm==0)
        {   return 0;
        }
        if(dp[bm]!=-1)
            return dp[bm];
        //cout<<bm<<endl;
        ll maxi=0;
        lfl(i,sub[bm])
        {   maxi=max(maxi,valor[sub[bm][i]]+fun(bm^sub[bm][i]));
        }
        return dp[bm]=maxi;
    }
int main()
{   cin>>n;
    mat=VVI(n,VI(n,0));
    lf(i,0,n-1)
        lf(j,0,n-1)
            cin>>mat[i][j];
    memset(dp,-1,sizeof(dp));
    memset(dpsmm,-1,sizeof(dpsmm));
    fill_sub(0,0,0);
    calcula();
    cout<<fun((1<<n)-1);
}

//thuanqvbn03
#include <bits/stdc++.h>

using namespace std;

int n;
int a[16][16];
long long dp[1 << 16];

int GetBit(int num, int pos)
{
    return (num >> pos) & 1;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            cin >> a[i][j];
        }
    }
    int m = (1 << n) - 1;
    for (int i = 1; i <= m; i++)
    {
        dp[i] = 0;
        for (int j = 0; j < n; j++)
        {
            if (GetBit(i, j))
            {
                for (int k = j + 1; k < n; k++)
                {
                    if (GetBit(i, k))
                    {
                        dp[i] += a[j][k];
                    }
                }
            }
        }
        int t = m / 2;
        for (int j = 1; j <= t; j = ((i & j) == j ? j + 1 : j + (j & -j)))
        {
            if ((i & j) == j)
            {
                dp[i] = max(dp[i], dp[j] + dp[i ^ j]);
            }
        }
    }
    cout << dp[m];
    return 0;
}


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

#define vec                vector<int>
#define vecll              vector<ll>
#define vecd               vector<double>
#define vecst              vector<string>
#define vecb               vector<bool>
#define vec2(var, n, m)    vector<vector<int>> var(n, vector<int>(m, 0))
#define vecb2(var, n, m)    vector<vector<bool>> var(n, vector<bool>(m, false))
#define vecll2(var, n, m)  vector<vector<ll>> var(n, vector<ll>(m, 0))

#define rep(i,n)         for(ll i=(ll)0; i<(ll)n; i++)
#define REP(i,m,n)       for(ll i=(ll)m; i<(ll)n; i++)
#define arr(var, n)      vec var(n); rep(i,n){cin >> var[i];}
#define arrll(var, n)    vecll var(n); rep(i,n){cin >> var[i];}
#define arrst(var, n)    vecst var(n); rep(i,n){cin >> var[i];}
#define all(var)         (var).begin(), (var).end()
#define sortall(var)     sort(all(var))
#define uniqueall(v)     v.erase(unique(v.begin(), v.end()), v.end());
#define f_sum(var)       accumulate(all(var), 0)
#define f_sumll(var)     accumulate(all(var), 0LL)
#define chmin(v1, v2)    v1 = min(v1, v2)
#define chmax(v1, v2)    v1 = max(v1, v2)
#define pb(var)          push_back(var)
#define prt(var)         cout << (var) << "\n"
#define prtd(n, var)     cout << fixed << setprecision(n) << (var) << "\n"
#define prtfill(n, var)  cout << setw(n) << setfill('0') << (var);
#define prt2(v1, v2)     cout << (v1) << " " << (v2) << "\n"
#define prt3(v1, v2, v3) cout << (v1) << " " << (v2) << " " << (v3) << "\n"
#define prtall(v)        rep(i,v.size()){cout<<v[i]<<(i!=v.size()-1?" ":"\n");}
void prtok(bool ok){prt(ok ? "Yes" : "No");}
//----------------------------------------------------------------
vector<int> make_divisors(ll x){
  vector<int> v;
  for(ll i=1; i*i<=x; i++){
    if(x%i==0){
      v.push_back(i);
      if(i!=x/i) v.push_back(x/i);
    }
  }
  sort(v.begin(), v.end());
  return v;
}

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

  vec v = make_divisors(m);
  rep(i,v.size()){
    if(m/v[v.size()-1-i]>=n){
      prt(v[v.size()-1-i]);
      return 0;
    }
  }
}


#include <bits/stdc++.h>
#include <iomanip>
#include <math.h>
#include <unistd.h>
//#include <stdio.h>
#define rep(i,x) for(ll i = 0;i<x;i++)
#define all(a) (a).begin(),(a).end()
using ll = long long;
using ld = long double;
using namespace std;
using dou = double;
const ll INF = 1LL << 60;
const ll mod = 1000000007;
typedef pair<ll,ll> P;
using graph = vector<vector<ll>>;
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;}
ll gcd(ll a,ll b){
    if(a < b)swap(a,b);
    if(a % b == 0)return b;
    else return gcd(b,a%b);
}
//素数判定O(sqrt(N))
bool isp(ll n){
    bool res = true;
    if(n == 1)return false;
    else{
        for(ll i = 2;i * i <= n;i++){
            if(n % i == 0){
                res = false;
                break;
            }
        }
        return res;
    }
}

ll fac[510000], finv[510000], inv[510000];

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

// 二項係数計算
ll COM(ll n, ll k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}
//nのm乗をMODで割ったあまりO(logN)
ll modpow(ll n,ll m,ll MOD){
    ll res = 1;
    while(m){
        if(m & 1)res = (res * n) % MOD;
        m >>= 1;
        n *= n;
        n %= MOD;
    }
    return res;
}

//printf("%.10f\n", N);
/*  vector<ll> vec = {1,2,5,6,8,9,10};
    cout << binary_search(all(vec),5) << endl; -> true*/
/*
    vector<ll> vec = {1,2,5,6,8,9,10};
    auto n = upper_bound(all(vec),4);
    cout << *n << endl; -> 5*/
/*
    cout << fixed << setprecision(15);
*/
int main(){
    ll N;
    cin >> N;
    vector<ll>num(N);
    rep(i,N)cin >> num[i];
    map<ll,ll> odd,even;
    rep(i,N){
        if(i % 2 == 0)odd[num[i]]++;
        else even[num[i]]++;
    }
    vector<P> od,ev;
    for(auto x : odd){
        od.push_back(P(x.second,x.first));
    }
    for(auto x : even){
        ev.push_back(P(x.second,x.first));
    }
    sort(all(od));sort(all(ev));reverse(all(od));reverse(all(ev));
    ll os = od.size(),es = ev.size();
    if(od[0].second != ev[0].second){
        cout << N - ev[0].first - od[0].first << endl;
    }
    else{
        if(os == 1 && es == 1){
            cout << N - max(od[0].first,ev[0].first) << endl;
        }
        else if(es == 1){
            cout << N - ev[0].first - od[1].first << endl;
        }
        else if(os == 1){
            cout << N - ev[1].first - od[0].first << endl;
        }
        else{
            cout << N - max(od[0].first + ev[1].first,od[1].first + ev[0].first) << endl;
        }
    }
}

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

// int main(){
//     ios::sync_with_stdio(0);
//     cin.tie(0);
//     cout.tie(0);
//     ll t=1;
//     //cin>>t;
//     while(t--){
//     	int n;
//     	cin>>n;
//     	double d;
//     	ll x;
//     	int num2,num5;
//     	map<pair<int,int>,int> num;
//     	for(int i=0;i<n;i++){
//     		cin>>d;
//     		x=llround(d*1e9);
//     		//cout<<x<<"\n";
//     		num2=0,num5=0;
//     		while(x%2==0) num2++,x/=2;
//     		//cout<<x<<"\n";
//     		while(x%5==0) num5++,x/=5;
//     		num[{num2,num5}]++;
//     	}
//     	int ans=0;
//     	for(auto p1:num){
//     		for(auto p2:num){
//     			if(p1.first.first+p2.first.first>=18 && p1.first.second+p2.first.second>=18){
//     				if(p1<p2) ans+= p1.second*p2.second;
//     				if(p1==p2)ans+=(p1.second*(p2.second-1))/2;
//     			}
//     		}
//     	}
//     	cout<<ans<<"\n";
//     }
// }

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

int num[19][19];

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ll t=1;
    //cin>>t;
    while(t--){
    	ll n;
    	cin>>n;
    	double d;
    	ll x,y=1000000000;
    	int num2,num5;
    	for(int i=0;i<=18;i++)
    		for(int j=0;j<=18;j++)
    			num[i][j]=0;
    	vector<pair<int,int>> vec;
    	for(int i=0;i<n;i++){
    		cin>>d;
    		x=llround(d*1e9);
    		//cout<<x<<"\n";
    		num2=0,num5=0;
    		while(x%2==0) num2++,x/=2;
    		//cout<<x<<"\n";
    		while(x%5==0) num5++,x/=5;
    		if(num2>18) num2=18;
    		if(num5>18) num5=18;
    		vec.push_back({num2,num5});
    		//cout<<num2<<" "<<num5<<"\n";
    		for(int j=0;j<=18;j++){
    			for(int k=0;k<=18;k++){
    				if(18-num2<=j && 18-num5<=k) num[j][k]++;
    			}
    		}
    	}

    	ll ans=0;
    	for(int i=0;i<n;i++){
    		num2=vec[i].first;
    		num5=vec[i].second;
    		for(int j=0;j<=18;j++){
    			for(int k=0;k<=18;k++){
    				if(18-num2<=j && 18-num5<=k) num[j][k]--;
    			}
    		}
    		ans+=num[num2][num5];
    	}
    	cout<<ans<<"\n";
    }
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
 long long i,j,k,a,b,c=0,n,t;
 cin>>n;
 long long A[n];
 for(i=0;i<n;i++)
 cin>>A[i];
 sort(A,A+n);
 for(i=0;i+2<n;i++)
 {
   for(j=i+1;j+1<n;j++)
   {
     for(k=j+1;k<n;k++)
     {
       if(A[i]!=A[j]&&A[j]!=A[k]&&A[k]!=A[i]&&A[i]+A[j]>A[k]) c++;
     }
   }
 }
 cout<<c<<endl;
return 0;
}

//#include"bits/stdc++.h"
#include<iostream>
#include<ostream>
#include<vector>
#include<string>
#include<algorithm>

#define FOR(i,a,b) for (ll i=(a),__last_##i=(b);i<__last_##i;i++)
#define RFOR(i,a,b) for (ll i=(b)-1,__last_##i=(a);i>=__last_##i;i--)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define __GET_MACRO3(_1, _2, _3, NAME, ...) NAME
#define rep(...) __GET_MACRO3(__VA_ARGS__, FOR, REP)(__VA_ARGS__)
#define rrep(...) __GET_MACRO3(__VA_ARGS__, RFOR, RREP)(__VA_ARGS__)

using namespace std;
using ll = long long;

void solve();

int main() {

   solve();
   return 0;

}

void solve() {

	ll n;
	cin >> n;
	REP(i, n) {

		ll score=0;
		ll runner=0;
		ll out=0;
		string in;

		while (out < 3) {
			cin >> in;
			if (in == "OUT") {
				out++;
			}
			else if (in == "HIT") {
				runner++;
				if (runner == 4) {
					score++;
					runner = 3;
				}
			}
			else if (in == "HOMERUN") {
				score += (runner + 1);
				runner = 0;
			}
		}
		cout << score << endl;
	}

}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
int main(){
  ll n;
  cin >> n;
  vector<ll> a(n),ev(100001),od(100001);
  rep(i,n){
    cin >> a.at(i);
    if(i%2==0){
      od.at(a.at(i))++;
    }else{
      ev.at(a.at(i))++;
    }
  }
  ll maxiev=0,maxiod=0,soeev,soeod;
  rep(i,100001){
    if(maxiev<ev.at(i)){
      maxiev=ev.at(i);
      soeev=i;
    }
    if(maxiod<od.at(i)){
      maxiod=od.at(i);
      soeod=i;
    }
  }
  if(soeev!=soeod){
    cout << n/2-maxiev+n/2-maxiod;
  }else{
    if(maxiev<maxiod){
      ll q=0;
      rep(i,100001){
        if(soeev!=i && q<ev.at(i)){
          q=ev.at(i);
        }
      }
      cout << n/2-maxiod+n/2-q;
    }else if(maxiev>maxiod){
      ll q=0;
      rep(i,100001){
        if(soeod!=i && q<od.at(i)){
          q=od.at(i);
        }
      }
      cout << n/2-maxiev+n/2-q;
    }else{
      ll p=0,q=0;
      rep(i,100001){
        if(soeev!=i && q<ev.at(i)){
          q=ev.at(i);
        }
      }
      rep(i,100001){
        if(soeod!=i && p<od.at(i)){
          p=od.at(i);
        }
      }
      ll ans=max(p,q);
      cout <<n/2-ans+n/2-maxiod;
    }
  }
}


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

int main()
{
	int n;

	while (cin >> n, n){
		long long e[5000] = {0}, p, q, t;
		bool out = false, f = false;
		vector<int> s;
		for (int i = 0; i < n; i++){
			cin >> t >> p >> q;
			for (int j = 0; j < s.size(); j++){
				if (s[j] == t){
				 	f = true;
					break;
				}
			}
			if (f == false){
				s.push_back(t);
			}
			e[t] += p * q;
		}
		for (int i = 0; i < s.size(); i++){
			if (e[s[i]] >= 1000000){
				cout << s[i] << endl;
			  out = true;
			}
		}
		if (out == false) cout << "NA" << endl;
	}
	return (0);
}

#include <iostream>
#include <string>

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

using namespace std;

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

void dfs(int x,int y)
{
	s[x][y] = '0';

	rep(i,4)
	{
		int nx = x + dx[i];
		int ny = y + dy[i];

		if(0 <= nx && nx < 12 && 0 <= ny && ny < 12)
		{
			if(s[nx][ny] == '1')
			{
				dfs(nx,ny);
			}
		}
	}
	return;
}

int main()
{
	while(cin >> s[0])
	{
		REP(i,1,12)
		{
			cin >> s[i];
		}

		int res = 0;
		rep(i,12)
		{
			rep(j,12)
			{
				if(s[i][j] == '1')
				{
					dfs(i,j);
					res++;
				}
			}
		}
		cout << res << endl;
	}
	return 0;
}


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

int main() {
	double x1, y1, x2, y2, num, ans = 0, dou = 1, xx;
	cin >> x1 >> y1 >> x2 >> y2;
	num = (x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1);
	while (dou < num) {
		dou *= 10;
	}
	xx = dou;
	for (int i = 0; i < 100; i++) {
		for (int j = 0; j < i; j++) {
			dou *= 0.1;
		}
		if (dou < 0.000001)break;
		for (int j = 0; j < 10; j++) {
			ans += dou;
			//cout << ans << " " << j << " " << dou << endl;
			if (ans*ans > num) {
				ans -= dou;
				break;
			}
		}
		dou = xx;
	}
	printf("%f\n", ans);
	//cout << ans;

	return 0;
}

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>

using namespace std;

int main()
{
    double x1, y1, x2, y2;
    scanf("%lf %lf %lf %lf", &x1, &y1, &x2, &y2);
    printf("%lf", sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2)));
}


#include <iostream>
using namespace std;

int f[102][102];

bool dfs(int ys,int xs,int yg,int xg,int c){
  if(f[ys][xs]!=c)return 0;

  if(ys==yg && xs==xg)return 1;;

  f[ys][xs]=0;

  bool flag=0;
  flag += dfs(ys+1,xs,yg,xg,c);
  flag += dfs(ys-1,xs,yg,xg,c);
  flag += dfs(ys,xs+1,yg,xg,c);
  flag += dfs(ys,xs-1,yg,xg,c);
  return flag;
}

void set(int c,bool d,int x,int y){
  if(d==0){
    for(int i=0; i<4; i++){
      f[y][x+i]=c;
      f[y+1][x+i]=c;
    }
  }else if(d==1){
    for(int i=0; i<4; i++){
      f[y+i][x]=c;
      f[y+i][x+1]=c;
    }
  }
}
;
int main(){
  while(1){

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

  int xs,ys;
  cin >> xs >> ys;
  int xg,yg;
  cin >> xg >> yg;
  int n;
  cin >> n;

  for(int i=0; i<n; i++){
    bool d;
    int c,x,y;
    cin >> c >> d >> x >> y;
    set(c,d,x,y);
  }

  if(dfs(ys,xs,yg,xg,f[ys][xs])){
    cout << "OK\n";
  }else{
    cout << "NG\n";
  }
  }

  return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, s, n) for(int i = s; i < (int)n; i++)
#define per(i, n) for(int i = n; i >= 0; i--)
#define ROF(i, s, n) for(int i = s; i >= (int)n; i--)
#define FORIT(i, A) for (auto i : A)
#define PRINT(x) cout << (x) << "\n"
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define MP make_pair
#define EACH(i, n) for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i)
#define SZ(a) int((a).size())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define CLR(a) memset((a), 0 ,sizeof(a))
#define NCLR(a) memset((a), -1 ,sizeof(a))
#define dump(x) cout << #x << " = " << (x) << "\n";
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << "\n";
#define sq(n) (n) * (n)
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef priority_queue<int> maxpq;
typedef priority_queue< int, vector<int>, greater<int> > minpq;
typedef complex<double> P;
static const double EPS = 1e-10;
static const double PI = acos( -1.0 );
static const int mod = 1000000007;
static const int INF = 1 << 29;
static const LL LL_INF = 1152921504606846976;
static const int dx[] = { -1, 0, 1, 0, 1, -1, 1, -1 };
static const int dy[] = { 0, -1, 0, 1, 1, 1, -1, -1 };

int n, m;
int cl[ 100 ][ 100 ];

int main() {
	queue<PII> q;
	set<PII> s;
	while ( ~scanf("%d %d", &n, &m) && n && m ) {
		CLR( cl );
		bool can = false;
		int sx, sy, gx, gy;
		scanf( "%d %d", &sx, &sy );
		--sx;
		--sy;
		scanf( "%d %d", &gx, &gy );
		--gx;
		--gy;
		int k;
		scanf( "%d", &k );
		for ( int i = 0; i < k; i++ ) {
			int c, d, x, y;
			scanf( "%d %d %d %d", &c, &d, &x, &y );
			--x;
			--y;
			if ( d ) {
				for ( int i = 0; i < 4; i++ ) {
					for ( int j = 0; j < 2; j++ ) {
						cl[ y + i ][ x + j ] = c;
					}
				}
			}
			else {
				for ( int i = 0; i < 2; i++ ) {
					for ( int j = 0; j < 4; j++ ) {
						cl[ y + i ][ x + j ] = c;
					}
				}
			}
		}
		q.push( MP( sx, sy ) );
		while ( q.size() ) {
			int x = q.front().first;
			int y = q.front().second;
			q.pop();
			if ( s.count( MP( x, y ) ) ) {
				continue;
			}
			s.insert( MP( x, y ) );
			if ( x == gx && y == gy ) {
				can = true;
				break;
			}
			for ( int i = 0; i < 4; i++ ) {
				if ( x + dx[ i ] < 0 || x + dx[ i ] == n || y + dy[ i ] < 0 || y + dy[ i ] == m || cl[ y ][ x ] != cl[ y + dy[ i ] ][ x + dx[ i ] ] ) {
					continue;
				}
				q.push( MP( x + dx[ i ], y + dy[ i ] ) );
			}
		}
		printf( "%s\n", can ? "OK" : "NG" );
		while ( q.size() ) {
			q.pop();
		}
		s.clear();
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
/* typedef */
typedef long long ll;
typedef pair<int, int> pii;
/* constant */
const int INF = 1 << 30;
const ll LINF = 1LL << 50;
const int NIL = -1;
const int MAX = 10000;
const int mod = 1000000007;
const double pi = 3.141592653589;
/* global variables */
/* function */
/* main */
int main(){
    int a, b, c;
    cin >> a >> b >> c;
    if (a > b) swap(a, b);
    if (a < c && c < b) cout << "Yes" << '\n';
    else cout << "No" << '\n';
}

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define range(i,a,b) ((a)<=(i) && (i)<(b))
#define debug(x) cout << #x << ' ' << '=' << ' ' << (x) << endl;
#define fs first
#define sc second
#define pb push_back
typedef long long ll;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> T;
typedef priority_queue<ll> PQI;
typedef priority_queue<P> PQP;
typedef vector<vector<P>> pvec;
typedef vector<vector<ll>> vvec;
typedef vector<ll> vec;
const vector<int> dx = {0, -1, 0, 1, 1, 1, -1, -1};
const vector<int> dy = {1, 0, -1, 0, 1, -1, 1, -1};
const int MOD = (1000000007);
// const int MOD = (998244353);
// const int INF = (1 << 30);
const int INF = (1LL << 60);
const double EPS = (1 >> 30);
template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b; return 1;} return 0;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b; return 1;} return 0;}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(15);
    //---------------------------------------------

    int a,b,k;
    cin>>a>>b>>k;
    rep(i,k){
        if(i%2==0){
        if(a%2==1){
            a--;
        }
        a/=2;
        b+=a;
        }
        else{
        if(b%2==1){
            b--;
        }
        b/=2;
        a+=b;
        }
    }
    cout<<a<<" "<<b<<endl;
}


#include<cstdio>
#include<algorithm>
using namespace std;
const int N=1001000;
int ii,i,j,k,l,n,m,ch,Last,ans;
char s[N],t[N],f[N];
struct cc { int x,l,r;} A[N];
int main() {
	scanf("%d",&n);
	scanf("%s",s+1);
	scanf("%s",t+1);
	for (i=1;i<=n;i++) if (s[i]!=t[i]) break;
	if (i>n) return puts("0"),0;
	Last=n;
	for (i=n;i;i--) {
		j=i;
		while (j && t[j-1]==t[i]) j--;
		k=min(j,Last);
		while (k && s[k]!=t[j]) k--;
		if (!k) return puts("-1"),0;
		A[k].x=k;
		A[k].l=j;A[k].r=i;
		Last=k-1;
		i=j;
	}
	for (i=1;i<=n;i++) if (A[i].x) A[++m]=A[i];
	ans=1;f[m]=1;j=m+1;
	for (i=m-1;i;i--) {
		while (j>i && A[j-1].x-(j-1-i)>=A[i].r) j--;
		if (j-i>ans) ans=j-i;
	}
	printf("%d\n",ans);
}

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

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

	vector<int> a(n);
	bool f_zero = false;
	int num_m = 0;
	long long s = 0;
	int m = 1e9;
	for(int i=0; i<n; i++){
		cin >> a[i];
		if(a[i]==0) f_zero = true;
		if(a[i]<0) num_m++;
		s += abs(a[i]);
		m = min(m,abs(a[i]));
	}

	if(f_zero || num_m%2==0) cout << s << endl;
	else cout << s-2*m << endl;
}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <math.h>

using namespace std;

struct Info{
	int number;
};

void strcpy(char* to,char* str){
	for(int i=0;str[i] != '\0';i++){
		to[i] = str[i];
		to[i+1] = '\0';
	}
}

//base??¨comp??????????????????????????¢??°
bool strCmp(char* base, char* comp){
	int length1 = 0,length2 = 0;
	for(int i=0;base[i] != '\0'; i++)length1++;
	for(int i=0;comp[i] != '\0'; i++)length2++;
	if(length1 != length2)return false;

	for(int i=0;base[i] != '\0'; i++){
		if(base[i] != comp[i])return false;
	}
	return true;
}

void func(int n,int k){

	Info info[5041];
	int index = 0,cards[10],tmp;

	for(int i = 0; i < n; i++)scanf("%d",&cards[i]);

	//k????????????????????¶?????°
	if(k == 2){

		for(int i = 0; i < n; i++){
			for(int k = 0; k < n; k++){
				if(i != k){
					//???????????°??????????????????
					if(cards[k] >= 10){
						tmp = 100*cards[i] + cards[k];
					}else{
						tmp = 10*cards[i] + cards[k];
					}

					if(index == 0){
						info[0].number = tmp;
						index++;
					}else{ //??¢??????????????????????????°??¨?????????????????????
						bool Found = false;
						for(int p = 0; p < index; p++){
							if(info[p].number == tmp){
								Found = true;
								break;
							}
						}
						if(!Found){ //?????¨???????????´???????????????
							info[index].number = tmp;
							index++;
						}
					}
				}
			}
		}

		printf("%d\n",index);
	}else if(k == 3){
		for(int i = 0; i < n; i++){
			for(int k = 0; k < n; k++){
				for(int a = 0; a < n; a++){
					if(i != k && i != a && k != a){
						//???????????°??????????????????
						if(cards[a] >= 10){
							tmp = 100*cards[k] + cards[a];
						}else{
							tmp = 10*cards[k] + cards[a];
						}

						if(tmp <= 99){ //2???
							tmp += 100*cards[i];
						}else if(tmp <= 999){ //3???
							tmp += 1000*cards[i];
						}else{ //4???
							tmp += 10000*cards[i];
						}

						if(index == 0){
							info[0].number = tmp;
							index++;
						}else{ //??¢??????????????????????????°??¨?????????????????????
							bool Found = false;
							for(int p = 0; p < index; p++){
								if(info[p].number == tmp){
									Found = true;
									break;
								}
							}
							if(!Found){ //?????¨???????????´???????????????
								info[index].number = tmp;
								index++;
							}
						}
					}
				}
			}
		}
		printf("%d\n",index);
	}else{ //k == 4
		for(int i = 0; i < n; i++){
			for(int k = 0; k < n; k++){
				for(int a = 0; a < n; a++){
					for(int b = 0; b < n; b++){
						if(i != k && i != a && i != b && k != a && k != b && a != b){
							//???????????°??????????????????
							if(cards[b] >= 10){
								tmp = 100*cards[a] + cards[b];
							}else{
								tmp = 10*cards[a] + cards[b];
							}

							if(tmp <= 99){ //2???
								tmp += 100*cards[k];
							}else if(tmp <= 999){ //3???
								tmp += 1000*cards[k];
							}else{ //4???
								tmp += 10000*cards[k];
							}

							if(tmp <= 999){ //3???
								tmp += 1000*cards[i];
							}else if(tmp <= 9999){ //4???
								tmp += 10000*cards[i];
							}else if(tmp <= 99999){ //5???
								tmp += 100000*cards[i];
							}else{ //6???
								tmp += 1000000*cards[i];
							}
							if(index == 0){
								info[0].number = tmp;
								index++;
							}else{ //??¢??????????????????????????°??¨?????????????????????
								bool Found = false;
								for(int p = 0; p < index; p++){
									if(info[p].number == tmp){
										Found = true;
										break;
									}
								}
								if(!Found){ //?????¨???????????´???????????????
									info[index].number = tmp;
									index++;
								}
							}
						}
					}
				}
			}
		}
		printf("%d\n",index);
	}
}

int main(){

	int n,k;

	while(true){
		scanf("%d %d",&n,&k);

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

		func(n,k);
	}

    return 0;
}

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cassert>
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<utility>
#include<numeric>
#include<algorithm>
#include<bitset>
#include<complex>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int,int> pint;
#define mp make_pair

template<class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; }
template<class T> void chmin(T &t, T f) { if (t > f) t = f; }
template<class T> void chmax(T &t, T f) { if (t < f) t = f; }
int in() { int x; scanf("%d", &x); return x; }

int cards[10];
set<int> nums;
int n;

void f(int m,int k,int used){
	if(k==0){
		nums.insert(m);
		return;
	}
	int i;
	for(i=0;i<n;i++){
		if(used&1<<i)continue;
		f(m*(cards[i]<10?10:100)+cards[i],k-1,used|1<<i);
	}
}

int main() {
	int k;
	while(n=in(),k=in()){
		nums.clear();
		int i;
		for(i=0;i<n;i++){
			cards[i]=in();
		}
		f(0,k,0);
		cout<<nums.size()<<endl;
	}
	return 0;
}


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

class crop{
public:
  string name;
  int price,seeding,youngL,L,flower,
    fruit,numFruit,fruitPrice,times;
  int StoL,LtoFruit,totaltime;
  double sum;
  crop();
  crop(string name,int price,int seeding,int youngL,
       int L,int flower,int fruit,
       int numFruit,int fruitPrice,
       int times):name(name),price(price),
		  seeding(seeding),youngL(youngL),L(L),
		  flower(flower),fruit(fruit),numFruit(numFruit),
		  fruitPrice(fruitPrice),times(times){
    this->StoL= seeding + youngL + L;
    this->LtoFruit = flower + fruit;
    this->totaltime=StoL + LtoFruit * times;
    this->sum = ((times * numFruit  * fruitPrice) - price) / (double)totaltime;
  }
  ~crop(){}

  bool operator<(const crop &a)const{
    if(this->sum != a.sum){
      return this->sum > a.sum;
    }else{
      return this->name < a.name;
    }
  }
};

void printCrop(crop &a){
  cout << "name:" << a.name << endl;
  cout << "price:" << a.price << " sum:" << a.sum <<
    " totaltime:"<< a.totaltime<< endl;
}
int main(){
  int n;
  while(cin>>n,n){
    vector<crop> C;
    for(int i=0;i<n;i++){
      string l;
      int p,a,b,c,d,e,f,s,m;
      cin>>l>>p>>a>>b>>c>>d>>e>>f>>s>>m;
      C.push_back(crop(l,p,a,b,c,d,e,f,s,m));
    }
    sort(C.begin(), C.end());

    for(int i=0;i<n;i++){
      //printCrop(C[i]);
      cout << C[i].name << endl;
    }
    cout << "#" << endl;
  }
  return 0;
}

#include <iostream>

using namespace std;

int main()
{
	int n;
	cin >> n;
	int k[100];
	char s[101];
	while (n != 0) {
		for (int i = 0; i < n; i++) cin >> k[i];
		cin >> s;
		int j = 0;
		for (int i = 0; s[i] != '\0'; i++) {
			if (s[i] >= 'a') s[i] -= 6;
			s[i] -= k[j];
			if (s[i] < 'A') s[i] += 52;
			j++;
			if (j == n) j = 0;
		}
		for (int i = 0; s[i] != '\0'; i++) {
			if (s[i] > 'Z') s[i] += 6;
		}

		cout << s << endl;
	cin >> n;
	}
	return 0;
}


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

#define fastio                  ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define input_output            freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);

#define ll                      long long
#define read1(x)                 cin>>x
#define read2(x,y)               cin>>x>>y
#define print(x)                cout<<x<<endl
#define pii                     pair<int, int>
#define pli                     pair<ll, int>
#define pil                     pair<int, ll>
#define pll                     pair<ll, ll>
#define vi                      vector<int>
#define vll                     vector<ll>
#define vb                      vector<bool>
#define vd                      vector<double>
#define vs                      vector<string>
#define ff                      first
#define ss                      second
#define pb                      push_back
#define ppb                     pop_back
#define pf                      push_front
#define ppf                     pop_front
#define vpii                    vector<pii>
#define umap                    unordered_map
#define uset                    unordered_set
#define clr(a,b)                memset(a,b,sizeof a)
#define all(ds)                 ds.begin(), ds.end()
#define rev(ds)                 ds.rbegin(), ds.rend()
#define asc(A)                  sort(A.begin(), A.end())
#define dsc(A)                  sort(A.begin(), A.end(), greater<int>())
#define gcd(x,y)                __gcd(x,y)
#define clr(a,b)                memset(a,b,sizeof a)
#define fr(i, n)                for(int i=0; i<n;++i)
#define fr1(i, n)               for(int i=1; i<=n; ++i)
#define rfr(i, n)               for(int i=n-1; i>=0; --i)
#define printVector(v)          for(int i= 0;i<v.size();i++) {cout<<v[i]<<" ";} cout<<endl;
#define precise(x)              cout<<fixed<<setprecision(x)
#define inf                     10000000000000

ll lcm(ll a, ll b) {return (a*b)/__gcd(a, b); }
ll power(ll x, ll y, ll m) { if(!y) return 1; ll p=power(x, y>>1, m)%m; p=p*p%m; return (y&1)?(x*p)%m:p;}
ll modInverse(ll a, ll m){ return power(a, m-2, m); }
bool isPrime(ll num){bool flag=true;for(ll i =2;i<=num/2;i++){if(num%i==0) {flag=false; break;} }return flag;}
ll sumofdigits(ll n){ll c=0;while(n>0){c++;n/=10;}return c;}
bool sortinrev(const pair<int,int>&a,const pair<int,int>&b) {return a.first > b.first; }
bool sortbysec(const pair<int,int> &a, const pair<int,int> &b) {return a.second < b.second; }

//const double PI= 2*acos(0.0);
const long long MOD= 1000000007;

void solve(){
    int n; cin>>n;
    vi v;
    fr(i, n){
        int x; cin>>x;
        v.push_back(x);
    }
    sort(all(v));
    if(v.size() <= 2){
        print(0);
        return;
    }
    int count = 0;
    for(int i = 0; i < n-2; ++i){
        for(int j = i+1; j < n-1; ++j){
            if(v[i] == v[j])continue;
            for(int k = j+1; k < n; ++k){
                if(v[i] == v[k] || v[j] == v[k])continue;
                if(v[i] + v[j] > v[k])count++;
            }
        }
    }
    cout<<count<<endl;

}
int main()
{
#ifndef ONLINE_JUDGE
    input_output
#else
    fastio
#endif

    int t =1 ;
    // cin>>t;
    while(t--){
        solve();
    }

    return 0;
}

#include "bits/stdc++.h"
using namespace std;
#ifndef LOCAL
#define endl '\n'
#endif

#define fr(i, a, b) for(int i = a; i <= b; i++)
#define rf(i, a, b) for(int i = a; i >= b; i--)
#define pf push_front
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()
#define lb lower_bound
#define ub upper_bound

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

int pct(int x) { return __builtin_popcount(x); }
int pct(ll x) { return __builtin_popcountll(x); }
int bt(int x) { return 31 - __builtin_clz(x); } // floor(log2(x))
int bt(ll x) { return 63 - __builtin_clzll(x); } // floor(log2(x))
int cdiv(int a, int b) { return a / b + !(a < 0 || a % b == 0); }
ll cdiv(ll a, ll b) { return a / b + !(a < 0 || a % b == 0); }
int nxt_C(int x) { int c = x & -x, r = x + c; return (((r ^ x) >> 2) / c) | r; }
ll nxt_C(ll x) { ll c = x & -x, r = x + c; return (((r ^ x) >> 2) / c) | r; }

vector<int> get_bits(int mask) {
	vector<int> bb;
	while(mask) { int b = bt(mask); bb.pb(b); mask ^= (1 << b); }
	reverse(all(bb));
	return bb;
}

int get_mask(vector<int> v) {
	int mask = 0;
	for(int x : v) { mask ^= (1 << x); }
	return mask;
}

template<typename T>
void uniq(vector<T> &v) { sort(all(v)); v.resize(unique(all(v)) - v.begin()); }

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

ll rand(ll l, ll r){
	uniform_int_distribution<ll> uid(l, r);
	return uid(rng);
}

void sc() {}

template <typename Head, typename... Tail>
void sc(Head &H, Tail &... T) { cin >> H; sc(T...); }

#ifdef LOCAL
#define debug(...) cerr << "[L:" << __LINE__ << "][" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

// #ifndef LOCAL
// string to_string(__int128 x) {
// 	string s = "";
// 	bool neg = 0;
// 	if(x < 0) { s += "-"; neg = 1; x = -x; }
// 	if(!x) s += '0';
// 	while(x) {
// 		int rem = x % 10;
// 		s += to_string(rem);
// 		x /= 10;
// 	}
// 	reverse(s.begin() + neg, s.end());
// 	return s;
// }
// #endif

const int mod = 1e9 + 7; // 998244353;

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

/*
	Lookout for overflows!!
	Check array sizes!!
	Clear before test cases!!
	Use the correct modulo!!
	Check for corner cases!!
	Are you forgetting something?!
	Read problem statement carefully!!!
*/

const int N = 1e5 + 5;
int dsu[N], sz[N];
bool odd[N], par[N];

int root(int u) {
    while(u != dsu[u]) u = dsu[u];
    return u;
}

int get_par(int u) {
    int p = 0;
    while(u != dsu[u]) {
        p ^= par[u];
        u = dsu[u];
    }
    return p;
}

void uni(int u,int v) {
    int uu = root(u), vv = root(v);
    if(uu == vv) {
        if(get_par(u) == get_par(v))
            odd[uu] = 1;
        return;
    }
    if(sz[uu] > sz[vv]) {
        swap(uu, vv), swap(u, v);
    }
    dsu[uu] = vv;
    sz[vv] += sz[uu];
    odd[vv] |= odd[uu];
    par[uu] = get_par(u) ^ get_par(v) ^ 1;
}

void solve() {
	int n, m;
    sc(n, m);
    fr(i, 1, n) {
        sz[i] = 1;
        dsu[i] = i;
        odd[i] = 0;
        par[i] = 0;
    }
    fr(i, 1, m) {
        int u, v;
        sc(u, v);
        uni(u, v);
    }
    int c1 = 0, c2 = 0, c3 = 0;
    fr(i, 1, n) {
        if(i == dsu[i]) {
            if(sz[i] == 1) {
                c1++;
            }
            else if(odd[i]) {
                c2++;
            }
            else {
                c3++;
            }
        }
    }
    ll ans = 0;
    while(c1) {
        ans += 2 * n - 1;
        n--;
        c1--;
    }
    while(c2) {
        ans += 2 * (c2 + c3) - 1;
        c2--;
    }
    ans += (c3 * 1LL * (c3 - 1)) * 2 + 2LL * c3;
    cout << ans;
}

int main() {
	ios :: sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int t = 1;
	// cin >> t;
	for(int tt = 1; tt <= t; tt++) {
		solve();
	}
	return 0;
}