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

using namespace std;

typedef double db;
typedef long long ll;
typedef long double ld;
typedef unsigned int ui;
typedef unsigned long long ull;

typedef pair < ll, ll > pll;
typedef pair < int, ll > pil;
typedef pair < ll, int > pli;
typedef pair < int, int > pii;

typedef pair < db, db > pdd;
typedef pair < db, ld > pdl;
typedef pair < ld, db > pld;
typedef pair < ld, ld > ldp;

#define F first
#define S second

#define en end()
#define bg being()

#define rev reverse
#define mp make_pair
#define pb push_back

#define y1 y0123456789
#define um unordered_map

#define all(x) x.bg, x.en
#define sz(x) (int)x.size()
#define len(x) (int)strlen(x)

#define sqr(x) ((x) * 1ll * (x))
#define sqrd(x) ((x) * 1.0 * (x))

#define forn(i, x) for (int i = 1; i <= x; i++)

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

const ll inf = (ll)1e18;
const ll mod = (ll)1e9 + 7;

const db eps = (db)1e-9;
const db pi = acos(-1.0);

const int N = 100500;

int n, m, ans, a[N], add[N], dp[2][N], t[2][N];

inline void upd(int pos, int tp, int val) {
    for (int i = pos; i <= 100000; i |= (i + 1))
        t[tp][i] = max(t[tp][i], val);
}

inline int get(int pos, int tp) {
    int res = 0;
    for (int i = pos; i >= 0; i = (i & (i + 1)) - 1)
        res = max(res, t[tp][i]);
    return res;
}

int main() {
    //freopen(".in", "r", stdin);
    //freopen(".out", "w", stdout);
    //freopen(".err", "w", stderr);

    //srand(time(NULL));

    cin.tie(NULL);
    cout.tie(NULL);
    ios_base::sync_with_stdio(false);

    //cout << setprecision(10) << fixed;

    cin >> n >> m;

    for (int i = 1; i <= n; i++) {
        int l, r;
        cin >> l >> r;
        add[l]++;
        add[r + 1]--;
    }

    int cur = 0;

    for (int i = 1; i <= m; i++) {
        cur += add[i];
        a[i] = cur;
    }

    for (int i = 1; i <= m; i++) {
        dp[0][i] = get(a[i], 0) + 1;
        upd(a[i], 0, dp[0][i]);
    }

    for (int i = m; i >= 1; i--) {
        dp[1][i] = get(a[i], 1) + 1;
        upd(a[i], 1, dp[1][i]);
    }

    for (int i = 1; i <= m; i++)
        ans = max(ans, dp[0][i] + dp[1][i] - 1);

    cout << ans;

    //cerr << (clock() + 0.0) / CLOCKS_PER_SEC;

    return 0;
}


#include<stdio.h>
int main()
{
	int a,b,c,d;
	scanf("%d%d%d%d",&a,&b,&c,&d);
	int temp;
	int i,sum=0;
	if(a>b)
	{
		temp=a;;
	    a=b;
		b=temp;
	}
	if(a>c)
	{
		temp=a;;
		a=c;
		c=temp;
	}
	if(b>c)
	{
		temp=b;;
		b=c;
		c=temp;
	}

     if(c-a>=2*d)
	{
		if(b-a>=d&&c-b>=d)
			sum=0;
		if(b-a<d)
		{
			for(i=1;b+i-a<d;i++);
			sum=i;
		}
		if(c-b<d)
		{
			for(i=1;c+i-b<d;i++);
		    sum=i;
		}
		printf("%d\n",sum);
	}
	else
	{
		if(b-a>=c-b)
		{
			for(i=1;c+i-b<d;i++);
				sum+=i;
			for(i=0;b+i-a<d;i++);
			    sum+=i;
			printf("%d\n",sum);
		}
		else if(b-a<c-b)
		{
			for(i=1;b+i-a<d;i++);
			    sum+=i;
			for(i=0;c+i-b<d;i++);
				sum+=i;
			printf("%d\n",sum);
		}
	}
	int x;
	scanf("%d",&x);
	return 0;
}


/**
 *       Compiler:  gcc
 *         Author:  bdep__
 *        Created:  03/27/2012 10:38:06 PM
 ***/

#include <cstring>
#include <cstdio>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#define N 1010
#define M 1000010
#define S 1000100
#define MOD 1000000007

typedef long long LL;
using namespace std;

inline int L(int i){ return i<<1; }
inline int R(int i){ return (i<<1)|1; }
inline int B(int i){ return 1<<i; }
inline int low_bit(int x){ return x&(-x); }
LL cnt[N],top;
LL tmp,mul;

bool must_win(LL a,LL b)
{
	if(a==0||b==0)
		return 0;
	if(a>b)
		tmp=a,a=b,b=tmp;
	if(must_win(b%a,a)==0)
		return 1;
	tmp=b;
	tmp/=a;
	tmp%=a+1;
	if(tmp&1)
		return 0;
	return 1;
}

int main()
{
	int t;
	LL a,b;
	cin>>t;
	while(t--)
	{
		cin>>a>>b;
		if(must_win(a,b))
			puts("First");
		else
			puts("Second");
	}
	return 0;
}


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

int k2, k3, k5, k6, temp;
unsigned long long int ans = 0;

int main()
{
	cin >> k2 >> k3 >> k5 >> k6;
	temp = k2;

	ans += (min(k2, min(k5, k6)) * 256);
	k2 -= min(k2, min(k5, k6));
	ans += ((k2 > 0) ? (min(k2, k3) * 32) : 0);

	cout << ans;

	return 0;
}


/*----------------by syr----------------*/
/*

         iii                  ii
     rBQBBBBBBE       BBR iBBBBQBBL     XBBBBBBQBBBBB
   iBBQJ    7BBB      BBsSBBr   BBQ      i      cBBB
  rBBU       iBBw     BBBQi     HBBi           KBBi
  BBH         BB5    iBBB       isL          wBB5
 GBB         iBBi    6BB                   iBBB
 BBQ         BQB     BBD                  QBBi
 BBB        BQB     iQBi                1BBv
 sBBg     wBBB      QBB               iBBB
  7BBBBBBBBBi       BBR              wBBBBBBBBBBBBB
     irvi           ii               ii    i i iii
                i5U
               BBB
               BB7
              1BB
      iPBBBBBKBBR    JBR1       rQBO   BR  UBQP  iBBQi
    7BBBGs7sXBBBi     QBBr     gBBE   rBB BB2BB7HBZQBB
   QBBi      sBQ       BBB   iBBB     SQBBR  BBBB  cBQ
  gBQ        BBg        BBB KBBi      MBBH   BBB   BBs
 iBBv       iBBi         QBBBL        BBR   pBB   iBB
 pBB        BBB         iBBBB        iBB    BBL   KBB
 MBB       BBBR        BBB JBBi      DBR   iBQ    BBL
 GBB     7BBBB2      PBBH   BBBi     BQr   DBB   iBB
  BQBXwgBBP BB7    1BBB      BBQ7   1BB    BBc   BBB
   2BBBBw   BB    EBBS        QBBi  HBa   iBB    BB7
*/
#include<bits/stdc++.h>

using namespace std;

#define PH push
#define MP make_pair
#define PB push_back
#define fst first
#define snd second
#define FOR(i, x, y) for(int i = (x); i < (y); ++i)
#define REP(i, x, y) for(int i = (x); i <= (y); ++i)
#define x0 x0123456789
#define y0 y0123456789
#define x1 x1234567890
#define y1 y1234567890
#define x2 x2345678901
#define y2 y2345678901
typedef double db;
typedef long long ll;
typedef long double ldb;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 1e9 + 9;
const int maxh = 35;

int n, h, m, ans;
int dp[2][238576];
int state[238577][4];
int match[maxh][maxh][maxh][maxh];

inline void add(int &x, int y){(x += y) -= (x >= INF ? INF : 0);}

int main(){
	scanf("%d%d", &n, &h);
	dp[0][0] = 1;
	int p = 0, q = 1;
	REP(i, 0, h + 1) REP(j, 0, h + 1) REP(k, 0, h + 1) REP(l, 0, h + 1) match[i][j][k][l] = 238575;
	REP(i, 0, h + 1) REP(j, 0, h + 1) REP(k, 0, h + 1) REP(l, 0, h + 1){
	   if((!(i % (h + 1)) || !(j % (h + 1)) || !(k % (h + 1)) || !(l % (h + 1))) && (i <= j && j <= k && k <= l)){
	       state[m][0] = i, state[m][1] = j, state[m][2] = k, state[m][3] = l;
	       match[i][j][k][l] = match[i][j][l][k] = match[i][l][j][k] = match[i][l][k][j] = match[i][k][j][l] = match[i][k][l][j] = m;
	       match[j][i][k][l] = match[j][i][l][k] = match[j][k][i][l] = match[j][k][l][i] = match[j][l][k][i] = match[j][l][i][l] = m;
	       match[k][i][j][l] = match[k][i][l][j] = match[k][j][i][l] = match[k][j][l][i] = match[k][l][i][j] = match[k][l][j][i] = m;
	       match[l][i][j][k] = match[l][i][k][j] = match[l][j][i][k] = match[l][j][k][i] = match[l][k][i][j] = match[l][k][j][i] = m++;
	   }
	}
	FOR(it, 0, n){
        FOR(s, 0, m) if(dp[p][s]){
			int a = min(state[s][0] + 1, h + 1),
				b = min(state[s][1] + 1, h + 1),
				c = min(state[s][2] + 1, h + 1),
				d = min(state[s][3] + 1, h + 1);
			int res = dp[p][s];
			add(dp[q][match[a == h + 1 ? h + 1 : 0][b][c][d]], res);
			add(dp[q][match[a][b == h + 1 ? h + 1 : 0][c][d]], res);
			add(dp[q][match[a][b][c == h + 1 ? h + 1 : 0][d]], res);
			add(dp[q][match[a][b][c][d == h + 1 ? h + 1 : 0]], res);
		}
		p ^= 1, q ^= 1;
		memset(dp[q], 0, sizeof(dp[q]));
	}
	FOR(i, 0, m) if(state[i][0] < h || state[i][1] < h || state[i][2] < h || state[i][3] < h)
		add(ans, dp[n & 1][i]);
	printf("%d\n", ans);
	return 0;
}


#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <cstring>

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;

#define SIZE(x) (int((x).size()))
#define rep(i,l,r) for (int i=(l); i<=(r); i++)
#define repd(i,r,l) for (int i=(r); i>=(l); i--)
#define rept(i,c) for (typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)

#ifndef ONLINE_JUDGE
#define debug(x) { cerr<<#x<<" = "<<(x)<<endl; }
#else
#define debug(x) {}
#endif

#define MD 1000000007

void su(int &a, int b)
{
	a+=b; if (a>=MD) a-=MD;
}

int bitcount(int x)
{
	int ret=0;
	while (x) x-=x&-x, ret++;
	return ret;
}

int tmask[7]={0,3,6,12,9,15,15};
int tcost[7]={0,1,1,1,1,2,2};
int C[10][10], g[16][13][16], all[16], cs[16][210][3], dp[210][210][16];

void lemon()
{
	int n,m; scanf("%d%d",&n,&m);
	C[0][0]=1;
	rep(i,1,5)
	{
		C[i][0]=C[i][i]=1;
		rep(j,1,i-1) C[i][j]=C[i-1][j-1]+C[i-1][j];
	}
	//dp[h][c][mask]
	//current height h, c triangles, covering section mask
	rep(mask,0,15)
		rep(ti,0,6)
			if ((mask&tmask[ti])==tmask[ti])
				rep(tk,0,15)
					if (((mask^tmask[ti])&tk)==tk)
					{
						int nmask=tmask[ti]|tk, cav=0;
						rep(i,0,3) if ((!(nmask&(1<<i))) && (!(nmask&(1<<((i+1)&3))))) cav|=(1<<i);
						rep(ci,0,15)
						{
							int cover=(ci|((ci&7)<<1)|(ci>>3));
							if ((cav&ci)==ci && (mask&cover)==cover)
							{
								int av[4], tv[4];
								rep(i,0,3) if (nmask&(1<<i)) av[i]=0; else av[i]=3;
								rep(i,0,3) if (ci&(1<<i)) av[i]--, av[(i+1)&3]--;
								rep(t0,0,av[0])
									rep(t1,0,av[1])
										rep(t2,0,av[2])
											rep(t3,0,av[3])
											{
												int cost=tcost[ti]+bitcount(tk)+bitcount(ci)+t0+t1+t2+t3;
												tv[0]=t0; tv[1]=t1; tv[2]=t2; tv[3]=t3;
												int gmask=0; rep(i,0,3) if (tv[i]) gmask|=(1<<i);
												if ((mask&gmask)!=gmask) continue;
												int co=1;
												rep(i,0,3) co=co*C[av[i]][tv[i]];
												rep(mmask,0,15)
													if ((mmask&nmask)==0 && (mmask|nmask|gmask|cover)==mask)
														su(g[mask][cost][mmask],co);
											}
							}
						}
					}

	memset(all,0,sizeof all);
	rep(mask,0,15)
		rep(cost,0,12)
			rep(mmask,0,15)
				if (g[mask][cost][mmask])
				{
					all[mask]++;
					cs[mask][all[mask]][0]=cost;
					cs[mask][all[mask]][1]=mmask;
					cs[mask][all[mask]][2]=g[mask][cost][mmask];
				}

	dp[0][0][0]=1;
	dp[0][1][1]=1; dp[0][1][2]=1; dp[0][1][4]=1; dp[0][1][8]=1;
	dp[0][1][3]=1; dp[0][1][6]=1; dp[0][1][12]=1; dp[0][1][9]=1;
	dp[0][2][3]=1; dp[0][2][5]=1; dp[0][2][9]=1;
	dp[0][2][6]=1; dp[0][2][10]=1; dp[0][2][12]=1;
	dp[0][2][14]=2; dp[0][2][13]=2; dp[0][2][11]=2; dp[0][2][7]=2; dp[0][2][15]=2;
	dp[0][3][14]=1; dp[0][3][13]=1; dp[0][3][11]=1; dp[0][3][7]=1; dp[0][3][15]=4;
	dp[0][4][15]=1;
	rep(h,1,n)
		rep(c,0,m)
			rep(mask,0,15)
				rep(k,1,all[mask])
					if (c>=cs[mask][k][0])
						su(dp[h][c][mask],LL(dp[h-1][c-cs[mask][k][0]][cs[mask][k][1]])*cs[mask][k][2]%MD);

	int s=0;
	rep(mask,0,15) su(s,dp[n][m][mask]);
	rep(i,1,m) s=LL(s)*i%MD;
	printf("%d\n",s);
}

int main()
{
	ios::sync_with_stdio(true);
	#ifndef ONLINE_JUDGE
		freopen("336E.in","r",stdin);
	#endif
	lemon();
	return 0;
}


#include <iostream>

using namespace std;

int main()
{
    int a,b,c,t;
    cin>>a>>b>>c;
    if(a > b)
    {
        t = a;
        a = b;
        b = t;

    }
    if(b > c)
    {
        t = c;
        c = b;
        b = t;

    }
    if((a+b) <= c)
    {
        cout<<((c-(a+b)) + 1);
    }
    else
    cout<< 0;
    return 0;
}


#include <bits/stdc++.h>

/*............................*/

#define uint uint64_t
#define mx 100001
#define ll long long int
#define ull unsigned long long int
#define pii pair<int, int>
#define pll pair<long long int, long long int>
#define mp make_pair
#define pb push_back
#define inf INT_MAX
#define pi acos(-1)
#define eps 1e-8
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) (a * b) / __gcd(a, b)
#define input(ara, n)           \
    for (int i = 0; i < n; i++) \
    scanf("%d", &ara[i])
#define inputl(ara, n)                    \
    for (long long int i = 0; i < n; i++) \
    scanf("%lld", &ara[i])
#define pf(a) printf("%d\n", a);
#define pfl(a) printf("%lld\n", a);
#define pfi(a) printf("%d ", a);
#define pfll(a) printf("%lld ", a);
#define sc(a) scanf("%d", &a)
#define scc(a, b) scanf("%d %d", &a, &b)
#define sccdbl(a, b) scanf("%lf %lf", &a, &b)
#define sccc(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define scccdbl(a, b, c) scanf("%lf %lf %lf", &a, &b, &c)
#define scl(a) scanf("%lld", &a)
#define sccl(a, b) scanf("%lld %lld", &a, &b)
#define scccl(a, b, c) scanf("%lld %lld %lld", &a, &b, &c)
using namespace std;
//int X[]={1,-1,0,0}; //co-ordinate move
//int Y[]={0,0,1,-1}; //co-ordinate move
//int X[]={1,1,2,2,-1,-1,-2,-2}; //Knights move
//int Y[]={2,-2,1,-1,2,-2,1,-1}; //Knights move
int main()
{
    int n, m;
    scc(n,m);
    if (m == 0 || m == 1)
    {
        pf(1);
    }
    else if (n == m)
    {
        pf(0);
    }
    else
    {
        n -= m;
        if (n >= m)
        {
            pf(m);
        }
        else
        {
            pf(n);
        }
    }

    return 0;
}


#include<bits/stdc++.h>
#define rep(i,k,n) for(ll i= (ll) k;i< (ll) n;i++)
#define all(v) (v).begin(), (v).end()
#define SZ(v) (int)((v).size())
#define pb push_back
#define ft first
#define sd second
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const long long INF = 1e18L + 1;
const int IINF = 1e9 + 1;

using namespace std;

template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;
  cerr<<'='<<h<<','; _dbg(sdbg+1, a...);
}

#ifdef LOCAL
#define DBG(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define DBG(...) (__VA_ARGS__)
#define cerr if(0)cout
#endif

const ll N = 50, M = N * N;

ll dp[M + 1][N + 1], r[N + 1], n, m, s, k;

int main()
{
#ifndef LOCAL
	ios_base::sync_with_stdio(0);
    cin.tie(0);
#endif
	rep(i, 0, M + 1){
		rep(j, 0, N + 1){
			dp[i][j] = IINF;
		}
	}
  	cin >> n >> s >> k;
	rep(i, 1, n + 1){
		cin >> r[i];
		m += r[i];
	}
	string colours; cin >> colours;
	rep(i, 1, n + 1){
		dp[r[i]][i] = min(dp[r[i]][i], abs(i - s));
	}
	ll res = IINF;
	rep(i, 1, m + 1){
		rep(j, 1, n + 1){
			rep(p, 1, n + 1){
				if(colours[p - 1] != colours[j - 1] and r[j] > r[p] and i >= r[j]){
					dp[i][j] = min(dp[i][j], dp[i - r[j]][p] + abs(j - p));
				}
			}
			DBG(i, j, dp[i][j]);
			if(i >= k){
				res = min(res, dp[i][j]);
			}
		}
	}
	if(res < IINF){
		cout << res << "\n";
	} else {
		cout << "-1\n";
	}
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
void File()
{
freopen("output.txt", "w", stdout);
freopen("input.txt", "r", stdin);
}

int countDivisors(long long  n){
int cnt = 0;
for (long long i = 1; i <= sqrt(n); i++) {
if (n % i == 0) {
// If divisors are equal,
// count only one
if (n / i == i)
    cnt++;

else // Otherwise count both
   cnt+=2;

}
}
return cnt;
}

typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<pair<int,int> > vii;
typedef map<int, int> mii;
typedef map<string, int> msi;
typedef map<long, long> mll;
typedef set<int> si;
typedef set<string> sst;
typedef set<long> sl;
typedef long long ll;
typedef pair<int, int> pii;
#define lp(i,n) for(int i = 0;i < int(n);i++)
#define rlp(i, n) for (int i = (n) - 1; i >= 0; i--)
#define rng(i,a,b) for(int i = int(a);i <= int(b);i++)
#define mp(x,y)  	make_pair((x),(y))
#define pb(x)    	push_back((x))
#define all(v)  	((v).begin()),((v).end())
#define sz(x)  		((int) (x).size())
#define setzero(a)	memset(a,0,sizeof(a))
#define bits(a) 	__builtin_popcountll(a)
#define fr first
#define sc second
#define el      	 "\n"
#define pii pair<int, int>
#define sortva(v) sort(all(v))
#define sortvd(v) sort(v.rbegin(),v.rend())
#define sortaa(a,n) sort(a,a+n)
#define sortad(a,n) sort(a,a+n),reverse(a,a+n)
#define sfi1(v) scanf("%d",&v)
#define sfi2(v1,v2) scanf("%d %d",&v1,&v2)
#define sfi3(v1,v2,v3) scanf("%d %d %d",&v1,&v2,&v3)
#define sfll1(v) scanf("%I64d",&v);
#define sfll2(v1,v2) scanf("%I64d %I64d",&v1,&v2)
#define sfll3(v1,v2,v3) scanf("%I64d %I64d %I64d",&v1,&v2,&v3)
#define sfstr(v) scanf("%s", v);
#define sfs1(v) cin>>v;
#define ndl puts("")
#define SS stringstream
#define rep(i, a, n)	for(int i = a; i<=n; i++)
#define ll long long
#define clr(x, y) memset(x, y, sizeof(x))
int dx[] = { 0, 0, 1, -1, 1, -1, 1, -1 };
int dy[] = { 1, -1, 0, 0, -1, 1, 1, -1 };

int const N = 6e5 + 9, MOD = 1e9+7 , oo = 1e9+7;
int vis[N];
int col[N];
int par[N];
vector<vector<int>>v(N);

vector<int> BFS(int s, vector<vector<int> > & adjList) {
	vector<int> len(sz(adjList), oo);
	queue<  int > q;
	q.push(s), len[s] = 0,par[s]=-1;

	int cur;
	while(!q.empty()) {
		int cur = q.front();	 q.pop();

		int n=adjList[cur].size();
		lp(i,n ) if (len[adjList[cur][i]] == oo)
			q.push(adjList[cur][i]), len[adjList[cur][i]] = len[cur]+1,par[adjList[cur][i]]=cur;
	}

	return len;	//cur is the furthest node from s with depth dep
}

int get(ll i){
int s=0;
while(i){
    s+=i%10;
    i/=10;
}
return s;
}

int main()
{
    ll n;
    cin>>n;
    ll x=sqrt(n);
    for (int i=1;i<=1e2;i++){
        if (x*x+get(x)*x==n)return cout<< x,0;
        x--;
    }

    cout<< -1;

    /*
    cin>>t;
    while(t--){
            m=oo;
        cin>>n;
        rng(i,1,n){
        sum=0;
        x=n/i;
        sum+=x*2+i*2;
        if (n%i)sum+=2;
    int h=(i*x)*2+(n%i)*2;
        sum+=h;
        m=min(m,sum);
        }
    cout<< m;
    if (t!=0)cout<< '\n';
    }

/*
*/

return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<vector>
#include<string>
#include<queue>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define ll long long
#define INF 0x3f3f3f3f
#define maxn 100000+5
#define eps 1e-12
#define mod 1000000007
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') {x=10*x+ch-'0';ch=getchar();}
    return x*f;
}
int a[105],vis[105];
int main()
{
   int n;
   n=read();
   mem(vis,0);
   for(int i=1;i<=n/2;++i) a[i]=read(),vis[a[i]]=1;
   sort(a+1,a+n/2+1);
   int sum1=0,sum2=0;
   for(int i=1;i<=n/2;++i)
   {
       if(a[i]%2)
       {
           for(int j=2;j<=n;j+=2)
           {
               if(vis[j]==0)
               {
                   sum1+=abs(a[i]-j);vis[j]=1; break; }
           }
       }
   }
   mem(vis,0);
   for(int i=1;i<=n/2;++i) vis[a[i]]=1;
   for(int i=1;i<=n/2;++i)
   {
       if(a[i]%2==0)
       {
           for(int j=1;j<=n;j+=2)
           {
               if(vis[j]==0)
               {
                   sum2+=abs(a[i]-j);vis[j]=1;break;
               }
           }
       }
   }
   //cout<<"sum1="<<sum1<<endl;
   //cout<<"sum2="<<sum2<<endl;
   cout<<min(sum1,sum2)<<endl;
}


// IN THE NAME OF ALLAH
#include <math.h>
#include <vector>
#include <set>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <queue>
#include <stack>
#include<cstring>
#include <cstdlib>
#include <string>
#include <ctime>
#include <cstdio>
#include <cmath>

#define int ll

#define F first
#define S second
#define mp make_pair
#define Left(x) (2*(x))
#define Right(x) ((2*(x))+1)
#define pii pair<int , int>

using namespace std;
typedef long long ll;

const int N = 2e6 + 5 , MOD = 2 , MAXN = 1e5 , LG = 20 , M = 1e6 + 5 , P = 727 , Sq = 100;
const ll inf = 1e18 + 10;

int f[N] , mark[N];
vector<int> v;

int32_t main(){
      string s , t;
      int tmp1 = 0 , tmp2 = 0;
      cin>>s>>t;
      string a[7] = {"monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"};
      for(int i = 1 ; i <= 7 ; i++)
            if(s == a[i])
                  tmp1 = i;
      for(int i = 1 ; i <= 7 ; i++)
            if(t == a[i])
                  tmp2 = i;
      int mm[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
      if(tmp2 == tmp1 || tmp2 == (tmp1 + 2) % 7 || tmp2 == (tmp1 + 3) % 7)
            return cout<<"YES" , 0;
      return cout<<"NO" , 0;
}


#include <iostream>

using namespace std;

int main()
{
   int n;
   cin>>n;
   int a=0;
   for(int i=1;;i++){
   int x=(i*(i+1))/2;
    a=a+x;
    if(a==n){
        cout<<i;
        break;
    }
    else if(a>n){
        cout<<i-1;
        break;
    }
   }
}


#include<stdio.h>
int main()
{
    int a,b,c;
    scanf("%d %d",&a,&b);
    if(a==0&&b==0)
       printf("NO");
       else
        {
    if(a>b)
        c=a-b;
    else c=b-a;
    if(c==1||c==0)
       printf("YES");
       else
        printf("NO");}
    return 0;
}


#pragma comment (linker, "/STACK:256000000")

#define _USE_MATH_DEFINES
#define _CRT_NO_DEPRECEATE
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <vector>
#include <utility>
#include <algorithm>
#include <functional>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <cmath>
#include <queue>
#include <memory.h>
#include <sstream>
#include <cassert>
#include <ctime>
#include <complex>
#include <random>

using namespace std;

typedef unsigned int uint32;
typedef long long int64;
typedef unsigned long long uint64;
typedef double ldouble;
typedef pair<int, int> pii;
typedef pair<int64, int64> pll;
typedef pair<pii, pii> piiii;

#define pb push_back
#define sq(x) ((x)*(x))
#define tmin(x,y,z) (min(min((x),(y)),(z)))
#define rand32() (((unsigned int)(rand()) << 16) | (unsigned int)(rand()))
#define rand64() (((unsigned int64)(rand32()) << 16) | (unsigned int64)(rand32()))
#define bit(mask, b) ((mask >> b) & 1)
#define biton(mask, bit) (mask | (((uint64)(1)) << bit))
#define bitoff(mask, bit) (mask & (~(((uint64)(1)) << bit)))
#define bitputon(mask, bit) (mask |= (((uint64)(1)) << bit))
#define bitputoff(mask, bit) (mask &= (~(((uint64)(1)) << bit)))
#define FAIL() (*((int*)(0)))++
#define INF ((int)(1e9) + 1337)
#define LINF ((int64)(1e18))
#define EPS (1e-7)
#define PI (3.1415926535897932384626433832795)
#define y1 yy1
#define y0 yy0
#define j0 jj0
#define MOD 1000000007LL
#define HMOD 1234567891LL

//#define HMOD ((int64)(1e18) + 3LL)
//#ifdef _MY_DEBUG
//#define HMOD 1000000007
//#endii
#define MAX 2000000000
#define HBASE 1000003
mt19937 ggen;

int64 x1, y1, x2, y2;

void solve()
{
    cin >> x1 >> y1 >> x2 >> y2;
    if ((x1 & 1) != (y1 & 1))
    {
        x1--;
        x2--;
    }
    if ((y1 & 1) == (y2 & 1))
    {
        int64 b = (y2 - y1) / 2;
        int64 ctr = (x2 - x1) / 2 + !((y1 & 1) ^ (x1 & 1));
        cout << b * (x2 - x1 + 1) + ctr;
    }
    else
    {
        int64 b = (y2 - y1) / 2 + 1;
        cout << b * (x2 - x1 + 1);
    }
}

int main()
{
    //testgen(1000, 1000);// return 0;
    ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef _MY_DEBUG
    freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout);
#else
    //freopen(TASK ".in", "rt", stdin); freopen(TASK ".out", "wt", stdout);
#endif
    //stresstest(); return 0;
    srand(1313);
    ggen = mt19937(13);

    solve();

    return 0;
}


#include <iostream>
using namespace std;
//Karswrmepl
int main ()
{
long long n;
cin>>n;
if (n%2){
	cout<<1;
}
else {
	cout<<2;
}
}


#include <bits/stdc++.h>
#ifndef M_PI
#define M_PI 3.14159265359
#endif // M_PI
#define endl "\n"
#define FOR(x, y, z) for (int x = (y); x < (z); ++x)
#define FORR(x, y, z) for (int x = (y); x > (z); --x)
#define GET(a, n) for (int __i = 0; __i < (n); ++__i) cin >> a[__i];
#define GETM(a, n, m) for (int __i = 0; __i < (n); ++__i) for (int __j = 0; __j < m; ++__j) cin >> a[__i][__j];
#define PRINTM(a, n, m) for (int __i = 0; __i < (n); ++__i) { for (int __j = 0; __j < m; ++__j) cout << a[__i][__j] << " ";  cout << endl; };
#define PRINT(a, n) for (int __i = 0; __i < (n); ++__i) cout << a[__i] << " ";
#define IT(a) a.begin(), a.end()
#define SQR(x) (x) * (x)
#define CASE(a, s) cout << "Case #" << a << ": " << s << endl;
#define DEB(a) cout << #a << " = " << (a) << endl; cout.flush();
#define DEBA(a) for (auto __i: a) cout << __i << " "; cout << endl; cout.flush();
#define IFDEB(b, a) if (b) { cout << #a << " = " << (a) << endl; cout.flush(); }
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector <int> VI;
typedef vector <vector <int>> VVI;
typedef pair <int, int> PII;
const int MOD = 1000000007;
template <class T> typename T::value_type arr_sum(const T& v, int n) { typename T::value_type sum = 0; FOR(i, 0, n) sum += v[i]; return sum; }
struct Sync_stdio { Sync_stdio() { cin.tie(NULL); ios_base::sync_with_stdio(false); } } _sync_stdio;

int gcd_ext(int a, int b, int &x, int &y) {
    if (a == 0) {
        x = 0; y = 1;
        return b;
    }
    int x1, y1;
    int d = gcd_ext(b % a, a, x1, y1);
    x = y1 - (b / a) * x1;
    y = x1;
    return d;
}

LL inv(LL t, LL mod)
{
    if (t == 0) {
        return 0;
    }
    int x;
    int y;
    gcd_ext(t, mod, x, y);
    return (x % mod + mod) % mod;
}

vector <LL> fact(10002);
vector <LL> inv_fact(10002);

int a(int n, int k)
{
    if (n < k) {
        return 0;
    }
    LL res = fact[n];
    res *= inv_fact[n - k];
    res %= MOD;
    return res;
}

void init_comb()
{
    fact[0] = 1;
    inv_fact[0] = 1;
    FOR (i, 1, fact.size()) {
        fact[i] = fact[i - 1] * i;
        fact[i] %= MOD;
        inv_fact[i] = inv(fact[i], MOD);
    }
}

int main()
{
    string s;
    cin >> s;
    int c = 0;
    int q = count(IT(s), '?');
    FOR (i, 'A', 'J' + 1) {
        c += count(IT(s), i) > 0;
    }
    init_comb();
    if ('0' <= s[0] && s[0] <= '9') {
        cout << a(10, c);
        FOR (i, 0, q) {
            cout << '0';
        }
        return 0;
    }
    if ('A' <= s[0] && s[0] <= 'J') {
        cout << a(9, c - 1) * 9;
        FOR (i, 0, q) {
            cout << '0';
        }
        return 0;
    }
    cout << a(10, c) * 9;
    FOR (i, 0, q - 1) {
        cout << '0';
    }
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <queue>
#include <set>
using namespace std;
#define eps 1e-8
#define rep(i,a,b) for(int i = (a); i < (b); ++i)
#define rrep(i,b,a) for(int i = (b); i >= (a); --i)
#define clr(a,x) memset(a,(x),sizeof(a))
#define mp make_pair
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ll long long
#define ld long double
const int maxn = 10;
const int inf = 1e9;
int L[maxn],R[maxn];
int id[maxn];

ll sum[10010];
int main()
{
//    Getinput(); return 0;
    #ifdef ACM
//        freopen("in.txt","r",stdin);
//        freopen("out.txt","w",stdout);
    #endif // ACM
    int n; cin >> n;
    rep(i,0,n) scanf("%d%d",L+i, R+i);
    rep(s,1,(1<<n)) {
        int l = 0, r = inf;
        vector<int> v[2];
        rep(j,0,n) {
            if (s & (1<<j)) {
                l = max(l, L[j]);
                r = min(r, R[j]);
                v[1].push_back(j);
            } else {
                v[0].push_back(j);
            }
        }
        rep(d, l , r + 1) {
            for(auto x : v[0]) {
                ll mul = max(0,R[x] - max(L[x]-1,d));
                if (mul <= 0) continue;
                for(auto y : v[0]) if (x != y) {
                    mul *= max(0, min(d,R[y]+1) - L[y]);
                }
                sum[d] += mul;
            }
        }
    }

    rep(s,1,(1<<n)) {
        int l = 0, r = inf;
        vector<int> v[2];
        rep(j,0,n) {
            if (s & (1<<j)) {
                l = max(l, L[j]);
                r = min(r, R[j]);
                v[1].push_back(j);
            } else {
                v[0].push_back(j);
            }
        }
        if (v[1].size() < 2) continue;
        rep(d, l , r + 1) {
            ll mul = 1;
            for(auto x : v[0]) mul *= max(0, min(d,R[x]+1) - L[x]);
            sum[d] += mul;
        }
    }
    ld ans = 0;
    ld fenmu = 1;
    rep(i,0,n) fenmu *= R[i] - L[i] + 1;
    rep(i,1,10001) ans += sum[i] / fenmu * (ld)i;
    printf("%.10f\n",(double)ans);
}


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

#define clr(i, j)     memset(i, j, sizeof(i))
#define pb            push_back

typedef long long     ll;

//-*-*-*-*-*-*-*-*-
ll sum,n,b; ll pre[100000]; ll pref[1000000]; ll pr[100000];
ll k[1000000];
 ll chek(ll x){
   clr(k,0);
    ll s=pref[x-1];ll j=x-1;
    ll d=0;
    while(s<b&&j<n){
       j++;
       //cout<<"l";
       //s=pref[j];

       k[d]++;ll dd=0;
       s+=max(pref[j]-pref[j-1]-k[d],dd);
       d++;
       d%=x;
    }
 //cout<<pref[4];
 return (s>=b);
 }
ll a[300000];
/*
 cin>>m>>s;//ll ans=0;
 set<char>hotset;
 ll k=0;
 for(int i=0;i<s.size();i++){
  hotset.insert(s[i]);
  k++;
   if(k==m){f+=s[0];s.clear();k=0;}
 } if(k){f+=s[0];}*/

int main()
{//solved in contest bitch newbie react 555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555

   vector<ll>v;

cin>>n>>b; ll d;
for(int i=0;i<n;i++){cin>>d;v.push_back(d);sum+=d;}
sort(v.rbegin(),v.rend());pref[0]=v[0];
for(int i=0;i<n;i++){ if(i>0){pref[i]=pref[i-1]+v[i];}}
ll x=1;ll y=n;ll ans;
if(pref[n-1]<b){cout<<-1;return 0;}
while(x<=y){
  // *** ...
   ll mid=(x+y)/2;
   if(chek(mid)){y=mid-1;ans=mid;}else{x=mid+1;}

}cout<<ans;
   }


#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
#pragma GCC optimize 03
#pragma GCC optimize("unroll-loops")

#include <iostream>
#include <iomanip>
#include <algorithm>
#include <iterator>
#include <cmath>
#include <ctime>
#include <vector>
#include <deque>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <random>
#include <numeric>
#include <unordered_set>
#include <unordered_map>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef std::pair<int, int> pii;
typedef std::pair<short, char> psc;
typedef unsigned long long ull;
typedef long long ll;
typedef long double lb;
typedef tree<int,
             null_type,
             less<int>,
             rb_tree_tag,
             tree_order_statistics_node_update> ordered_set;

#define fast ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define file_in freopen("input.txt", "r", stdin);
#define file_in_out freopen("commander.in", "r", stdin); freopen("commander.out", "w", stdout);
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second

template<typename T>
istream& operator>>(istream &in, vector<T> &v) {
    for (auto &it : v) {
        in >> it;
    }
    return in;
}

template<typename T>
ostream& operator<<(ostream &out, const vector<T> &v) {
    for (auto &it : v) {
        out << it << " ";
    }
    return out;
}

template<typename T1, typename T2>
istream& operator>>(istream &in, pair<T1, T2> &v) {
    in >> v.fi >> v.se;
    return in;
}

template<typename T1, typename T2>
ostream& operator<<(ostream &out, const pair<T1, T2> &v) {
    out << v.fi << " " << v.se;
    return out;
}

void solve() {
    int n, m;
    cin >> n >> m;
    vector<int> cnt(17);
    while (n) {
        cnt[n % m]++;
        if (cnt[n % m] > 1) {
            cout << "NO" << '\n';
            return;
        }
        n /= m;
    }
    cout << "YES" << '\n';
}

int main() {
    fast
    // file_in
    // file_in_out

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


#include <iostream>
#include <cmath>
#include <iomanip>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <deque>
#include <set>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <fstream>

#define pb push_back
#define mp make_pair
#define F first
#define S second

#define foreach(i, c) for (typeof(c.begin()) i = c.begin(); i != c.end(); ++i)

#ifdef LOCAL
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif // LOCAL

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long double ld;

const int INF = int(1e9);
const ll INFll = 1ll * INF * INF;
const ld EPS = 1e-26;
const int MOD = 1000000007;
const int N = 1e5;  // limit for array size

#define FILE "knapsack"
int main() {
    ios_base::sync_with_stdio(0);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int w, c;
    cin >> w >> c;
    if (w == 2 || w == 3) {
        cout << "YES";
        return 0;
    }
    vector<int> v;
    while (c > 0) {
        v.pb(c % w);
        #ifdef LOCAL
        cout << c % w << " ";
        #endif
        c /= w;
    }
    #ifdef LOCAL
    cout << endl;
    #endif
    v.pb(0);
    for (int i = 0; i < v.size()-1; i++) {
        if (v[i] != 0 && v[i] != 1 && v[i] != w - 1 && v[i] != w - 2) {
            cout << "NO";
            return 0;
        }
        if (v[i] == w - 2 && (i == 0 || (v[i-1] != w - 2 && v[i-1] != w - 1))) {
            cout << "NO";
            return 0;
        }
        if ((v[i] == w - 1 || v[i] == w - 2) && v[i+1] == 1) {
            cout << "NO";
            return 0;
        }
    }
    cout << "YES";
    return 0;
    reverse(v.begin(), v.end());
    for (int i = 0; i < v.size(); i++) {
        cout << v[i] << " ";
    }
    debug("\nTIME = %ld\n", clock());
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <functional>
#include <math.h>
#include <string>

using namespace std;

int getOperation(string& s, int pos)
{
	string pattern = "ACTG";

	int op = 0;

	for (int i = pos, j = 0; i < pos + 4; i++, j++)
	{
		int move = abs(pattern[j] - s[i]);
		op += move <= 13 ? move : (26 - move);
	}

	return op;
}

int main()
{
	int length;
	string s;

	cin >> length;
	cin >> s;

	int minOp = 1000000;
	for (int i = 0; i <= length - 4; i++)
	{
		minOp = min(minOp, getOperation(s, i));
	}

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

	return 0;
}


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

LL MOD;

LL powmod(LL a, LL n){
    if(n == 0) return 1;
    if(n % 2) return (a*powmod(a,n-1)) % MOD;
    LL c = powmod(a, n/2);
    return (c*c) % MOD;
}
LL inv(LL a){
    return powmod(a, MOD-2);
}
LL fact[110000];
LL invfact[110000];

LL ncr(LL n, LL r){
    if(r < 0 || n < 0) return 0;
    if(n < r) return 0;
    LL a = fact[n];
    a = (a * invfact[r]) % MOD;
    a = (a * invfact[n-r]) % MOD;
    return a;
}

LL get(LL a, LL b){
	LL ans = 0;
	for(LL q = 2; q <= a+b; q++){
		LL f = min(a, q-1) - max(1LL, q-b) + 1;
		ans += f * inv(q);
		ans %= MOD;
	}
	return ans;
}

int main(){
	LL n, k, q;
	cin >> n >> k >> q;
	MOD = q;

	map<LL,LL> r;
	r[n] = 1;
	for(int j = 1; j < k; j++){
		map<LL,LL> rr;
		for(auto x : r){
			if(x.first == 1){
				rr[x.first] += x.second;
			} else {
				rr[x.first / 2] += x.second;
				rr[(x.first + 1) / 2] += x.second;
			}
		}
		r = rr;
		if(rr.count(1) && rr[1] == n) break;
	}
	LL avg = n * (n-1) / 2;
	avg %= MOD;
	avg = (avg * inv(2)) % MOD;

	if(r.size() == 1){
		auto x = *r.begin();
		LL freq = x.second;
		LL g = x.first;
		avg -= (freq * (freq - 1) / 2) % MOD * get(g,g);
		avg %= MOD;
	} else if(r.size() == 2){
		vector<pair<int,int> > x;
		for(auto b : r) x.push_back(b);
		LL freq0 = x[0].second;
		LL freq1 = x[1].second;
		avg -= (freq0 * (freq0 - 1) / 2) % MOD * get(x[0].first, x[0].first);
		avg %= MOD;
		avg -= (freq1 * (freq1 - 1) / 2) % MOD * get(x[1].first, x[1].first);
		avg %= MOD;
		avg -= (freq0 * freq1) % MOD * get(x[0].first, x[1].first);
		avg %= MOD;
	}
	avg %= MOD;
	if(avg < 0) avg += MOD;
	cout << avg << '\n';
}


#include <iostream>
#include <set>
#include <string>

void answer(size_t v)
{
    std::cout << v << '\n';
}

void solve(std::string& s)
{
    const size_t n = s.length();

    std::set<std::string> words;
    for (size_t i = 0; i < n; ++i)
        words.insert(s.substr(i+1) + s.substr(0, i+1));

    answer(words.size());
}

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

    solve(s);

    return 0;
}


/**Bismillahir Rahmanir Rahim
   In the name of ALLAH, most gracious, most merciful */
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cctype>
#include <cstdio>
#include <iomanip>
#include <sstream>
#include <cstdlib>
#include <cassert>
#include <climits>
#include <complex>
#include <numeric>
#include <valarray>
#include <iostream>
#include <memory.h>
#include <algorithm>
using namespace std;
#define inf           (1<<29)
#define pb            push_back
#define mp            make_pair
#define eps           1e-9
#define nl            printf("\n")
#define spc           printf(" ")
#define sci(n)        scanf("%ld",&n)
#define sc64(n)       scanf("%I64d",&n)
#define scii(a,b)     scanf("%ld %ld",&a,&b)
#define sc6464(a,b)   scanf("%I64d %I64d",&a,&b)
#define scs(s)        scanf("%s",s)
#define scss(a,b)     scanf("%s %s",a,b)
#define scd(f)        scanf("%lf",&f)
#define scdd(a,b)     scanf("%lf %lf",&a,&b)
#define prc(k)        printf("Case %ld: ",k);
#define pri(a)        printf("%ld",a)
#define pr64(a)       printf("%I64d",a)
#define prii(a,b)     printf("%ld %ld",a,b)
#define pr6464(a,b)   printf("%I64d %I64d",a,b)
#define prs(a)        printf("%s",a)
#define prss(a,b)     printf("%s %s",a,b)
#define prd(a)        printf("%lf",a)
#define prdd(a,b)     printf("%lf %lf",a,b)
#define rep(i,n)      for(int i(0),_n(n);i<_n;++i)
#define repr(i,n)     for(int i=n;i>=0;i--)
#define repi(i,a,b)   for(int i(a),_b(b);i<=_b;++i)
#define repl(i,n)     for(int i(1),_n(n);i<=_n;++i)
#define repir(i,a,b)  for(int i=a;i>=b;i--)
#define chk(a,k) ((bool)(a&(1<<(k))))
#define off(a,k) (a&(~(1<<(k))))
#define on(a,k) (a|(1<<(k)))
#define ff            first
#define ss            second
#define all(a)        a.begin(),a.end()
#define mem(x,a)      memset(x,a,sizeof(x))
#define repe(it,c)    for(__typeof((c).begin()) it=(c).begin();it!=(c).end();++it)
int dx[]={0,0,1,-1,1,-1,1,-1},dy[]={1,-1,0,0,1,-1,-1,1};
typedef vector<int> vi;
typedef vector<long> vl;
typedef vector<long long> vll;
typedef vector<string> vs;
#define ll long long
typedef vector<vector<int> > vvi;
inline void cn( long &n )//fast input function
{
n=0;
long ch=getchar();int sign=1;
while( ch < '0' || ch > '9' ){if(ch=='-')sign=-1; ch=getchar();}
while(ch >= '0' && ch <= '9')
n = (n<<3)+(n<<1) + ch-'0', ch=getchar();
n=n*sign;
}
template<class T> void cmin(T &a,T b){if(b<a) a=b;}
template<class T> void cmax(T &a,T b){if(b>a) a=b;}
template<class T> int len(const T&c){return (int)c.size();}
template<class T> int len(char c[]){return (int)strlen(c);}
string itos(long n){string s;while(n){s+=(n%10+48);n/=10;}reverse(all(s));return s;}
long stoi(string s){long n=0;rep(i,len(s))n=n*10+(s[i]-48);return n;}

//Polya-Burnside theory : (n^6+3n^4+12n^3+8n^2)/24
#define DEBUG 1
#if DEBUG && !ONLINE_JUDGE
	#define debug(args...) (Debugger()) , args
	class Debugger { public: Debugger(const std::string& _separator = ", ") : first(true), separator(_separator){} template<typename ObjectType> Debugger& operator , (const ObjectType& v) { if(!first) std::cerr << separator; std::cerr << v; first = false; return *this; } ~Debugger() { std::cerr << endl;} private: bool first; std::string separator; }; template <typename T1, typename T2> inline std::ostream& operator << (std::ostream& os, const std::pair<T1, T2>& p) { return os << "(" << p.first << ", " << p.second << ")"; } template<typename T> inline std::ostream &operator << (std::ostream & os,const std::vector<T>& v) { bool first = true; os << "["; for(unsigned int i = 0; i < v.size(); i++) { if(!first) os << ", "; os << v[i]; first = false; } return os << "]"; } template<typename T> inline std::ostream &operator << (std::ostream & os,const std::set<T>& v) { bool first = true; os << "["; for (typename std::set<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii) { if(!first) os << ", "; os << *ii; first = false; } return os << "]"; } template<typename T1, typename T2> inline std::ostream &operator << (std::ostream & os,const std::map<T1, T2>& v) { bool first = true; os << "["; for (typename std::map<T1, T2>::const_iterator ii = v.begin(); ii != v.end(); ++ii) { if(!first) os << ", "; os << *ii ; first = false; } return os << "]"; }
#else
		#define debug(args...)
#endif
#define mod 1000000007

int main()
{

   long long a,b,aa,bb,r1,r2;

   while(cin>>a>>b){
        long long s,d,tot=0;
      for(long i=1;i<b;i++)
      {
         // s=i; e=a*i;
          d=i*(a-1)+1;
          s=(a*(a+1)/2)%mod;
          s=(s*i)%mod;
          s=(s*b)%mod;
          s= s+ (a*i)%mod;
          s=s%mod;
        //  cout<<s<<endl;
          tot=(tot+s)%mod;
      }
   cout<<tot<<endl;
   }

return 0;
}


// In the name of GOD!
// 1396 / 03 / 23

#include <iostream>
#include <map>

using namespace std;

map<string, int> d;

void pre();
string chrd(string x, string y, string z);

int main() {
	string x, y, z;
	cin >> x >> y >> z;
	pre();
	if (chrd(x, y, z) != "0") cout << chrd(x, y, z) << '\n';
	else if (chrd(x, z, y) != "0") cout << chrd(x, z, y) << '\n';
	else if (chrd(y, x, z) != "0") cout << chrd(y, x, z) << '\n';
	else if (chrd(y, z, x) != "0") cout << chrd(y, z, x) << '\n';
	else if (chrd(z, x, y) != "0") cout << chrd(z, x, y) << '\n';
	else if (chrd(z, y, x) != "0") cout << chrd(z, y, x) << '\n';
	else cout << "strange\n";
}

void pre() {
	d["C"] = 0;
	d["C#"] = 1;
	d["D"] = 2;
	d["D#"] = 3;
	d["E"] = 4;
	d["F"] = 5;
	d["F#"] = 6;
	d["G"] = 7;
	d["G#"] = 8;
	d["A"] = 9;
	d["B"] = 10;
	d["H"] = 11;
}

string chrd(string x, string y, string z) {
	int a, b;
	a = (d[y] - d[x] + 12) % 12;
	b = (d[z] - d[y] + 12) % 12;
	if (a == 4 && b == 3) return "major";
	if (a == 3 && b == 4) return "minor";
	return "0";
}


#include <stdio.h>
#include <stack>
#include <map>
#include <string.h>
#include <string>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <vector>
#include <set>
#include <queue>
#include <functional>
#include <cfloat>
#include <limits>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
//#define ld long double
const double sn = 1e-6;
int a, b, l, r;
int main() {
	scanf("%d%d%d%d", &a, &b, &l, &r);
	if (r - l + 1 >= (a + b)*2) {
		if (b >= a) {
			printf("%d\n", a+1);
		}
		else {
			printf("%d\n", a+ (a-b));
		}
	}
	else {
		int t = 1, v2 = r % ((a + b) * 2), co = 0, temp = 1;
		vector<int> ve = {};
		bool last = false;
		int t2 = 1, t3 = 1;
		while (true) {
			if (t2 == v2 && t3 >= (r - l) + 1)
				break;
			t2 = (t2 + 1) % (2*(a+b));
			t3++;
		}
		while (true) {
			if ((t >= 1 && t <= a) || (t >= a + b + 1 && t <= 2 * a + b)) {
				last = false;
				int dig = 1;
				//cout << "c  " << co << "," << ve.size() << endl;
				while (true) {
					bool val = true;
					for (int i = ve.size() - 1; i >= max((int)ve.size() - a-co, 0); i--) {
						if (ve[i] == dig) {
							val = false;
							break;
						}
					}
					if (val)
						break;
					else
						dig++;
				}
				ve.push_back(dig);
				co++;
			}
			else {
				co = 0;
				if (b >= a || last) {
					ve.push_back(ve[ve.size()-1]);
				}
				else {
					int sz = ve.size();
					int tv = t3 - (r - l + 1);
					//cout << tv << "," << sz << endl;
					if (sz - (a - b) >= tv || sz <= tv) {
						ve.push_back(ve[sz - (a - b)]);
					}
					else {
						ve.push_back(ve[tv]);
					}
				}
				last = true;
			}
			if (t == v2 && temp >= (r-l)+1)
				break;
			t = (t + 1) % ((a + b) * 2);
			temp++;
		}
		bool vis[30] = {};
		int ans = 0, sz = ve.size();
		for (int i = sz - 1; i >= sz - (r - l + 1); i--) {
			//cout << ve[i] << endl;
			if (!vis[ve[i]]) {
				vis[ve[i]] = true;
				ans++;
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int x,y,z;
    cin>>x>>y>>z;
    y=max(max(x,y),z)-min(min(x,y),z);
    cout<<y<<endl;
    return 0;
}


#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;

int h[256];
bool visit[6];
vector<int> v;

void Init()
{
    h['R']=0;h['O']=1;h['Y']=2;h['G']=3;h['B']=4;h['V']=5;
}

int c[6],f[6],r[6];
char s[10];

int Cal()
{
    int ret=0;
    for(int i=0;i<6;i++)
        ret=ret*6+f[i];
    return ret;
}

void R1()
{
    int t=f[0];
    f[0]=f[1];f[1]=f[2];f[2]=f[3];f[3]=t;
}

void R2()
{
    int t=f[0];
    f[0]=f[5];f[5]=f[2];f[2]=f[4];f[4]=t;
}

void R3()
{
    int t=f[3];
    f[3]=f[4];f[4]=f[1];f[1]=f[5];f[5]=t;
}

bool Exist(int val)
{
    for(int i=0;i<v.size();i++)
        if(v[i]==val)
            return true;
    return false;
}

void Func()
{
    bool flag=false;
    for(int i=0;i<4;i++)
    {
        R1();
        for(int j=0;j<4;j++)
        {
            R2();
            for(int k=0;k<4;k++)
            {
                R3();
                int val=Cal();
                if(Exist(val)) { flag=true; goto R; }
            }
        }
    }
R:  if(!flag)
        v.push_back(Cal());
}

void DFS(int p)
{
    if(p==6)
    {
        memcpy(r,f,sizeof(f));
        Func();
        memcpy(f,r,sizeof(r));
        /*
        for(int i=0;i<6;i++)
            printf("%d ",r[i]);
        printf("\n");
        */
        return;
    }
    for(int i=0;i<6;i++)
    {
        if(!visit[i])
        {
            f[p]=c[i];
            visit[i]=true;
            DFS(p+1);
            visit[i]=false;
        }
    }
}

int main()
{
    Init();
    while(scanf("%s",s)!=EOF)
    {
        for(int i=0;s[i];i++)
            c[i]=h[s[i]];
        v.clear();
        memset(visit,0,sizeof(visit));
        DFS(0);
        printf("%d\n",v.size());
    }
    return 0;
}


#pragma comment (linker, "/STACK:200000000")
#define _CRT_SECURE_NO_DEPRECATE
#define _SECURE_SCL 0
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <sstream>
#include <vector>
#include <utility>
#include <cmath>
using namespace std;

#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define get(a,b); a b; cin >> b;

#define forn(i,n) for (int i=0; i<int(n); ++i)
#define ford(i,n) for (int i=int(n)-1; i>=0; --i)
#define fore(i,a,b) for (int i=int(a); i<=int(b); ++i)

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

const int INF = 1e9;
const long long INF64 = 1e18;
const long double eps = 1e-9;
const long double pi = 3.14159265358979323846;

int main(){
    double n,R,r;
    cin >> n >> R >> r;
    if (n == 1){
        if (r-eps<R)
            cout << "YES";
        else
            cout << "NO";
        return 0;
    }
    double rm = R*sin(pi/n)/(sin(pi/n)+1);
    if (r-eps<rm)
        cout << "YES";
    else
        cout << "NO";
    return 0;
}


//#include <bits/stdc++.h>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <vector>
#include <string>
#include <climits>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <stdio.h>
//#define int long long
using namespace std;
#define loop(n) for(int i=0;i<n;i++)
#define endl '\n'
#define OnlineJudge5
#define R_S(rst)   random_shuffle(rst.begin(), rst.end())
#define F first
#define S second
#define  precision(n)  cout << fixed << setprecision(n)
#define clr(v,d ) memset(v, d, sizeof(v))
#define all(v) ((v).begin()),((v).end())
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<int,pii> pnii; /// weighted grid
typedef vector<pii>vii;
void init()
{
    cin.tie(0);
    cin.sync_with_stdio(0);
    cout.tie(0);
    #ifdef OnlineJudge
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
}
ll S(ll x){ll s = 0;while(x){s += x % 10;x /= 10;}return s;} /// sum of digits.
ll powe(ll a , ll n){if(n == 1)return a;return a * powe(a ,n-1);}
ll com(int n,int m){ll z = 1;for(int i = 0; i < m;i++){z = z * ( n - i) / (i + 1) ;}return z;}
int fix (int a,int k){ return (a%k + k)%k; } // convert N mod to P mode
int * LIS_Lengths(vector<int> a) { int* lis = new int[a.size()];int b[a.size()];int maxLength = 1, lowerBound;lis[0] = 1, b[0] = a[0];for (size_t i = 1; i < a.size(); i++) {lowerBound = lower_bound(b, b + maxLength, a[i]) - b;
lis[i] = lowerBound + 1;if (lowerBound == maxLength)    b[maxLength++] = a[i];else    b[lowerBound] = a[i];}return lis;}
bool is_prime(ll n){if(n==2) return true;if(n<2 || n%2 == 0)return false;for(int i=3; i * i <= n; i+=2){if(n % i == 0)return false;}return true;}
ll LCM(ll a, ll b){return (a * b)/__gcd(a,b);}
int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};
int dr[] = {0,0,0,1,1,1,-1,-1,-1};
int dc[] ={-1,0,1,-1,0,1,-1,0,1};
/// <<------------------------------------------------------------------------------>>

/// <<----;------------------------------------------------------------------------->>
int main()
{
    init();
    ll n;
    cin >> n;
    bool ok = 0;
    for(ll m = 1 ; m < ll(1e8) ; m++)
    {
        ll d = m*(m + 1) / 2;
        if(d >= n)
        {
            cout << m - (d - n) << endl;
            return 0;
        }
    }
}


#include <cstdio>
#include <cstring>
using namespace std;
#define LL long long
const int N = 1500001;
int vis[N];
LL a[N], b[N];
bool judge(int x)
{
    char s[13];
    sprintf(s,"%d",x);
    int len = (int)strlen(s);
    for(int i=0; i<len>>1; i++)
        if(s[i] != s[len-1-i]) return false;
    return true;
}
void Init()
{
    for(int i=2; i*i<N; i++)
        if(!vis[i]) {
            for(int j=i*i; j<N; j+=i)
                vis[j] = 1;
        }
    a[0] = a[1] = 0;
    for(int i=2; i<N; i++) a[i] = !vis[i] ? a[i-1]+1 : a[i-1];
    b[0] = 0;
    for(int i=1; i<N; i++)
        if(judge(i)) b[i] = b[i-1]+1;
        else b[i] = b[i-1];
}
int main()
{

    int i;
    LL p,q;
    Init();
    while(scanf("%lld %lld",&p,&q) == 2)
    {
        int ans = -1;
        for(i=1500000; i>=1 ;i--)
            if(q*a[i] <= p*b[i])
            {
                ans = i;
                break;
            }
        if(ans == -1) puts("Palindromic tree is better than splay tree");
        else printf("%d\n",ans);
    }
    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
ll modpow(ll x,ll n){
	ll res=1;
	while(n>0){
		if(n&1) res=res*x%mod;
		x=x*x%mod;
		n>>=1;
	}
	return res;
}
int n,m;
ll cnt[(1<<20)],cnt2[(1<<20)];
ll a[10][10];
int rev[(1<<20)],rev2[(1<<20)];
void dfs(int cur,vector<int>con,ll prob){
	if(cur == m){
		int nxt = 0, id = 0;
		rep(j,(1<<n)){
			if(__builtin_popcount(j) != m) continue;
			vector<int>vi; rep(i,n) if(((j>>i)&1)) vi.pb(i);
			int ok = 0;
			do{
				rep(x,m) if(!((con[x]>>vi[x])&1)) goto fail;
				ok = 1; break; fail:;
			}while(next_permutation(vi.begin(),vi.end()));
			if(ok) id |= (1<<nxt);
			nxt++;
		}
		cnt[id] += prob;
		return;
	}
	rep(j,(1<<n)){
		con.pb(j);
		ll nw = prob;
		rep(k,n){
			if(((j>>k)&1)){
				nw = nw*a[cur][k]%mod;
			}
			else{
				nw = nw*(mod+1-a[cur][k])%mod;
			}
		}
		dfs(cur+1,con,nw);
		con.pop_back();
	}
}
void dfs2(int cur,vector<int>con,ll prob){
	if(cur == n){
		int nxt = 0, id = 0;
		rep(j,(1<<n)){
			if(__builtin_popcount(j) != n-m) continue;
			vector<int>vi; rep(i,n) if(((j>>i)&1)) vi.pb(i);
			int ok = 0;
			do{
				rep(x,n-m) if(!((con[x]>>vi[x])&1)) goto fail2;
				ok = 1; break; fail2:;
			}while(next_permutation(vi.begin(),vi.end()));
			if(ok) id |= (1<<nxt);
			nxt++;
		}
		cnt2[id] += prob;
		return;
	}
	rep(j,(1<<n)){
		con.pb(j);
		ll nw = prob;
		rep(k,n){
			if(((j>>k)&1)){
				nw = nw*a[cur][k]%mod;
			}
			else{
				nw = nw*(mod+1-a[cur][k])%mod;
			}
		}
		dfs2(cur+1,con,nw);
		con.pop_back();
	}
}

int main(){
	cin >> n;
	rep(i,n) rep(j,n){
		cin >> a[i][j]; a[i][j] = a[i][j]*modpow(100LL,mod-2)%mod; a[i][j] = (a[i][j]%mod+mod)%mod;
	}
	if(n == 1){
		cout << a[0][0] << endl;
		return 0;
	}
	m = n/2;
	dfs(0,vector<int>(),1);
	rep(i,(1<<20)) cnt[i] %= mod;
	//cnt[j]... j no submask
	for(int i=0;i<20;i++){
		for(int j=0;j<(1<<20);j++){
			if(((j>>i)&1)){
				cnt[j] += cnt[j^(1<<i)];
				cnt[j] %= mod;
			}
		}
	}
	ll all = cnt[(1<<20)-1];
	dfs2(m,vector<int>(),1);
	rep(i,(1<<20)) cnt2[i] %= mod;
	memset(rev,-1,sizeof(rev));
	int nd = 0;
	rep(i,(1<<n)){
		if(__builtin_popcount(i) != m) continue;
		rev[i] = nd++;
	}
	memset(rev2,-1,sizeof(rev2));
	nd = 0;
	int cor[25]={};
	rep(i,(1<<n)){
		if(__builtin_popcount(i) != n-m) continue;
		cor[nd] = rev[(1<<n)-1-i];
		rev2[i] = nd++;
	}
	ll ans = 0;
	rep(i,(1<<nd)){
		ll cur = cnt2[i];
		int mask = 0;
		rep(j,nd){
			if(((i>>j)&1));
			else mask += (1<<cor[j]);
		}
		ans += cur * (all-cnt[mask]) %mod;
	}
	cout << (ans%mod+mod)%mod << endl;
}


#include<bits/stdc++.h>
using namespace std;
const int N = 60;
char p[N][10];
int vis[N][N][N][N];
bool dfs(int id,int a,int b,int c) {
	if (id == 1)return 1;
	if (vis[id][a][b][c])return 0;
	if (p[b][0] == p[c][0] || p[b][1] == p[c][1]) {
		if (dfs(id - 1, id - 3, a, c))return 1;
	}
	if (id > 3) {
		if (p[id - 3][0] == p[c][0] || p[id - 3][1] == p[c][1]) {
			if (dfs(id - 1, c, a, b))return 1;
		}
	}
	vis[id][a][b][c]=1;
	return 0;
}
int main() {
	int n;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
		scanf("%s", p[i]);
	printf(n == 1 || dfs(n, n - 2, n - 1, n) ? "YES\n" : "NO\n");
	return 0;
}


#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
#include <bits/stdc++.h>
using namespace std;
#define sim template <class c
#define mor > muu & operator<< (
#define ris return *this
#define R22(r) sim> typename enable_if<1 r sizeof(dud<c>(0)), muu&>::type operator<<(c g) {
sim> struct rge {c h, n;};
sim> rge<c> range(c h, c n) {return rge<c>{h, n};}
sim, class F> struct rgm {c h, n; F f;};
sim, class F> rgm<c, F> map_range(c b, c e, F f) {return rgm<c, F>{b, e, f};}
sim, class F> rgm<typename c::const_iterator, F> map_range(const c &x, F f) {return map_range(x.begin(), x.end(), f);}
sim> auto dud(c *r) -> decltype(cerr << *r);
sim> char dud(...);
struct muu {
#ifdef LOCAL
	stringstream a;
	~muu() {cerr << a.str() << endl;}
	R22(<) a << boolalpha << g; ris;}
	R22(==) ris << range(begin(g), end(g));}
	sim mor rge<c> u) {
		a << '[';
		for (c s = u.h; s != u.n; ++s)
			*this << ", " + 2 * (s == u.h) << *s;
		ris << ']';
	}
	sim, class n mor pair <c,n> r) {ris << "(" << r.first << ", " << r.second << ")";}
	sim, class F mor rgm<c, F> u) {
		for (c s = u.h; s != u.n; ++s)
			u.f(*this << ", " + 2 * (s == u.h), *s);
		ris;
	}
	#else
	sim mor const c&) {ris;}
	#endif
	muu & operator()() {ris;}
};
#define debug (muu() << __FUNCTION__ << "#" << __LINE__ << ": ")
#define imie(r) "[" #r ": " << (r) << "] "
#define imask(r) "[" #r ": " << bitset<8 * sizeof(r)>(r) << "] "
#define arr(a, i) "[" #a imie(i) ": " << a[i] << "] "
#define arr2(a, i, j) "[" #a imie(i) imie(j) << ": " << a[i][j] << "] "
const int nax = 101;
using ll = long long;
const ll mod = 1e9 + 7;//588518285577289787ll;
ll ways[nax][nax * nax];
int cou[nax];
void add(ll &a, ll b) {
	a += b;
	if (a > mod)
		a -= mod;
}
ll binom[nax][nax];
int main() {
	int diff = 0;
	for (int i = 0; i < nax; ++i)
		binom[i][i] = binom[i][0] = 1;
	for (int i = 1; i < nax; ++i)
		for (int j = 1; j < i; ++j)
			binom[i][j] = (binom[i - 1][j] + binom[i - 1][j - 1]) % mod;
	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		int x;
		scanf("%d", &x);
		cou[x]++;
	}
	for (int i = 0; i < nax; ++i)
		if (cou[i])
			diff++;
	ways[0][0] = 1;
	int seen_so_far = 0, sum_so_far = 0;
	for (int i = 1; i < nax; ++i) {
		for (int l = 0; l < cou[i]; ++l) {
			for (int c = seen_so_far; c >= 0; --c)
				for (int s = 0; s <= sum_so_far; ++s)
					add(ways[c + 1][s + i], ways[c][s]);
			seen_so_far++;
			sum_so_far += i;
		}
	}
	#ifdef LOCAL
	for (int i = 0; i < nax; ++i)
		for (int j = 0; j < nax * nax; ++j)
			if (ways[i][j])
				debug << arr2(ways, i, j);
	#endif
	int ans = 1;
	bool whole = false;
	for (int i = 1; i < nax; ++i) {
		for (int l = ans + 1; l <= cou[i]; ++l) {
			debug << imie(i) imie(l) arr2(ways, l, i * l) arr2(binom, cou[i], l);
			if (ways[l][i * l] == binom[cou[i]][l]) {
				debug << "match";
				ans = l;
				if (l == cou[i])
					whole = true;
			}
		}
	}
	if (whole && diff <= 2)
		ans = n;
	printf("%d\n", ans);
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define per(i, a, b) for (auto i = (b); i-- > (a); )
#define eb(x...) emplace_back(x)
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) int((x).size())
#define lb(x...) lower_bound(x)
#define ub(x...) upper_bound(x)

template<class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
template<class T> T gcd(T a, T b) { return b != T(0) ? gcd(b, a % b) : a; }

const int N = 1 << 18, M = 1 << 19, Q = 1 << 21, B = 20;
struct event { bool delt; int v; } E[Q];

int delt[M]; pair<int, int> edges[M];

int n, m;

int val[N], arr[N];

int nxt[N + M], p[N + M][B];
int head(int u) { return nxt[u] != u ? nxt[u] = head(nxt[u]) : u; }
void join(int e) {
	auto [u, v] = edges[e];
	u = head(u); v = head(v);
	if (u == v) return;
	p[u][0] = p[v][0] = nxt[u] = nxt[v] = e + n;
}

vector<int> g[N + M];
int in[N + M], out[N + M], timer = 0;
void dfs(int u) {
	in[u] = timer;
	if (u < n) arr[timer++] = val[u];
	for (int v : g[u]) dfs(v);
	out[u] = timer;
}

pair<int, int> t[N << 1];
void build(int v, int l, int r) {
	if (r - l == 1) return void(t[v] = {arr[l], l});
	int m = (l + r) >> 1;
	build(v << 1|0, l, m);
	build(v << 1|1, m, r);
	t[v] = max(t[v << 1], t[v << 1|1]);
}
int lo, hi, x;
pair<int, int> find(int v, int l, int r) {
	if (hi <= l || r <= lo) return {-1, -1};
	if (lo <= l && r <= hi) return t[v];
	int m = (l + r) >> 1;
	return max(find(v << 1, l, m), find(v << 1|1, m, r));
}
void clear(int v, int l, int r) {
	if (r - l == 1) return void(t[v] = {0, l});
	int m = (l + r) >> 1;
	if (x < m) clear(v << 1, l, m);
	else clear(v << 1|1, m, r);
	t[v] = max(t[v << 1], t[v << 1|1]);
}
int query(int l, int r) {
	lo = l, hi = r; auto p = find(1, 0, n);
	x = p.second; clear(1, 0, n);
	return p.first;
}

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

	int q; cin >> n >> m >> q;
	iota(nxt, nxt + n + m, 0);
	fill(delt, delt + m, -1);

	rep(u, 0, n) cin >> val[u];
	rep(e, 0, m) {
		int a, b; cin >> a >> b;
		edges[e] = {--a, --b};
	}
	rep(z, 0, q) {
		int t, v; cin >> t >> v;
		E[z] = {bool(--t), --v};
		if (t) delt[v] = z;
	}
	rep(e, 0, m) if (delt[e] < 0) delt[e] = q, E[q++] = {1, e};

	per(z, 0, q) if (E[z].delt) join(E[z].v);

	rep(u, 0, n + m) {
		if (nxt[u] == u) p[u][0] = u;
		if (p[u][0] != u) g[p[u][0]].push_back(u);
	}
	rep(j, 1, B) rep(u, 0, n + m) p[u][j] = p[p[u][j - 1]][j - 1];

	rep(u, 0, n) {
		int rt = p[u][B - 1];
		if (!out[rt]) dfs(rt);
	}

	build(1, 0, n);

	rep(z, 0, q) {
		if (E[z].delt) continue;
		int u = E[z].v;
		per(b, 0, B) if (n <= p[u][b] && z < delt[p[u][b] - n]) u = p[u][b];
		cout << query(in[u], out[u]) << '\n';
	}
}


#include<bits/stdc++.h>
using namespace std;
#define il inline
#define re register
#define rep(i, a, b) for(re int i = (a); i <= (b); ++ i)
#define mem(k, p) memset(k, p, sizeof(k))
#define int long long
#define mod 1000000007
il int read() {
    re int x = 0, f = 1; re char c = getchar();
    while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - 48, c = getchar();
    return x * f;
}
#define N 105
il int qpow(int a, int b) { int r = 1; while(b) { if(b & 1) r = r * a % mod; a = a * a % mod, b >>= 1; } return r; }
#define K (1 << 15) + 5
int n, m, a[N], du[N], vis[N], st[N], top, val[N], c[K], dp[N][K], fac[N], inv[N], pax[N], Ans = 1, cnt, tot;
il int C(int a, int b) {
	if(a < b) return 0;
	return fac[a] * inv[b] % mod * inv[a - b] % mod;
}
il void dfs(int x, int st) {
	if(pax[x]) return;
	++ tot, pax[x] = st;
	rep(i, 1, n) if(a[x] % a[i] == 0 || a[i] % a[x] == 0) dfs(i, st);
}
signed main() {
	n = read(), fac[0] = inv[0] = 1;
	rep(i, 1, n) fac[i] = i * fac[i - 1] % mod, inv[i] = qpow(fac[i], mod - 2);
	rep(i, 1, n) a[i] = read();
	rep(i, 1, n) rep(j, 1, n) if(i != j && a[i] % a[j] == 0) ++ du[i];
	rep(T, 1, n) {
		if(pax[T]) continue;
		tot = 0, dfs(T, T), mem(dp, 0), mem(c, 0), top = -1, mem(vis, 0);
		rep(i, 1, n) if(pax[i] == T && du[i] == 0) vis[i] = 1, st[++ top] = a[i];
		if(tot - top <= 2) continue;
		rep(i, 1, n) {
			if(vis[i] == 1 || pax[i] != T) continue;
			rep(j, 0, top) if(a[i] % st[j] == 0) val[i] |= (1 << j);
		}
		int M = (1 << (top + 1)) - 1;
		rep(i, 0, M) rep(j, 1, n) if(pax[j] == T && vis[j] == 0 && ((i & val[j]) == val[j])) ++ c[i];
		rep(i, 1, n) if(vis[i] == 0 && pax[i] == T) ++ dp[1][val[i]];
		rep(i, 1, tot - top - 2) {
			rep(S, 0, M) {
				if(dp[i][S] == 0) continue;
				if(c[S] > i) dp[i + 1][S] = (dp[i + 1][S] + dp[i][S] * (c[S] - i)) % mod;
				rep(j, 1, n) if(pax[j] == T && vis[j] == 0 && ((S & val[j]) != val[j]) && (S & val[j]))
					dp[i + 1][S | val[j]] = (dp[i + 1][S | val[j]] + dp[i][S]) % mod;
			}
		}
		int ans = 0;
		tot = tot - top - 1;
		rep(i, 0, M) ans = (ans + dp[tot][i]) % mod;
		Ans = Ans * ans % mod * C(cnt + tot - 1, tot - 1) % mod;
		cnt += tot - 1;
	}
	cout << Ans;
	return 0;
}


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

	using namespace std;

	#define ll long long
	#define D double
	#define ff first
	#define ss second
	#define FOR(a,n) for(i=0;i<n;i++){	cin>>a[i];}
	#define pb push_back
	#define INF 1000000000

	vector< vector<ll> > v;
	vector<bool> th(1010),ch(1010);

	 bool isPrime(int n)
		{

		    if (n <= 1)  return false;
		    if (n <= 3)  return true;

		    if (n%2 == 0 || n%3 == 0)
		    	return false;

		    for (int i=5; i*i<=n; i=i+6)
		    {
		        if (n%i == 0 || n%(i+2) == 0)
		           return false;
		    }

		    return true;
		}

	void swap(char a,char b)
	{
		char t;
		t=a;
		a=b;
		b=t;
	}

	ll fact(ll n)
	{
		if(n<=1)
		return 1;
		else
		return n*fact(n-1);
	}

	ll comb(ll n, ll k)
{
    ll res = 1;

    if ( k > n - k )
        k = n - k;

    for (ll i = 0; i < k; ++i)
    {
        res *= (n - i);
        res /= (i + 1);
    }

    return res;
}

	ll GCD(ll a,ll b)
	{
		if(b==1)
		return 1;
		else
		return GCD(b,a%b);
	}

	ll dig(ll n)
	{
		ll c=0;
		while(n>0)
		{
			ll x=n%10;
			if(x!=0)
			c++;
			n=n/10;
		}
		return c;
	}

	bool DFS(ll n)
	{	bool ch[1010];
		stack<ll> s;
		ll ans=0;
		ch[n]=true;
		s.push(n);
		if(th[n]==true)
		ans++;
		while(s.size()>0)
		{
			ll x=s.top();
			s.pop();
		cout<<"X";
			for(ll i=0;i<v[x].size();i++)
			{
				if(ch[v[x][i]]==false)
				{

					s.push(v[x][i]);
					ch[v[x][i]]=true;
					if(th[v[x][i]]==true)
					ans++;
					if(ans>=2)
					{
						return false;
					}
				}
			}

		}
		return true;

	}

		queue<int> q;
	void BFS(ll n)
	{
		ll i,le[n+1];
		q.push(n);
		le[n]=0;
		ch[n]=true;

		while(q.size()>0)
		{
			ll x=q.front();
			q.pop();

			for(i=0;i<v[x].size();i++)
			{
				if(ch[v[x][i]]==false)
				{
					q.push(v[x][i]);
					ch[v[x][i]]=true;
					le[v[x][i]]=le[x]+1;

				}
			}
		}

	}

ll count(ll n)
	{
		ll c=0,k=n;

		while(k>0)
		{
			ll x=k%10;

			c++;
			k=k/10;
			//cout<<n<<" ";
		}
		return c;
		}

D area(ll x1,ll y1,ll x2,ll y2,ll x3,ll y3)
{
	return abs((D)((x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2)))/(D)(2));

}

bool isBin(ll n)
{
	while(n>0)
	{
		ll x=n%10;
		if(x>1)
		return false;
		n/=10;
	}
	return true;
}

	int main()
	{
		ll n,k;
		cin>>n;
		ll y=n;
	    ll c=k=count(n);
	    string s="";
	    while(c>0)
	    {
	        s=s+'1';
	        c--;
	    }
	    string t=to_string(n);
	   // cout<<t;
	    ll i=0;
	    while(i<t.size())
	    {
	        if((t[i]-48)%10==0)
	        {
	            string str=s;
	            //str[i]='0';
	            //reverse(str.begin(),str.end());
	            ll x=stoi(str);
	         //  cout<<x<<" ";
	            if(x>n)
	            {s[i]='0';
	               // cout<<s<<endl;
	            }

	        }
	        i++;

	    }

	//	cout<<s<<" ";
	//	reverse(s.begin(),s.end());
		n=stoi(s);
    	//cout<<n<<" ";
	 i=0;
		ll ans=0;
		while(n>0)
		{
			ll x=n%10;
			ans+=x*pow(2,i);
			i++;
			n/=10;
		}
		cout<<ans;

	}


#include <bits/stdc++.h>
#define dbg() cerr <<
#define name(x) (#x) << ": " << (x) << ' ' <<

using namespace std;

int MOD;

int add(int a, int b) {
  a += b;
  while (a < 0) a += MOD;
  while (a >= MOD) a -= MOD;
  return a;
}

void Add(int &a, int b) {
  a = add(a, b);
}

int Mul(int a, int b) {
  return 1LL * a * b % MOD;
}

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

  int n, k; cin >> n >> k >> MOD;

  vector<int> pw10(n + 1);
  pw10[0] = 1;
  for (int i = 1; i <= n; ++i) {
    pw10[i] = Mul(pw10[i - 1], 10);
  }

  vector<vector<int>> dp(k,
      vector<int>(2));

  // dp[r][dif_0]

  dp[0][0] = 1;

  int pw = 1;
  int ans = 0;
  for (int i = 0; i <= n; ++i) {
    vector<vector<int>> nxt(k,
      vector<int>(2));

    if (i < n)
      Add(ans, Mul(Mul(pw10[n - i - 1], 9), dp[0][1]));
    else
      Add(ans, dp[0][1]);
    dp[0][1] = 0;
    for (int r = 0; r < k; ++r) {
//        dbg() name(i) name(r) "";
//        dbg() name(dp[r][0]) name(dp[r][1]) endl;

      for (int c = (i == n - 1); c < 10 && i < n; ++c) {
        int nr = (r + c * pw) % k;
        for (int dif_0 = 0; dif_0 < 2; ++dif_0) {
          int ndif = dif_0 || (c != 0);
          Add(nxt[nr][ndif], dp[r][dif_0]);
        }
      }
    }
    if (i < n)
      dp = move(nxt);
    pw = pw * 10 % k;
  }

  cout << ans << endl;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <map>
#include <set>

using namespace std;

#define MP(a, b) make_pair(a, b)

int n;

int gcd(int a, int b) {
    while (b) b ^= a ^= b ^= a %= b;
    return a;
}

const int INF = 1000000000;

vector <int> c;
vector <vector <int> > g;
vector <int> rm;
int comp[4][4];
vector<int> roots;

int main() {

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

    cin >> n;
    rm.resize(n, 0);
    c.resize(n);
    g.resize(n);
    vector<int> anss;

    comp[1][1] = comp[2][2] = comp[3][3] = 0;
    comp[1][2] = 1;
    comp[1][3] = 2;
    comp[2][3] = 1;
    comp[3][2] = 2;
    comp[2][1] = 2;
    comp[3][1] = 1;

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

    for (int i = 0; i < n; i++) {
        int k; cin >> k;
        for (int j = 0; j < k; j++) {
            int u; cin >> u;
            g[i].push_back(u - 1);
        }
    }

    for (int i = 0; i < n; i++) if (g[i].size() == 0) roots.push_back(i);

    int final_ans = INF;

    for (int q = 0; q < roots.size(); q++) {

        int curc = c[roots[q]];
        rm[roots[q]] = 1;
        int movec = 0;
        int ans = 1;

        for (int i = 0; i < n; i++) {
            bool check = false;
            for (int p = 0, movec = curc - 1; p < 3; p++, movec += 1, movec %= 3) {
                for (int j = 0; j < n; j++) {
                    if (rm[j] || c[j] != movec + 1) continue;

                    check = true;
                    for (int k = 0; k < g[j].size(); k++) {
                        if (!rm[g[j][k]])
                            check = false;
                    }
                    if (check) {
                        rm[j] = 1;
                        ans += comp[curc][movec + 1] + 1;
                        curc = movec + 1;
                        break;
                    }
                }

                if (check) break;
            }
        }

        int check = 1;
        for (int i = 0; i < n; i++) check &= rm[i];
        rm.assign(n, 0);
        if (check) final_ans = min(final_ans, ans);
    }

    cout << final_ans;
}


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

#define ll long long int
#define pb push_back
#define f(i,x,y) for(i=x;i<y;i++)
#define vll vector<ll>
#define fast    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl

void solve()
{
    ll n,t,i,j=0,l=1,c=0;
    cin>>n>>t;

    vll level(55,0);
    c=0;
    l=1;
    f(i,1,11)
    {
        f(j,0,i)
        {
            level[c]=l;
            c++;
        }
        l++;
    }
    vector<float> g(55,0);

    while(t--)
    {
        queue<pair<ll,float>> q;
        q.push({0,1.0});
        while(q.empty()==0)
        {
            pair<ll,float> p=q.front();
            q.pop();

            if(p.first>=n*(n+1)/2)
                continue;
            if(g[p.first]!=1)
            {
                g[p.first]+=p.second;
            }
            else
            {
                q.push({p.first+level[p.first],p.second/2.0});
                q.push({p.first+level[p.first]+1,p.second/2.0});
            }
        }
        /*f(i,0,55)
        {
            cout<<g[i]<<" ";
        }
        cout<<endl;*/
    }
    ll ans=0;
    f(i,0,55)
    {
        if(g[i]==1)
            ans++;
    }
    cout<<ans<<endl;
}

int main()
{
    fast;
    ll t=1;
    //cin>>t;
    while(t--)
        solve();
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 55;
ll n,ans,pw[N],f[N][N * 2][2];int m,p;
ll sub(ll a,ll b) {return a >= b ? a - b : a;}
ll calc(ll s,ll q,int a,int b,int t)
{
    memset(f[p],0,sizeof(f[p]));f[p][0][0] = 1;
    for (int i = 1;i <= log2(s) + 1;i++)
    {
        int d = (s >> i) & 1;p ^= 1;memset(f[p],0,sizeof(f[p]));
        for (int j = 0;j <= i * 2 - 2;j++)
            for (int k = 0;k < 2;k++)
                if (f[p ^ 1][j][k])
                    for (int x = 0;x < 2;x++) if (!x || i < a)
                    for (int y = 0;y < 2;y++) if (!y || i < b)
                        if ((k + x + y) % 2 == d)
                            f[p][j + x + y][(k + x + y) / 2] += f[p ^ 1][j][k];
    }
    return f[p][t][0];
}
int main ()
{
    ios::sync_with_stdio(false);
    cin >> n;pw[0] = 1;ll x,ret;
    while (!m || pw[m - 1] <= n) pw[m + 1] = pw[m] << 1,++m;
    --m;
    for (int i = 1;i <= m;i++)
        if ((x = n / (pw[i] - 1)) > 0)
        {
            ret = n - x * (pw[i] - 1);
            for (int j = i - 1;j >= 0;--j) if (ret >= pw[j] - 1) ret = ret - (pw[j] - 1);
            if (!ret) ++ans;
        }
    for (int i = 1;i <= m;i++)
        for (int j = 1;pw[j] - 1 <= n;j++)
            if ((x = (n - pw[j] + 1) / (pw[i + 1] + pw[j + 1] - 3)) > 0)
            {
                ret = (n - pw[j] + 1) - x * (pw[i + 1] + pw[j + 1] - 3);
                if (!ret) {++ans;continue;}
                if (i == 1 && j == 1) {ans += (ret == 5 * x + 1);continue;}
                for (int k = 1;k <= i + j;k++)
                    if (~(ret + k) & 1) ans += calc(ret + k,x,i,j,k);
            }
    cout << ans << endl;
    return 0;
}


#include <iostream>
#include <string.h>
#include <cstdio>
#define ll long long

using namespace std;

ll C(ll n,ll m){
	ll ans=1;
	for(ll i=n;i>=n-m+1;i--)
		ans*=i;
	for(ll i=1;i<=m;i++)
		ans/=i;
	return ans;
}

int main(){
	ll n, k;
	//scanf ("%lld%lld", &n, &k);
	cin >> n >> k;
	if (k == 1)
		cout << "1";
	else if (k == 2)
		cout << 1 + C(n, 2);
	else if (k == 3)
		cout << 1 + C(n,  2) + C(n, 3) * 2;
	else{
		ll x = 1 + C(n, 2) + C(n, 3) * 2 + C(n, 4) * 9;
		cout << x;
	}
	return 0;
}


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

#define ms(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORd(i, a, b) for (int i = (a) - 1; i >= (b); i--)
#define FORall(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
#define sz(a) int((a).size())
#define present(t, x) (t.find(x) != t.end())
#define all(a) (a).begin(), (a).end()
#define uni(a) (a).erase(unique(all(a)), (a).end())
#define pb push_back
#define pf push_front
#define mp make_pair
#define fi first
#define se second
#define prec(n) fixed<<setprecision(n)
#define bit(n, i) (((n) >> (i)) & 1)
#define bitcount(n) __builtin_popcountll(n)
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vii;
const int MOD = (int) 1e9 + 7;
const int INF = (int) 1e9;
const ll LINF = (ll) 1e18;
const ld PI = acos((ld) -1);
const ld EPS = 1e-9;
inline ll gcd(ll a, ll b) {ll r; while (b) {r = a % b; a = b; b = r;} return a;}
inline ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
inline ll fpow(ll n, ll k, int p = MOD) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = r * n % p; n = n * n % p;} return r;}
template<class T> inline int chkmin(T& a, const T& val) {return val < a ? a = val, 1 : 0;}
template<class T> inline int chkmax(T& a, const T& val) {return a < val ? a = val, 1 : 0;}
inline ll isqrt(ll k) {ll r = sqrt(k) + 1; while (r * r > k) r--; return r;}
inline ll icbrt(ll k) {ll r = cbrt(k) + 1; while (r * r * r > k) r--; return r;}
inline void addmod(int& a, int val, int p = MOD) {if ((a = (a + val)) >= p) a -= p;}
inline void submod(int& a, int val, int p = MOD) {if ((a = (a - val)) < 0) a += p;}
inline int mult(int a, int b, int p = MOD) {return (ll) a * b % p;}
inline int inv(int a, int p = MOD) {return fpow(a, p - 2, p);}

struct Num {
	int a, b;
	Num() : a(0), b(0) {}
	Num(int a, int b) : a(a), b(b) {}
	Num(const Num& rhs) : a(rhs.a), b(rhs.b) {}
	Num operator + (const Num& rhs) const {
		Num res;
		res.a = a + rhs.a;
		if (res.a >= MOD) res.a -= MOD;
		res.b = b + rhs.b;
		if (res.b >= MOD) res.b -= MOD;
		return res;
	}
	Num operator - (const Num& rhs) const {
		Num res;
		res.a = a - rhs.a;
		if (res.a < 0) res.a += MOD;
		res.b = b - rhs.b;
		if (res.b < 0) res.b += MOD;
		return res;
	}
	Num operator * (const Num& rhs) const {
		Num res;
		res.a = ((long long) a * rhs.a + (long long) 5 * b * rhs.b) % MOD;
		res.b = ((long long) a * rhs.b + (long long) b * rhs.a) % MOD;
		return res;
	}
	Num operator ^ (long long k) {
		if (!k) return Num(1, 0);
		Num tmp = Num(a, b), res = Num(a, b); k--;
		while (k) {
			if (k & 1) res = res * tmp;
			tmp = tmp * tmp;
			k >>= 1;
		}
		return res;
	}
	friend Num geometricseries(Num a, long long k) {
		if (!k) return Num(1, 0);
		if (k == 1) return a;
		vector<int> bit;
		while (k) {
			bit.push_back(k & 1);
			k >>= 1;
		}
		Num res = a, tmp = a;
		for (int i = bit.size() - 2; i >= 0; i--) {
			res = res + (res * tmp);
			tmp = tmp * tmp;
			if (bit[i] & 1) {
				tmp = tmp * a;
				res = res + tmp;
			}
		}
		return res;
	}
};

const int maxk = 200 + 10;
int k;
long long l, r;
int c[maxk][maxk];
int s[maxk][maxk];
int fac[maxk];
Num n1[maxk];
Num n2[maxk];

int calc(long long x) {
	int res = 0;
	FOR(i, 1, k + 1) {
		FOR(j, 0, i + 1) {
			Num tmp = n1[j] * n2[i - j];
			tmp = geometricseries(tmp, x);
			if ((i - j) & 1) {
				tmp.a = (MOD - tmp.a) % MOD;
				tmp.b = (MOD - tmp.b) % MOD;
			}
			tmp = tmp * Num(c[j][i], 0);
			if (i & 1) {
				addmod(res, mult(s[k][i], mult(tmp.b, inv(fpow(5, i >> 1)))));
			}
			else {
				addmod(res, mult(s[k][i], mult(tmp.a, inv(fpow(5, i >> 1)))));
			}
		}
	}
	return res;
}

void solve() {
	FOR(i, 0, maxk) c[0][i] = 1;
	FOR(i, 1, maxk) FOR(j, 1, maxk) c[i][j] = (c[i][j - 1] + c[i - 1][j - 1]) % MOD;
	s[0][0] = 1;
	FOR(i, 1, maxk) FOR(j, 1, maxk) {
		submod(s[i][j], mult(i - 1, s[i - 1][j]));
		addmod(s[i][j], s[i - 1][j - 1]);
	}
	fac[0] = 1; FOR(i, 1, maxk) fac[i] = mult(fac[i - 1], i);
	n1[0] = n2[0] = Num(1, 0);
	n1[1] = Num(inv(2), inv(2)), n2[1] = Num(inv(2), MOD - inv(2));
	FOR(i, 1, maxk) n1[i] = n1[i - 1] * n1[1], n2[i] = n2[i - 1] * n2[1];
	cin >> k >> l >> r;
	int ans = calc(r + 2);
	submod(ans, calc(l + 1));
	ans = mult(ans, inv(fac[k]));
	cout << ans << "\n";
}

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef _LOCAL_
	freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout);
#endif
	solve();
#ifdef _LOCAL_
	//printf("\nTime elapsed: %dms", 1000 * clock() / CLOCKS_PER_SEC);
#endif
	return 0;
}


#include <iostream>
using namespace std;
int n,minn=0x3f3f3f3f,a[12];
int main() {
	cin>>n;
	for (int i=1;i<=n;i++) {
		cin>>a[i];
		minn=min(minn,a[i]);
	}
	cout<<(minn^a[3])+2<<endl;
	return 0;
}


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

const int N=20100;
int b[N], c[N], x, y, z, n;

int main()
{
    int i, j, l;
    while(scanf("%d%d%d%d", &n, &x, &y, &z)!=EOF)
    {
        if(x+y*2+z*4 < n)
        {
            printf("0\n");
            break;
        }
        n *= 2;
        for(i=0; i<=x; i++)
            b[i] = 1;
        for( ; i<N; i++)
            b[i] = 0;

        for(i=0; i<N; i++)
            c[i] = 0;
        for(i=0; i<N; i++)
            for(j=0; j+i<N && j<=y*2; j+=2)
                c[i+j] += b[i];
        for(i=0; i<N; i++)
            b[i] = c[i];

        for(i=0; i<N; i++)
            c[i] = 0;
        for(i=0; i<N; i++)
            for(j=0; j+i<N && j<=z*4; j+=4)
                c[i+j] += b[i];
        printf("%d\n", c[n]);
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#define p 1e6+7
#define N 10010
#define mc(a,b) memset(a,b,sizeof(a))
#define close std::ios::sync_with_stdio
using namespace std;
using namespace std;
const double eps(1e-8);
typedef long long lint;
const lint mod = 1000000007LL;
int n, m, g;
lint fac[200010];
void init()
{
    fac[0] = fac[1] = 1;
    for(int i = 2; i <= 200000; i++)
        fac[i] = fac[i - 1] * i % mod;
    return;
}

lint quick_pow(lint base, lint pow)
{
    lint ret = 1;
    while(pow)
    {
        if(pow & 1)
            ret = (ret * base) % mod;
        base = base * base % mod;
        pow >>= 1;
    }
    return ret;
}

lint C(int r, int k)//C[r][k]
{
    return fac[r]*quick_pow(fac[k]*fac[r - k] % mod, mod - 2LL) % mod;
}

int main()
{
    scanf("%d %d %d", &n, &m, &g);
    if(n == 0)
    {
        if(g == 0)
        {
            if(m == 1)
                printf("0\n");
            else
                printf("1\n");
        }
        else
        {
            if(m == 1)
                printf("1\n");
            else
                printf("0\n");
        }
        return 0;
    }
    if(m == 0)
    {
        if(g == 0)
        {
            if(n & 1)
                printf("1\n");
            else
                printf("0\n");
        }
        else
        {
            if(n & 1)
                printf("0\n");
            else
                printf("1\n");
        }
        return 0;
    }
    init();
    lint all = C(n + m, n);
    lint ans = 0;
    for(int t = 0; t <= n; t += 2)
        ans = (ans + C(n + m - 1 - t, m - 1)) % mod;
    if(m == 1 && (n & 1) == 0)
        ans = (ans - 1 + mod) % mod;
    if(m == 1 && (n & 1))
        ans = (ans + 1) % mod;
    if(g == 0)
        printf("%I64d\n", ans);
    else
        printf("%I64d\n", (all - ans + mod) % mod);
    return 0;
}


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

int main()
{
    int N;
    scanf("%d",&N);
    int ans=floor(log2(1.0*N)),res=N-(1<<ans-1);
    printf("%d\n",res==0?ans:ans+1);
}


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

using namespace std;

char c[10],d[10];
typedef long long ll;

ll cton(char ch) {
    if(ch >= '0' && ch <= '9') return ch-'0';
    return ch-'A'+10;
}

ll to_number(string s, ll base) {
    ll res = 0;
    ll pw = 1;
    for(int i = s.length()-1; i >= 0; --i) {
        int c = cton(s[i]);
        if(c >= base) return -1;
        res += c*pw;
        pw *= base;
    }
    return res;
}

int main() {
    string in, a, b;
    cin >> in;
    int j = 0;
    for(int i = 0; i < in.length(); ++i)
        if(in[i] == ':') j = i;
    a = in.substr(0, j), b = in.substr(j+1, in.length());
    vector<int> out;
    for(int i = 2; i < 100; ++i) {
        ll x = to_number(a, i), y = to_number(b, i);
        if(x != -1 && y != -1 && x < 24 && y < 60) out.push_back(i);
    }
    if(out.size()) {
        if(out.back() > 60) printf("-1");
        else for(int i = 0; i < out.size(); ++i) printf("%d ", out[i]);
    } else printf("0");
}


#include<bits/stdc++.h>
using namespace std;
const long long maxn=55;
const long long mod=10000007;
long long n,t,len,a[maxn],f[maxn][maxn];
void get(long long x,bool pa){
	len=0;
	while(x){
		if(pa)a[len+1]=x%2;
		x>>=1;
		len++;
	}
}
long long dp(long long pos,long long num,long long ask,bool limit){
	if(pos==0)return num==ask;
	if(f[pos][num]!=-1&&!limit)return f[pos][num];
	long long up=limit?a[pos]:1;
	long long ans=0;
	for(long long i=0;i<=up;i++){
		ans+=dp(pos-1,num+i,ask,(limit&&(i==up)));
	}
	return limit?ans:f[pos][num]=ans;
}
long long fsm(long long a,long long b){
	long long res=1;
	while(b){
		if(b&1)res=res*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return res%mod;
}
int main(){
	//freopen("test.cpp","r",stdin);
	cin>>n>>t;
	n++;
	get(n,1);
	if(t-(t&(-t))!=0){
		puts("0");
		return 0;
	}
	memset(f,-1,sizeof f);
	long long ans=dp(len,0,log2(t)+1,1);
	if(t==1)ans--;
	//for(long long i=1;i<=len;i++){
	//	memset(f,-1,sizeof f);
	//	ans*=fsm(i,dp(len,0,i,1));
	//	ans%=mod;
	//}
	cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MaxN = 2e3 + 23;

const int MOD = 998244353;

int pless, pgreater;
int CP[MaxN][MaxN];
int STRONG[MaxN];
int ans[MaxN];

int bpow(int a, int b)
{
    if(!b)
        return 1;
    int res = bpow(a, b >> 1);
    res = (res * 1ll * res) % MOD;
    if(b & 1)
        res = (res * 1ll * a) % MOD;
    return res;
}

void calcCP(int n, int s)
{
    if(!s)
        CP[n][s] = 1;
    else
    {
        CP[n][s] = (CP[n - 1][s - 1] * 1ll * bpow(pgreater, n - s)) % MOD + (CP[n - 1][s] * 1ll * bpow(pless, s)) % MOD;
        if(CP[n][s] >= MOD)
            CP[n][s] -= MOD;
    }
}

void calcStrong(int n)
{
    STRONG[n] = 1;
    if(n > 1)
    {
        for(int i = 1; i < n; ++i)
        {
            STRONG[n] -= (STRONG[i] * 1ll * CP[n][i]) % MOD;
            if(STRONG[n] < 0)
                STRONG[n] += MOD;
        }
    }
}

void calcAns(int n)
{
    if(n < 2)
        ans[n] = 0;
    else
    {
        for(int i = 1; i <= n; ++i)
        {
            ans[n] += STRONG[i] * 1ll * CP[n][i] % MOD * 1ll * (i * (n - i) + (i * (i - 1)) / 2 + (long long) (i < n ? ans[i] : 0) + ans[n - i]) % MOD;
            if(ans[n] >= MOD)
                ans[n] -= MOD;
        }
        int coef = 1 - (STRONG[n] * 1ll * CP[n][n]) % MOD;

        if(coef < 0)
            coef += MOD;

        ans[n] = (ans[n] * 1ll * bpow(coef, MOD - 2)) % MOD;
    }
}

main()
{
//    freopen("input.txt", "r", stdin);
    ios_base :: sync_with_stdio(false);
    cin.tie(NULL);

    int n, a, b;
    cin >> n >> a >> b;

    pless = (a * 1ll * bpow(b, MOD - 2)) % MOD;
    pgreater = ((b - a) * 1ll * bpow(b, MOD - 2)) % MOD;

    for(int i = 0; i <= n; ++i)
        for(int j = 0; j <= i; ++j)
            calcCP(i, j);

    for(int i = 0; i <= n; ++i)
        calcStrong(i);

    for(int i = 0; i <= n; ++i)
        calcAns(i);

    cout << ans[n] << '\n';

    return 0;
}


#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <map>
#include <set>

using namespace std;

#define inf 1000000000

typedef vector<int> vint;
typedef pair<int, int> pii;

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

int a[10]={}, b[10]={}, org[10]={};
char res1[200000]={}, res2[200000]={};

int main()
{
    char c;
    while (cin >> c) ++a[c-'0'];
    int l=0;
    for (int i=0; i<10; ++i) b[i]=a[i], l+=a[i], org[i]=a[i];

    int m_first, m_zeros, MAX=0;

    ++a[0], ++b[0];
    for (int z=0; a[0]>=0; ++z) {
        --a[0];
        --b[0];
        for (int i=5; i<10; ++i) {
            --a[i], --b[10-i];
            int cur = 1;
            if (a[i]>=0 && b[10-i]>=0) {
                cur += min(a[9], b[0]);
                cur += min(a[8], b[1]);
                cur += min(a[7], b[2]);
                cur += min(a[6], b[3]);
                cur += min(a[5], b[4]);
                cur += min(a[4], b[5]);
                cur += min(a[3], b[6]);
                cur += min(a[2], b[7]);
                cur += min(a[1], b[8]);
                cur += min(a[0], b[9]);
                if (cur+z>MAX) {
                    MAX=cur+z;
                    m_first=i;
                    m_zeros=z;
                }
            }
            ++a[i], ++b[10-i];
        }
    }

///    cout << MAX << " " << m_first;
    if (MAX==0 && org[0]>0) {
        for (int i=1; i<10; ++i) for (int j=0; j<a[i]; ++j) cout << char(i+'0');
                                 for (int j=0; j<org[0]; ++j) cout << '0';
        cout << endl;
        for (int i=1; i<10; ++i) for (int j=0; j<a[i]; ++j) cout << char(i+'0');
                                 for (int j=0; j<org[0]; ++j) cout << '0';
        return 0;
    }

    if (MAX==0) {
        for (int i=0; i<10; ++i) for (int j=0; j<a[i]; ++j) cout << char(i+'0');
        cout << endl;
        for (int i=0; i<10; ++i) for (int j=0; j<a[i]; ++j) cout << char(i+'0');
        return 0;
    }

    for (int i=0; i<10; ++i) a[i]=org[i];
    for (int i=0; i<10; ++i) b[i]=org[i];

    for (int i=0; i<m_zeros; ++i) {
        res1[l-i-1]='0';
        res2[l-i-1]='0';
        --a[0], --b[0];
    }

    res1[l-m_zeros-1] = m_first+'0';
    res2[l-m_zeros-1] = 10-m_first+'0';
    --a[m_first], --b[10-m_first];

    int cur_pos = l-m_zeros-1;
    for (int i=0; i<10; ++i)
        for (int j=0; min(a[i], b[9-i])>0; ++j) --cur_pos, res1[cur_pos]=i+'0',  res2[cur_pos]=9-i+'0', --a[i], --b[9-i];
    /*for (int i=0; i<10; ++i) {
        for (int j=0; j<min(a[9], b[0]); ++j) --cur_pos, res1[cur_pos]='9',  res2[cur_pos]='0', --a[9], --b[0];
        for (int j=0; j<min(a[8], b[1]); ++j) --cur_pos, res1[cur_pos]='8',  res2[cur_pos]='1', --a[8], --b[1];
        for (int j=0; j<min(a[7], b[2]); ++j) --cur_pos, res1[cur_pos]='7',  res2[cur_pos]='2', --a[7], --b[2];
        for (int j=0; j<min(a[6], b[3]); ++j) --cur_pos, res1[cur_pos]='6',  res2[cur_pos]='3', --a[6], --b[3];
        for (int j=0; j<min(a[5], b[4]); ++j) --cur_pos, res1[cur_pos]='5',  res2[cur_pos]='4', --a[5], --b[4];
        for (int j=0; j<min(a[4], b[5]); ++j) --cur_pos, res1[cur_pos]='4',  res2[cur_pos]='5', --a[4], --b[5];
        for (int j=0; j<min(a[3], b[6]); ++j) --cur_pos, res1[cur_pos]='3',  res2[cur_pos]='6', --a[3], --b[6];
        for (int j=0; j<min(a[2], b[7]); ++j) --cur_pos, res1[cur_pos]='2',  res2[cur_pos]='7', --a[2], --b[7];
        for (int j=0; j<min(a[1], b[8]); ++j) --cur_pos, res1[cur_pos]='1',  res2[cur_pos]='8', --a[1], --b[8];
        for (int j=0; j<min(a[0], b[9]); ++j) --cur_pos, res1[cur_pos]='0',  res2[cur_pos]='9', --a[0], --b[9];
    }*/
    int n1=0, n2=0;
    for (int i=0; i<cur_pos; ++i) {
        while(a[n1]<=0) ++n1;
        while(b[n2]<=0) ++n2;
        res1[i]=n1+'0';
        res2[i]=n2+'0';
        --a[n1];
        --b[n2];
    }

    cout << res1 << endl << res2;
    return 0;
}


#include <string>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>

#include <tuple>

void re(int& x);

template<class T, class... Ts> void re(T& t, Ts&... ts);

void pr(int x);

void pr(const char *x);

void ps();
template<class T, class... Ts> void ps(const T& t, const Ts&... ts);

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

int n, p;
int c[51];
int dp[51][51][51];
int binom[61][61];
int p2[51];
const int mod = 1000000007;

int rec(int cur, int bp, int wp) {
    if (cur == n) return (bp+wp)%2 == p;

    int &ret = dp[cur][bp][wp];
    if (ret == -1) {
        ret = 0;

        for (int cc = 0; cc <= 1; cc++) {
            if (c[cur] != -1 && c[cur] != cc) continue;

            int od;
            if (cc == 0) od = wp;
            else od = bp;
            int ev = cur - od;

            for (int con_od = 0; con_od <= od; con_od++) {
                int nbp = bp, nwp = wp;
                if (con_od % 2 == 0) {
                    if (cc == 0) nbp++;
                    else nwp++;
                }

                long long th = (rec(cur+1, nbp, nwp) * 1ll * p2[ev]) % mod;
                th = (th * 1ll * binom[od][con_od]) % mod;
                ret = (ret + th) % mod;
            }
        }

        //printf("%d %d %d -> %d\n", cur,bp,wp,ret);
    }
    return ret;
}

void solve()
{
    memset(dp,-1,sizeof(dp));
    re(n,p);
    rep(i,0,n) re(c[i]);

    p2[0] = 1;
    for (int i = 1; i <= 50; i++) p2[i] = (p2[i-1] + p2[i-1]) % mod;

    for (int i = 0; i <= 60; i++) {
        for (int j = 0; j <= i; j++) {
            if (j == 0) binom[i][j] = 1;
            else binom[i][j] = (binom[i-1][j-1] + binom[i-1][j]) % mod;
        }
    }

    ps(rec(0,0,0));
}

int main() {

    solve();
}

void re(int& x) { scanf("%d", &x); }

template<class T, class... Ts> void re(T& t, Ts&... ts) {
    re(t); re(ts...);
}

void pr(int x) { printf("%d", x); }

void pr(const char *x) { printf("%s", x); }

void ps() { pr("\n"); }
template<class T, class... Ts> void ps(const T& t, const Ts&... ts) {
    pr(t); if (sizeof...(ts)) pr(" "); ps(ts...);
}


#include <bits/stdc++.h>
#define endl '\n'

//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")

using namespace std;
template<class T, class T2> inline int chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline int chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
const int MAXN = (1 << 20);
const int MAXM = 500;
const int bound = (int)1e6;
const int MAGIC = 24;

int lp[MAXN];
vector<int> pr;
set<vector<int> > st;

void prepare()
{
	for(int i = 2; i <= bound; i++)
	{
		if(lp[i] == 0) lp[i] = i, pr.push_back(i);
		for(int j = 0; j < (int)pr.size() && i * 1ll * pr[j] <= bound && pr[j] <= lp[i]; j++)
			lp[i * pr[j]] = pr[j];
	}
}

vector<vector<int> > li;

vector<int> get(int i)
{
	int x = i;
	vector<int> curr;
	while(x != 1)
	{
		int d = lp[x], cnt = 0;
		while(x % d == 0) x /= d, cnt++;
		curr.push_back(cnt);
	}

	sort(curr.begin(), curr.end());
	return curr;
}

void read()
{
	for(int i = 1; i <= bound; i++)
	{
		int x = i;
		vector<int> curr;
		while(x != 1)
		{
			int d = lp[x], cnt = 0;
			while(x % d == 0) x /= d, cnt++;
			curr.push_back(cnt);
		}

		sort(curr.begin(), curr.end());
		st.insert(curr);
	}

	for(auto it: st) li.push_back(it);
}

int N, K;
int dp[MAXM][1 << 16];

int get_id(const vector<vector<int> > &li, vector<int> p)
{
	return lower_bound(li.begin(), li.end(), p) - li.begin();
}

vector<pair<int, int> > same;

vector<vector<int> > all_vec;
vector<int> curr_Vec;

void gen(int sum, int last = 1)
{
	all_vec.push_back(curr_Vec);
	if(sum == 0) return;

	for(int curr = last; curr <= sum; curr++)
	{
		curr_Vec.push_back(curr);
		gen(sum - curr, curr);
		curr_Vec.pop_back();
	}
}

int dist[MAXN];
vector<int> adj[MAXN];
vector<int> possible_divs;

int divs(vector<int> &l)
{
	int r = 1;
	for(int i: l)
		r *= (i + 1);
	return r;
}

int get_pos(int x) { return lower_bound(possible_divs.begin(), possible_divs.end(), x) - possible_divs.begin(); }

int D[MAXN];

void solve()
{
	gen(MAGIC);
	N = li.size();
	K = all_vec.size();

	for(int i = 0; i < K; i++)
	{
		for(int p = 0; p < (int)all_vec[i].size(); p++)
		{
			{
				vector<int> nw = all_vec[i];
				nw[p]--;

				sort(nw.begin(), nw.end());
				if(*nw.begin() == 0)
					nw.erase(nw.begin());

				int id = get_id(all_vec, nw);
				if(id != K && all_vec[id] == nw)
				{
					adj[i].push_back(id);
					adj[id].push_back(i);
				}
			}

			{
				vector<int> nw = all_vec[i];
				nw[p]++;
				sort(nw.begin(), nw.end());

				int id = get_id(all_vec, nw);
				if(id != K && all_vec[id] == nw)
				{
					adj[i].push_back(id);
					adj[id].push_back(i);
				}
			}
		}

		{
			vector<int> nw = all_vec[i];
			nw.push_back(1);
			sort(nw.begin(), nw.end());

			int id = get_id(all_vec, nw);
			if(id != K && all_vec[id] == nw)
			{
				adj[i].push_back(id);
				adj[id].push_back(i);
			}
		}
	}

	for(int i = 0; i < K; i++)
		possible_divs.push_back(divs(all_vec[i]));

	sort(possible_divs.begin(), possible_divs.end());
	possible_divs.erase(unique(possible_divs.begin(), possible_divs.end()), possible_divs.end());

	for(int i = 0; i < K; i++)
		D[i] = get_pos(divs(all_vec[i]));

	for(int ii = 0; ii < N; ii++)
	{
		int root = get_id(all_vec, li[ii]);
		for(int i = 0; i < K; i++)
			dist[i] = -1;

		queue<int> q;
		q.push(root);
		dist[root] = 0;

		while(!q.empty())
		{
			int u = q.front();
			q.pop();

			for(int v: adj[u])
				if(dist[v] == -1)
				{
					dist[v] = dist[u] + 1;
					q.push(v);
				}
		}

		for(int iii = 0; iii < (int)possible_divs.size(); iii++) dp[ii][iii] = MAXN;
		for(int j = 0; j < K; j++)
			chkmin(dp[ii][D[j]], dist[j]);
	}

	int t;
	cin >> t;

	while(t--)
	{
		int a, b;
		cin >> a >> b;
		vector<int> A = get(a), B = get(b);

		a = lower_bound(li.begin(), li.end(), A) - li.begin();
		b = lower_bound(li.begin(), li.end(), B) - li.begin();

		int answer = MAXN;
		for(int i = 0; i < (int)possible_divs.size(); i++)
			chkmin(answer, dp[a][i] + dp[b][i]);

		cout << answer << endl;
	}
}

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

	prepare();

	read();
	solve();
	return 0;
}


#include <bits/stdc++.h>
#define len(x) ((int)(x).size())
#define x first
#define y second
using namespace std;

const int K = 1010, MOD = 1e9 + 7;

int powmod(int a, int x){
	if(x == 0) return 1;
	if(x%2 == 0) return powmod(a*1LL*a%MOD, x/2);
	return a*1LL*powmod(a, x - 1)%MOD;
}

int dp[K][K];

int main(){
	int k, va, vb;
	scanf("%d %d %d", &k, &va, &vb);

	int den = powmod(va + vb, MOD - 2);
	int pa = va*1LL*den%MOD;
	int pb = vb*1LL*den%MOD;
	int one_b = pa*1LL*powmod(pb, MOD - 2)%MOD;

	for(int i = k - 1; i >= 0; i--){
		for(int j = k; j >= 1; j--){
			if(i + j >= k){
				dp[i][j] = (i + j + one_b)%MOD;
			}else{
				dp[i][j] = (pa*1LL*dp[i][j + 1] + pb*1LL*dp[i + j][j])%MOD;
			}
		}
	}
	printf("%d\n", dp[0][1]);
	return 0;
}


#include<bits/stdc++.h>

#define int long long
#define LG 16
#define all(x) (x).begin(), (x).end()
#define deb(x) if((x) > 100000000) while(true);

using namespace std;

int c[20][20], k, t, deep, a[20], f[20][20], size;

void ccout(int x) {
    if (x < 10)
        cout << (char)(x + '0');
    else
        cout << (char)(x - 10 + 'a');
}

int dp(int x) {
    for (int i = 0; i <= LG; i++)
        f[0][i] = 1;
    for (int i = 1; i <= x; i++)
        for (int j = 1; j <= LG; j++) {
            f[i][j] = 0;
            for (int k = 0; k <= min(i, a[j]); k++)
                f[i][j] += f[i - k][j - 1] * c[i][k];
        }
    return f[x][LG];
}

void solve(int vert, int par) {
    if (!par) return;
    for (int i = par == deep ? 2 : 1; i <= LG; i++) {
        if (!a[i]) continue;
        a[i]--, size = dp(par - 1);
        if (size >= vert) {
            ccout(i - 1); solve(vert, par - 1); break;
        }
        vert -= size, a[i]++;
    }
}
int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> k >> t;
    c[1][0] = c[1][1] = 1;
    for (int i = 2; i <= 10; i++) {
        c[i][0] = 1;
        for (int j = 1; j <= i; j++) c[i][j] = c[i - 1][j] + c[i - 1][j - 1];
    }
    for (int i = 1; i <= LG; i++) a[i] = t;
    deep = 1;
    while (true) {
        a[1]--; size = dp(deep - 1) * 15; a[1]++;
        if (size >= k) break; deep++;
        k -= size;
    }
    solve(k, deep);
    return 0;
}


// Author : Mohamed Sameh
#include<bits/stdc++.h>
using namespace std ;

typedef long long ll ;
////////////////////////////////////////////////////////////////////
#define pb push_back
#define f first
#define s second
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
///#Read And Write  From File
/*

  freopen("in.txt","r",stdin);
  freopen("out.txt","w",stdout);

*/
int ReadInt ()
{
    int t ;
    cin >> t ;
    return t ;
}
ll ReadLong ()
{
    ll t ;
    cin >> t ;
    return t ;
}
bool Check (string t )
{
    int l = 0 ;
    int r = t.size() -1 ;
    while (l <= r )
    {
        if ( t[l] != t[r]) return false ;
        l++;
        r--;
    }
    return true ;
}
bool check_User (string t )
{
    int c = 0, C = 0;
    bool Next = 0 ;
    int sc = 0 ;
    for (int i = 0 ; i < t.size() ; i ++ )
    {
        if ( t[i] == '@' )
        {
            if (i+1 == t.size() ) return false ;
            Next =  (c > 0 && c < 17 );
            sc = i+1 ;
            break ;
        }
        if (   !( ( t[i] >= 'a' && t[i] <= 'z' ) || ( t[i] >= 'A' && t[i] <= 'Z' ) ||  ( t[i] >= '0' && t[i] <= '9' )|| t[i] == '_'    ) )
            return false ;
        c++;
    }

    if ( Next )
    {
        Next = 0;
        bool SNext = 0 ;
        c = 0 ;
        C = 0 ;
        bool Turn = 0 ;
        bool Slash = 0;
        for (int i = sc ; i < t.size() ; i++)
        {
            if ( i+1 == t.size() && t[i] == '.')return false ;
            if ( t[i] == '/')
            {
                if ( i-1 > 0 && t[ i-1 ] == '.') return false ;
                Slash = 1;
                if ( Turn == 0 )
                    SNext = 1 ;
                sc = i+1 ;
                break ;
            }

            if (t[i] == '.'  )
            {
                Turn = 1 ;
                if (c >0 && c < 17 )
                {
                    c=0;
                    continue ;
                }else
                return false ;

            }

            if (!( ( t[i] >= 'a' && t[i] <= 'z' ) || ( t[i] >= 'A' && t[i] <= 'Z' ) ||  ( t[i] >= '0' && t[i] <= '9' )|| (t[i] == '_')    ) )
                return false ;
            c++;

        }

        ///
        if (!SNext && Slash )
        {

            c = 0 ;
            for (int i =sc ; i < t.size() ; i++)
            {

                if (!( ( t[i] >= 'a' && t[i] <= 'z' ) || ( t[i] >= 'A' && t[i] <= 'Z' ) ||  ( t[i] >= '0' && t[i] <= '9' )|| (t[i] == '_')    ) )
                    return false ;
                c++;
            }
            if ( c>0 && c < 17  ) return true ;
            return false ;

        }
        else
        {
            if (Slash)
            {
                c = 0 ;
                for (int i =sc ; i < t.size() ; i++)
                {

                    if (!( ( t[i] >= 'a' && t[i] <= 'z' ) || ( t[i] >= 'A' && t[i] <= 'Z' ) ||  ( t[i] >= '0' && t[i] <= '9' )|| (t[i] == '_')    ) )
                        return false ;
                    c++;
                }

                if ( c>0 && c < 17  ) return true ;
                return false ;
            }
            return true ;
        }

    }
    else
    {

        return false  ;
    }

}
int main ()
{

    string Id ;
    cin >> Id ;
    if ( check_User(Id))
        cout <<"YES\n";
    else
        cout <<"NO\n";

    return 0 ;
}