#define G 3
#include<map>
#define AK goto
#include<cmath>
#include<queue>
#include<bitset>
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
#define md ((l+r)>>1)
#define cht 1000000007
#define inf 1145141919810
#define mem(i,j)memset(i,j,sizeof(i))
#define F(i,j,n)for(int i=j;i<=n;i++)
#define D(i,j,n)for(int i=j;i>=n;i--)
using namespace std;
namespace sky_chen{
//	char buf[1<<23],*p1=buf,*p2=buf,obuf[1<<23],*O=obuf;
//	#define gc() p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++
	inline ll read(){ll ans=0,f=1;char a=getchar();while(a>'9'||a<'0'){if(a=='-')f=-1;a=getchar();}while(a>='0'&&a<='9')ans=ans*10+(a^48),a=getchar();return ans*f;}
	inline char red(){char a=getchar();while(a>'z'||a<'a')a=getchar();return a;}
//	inline void print(ll x){if(x>9)print(x/10);*O++=x%10+'0';}
	inline void cadd(ll &x,ll y){x=(x+y)%cht;}
	inline void cmin(ll &x,ll y){x=min(x,y);}
	inline void cmax(ll &x,ll y){x=max(x,y);}
	ll qpow(ll n,ll base=cht-2){ll ans=1;while(base){if(base&1)ans=ans*n%cht;n=n*n%cht;base>>=1;}return ans;}
	const int maxn = 1e6+5;
	const int maxc = 3e5+5;
	ll n,x[maxn],y[maxn];
	struct node{
		ll a=0,b=0,c=0;
	}p1,p2,t,f[maxn],g[maxn];
	int yyds(){
		n=read();
		F(i,1,n)x[i]=read(),y[i]=read();
		ll m=read();
		p1.a=1;p2.b=1;
		f[0]=p1;g[0]=p2;
		F(i,1,m){
			ll opt=read();
			if(!(opt-1)){
				t=p1;p1=p2;p2.a=-t.a;p2.b=-t.b;p2.c=-t.c;
				f[i]=p1;g[i]=p2;
			}
			else if(!(opt-2)){
				t=p2;p2=p1;p1.a=-t.a;p1.b=-t.b;p1.c=-t.c;
				f[i]=p1;g[i]=p2;
			}
			else if(!(opt-3)){
				p1.c=2*read()-p1.c;p1.b=-p1.b;p1.a=-p1.a;
				f[i]=p1;g[i]=p2;
			}
			else{
				p2.c=2*read()-p2.c;p2.b=-p2.b;p2.a=-p2.a;
				f[i]=p1;g[i]=p2;
			}
		}
//		F(i,0,m)cout<<f[i].a<<' '<<f[i].b<<' '<<f[i].c<<' '<<g[i].a<<' '<<g[i].b<<' '<<g[i].c<<endl;
		ll q=read();
		F(i,1,q){
			ll sb1=read(),sb2=read();
			ll za=x[sb2],zb=y[sb2];
			ll nza=f[sb1].a*za+f[sb1].b*zb+f[sb1].c,nzb=g[sb1].a*za+g[sb1].b*zb+g[sb1].c;
			printf("%lld %lld\n",nza,nzb);
		}
	    return 0;
	}
}
int main(){return sky_chen::yyds();}

//sky_chen txdy!

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=(n)-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define vtpl(x,y,z) vector<tuple<x,y,z>>
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[9]={0,1,-1,0,1,1,-1,-1,0};
const ll dx[9]={1,0,0,-1,1,-1,1,-1,0};
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
int main(){
    ll n;cin >> n;
    cout << (1<<n)-1 <<endl;
    repl(bit,1,1<<n){
        rep(cit,1<<n){
            if(__builtin_popcount(bit&cit)%2){
                cout << "A";
            }
            else cout << "B";
        }
        cout << endl;
    }
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define vl vector<ll>
#define vs vector<string>
#define mll map<ll,ll>
#define pll pair<ll,ll>
#define sl set<ll>
#define pql priority_queue<ll>

#define rep(i,n) for(i=0;i<n;i++)
#define repr(i,n) for(i=n-1;i>=0;i--)
#define REP(i,a,b) for(i=a;i<b;i++)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(), a.rend()
#define vsort(a) sort(all(a))
#define rvsort(a) sort(rall(a))

#define F first
#define S second
#define pi acos(-1)
#define inf 10000000000000000
#define mod 1000000007
#define MA LLONG_MAX
#define MI LLONG_MIN

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

long modInverse(long long n)
{
    return power(n, mod - 2);
}

long long ncr(long long n, ll r)
{
    if (n < r)
        return 0;
    if (r == 0)
        return 1;
    long long fac[n + 1];
    fac[0] = 1;
    for (int i = 1; i <= n; i++)
        fac[i] = (fac[i - 1] * i) % mod;

    return (fac[n] * modInverse(fac[r]) % mod*modInverse(fac[n - r])% mod)% mod;
}

void solve()
{
    ll n,m,i,j,ans=0,fans=0;
    cin>>n>>m;
    vl a(m),b(m);
    rep(i,m)
    cin>>a[i]>>b[i];
    ll k;
    cin>>k;
    vl c(k),d(k);
    rep(i,k)
    {
        cin>>c[i]>>d[i];
    }
    ll temp=pow(2,k)-1;
    for(i=0;i<=temp;i++)
    {
        bitset<20> bb(i);
        vl count(n+1,0);
        ans=0;
        for(j=0;j<k;j++)
        {
            if(bb[j]==0)
            count[c[j]]++;
            else
            count[d[j]]++;
        }
        for(j=0;j<m;j++)
        {
            if(count[a[j]]>0&&count[b[j]]>0)
            ans++;
        }
        //cout<<ans<<" ";
        fans=max(fans,ans);
    }
    cout<<fans<<endl;
}

int32_t main() {
	ios_base::sync_with_stdio(false);
   cin.tie(NULL); cout.tie(NULL);
// #ifdef ONLINE_JUDGE
// 	freopen("input.txt", "r", stdin);
// 	freopen("output.txt", "w", stdout);
// #endif

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

#include <bits/stdc++.h>
#include <vector>
#include<math.h>
#include<string.h>
using namespace std;
#define MAX 300005
#define MOD 1000000007
#define GMAX 19
#define INF 20000000000000000
#define EPS 0.000000001
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL)
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
int dishes[MAX];
pair<int,int> conditions[MAX];
int add[20][2];
int ans;
void backtrack(int index,int k,int m)
{
    int i;
    if(index==k)
    {
        int curr=0;
        for(i=0;i<m;i++)
        {
            curr=curr+(dishes[conditions[i].first]>0&&dishes[conditions[i].second]>0);
        }
        ans=max(ans,curr);
        return;
    }
    for(i=0;i<2;i++)
    {
        dishes[add[index][i]]++;
        backtrack(index+1,k,m);
        dishes[add[index][i]]--;
    }
}
int main()
{
    int n,m,k,i,j;
    scanf("%d %d",&n,&m);
    for(i=0;i<m;i++)
    {
        scanf("%d %d",&conditions[i].first,&conditions[i].second);
    }
    scanf("%d",&k);
    for(i=0;i<k;i++)
    {
        for(j=0;j<2;j++)
        {
            scanf("%d",&add[i][j]);
        }
    }
    backtrack(0,k,m);
    printf("%d",ans);
    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#include <complex>
#include <queue>
#include <set>
#include <unordered_set>
#include <list>
#include <chrono>
#include <random>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <stack>
#include <iomanip>
#include <fstream>
#include<cstring>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
typedef pair<ll,ll> pll;
typedef vector<ll> vll;
typedef vector<vll> vvl;
typedef vector<pll> vpll;

#define endl '\n'
#define fast std::ios_base::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define mp make_pair

void solve(){
   ll n;
   cin>>n;
   pair<ll,ll> p[n];
   ll t[n];
   for(int i=0;i<n;i++){
      cin>>t[i]>>p[i].first>>p[i].second;
   }
   int ans=0;
   for(int i=0;i<n;i++){
      for(int j=i+1;j<n;j++){

         if(p[i].first>p[j].second || p[i].second<p[j].first)continue;
         if(p[i].first==p[j].second){
            if(!((t[i]==1 || t[i]==2)&&(t[j]==1 || t[j]==3)))continue;
         }
         if(p[i].second==p[j].first){
            //cout<<"*"<<endl;
            if(!((t[j]==1 || t[j]==2)&&(t[i]==1 || t[i]==3)))continue;
         }
         ans++;
        // cout<<i<<" "<<j<<endl;
      }
   }
   cout<<ans<<endl;
}

int main(){
   fast;
   //clock_t begin = clock();
   ll tc=1;//cin>>tc;
   for(ll i=1;i<=tc;i++){
       //cout<<"Case #"<<i<<": ";
       solve();
   }
   //clock_t end = clock();double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;cerr << elapsed_secs;

}

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i = 0; i < n; i++)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define ALL(x) (x).begin(),(x).end()
#define SIZE(x) ((ll)(x).size())
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
#define INF 1e9
typedef long long ll;
typedef long double ld;

int main(){
  ll n;
  cin >> n;
  vector<pair<double,double>> v(n);
  REP(i,n){
    ll t;
    double l, r;
    cin >> t >> l >> r;
    if(t == 2)r-=0.1;
    else if(t==3)l+=0.1;
    else if(t==4){
      l+=0.1;
      r-=0.1;
    }
    v[i] = {l,r};
  }
  ll cnt = 0;
  REP(i,n-1){
    FOR(j,i+1,n){
      if(v[j].first <= v[i].second && v[i].first <= v[j].second)cnt++;
    }
  }
  cout << cnt << endl;
  return 0;
}


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

typedef long long LL;
#define int LL
const int N = 300;
int c[N][N];
signed main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  c[0][0] = 1;
  for (int i = 1; i < N; i++) {
    c[i][0] = 1;
    for (int j = 1; j <= i; j++) {
      c[i][j] = c[i - 1][j]+ c[i - 1][j - 1];
    }
  }

  int n;
  cin >> n;
  cout << c[n - 1][11] << endl;
  return 0;
}

#include<iostream>
#include<vector>
#include<algorithm>
#include<map>
#include<set>
using namespace std;
 using ll = long long;
void solve(ll n, ll r)
{

	long long p = 1, k = 1;

	if (n - r < r)
		r = n - r;

	if (r != 0) {
		while (r) {
			p *= n;
			k *= r;

			long long m = __gcd(p, k);
			p /= m;
			k /= m;

			n--;
			r--;
		}
	}

	else
		p = 1;

	cout << p << endl;
}

signed main()
{
	ll n;
	cin >> n;
	n--;

	solve(n, 11);

	return 0;
}

// ll fact(ll n);

// ll nCr(ll n, ll r)
// {
// 	cout << fact(n) << " " << fact(r) << " ";
//     return fact(n) / (fact(r) * fact(n - r));
// }

// ll fact(ll n)
// {
//     ll res = 1;
//     for (ll i = 2; i <= n; i++)
//         res = res * i;
//     return res;
// }

// ll main()
// {
//     ll n,r;
//     cin >> n;
//     n--;
//     cout << nCr(n,11);
//     return 0;
// }


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld long double
#define inf 2000000000
#define infLL 2000000000000000000
#define MAX5 100005
#define MAX6 1000006
#define MAX7 10000007
#define sf(a) scanf("%d", &a)
#define sfl(a) scanf("%lld", &a)
#define sfs(a) scanf("%s", a)
#define sline(a) scanf("%[^\n]%*c", a);
#define pf(a) printf("%d\n", a)
#define pfl(a) printf("%lld\n", a)
#define pfs(a) printf("%s\n", a)
#define Case(t) printf("Case %d: ", t)
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mod 1000000007
#define Mod 998244353
#define PI acos(-1.0)
#define eps 1e-9
#define mem(a, b) memset(a, b, sizeof(a))
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);

const int N = MAX5;

int main()
{
    //#ifndef ONLINE_JUDGE
    //    freopen("in.txt","r",stdin);
    //    freopen("out.txt","w",stdout);
    //#endif

    FASTIO;
    ll a, b, c, d;
    cin>>a>>b>>c>>d;
    if(a==0) cout<<0<<endl;
    else {
        ll mx = a, cnt = 5000000, cc = 0, ans = -1;
        for(int i = 1; i <= cnt; i++) {
            a += b;
            cc += c;
            if(a <= (cc * d)) {
                ans = i;
                break;
            }
            ll dif = a - (cc * d);
            if(dif > mx) {
                break;
            }
            mx = dif;
        }
        cout<<ans<<endl;
    }

    return 0;
}

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

#define ll long long int
#define pb(a) push_back(a)
#define vll vector<ll>
#define loop(i, n) for(ll i=1;i<=n;i++)
#define loop0(i, n) for(ll i=0;i<n;i++)
#define in(i) scanf("%lld", &i);
#define out(i) printf("%lld", i)
#define pll pair<ll, ll>
#define vpll vector<pair<ll, ll>>
#define mp(i, j) make_pair(i, j)
#define google cout<<"Case #"<<tc-t<<": ";
const ll mod = 1000000007;
const ll big = 2999999999999999999;
const ll small = -2999999999999999999;
void in2(ll& a, ll& b) { in(a); in(b); }
void in3(ll& a, ll& b, ll& c) { in(a); in(b); in(c); }
void swap(ll& a, ll& b) { a = a+b; b = a-b; a = a-b; }
void arrayIn(vll& a, ll& n) { loop0(i, n) in(a[i]); }

ll n, k;
vpll a;

void solve()
{
  in2(n, k);
  a.resize(n);
  loop0(i, n)
    in2(a[i].first, a[i].second);
  sort(a.begin(), a.end());
  ll rem = k;
  ll cv = 0;
  loop0(i, n)
  {
    if(cv + rem < a[i].first)
    {
      cv += rem;
      rem = 0;
      break;
    }
    else
    {
      rem -= (a[i].first-cv);
      rem += a[i].second;
      cv = a[i].first;
    }
  }
  cv += rem;
  cout<<cv<<"\n";
}

int main()
{
    solve();
}


#include <bits/stdc++.h>
#include <fstream>
#include <cstdio>
using namespace std;
// vector<pair<int,int> > v;
// cin.ignore();//twice getline(cin,s);
// g++ iterator.cpp -std=c++17
// cout<<(A + B == C ? "YES" : "NO")<<endl;
// __gcd(a,b)
// string s=to_string(n);
// string p=s.substr(0,len);
// string q=s.insert(0,str);
// sort(arr, arr+n, greater<int>());
// cout<<fixed<<setprecision(9)<<pi<<endl
// const  double pi = acos(-1.0);
// sort(v.rbegin(),v.rend());
#define ff first
#define ss second
#define pb push_back
#define mk make_pair
#define vll vector<ll>
#define mll map<ll,ll >
#define mlli map<ll,ll>::iterator
#define size size()
#define endl "\n"
#define ll long long int
#define ld long double
void fast(){ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);}
bool comp(int a,int b)
{
	return (a<b);
	// cout<<min({a,b,c,d},comp)<<endl;
}

int main(void)
{
	fast();
	ll n;
	cin>>n;
	ll x;
	map<ll,ll> m;
	for(ll i=0;i<n;i++)
	{
		cin>>x;
		m[x%200]++;
	}
	ll ans = 0;
	for(auto it=m.begin();it!=m.end();it++)
	{
		ans += ((it->second)*(it->second-1))/2;
	}
	cout<<ans<<endl;
}

/** I can do this all day **/

#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;

typedef long long                   ll;
typedef long double                 ld;
typedef pair<int,int>               pii;
typedef pair<ll,ll>                 pll;
#define all(x)                      (x).begin(),(x).end()
#define F                           first
#define S                           second
#define Mp                          make_pair
#define SZ(x)			    		(int)x.size()
#define fast_io                     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io                     freopen("in.txt" , "r+" , stdin) ; freopen("out.txt" , "w+" , stdout);

const int N = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll mod2 = 998244353;
const ll inf = 8e18;
const int LOG = 22;

ll pw(ll a , ll b, ll M)  { return (!b ? 1 : (b & 1 ? (a * pw(a * a % M, b / 2, M)) % M : pw(a * a % M, b / 2, M))); }

int n, A[N];

bitset < N > bt;

int main()
{
	cin >> n;
	bt[0] = 1;
	int sum = 0, tot = 1e9;
	for(int i = 1; i <= n; i ++)
	{
		cin >> A[i];
		bt |= bt << A[i];
		sum += A[i];
	}
	for(int i = 0; i < N ;i ++)
	{
		if(bt[i])
		{
			tot = min(tot, max(i, sum - i));
		}
	}
	cout << tot << endl;
    return 0;
}

/** test corner cases(n = 1?) watch for overflow or minus indices **/


#include <bits/stdc++.h>
#include <cmath>
#include <limits>

using namespace std;
typedef long long LL;
static long long INF = (1LL<<62);

int isPrime(LL p){
	if(p<=1) return 0;
	if(p==2 || p==3 || p==5 || p==7) return 1;
	if(p==4 || p==6 || p==8 || p==9) return 0;

	LL i;
	int ret = 1;
	for(i=2; i*i<=p; i++){
		if(p%i==0 && i!=p){
			ret = 0;
			break;
		}
	}
	return ret;
}

void _factorize(long long N, map<long long, int> &mp){
	if(N == 1){
		return;
	}
	if(isPrime(N)){
		mp[N] += 1;
		return;
	}

	long long i = 2, n = N;
	for(i = 2; n != 1 && i < (N/2) + 1; i++){
		if((n % i)==0){
			if(isPrime(i)){
				int e = 0;
				long long u = n;
				while((u % i)==0){
					e++;
					u = u / i;
					n = u;
				}
				mp[i] = e;
				if(isPrime(n)){
					mp[n] = 1;
					break;
				}
			}
		}
	}
	return;
}

vector<LL> A;
void dfs(map<long long, int> mp, vector<int> v){
	if(v.size() == mp.size()){
		LL d = 1;
		int i= 0;
		for(auto itr = mp.begin(); itr != mp.end(); itr++){
			LL p = itr->first;
			int e = v[i];
			for(int j=0; j<e; j++){
				d *= p;
			}
			i++;
		}
		A.push_back(d);
		return;
	}

	int s = v.size();
	int i = 0, e = -1;
	for(auto itr = mp.begin(); itr != mp.end(); itr++){
		if(i == s){
			e = itr->second;
			break;
		}
		i++;
	}
	for(int j=0; j<=e; j++){
		v.push_back(j);
		dfs(mp, v);
		v.pop_back();
	}
	return;
}

int main(int argc, char* argv[]){
	cin.tie(0);
	ios::sync_with_stdio(false);

	LL N; cin >> N;
	map<long long, int> mp;
	_factorize(N, mp);

	vector<int> v;
	dfs(mp, v);
	sort(A.begin(), A.end());
	for(int i=0; i<A.size(); i++){
		printf("%lld\n", A[i]);
	}
	return 0;
}

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

vector<long long> enum_divisors(long long N) {
    vector<long long> res;
    for (long long i = 1; i * i <= N; ++i) {
        if (N % i == 0) {
            res.push_back(i);
            // 重複しないならば i の相方である N/i も push
            if (N/i != i) res.push_back(N/i);
        }
    }
    // 小さい順に並び替える
    sort(res.begin(), res.end());
    return res;
}

int main() {
    long long N;
    cin >> N;
    const auto &res = enum_divisors(N);
    for (int i = 0; i < res.size(); ++i) cout << res[i] << " ";
    cout << endl;
}


#include <bits/stdc++.h>
#define fast() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ll long long int
using namespace std;
ll dp[105][100005];
ll t[105];
ll n;
ll totalsum=0;
ll solve(ll pos,ll sum)
{
    if(sum>totalsum/2)
    return INT_MIN;
    if(pos>=n&&sum<=totalsum/2)
    return sum;
    ll &ans=dp[pos][sum];
    if(ans!=-1)
    return ans;
    ans=0;
    ans=max(solve(pos+1,sum),solve(pos+1,sum+t[pos]));
    return ans;
}
int main() {
    fast();
    // ll t;
    // cin>>t;
    // while(t--)
    // {

    // }
    memset(dp,-1,sizeof dp);
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>t[i];
        totalsum+=t[i];
    }
    // cout<<totalsum<<" ";
    cout<<totalsum-solve(0,0);
}

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

const int maxl=2e5+10;
const int mod=1e9+7;

int n,k;
int a[maxl];
ll dp[maxl][17][2][2];
char s[maxl];

inline ll dfs(int pos,int st,bool up,bool lead)
{
	int cnt=__builtin_popcount(st);
	if(cnt>k) return 0;
	if(pos>n) return cnt==k;
	ll &ret=dp[pos][cnt][up][lead];
	if(ret!=-1)
		return ret;
	ret=0;
	int r=up?a[pos]:15;
	for(int i=0;i<=r;i++)
		ret=(ret+dfs(pos+1,(!i&&!lead)?st:st|(1<<i),up&&(i==r),lead|i))%mod;
	return ret;
}

int main()
{
	scanf("%s",s+1);scanf("%d",&k);
	n=strlen(s+1);
	for(int i=1;i<=n;i++)
	if(s[i]>='A' && s[i]<='Z')
		a[i]=s[i]-'A'+10;
	else
		a[i]=s[i]-'0';
	memset(dp,-1,sizeof(dp));
	printf("%lld\n",dfs(1,0,1,0));
	return 0;
}


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

#define pb              push_back
#define SZ(x)           (int)x.size()
#define F               first
#define S               second
#define ALL(x)          x.begin(), x.end()
#define fast            ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define FOR(i, a, b)    for(int i = a; i < b; ++i)

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

const ll INF = 1e18;
const int MOD = 1e9 + 7;
const ld PI = acosl(-1.0);
const ld EPS = 1e-9;
#define int long long

const int N = 1000 + 5;
int dp[N][N];
std::vector<int> a(N), b(N);

int f(int i, int j) {
        if(i == -1 && j == -1) return 0;
        if(i == -1) return j+1;
        if(j == -1) return i+1;
        if(dp[i][j] != -1) return dp[i][j];

        int &res = dp[i][j];
        res = INF;
        res = min(res, f(i-1, j) + 1);
        res = min(res, f(i, j-1) + 1);
        res = min(res, f(i-1, j-1) + (a[i] != b[j]));
        return res;
}

void solve() {
        memset(dp, -1, sizeof(dp));
        int n, m;
        cin >> n >> m;
        FOR(i, 0, n) {
                cin >> a[i];
        }
        FOR(j, 0, m) {
                cin >> b[j];
        }
        cout << f(n-1, m-1) << endl;
}

signed main() {
        fast;
        int T = 1;
        // cin >> T;
        while(T--) {
                solve();
        }
}

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

#define fo(i, x, y) for (int i = (x); i <= (y); ++i)
#define fd(i, x, y) for (int i = (x); i >= (y); --i)

typedef long long ll;

const int p[10][4] = {{0, 0, 0, 0}
					, {0, 0, 0, 0}
					, {6, 2, 4, 8}
					, {1, 3, 9, 7}
					, {6, 4, 0, 0}
					, {0, 0, 0, 0}
					, {0, 0, 0, 0}
					, {1, 7, 9, 3}
					, {6, 8, 4, 2}
					, {1, 9, 0, 0}};

int getint()
{
	char ch;
	int res = 0, p;
	while (!isdigit(ch = getchar()) && (ch ^ '-'));
	p = ch == '-'? ch = getchar(), -1 : 1;
	while (isdigit(ch))
		res = (res << 3) + (res << 1) + (ch ^ 48), ch = getchar();
	return res * p;
}

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

int main()
{
	ll a, b, c;
	cin >> a >> b >> c;
	a %= 10;
	int s2 = pw(b, c, 2), s4 = pw(b, c, 4);
	switch(a)
	{
		case 0: printf("0\n"); break;
		case 1: printf("1\n"); break;
		case 2: printf("%d\n", p[2][s4]); break;
		case 3: printf("%d\n", p[3][s4]); break;
		case 4: printf("%d\n", p[4][s2]); break;
		case 5: printf("5\n"); break;
		case 6: printf("6\n"); break;
		case 7: printf("%d\n", p[7][s4]); break;
		case 8: printf("%d\n", p[8][s4]); break;
		case 9: printf("%d\n", p[9][s2]); break;
	}
	return 0;
}


/*
मनस्वी म्रियते कामं कार्पण्यं न तु गच्छति ।
अपि निर्वाणमायाति नानलो याति शीतताम् ॥
*/

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;

#define mod 1000000007
#define pb push_back
#define ll long long int
#define ull unsigned ll
#define ld long double
#define vi vector<int>
#define vl vector<ll>
#define v2i vector<vector<int>>
#define v2l vector<vector<ll>>
#define ppi pair<int,int>
#define ppl pair<ll,ll>
#define vpi vector<ppi>
#define vpl vector<ppl>
#define all(x) x.begin(),x.end()
#define ff first
#define ss second
#define forn(i,a,b) for(int i=a;i<b;i++)
#define forr(i,a,b) for(int i=a;i>=b;i--)
#define forv(i,m) for(auto i : m)
#define p2d(v) for(auto a:v){for(auto b:a)cout<<b<<" ";cout<<endl;}
#define p1d(v) for(auto a:v)cout<<a<<" ";cout<<endl;
#define ppd(v) for(auto a:v)cout<<a.ff<<" "<<a.ss<<endl;
#define imx INT_MAX
#define imn INT_MIN
#define inf 9000000000000000000
#define minf -inf
#define endl "\n"
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define sze size()
#define rvs reverse
#define itr iterator
#define pre cout<<fixed<<setprecision(10);
#define umap unordered_map
#define uset unordered_set
#define pi 3.141592653589793
#define MAXN 100005
#define test1(x) cout << #x " = " << x << endl;
#define test2(x,y) cout << #x " = " << x << " " << #y " = " << y << endl;
#define test3(x,y,z) cout << #x " = " << x << " " << #y " = " << y << " " << #z " = " << z << endl;
#define test4(w,x,y,z) cout << #w " = " << w << " " << #x " = " << x << " " << #y " = " << y << " " << #z " = " << z << endl;
#define oset tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>

/*
const ll infinity = 9e18;

bool compare(ll a,ll b) {return a > b;}
bool compare1(ppl a,ppl b) {return a.ff > b.ff;}
bool compare2(ppl a,ppl b) {return a.ss < b.ss;}
bool isprime(ll n){if(n < 2) return 0; ll i = 2; while(i*i <= n){if(n%i == 0) return 0; i++;} return 1;}

ll Npower(ll a,ll b) {ll product = 1; while(b){if(b&1){product = product*a;}a = a*a;b = b>>1;} return product;}
ll power(ll a,ll b,ll md = mod) {ll product = 1; while(b){if(b&1){product = (product*a)%md;}a = (a*a)%md;b = b>>1;} return product%md;}
ll GCD(ll a,ll b) {return b==0 ? a:GCD(b,a%b);}
ll LCM(ll a,ll b) {return (a/GCD(a,b))*b; }
*/

int main()
{
    fast

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

    ll n,m,t;
    cin>>n>>m>>t;

    vpl vp(m);
    cin>>vp[0].ff>>vp[0].ss;

    ll total = n-vp[0].ff;

    bool val = 1;

    if(total <= 0)
    {
        val = 0;
    }

    forn(i,1,m)
    {
        if(total <= 0)
        {
            val = 0;
        }
        cin>>vp[i].ff>>vp[i].ss;
        total = total+(vp[i-1].ss-vp[i-1].ff);
        total = min(total,n);
        if(total <= 0)
        {
            val = 0;
        }
        total -= (vp[i].ff - vp[i-1].ss);
        if(total <= 0)
        {
            val = 0;
        }
    }
    total += (vp[m-1].ss-vp[m-1].ff);
    total = min(total,n);
    total -= (t-vp[m-1].ss);

    if(total <= 0)
    {
        val = 0;
    }

    if(val)
    {
        cout<<"Yes";
    }
    else
    {
        cout<<"No";
    }

    return 0;
}

/*
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
*/

#include <bits/stdc++.h>
#include <set>
#include <queue>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>

using namespace std;
using namespace __gnu_pbds;

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

template<typename Key, typename T> using omap =
tree<Key, T, less<Key>, rb_tree_tag, tree_order_statistics_node_update>;

#define pb push_back
#define rb pop_back
#define pf push_front
#define rf pop_front

#define heap priority_queue
#define mpr make_pair

#define fi first
#define se second
#define endl "\n"

#define pii pair<int, int>

#define int long long
#define float long double
#define multiTest cin >> t; for(test = 0; test < t; test++)
#define toFile freopen("a.inp", "r", stdin); freopen("a.out", "w", stdout);
#define speed ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

const int inf = 9e18;
const int mod = 1e9 + 7;

int t;
int test;

void solve() {
	int n, m, t;
	cin >> n >> m >> t;

	int bat = n;
	int tim = 0;
	bool failed = false;

	int a, b;
	for(int i = 0; i < m; i++) {
		cin >> a >> b;
		bat -= (a - tim);
		if(bat <= 0) {
			failed = true;
			break;
		}
		bat += b - a;
		if(bat > n) bat = n;

		tim = b;
	}

	bat -= (t - tim);
	if(bat <= 0) {
		failed = true;
	}
	if(failed) cout << "No";
	else cout << "Yes";
}

signed main() {
	speed;

	//toFile

	//multiTest
	solve();

	return 0;
}


#define _USE_MATH_DEFIMES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

const int MOD = 1'000'000'007;
const int MOD2 = 998'244'353;
const int INF = 1'000'000'000; //1e9
const int NIL = -1;
const long long LINF = 1'000'000'000'000'000'000; // 1e18
const long double EPS = 1E-10;

template<class T, class S> inline bool chmax(T &a, const S &b){
    if(a < b){
        a = b; return true;
    }
    return false;
}
template<class T, class S> inline bool chmin(T &a, const S &b){
    if(b < a){
        a = b; return true;
    }
    return false;
}

template<unsigned long long m> class comb_perm{
private:
    int n;
    std::vector<long long> fact, fact_inv;
public:
    comb_perm(): n(0), fact(1), fact_inv(1){
        fact[0] = fact_inv[0] = 1;
    }
    comb_perm(int N):n(N), fact(N+1), fact_inv(N+1){
        fact[0] = fact[1] = fact_inv[0] = fact_inv[1] = 1;
        for(int i(2); i <= n; ++i) fact[i] = fact[i - 1] * i % m;
        for(int i(2); i <= n; ++i) fact_inv[i] = inv(fact[i]);
    }

    bool resize(int N){
        if(N <= n) return false;
        fact.resize(N+1);
        fact_inv.resize(N+1);
        if(!n && N){
            fact[1] = fact_inv[1] = 1;
        }
        for(int i(std::max(2, n+1)); i <= N; ++i) fact[i] = fact[i - 1] * i % m;
        for(int i(std::max(2, n+1)); i <= N; ++i) fact_inv[i] = inv(fact[i]);
        n = N;
        return true;
    }

    long long power(long long a, unsigned long long b){
        long long ret(1);
        while(b){
            if(b & 1) ret = (ret * a) % m;
            a = (a * a) % m;
            b /= 2;
            if(ret < 0) ret += m;
            if(a < 0) a += m;
        }
        return ret;
    }

    long long inv(long long a){ // GCD(a, m) = 1
        return power(a, m-2);
    }

    long long permutation(int n, int r){
        if(n < r) return 0;
        if(n < 0 || r < 0) return 0;
        long long ret(fact[n]);
        ret = ret * fact_inv[n - r] % m;
        return ret;
    }

    long long combination(int n, int r){
        if(n < r) return 0;
        if(n < 0 || r < 0) return 0;
        long long ret(permutation(n, r));
        return ret * fact_inv[r] % m;
    }

    long long multi_combination(int n, int r){
        return combination(n + r - 1, r);
    }

    long long factorial(int a){
        return fact[a];
    }

    long long inv_factorial(int a){
        return fact_inv[a];
    }

};

std::unordered_map<long long, int> primeFactorisation_Map(long long n){
    std::unordered_map<long long, int> mp;
    for(long long i(2); i * i <= n; ++i)
        while(!(n % i)){
            ++mp[i]; n /= i;
        }
    if(n > 1) ++mp[n];
    return mp;
}

int main(){
    int N, M; std::cin >> N >> M;
    if(N == 1){
        std::cout << M << std::endl;
        return 0;
    }
  //  int mx{};
    comb_perm<MOD2> cp(N + 30);
    long long ans{};
   /* std::vector<long long> hs(21);
    hs[0] = 1;
    for(int i{1}; i < 21; ++i){
        hs[i] = hs[i-1] + cp.multi_combination(N - 1, i);
        hs[i] %= MOD2;
    }*/
    for(int i{1}; i <= M; ++i){
        auto pmp(primeFactorisation_Map(i));
        long long cm{1};
        for(auto [p, e]: pmp){
            cm *= cp.combination(N+e-1, e);
            cm %= MOD2;
         //   chmax(mx, e);
        }
        (ans += cm) %= MOD2;
    }
    if(ans < 0) ans += MOD2;
    std::cout << ans << std::endl;
    return 0;
}

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

#define all(v) v.begin(), v.end()
#define ll long long
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rep_up(i, a, b) for (ll i = a; i < b; ++i)
#define rep_down(i, a, b) for (ll i = a; i > b; --i)

#define P pair<ll, ll>
#define Graph vector<vector<ll>>
#define fi first
#define se second

#define vvvvll vector<vector<vector<vector<ll>>>>
#define vvvll vector<vector<vector<ll>>>
#define vvll vector<vector<ll>>
#define vll vector<ll>

#define vvvvdo vector<vector<vector<vector<double>>>>
#define vvvdo vector<vector<vector<double>>>
#define vvdo vector<vector<double>>
#define vdo vector<double>

#define pqll priority_queue<ll>
#define pqllg priority_queue<ll, vector<ll>, greater<ll>>

constexpr ll INF = (1ll << 60);
// constexpr ll mod = 998244353;
// constexpr ll mod = 67280421310721;
constexpr ll mod = 1000000007;
constexpr double pi = 3.14159265358979323846;
template <typename T>
inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T>
inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T>
void pt_vll(vector<T> &v) {
    ll vs = v.size();
    rep(i, vs) {
        cout << v[i];
        if (i == vs - 1)
            cout << "\n";
        else
            cout << " ";
    }
}
template <typename T>
void pt_vvll(vector<vector<T>> &v) {
    ll vs = v.size();
    rep(i, vs) pt_vll(v[i]);
}
template <typename T>
void pt(T val) {
    cout << val << "\n";
}

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

ll modinv(ll a, ll m) {
    ll b = m, u = 1, v = 0;
    while (b) {
        ll t = a / b;
        a -= t * b;
        swap(a, b);
        u -= t * v;
        swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
}

ll modpow(ll x, ll n) {
    ll ret = 1;
    while (n > 0) {
        if (n & 1) ret = ret * x % mod;
        x = x * x % mod;
        n >>= 1;
    }
    return ret;
}
ll minv(ll a, ll m) { return modpow(a, m - 2); }
struct UnionFind {
    vector<int> size, parents;
    UnionFind() {}
    UnionFind(int n) {  // make n trees.
        size.resize(n, 0);
        parents.resize(n, 0);
        for (int i = 0; i < n; i++) {
            makeTree(i);
        }
    }
    void makeTree(int x) {
        parents[x] = x;  // the parent of x is x
        size[x] = 1;
    }
    bool isSame(int x, int y) { return findRoot(x) == findRoot(y); }
    bool unite(int x, int y) {
        x = findRoot(x);
        y = findRoot(y);
        if (x == y) return false;
        if (size[x] > size[y]) {
            parents[y] = x;
            size[x] += size[y];
        } else {
            parents[x] = y;
            size[y] += size[x];
        }
        return true;
    }
    int findRoot(int x) {
        if (x != parents[x]) {
            parents[x] = findRoot(parents[x]);
        }
        return parents[x];
    }
    int treeSize(int x) { return size[findRoot(x)]; }
};
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    ll N;
    cin >> N;
    vll A(N), B(N);
    UnionFind uf(4e5 + 10);

    vector<bool> lp(4e5 + 10);

    rep(i, N) {
        ll a, b;
        cin >> a >> b;
        A[i] = a, B[i] = b;
        if (uf.isSame(a, b))
            lp[uf.findRoot(a)] = true;
        else
            uf.unite(a, b);
    }

    rep(i, 4e5 + 10) if (lp[i]) lp[uf.findRoot(i)] = true;

    vector<bool> seen(4e5 + 10);
    ll ans = 0;
    rep(i, N) {
        ll r = uf.findRoot(A[i]);
        if (seen[r]) continue;
        if (lp[r]) {
            ans += uf.treeSize(r);
            seen[r] = true;
        } else {
            ans += uf.treeSize(r) - 1;
            seen[r] = true;
        }
    }

    pt(ans);
    return 0;
}


#include <bits/stdc++.h>

#define int long long
#define double long double
#define ff first
#define ss second
#define endl '\n'
#define ii pair<int, int>
#define mp make_pair
#define mt make_tuple
#define DESYNC                      \
  ios_base::sync_with_stdio(false); \
  cin.tie(0);                       \
  cout.tie(0)
#define pb push_back
#define vi vector<int>
#define vii vector<ii>
#define all(x) x.begin(), x.end()
#define EPS 1e-9
#define INF 1e18
#define ROOT 1
#define curtime chrono::steady_clock::now().time_since_epoch().count
#define rep(i, beg, n, s) for (int i = beg; i < n; i += s)
using ll = long long;
using namespace std;
const double PI = acos(-1);
const int M = 1000000007;
// const int M = 998244353;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int mod(int n, int m = M) {
  int ret = n % m;
  if (ret < 0) ret += m;
  return ret;
}

int exp(int n, int k, int m = M) {
  if (k == 0) return 1;
  if (k == 1) return n;
  int ax = exp(n, k / 2, m);
  ax = mod(ax * ax, m);
  if (k % 2) ax = mod(ax * n, m);
  return ax;
}

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

int solve(const vector<int> &v, const vector<ii> &b, const vector<int> &calc) {
  int n = v.size();
  int g[n][n];
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      int id = upper_bound(all(b), ii(v[i] + v[j] - 1, INF)) - b.begin();
      id--;
      if (id == -1)
        g[i][j] = 0;
      else
        g[i][j] = calc[id];
    }
  }

  vector<int> d(n, 0);
  d[0] = 0;
  for (int i = 1; i < n; i++) {
    int sum = v[i];
    for (int j = i - 1; j >= 0; j--) {
      sum += v[j];
      int id = upper_bound(all(b), ii(sum - 1, INF)) - b.begin();
      id--;
      int l = 0;
      if (id != -1) l = calc[id];
      d[i] = max(d[i], d[j] + l);
    }
  }
  return d[n - 1];
}

//#define MULTIPLE_TEST_CASE
void solution() {
  int n, m;
  cin >> n >> m;
  vector<ii> b(m);
  vector<int> calc;
  vi v(n);
  int mx = -INF, mn = INF;
  for (int &x : v) cin >> x, mx = max(mx, x);
  for (ii &x : b) cin >> x.ss >> x.ff, mn = min(mn, x.ff);
  int ans = INF;

  if (mx > mn) {
    cout << -1 << endl;
    return;
  }

  sort(all(v));
  sort(all(b));

  for (ii x : b) {
    if (calc.size() == 0)
      calc.pb(x.ss);
    else
      calc.pb(max(calc.back(), x.ss));
  }

  do {
    ans = min(ans, solve(v, b, calc));
  } while (next_permutation(all(v)));

  cout << ans << endl;
}

int32_t main() {
  DESYNC;
  int t = 1;
#ifdef MULTIPLE_TEST_CASE
  cin >> t;
#endif
  while (t--) solution();
}

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

template<class T>
struct graph{
    struct edge{
        int to;
        T cost;
        edge(int to, T cost) : to(to), cost(cost){}
        bool operator<(const edge& r)const { return cost < r.cost; }
    };
    int V;
    vector< vector< edge > > adj;
    vector< T > dist;
    vector< int > prev;
    graph(){}
    graph(int n = 0){
        init(n);
    }

    void init(int size){
        V = size;
        adj.assign(size, vector<edge>());
    }

    void add_edge(int from, int to, T cost){
        adj[from].push_back(edge(to, cost));
    }

    void Dijkstra(int from, T inf){
        using P = pair<T, int>;
        priority_queue< P, vector<P>, greater<P> > q;
        dist.assign(V, inf);
        prev.assign(V, -2);
        dist[from] = 0;
        prev[from] = -1;
        q.push(P(0, from));
        while(!q.empty()){
            P p = q.top();
            q.pop();
            int v = p.second;
            if(dist[v] < p.first) continue;
            for(edge e: adj[v]){
                if(dist[v] + e.cost < dist[e.to]){
                    dist[e.to] = dist[v] + e.cost;
                    prev[e.to] = v;
                    q.push(P(dist[e.to], e.to));
                }
            }
        }
    }

    void get_path(int to, vector<int>& path){
        path.clear();
        if(prev[to] == -2) return;
        while(prev[to] != -1){
            path.push_back(to);
            to = prev[to];
        }
        path.push_back(to);
        reverse(path.begin(), path.end());
    }
};

int main(void){
    using P = pair<int, int>;
    int n, m;
    cin >> n >> m;
    vector< int > w(n);
    vector< P > vl;
    for(int i=0;i<n;i++) cin >> w[i];
    for(int i=0;i<m;i++){
        int l, v;
        cin >> l >> v;
        vl.push_back(P(v, l));
    }

    {
        int wmx = 0;
        for(int i=0;i<n;i++) wmx = max(wmx, w[i]);
        for(int i=0;i<m;i++){
            if(vl[i].first < wmx){
                puts("-1");
                return 0;
            }
        }
    }
    vl.push_back(P(1e9, 0));
    sort(vl.begin(), vl.end());
    vector< int > lb(m+1, 0);
    lb[0] = 0;
    for(int i=1;i<=m;i++){
        lb[i] = max(lb[i-1], vl[i-1].second);
    }
    auto f = [&](int v){
        int ng = -1, ok = m;
        while(ng + 1 < ok){
            int mid = (ng + ok) / 2;
            if(v <= vl[mid].first) ok = mid;
            else ng = mid;
        }
        return lb[ok];
    };
    int ans = 1e9;
    sort(w.begin(), w.end());

    do{
        graph<int> g(n);
        for(int i=0;i<n;i++){
            int c = w[i];
            for(int j=i+1;j<n;j++){
                c += w[j];
                g.add_edge(i, j, -f(c));
            }
        }
        g.Dijkstra(0, 1e9);
        ans = min(ans, -g.dist[n-1]);
    }while(next_permutation(w.begin(), w.end()));
    cout << ans << endl;
}


#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include <unordered_map>
//#include"testlib.h"
#define endl '\n'
#define all(v) v.begin(),v.end()
#define allr(s) s.rbegin(),s.rend()
#define RT(s) return cout<<s,0
#define sz(s)    (int)(s.size())
//#define PI acos(-1)
#define EPS 1e-8
#define watch(x) cout << (#x)<<" = "<<x<<endl
#define mk(x, y) make_pair(x, y)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
int dy[] = { 1, -1, 0, 0, -1, 1, 1, -1 };
int dx[] = { 0, 0, 1, -1, 1, -1, 1, -1 };
void file() {
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
#else
	//freopen("gcd.in", "r", stdin);
	//freopen("out.txt", "w", stdout);
#endif
}

void fast() {
	std::ios_base::sync_with_stdio(0);
	cin.tie(NULL);
}
const int N = 2e5 + 9;
vi adjL[N];
ll mem[N][10], cost[N];
int n;
ll solve(int u, int type) {
	if (!sz(adjL[u]) && !type) return -1e10;
	ll& ret = mem[u][type];
	if (ret != -1) return ret;
	ret = -1e10;
	if (!type) {
		for (auto& it : adjL[u]) {
			ret = max(ret, solve(it, 1) - cost[u]);
			ret = max(ret, solve(it, 0));
		}
	}
	else {
		ret = cost[u];
		for (auto& it : adjL[u])
			ret = max(ret, solve(it, 1));
	}
	return ret;
}
int main() {
	file();
	fast();
	memset(mem, -1, sizeof mem);
	int m; cin >> n >> m;
	for (int i = 1; i <= n; i++)
		cin >> cost[i];
	while (m--) {
		int x, y; cin >> x >> y;
		adjL[x].push_back(y);
	}
	ll ans = LLONG_MIN;
	for (int i = 1; i <= n; i++)
		ans = max(ans, solve(i, 0));
	cout << ans << endl;
	return 0;
}

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define isin(x,l,r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define uni(x) x.erase(unique(rng(x)),x.end())
#define snuke srand((unsigned)clock()+(unsigned)time(NULL));
#define show(x) cerr<<#x<<" = "<<x<<endl;
#define PQ(T) priority_queue<T,v(T),greater<T> >
#define bn(x) ((1<<x)-1)
#define dup(x,y) (((x)+(y)-1)/(y))
#define newline puts("")
using namespace std;
typedef long long int ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef tuple<int,int,int> T;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
typedef vector<T> vt;
const double eps = 1e-10;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
#define dame { puts("-1"); return 0;}
#define yn {puts("Yes");}else{puts("No");}
const int MX = 200005;

vi to[MX];
vi a;
vi dp(MX,-1);

int dfs(int u){
    if(dp[u]>-1) return max(a[u], dp[u]);
    int res = -1;
    for(int v: to[u]){
        res = max(res,dfs(v));
    }
    dp[u] = res;
    return max(a[u],res);
}

int main() {
    int n,m; cin >> n >> m;
    rep(i,n){
        int ai; cin >> ai;
        a.push_back(ai);
    }
    rep(i,m){
        int x,y; cin >> x >> y;
        --x,--y;
        to[x].push_back(y);
    }
    rep(i,n) dfs(i);
    int ans = -INF;
    rep(i,n){
        int tos = to[i].size();
        if(tos == 0) continue;
        if(dp[i] == -1) continue;
        // printf("size: %d\n",(int)to[i].size());
        // printf("ans: %d\n",dp[i]-a[i]);
        ans = max(dp[i]-a[i],ans);
    }
    // cout << endl;
    cout << ans << endl;
    /*
    rep(i,n){
      cout << dp[i] << endl;
    }
    */
    return 0;
}


/*
Over Again
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define sz(v)   ((int)(v).size())
#define  all(v)    ((v).begin()),((v).end())
#define  allr(v)    ((v).rbegin()),((v).rend())
#define   pb         push_back
#define   mp         make_pair
#define    clr(v,d)      memset( v, d ,sizeof(v))
#define    lcm(a,b)                ((a*b)/(__gcd(a,b)))
#define     PI                       acos(-1)
#define   vec(a,b)       ((b)-(a))
#define   length(a)      hypot( (a.imag()),(a.real()) )
#define   normalize(a)      (a)/(length(a))
#define     point                    complex<double>
#define    dp(a,b)          (((conj(a))*(b)).real())
#define     same(p1,p2)               ( dp(  vec(p1,p2),vec(p1,p2)) < eps )
#define    rotate0( a,ang)    ((a)*exp( point(0,ang) ))
#define    rotateA(about,p,ang)   (rotate0(vec(about,p),ang)+about)
typedef  long long     ll ;
typedef  unsigned long long ull;
const double eps= (1e-8);
const double EPS = 1e-6;
using namespace std;
using namespace __gnu_pbds;
int dcmp(double a,double b){   return fabs(a-b)<=eps ? 0: (a>b)? 1:-1  ;}
int getBit(ll num, int idx) {return ((num >> idx) & 1ll) == 1;}
int setBit1(int num, int idx) {return num | (1<<idx);}
ll setBit0(ll num, int idx) {return num & ~(1ll<<idx);}
ll flipBit(ll num, int idx) {return num ^ (1ll<<idx);}
void GO(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);}
int countNumBit1(int mask) {int ret=0; while (mask) 	{mask &= (mask-1);++ret;	}return ret;}
const ll N=2000+5,mod=1000000007,sh=201,inf=2000000000;
typedef tree<
pair<ll,ll> ,
null_type,
less<pair<ll,ll> >,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
double sx[N],sy[N],tx[N],ty[N];

double get_angle(int x,int y){

 return atan2(y,x);
}
int get_dist(int x,int y){

 return x*x+y*y;
}
double torad(double ang){

 return (ang*PI/180.0);
}
int main(){
     GO();

     int T=1;
     //cin>>T;
     while(T--){

        int n;
        cin>>n;
        vector< pair<double,pair<int,int> > > v2,v1;
        int sumx=0,sumy=0;
        for(int i=0;i<n;i++){
            cin>>sx[i]>>sy[i];

            sumx+=sx[i];
            sumy+=sy[i];
            sx[i]*=n;
            sy[i]*=n;
        }

        for(int i=0;i<n;i++){
            sx[i]-=sumx;
            sy[i]-=sumy;
        }
        sumx=0;
        sumy=0;
        for(int i=0;i<n;i++){
            cin>>tx[i]>>ty[i];
            sumx+=tx[i];
            sumy+=ty[i];
            tx[i]*=n;
            ty[i]*=n;
        }

        for(int i=0;i<n;i++){
            tx[i]-=sumx;
            ty[i]-=sumy;
        }

        for(int i=0;i<n;i++){
            if(sx[i]!=0||sy[i]!=0){
                swap(sx[i],sx[0]);
                swap(sy[i],sy[0]);
            }
        }

        bool ans=0;
        for(int i=0;i<n;i++){

            double d1= get_angle(sx[0],sy[0]);
            double d2= get_angle(tx[i],ty[i]);
            double dd= d2-d1;
            map< pair<double,double> ,int> mb;
            bool fok=1;
            for(int j=0;j<n;j++){
                double tsx= sx[j]*cos(dd)-sy[j]*sin(dd);
                double tsy= sx[j]*sin(dd)+sy[j]*cos(dd);
                bool ok=0;
                for(int j=0;j<n&&ok==0;j++){
                    if(dcmp(tx[j],tsx)==0&&dcmp(ty[j],tsy)==0){
                        ok=1;
                    }
                }
                if(ok==0){
                    fok=0;
                    break;
                }
               // if(i==2) cout<<tsx<<" "<<tsy<<" "<<mb[mp(tsx,tsy)]<<endl;
            }

            if(fok){
                ans=1;
                break;
            }
        }

        if(ans){
            cout<<"Yes\n";
        }
        else{
            cout<<"No\n";
        }

     }
}

#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

using namespace std;

#define rep(i,n) for(int i = 0; i < (n); ++i)
#define Erep(i,n) for(int i = 0; i <= (n); ++i)
#define repS(i,s,n) for(int i = s; i < (n); ++i)
#define ErepS(i,s,n) for(int i = s; i <= (n); ++i)
#define Sort(a) sort(a.begin(), a.end())
#define RSort(a) sort(a.rbegin(), a.rend())
#define Output(a) printf("%lld %s", a, "\n")

typedef long long int ll;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<string> vst;
typedef vector<double> vd;

const ll INF = 0x1fffffffffffffff;
const ll MOD = 1000000007;
const double PI = acos(-1);

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

ll n;
vd a, b, c, d;

void input(){
    cin >> n;
    a = vd(n);
    b = vd(n);
    c = vd(n);
    d = vd(n);
    rep(i, n) cin >> a[i] >> b[i];
    rep(i, n) cin >> c[i] >> d[i];
}

void solve(){
    string ans = "No";
    vd x(n), y(n);
    for(int k = 0; k < 360; k++){
        rep(i, n){
            double tx = a[i], ty = b[i];
            x[i] = tx * cos((k * PI ) / 180) - ty * sin((k * PI ) / 180);
            y[i] = tx * sin((k * PI ) / 180) + ty * cos((k * PI ) / 180);
        }
        double ave_s_x = 0, ave_s_y = 0, ave_t_x = 0, ave_t_y = 0;
        rep(i, n){
            ave_s_x += x[i];
            ave_s_y += y[i];
            ave_t_x += c[i];
            ave_t_y += d[i];
        }
        double diff_x = (ave_t_x - ave_s_x) / n;
        double diff_y = (ave_t_y - ave_s_y) / n;
        ll check = 1;
        rep(i, n){
            ll count = 0;
            rep(j, n){
                double ex = x[i] + diff_x, ey = y[i] + diff_y;
                //if(k == 90)
                //cout << ex << " " << c[j] << " " << ey << " " << d[j] << "\n";
                if(abs(ex - c[j]) <= (double) 0.05 && abs(ey - d[j]) <= (double) 0.05){
                  //cout << k << endl;
                  //cout << x[i] << " " << c[j] << " " << y[i] << " " << d[j] << "\n";
                    count++;
                }
            }
            if(count == 0){
                check = 0;
            }
        }
        if(check){
            ans = "Yes";
            break;
        }
    }
    cout << ans << endl;
}

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

    input();
    solve();
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int MAX_N=1e6+10;
ll n,k;
ll dp[4][MAX_N*3];
ll sum[MAX_N*3];
int main()
{
	cin>>n>>k;
	for(int i=1;i<=n;i++) dp[1][i]=1;
	for(int i=2;i<=3;i++){
		sum[0]=0;
		for(int j=1;j<=i*n;j++) sum[j]=sum[j-1]+dp[i-1][j];
		for(int j=1;j<=i*n;j++) dp[i][j]=sum[j-1]-sum[max(0ll,j-n-1)];
	}
	int tmp=1;
	while(k-dp[3][tmp]>0) k-=dp[3][tmp++];
	ll ans1=1;
	while(k-dp[2][tmp-ans1]>0) k-=dp[2][tmp-ans1++];
	tmp-=ans1;
	ll mn=max(1ll,tmp-n),ans2=mn+k-1,ans3=tmp-ans2;
	printf("%lld %lld %lld\n",ans1,ans2,ans3);

    return 0;
}

#line 1 "/Users/lynmisakura/kyopro/library_cp/template.cpp"
#include<bits/stdc++.h>
#line 3 "/Users/lynmisakura/kyopro/library_cp/template.cpp"
using namespace std;
#define REP(i,n) for(int i = 0;i < n;i++)
#define RNG(i,s,n) for(int i = s;i <= n;i++)
#define _RNG(i,e,s) for(int i = e;i >= s;i--)
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define dup(x,y) (x + y - 1) / (y)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define lb(x,key) lower_bound(all(x) , (key))
#define ub(x,key) upper_bound(all(x) , (key))
#define _size(v) int((v).size())

template<class T> bool chmax(T& a,T b){ if(a < b){ a = b; return true; }else return false; }
template<class T> bool chmin(T& a,T b){ if(a > b){ a = b; return true; }else return false; }

using ll = long long;
using ld = long double;
using vi = vector<int>;
using vl = vector<ll>;
using pi = pair<int,int>;
using pl = pair<ll,ll>;
using vpi = vector<pi>;
using vpl = vector<pl>;

#define debug(arr) cout << #arr << " = " << arr << '\n'
#define debug2(a,b) cout<<"["<<#a<<","<<#b<<"] = "<<"["<<a<<","<<b<<"]\n"
#define debug3(a,b,c) cout<<"["<<#a<<","<<#b<<","<<#c<<"] = "<<"["<<a<<","<<b<<","<<c<<"]\n"
template<class T> ostream &operator << (ostream& out, const vector<T>& arr) {
    cout << "{"; for (int i = 0; i < arr.size(); i++)cout << (!i ? "" : ", ") << arr[i]; cout << "}";
    return out;
}
template<class T> ostream &operator << (ostream& out, const vector<vector<T> >& arr) {
    cout << "{\n"; for (auto& vec : arr)cout << "  " << vec << ",\n"; cout << "}";
    return out;
}
template<class S,class T> ostream &operator << (ostream& out, const pair<S,T>& p){
    cout << "{" << p.first << "," << p.second << "}" << '\n';
    return out;
}
template<class T> istream &operator >> (istream& in, vector<T>& arr) {
    for (auto& i : arr)cin >> i; return in;
}
template<class S,class T> istream &operator >> (istream& in,pair<S,T>& p){
    cin >> p.first >> p.second; return in;
}

#define F first
#define S second
/////////////////////////////////////////////////////////////////////////
namespace cp{
    void iob(){ cin.tie(0); ios::sync_with_stdio(false); }
}
#line 2 "main.cpp"
// =========================== DIJKSTRA's SHORTEST PATH ===============================
namespace cp{
using std::vector;
using std::pair;
    template<class T> struct Dijkstra{
	using P = pair<T,int>;
	int N;
	std::vector<std::vector<P>> graph;
	Dijkstra(){}
	Dijkstra(int N):N(N){graph.resize(N);}
	void add_edge(int a,int b){ add_edge(a,b,1); }
	void add_edge(int a,int b,T c){ graph[a].emplace_back(c,b); }
	const T INF=std::numeric_limits<T>::max()/2;
	vector<T> dist;
	vector<int> prev,vis;
	void solve(int s){
	    dist.assign(N,INF);
	    prev.assign(N,-1);
	    vis.assign(N,0);
	    dist[s]=0,vis[s]=1;
	    std::priority_queue<P,vector<P>,std::greater<P>> Q;
	    Q.push({0,s});
	    while(!Q.empty()){
		auto[d,v]=Q.top();Q.pop();
		vis[v]=1;
		if(dist[v]<d)continue;
		for(auto& [c,to] : graph[v]){
		    if(dist[to]>dist[v]+c){
			dist[to]=dist[v]+c;
			prev[to]=v;
			Q.push({dist[to],to});
		    }
		}
	    }
	}
	vector<int> get_path(int to){
	    vector<int> res;
	    while(to!=-1){ res.push_back(to); to=prev[to]; }
	    reverse(res.begin(),res.end());
	    return res;
	}
	bool reachable(int x){ return vis[x]; }
    };
};

ll mindist[2020][2020];

int main(){

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

    REP(i,2020)REP(j,2020) mindist[i][j] = (1LL << 55);

    cp::Dijkstra<ll> graph(2 * n);

    REP(i,m){
	int a,b;cin >> a >> b;
	a--,b--;
	ll c;cin >> c;
	chmin(mindist[a][b],c);
	graph.add_edge(a,b,c);
    }

    for(int i = n;i < 2 * n;i++){
	for(int j = 0;j < n;j++){
	    if(mindist[i - n][j] == (1LL << 55))continue;
	    graph.add_edge(i,j,mindist[i - n][j]);
	}
    }

    for(int i = n;i < 2 * n;i++){
	graph.solve(i);
	if(graph.reachable(i - n)){
	    cout << graph.dist[i-n] << '\n';
	}else{
	    cout << -1 << '\n';
	}
    }

}


/*
皮卡丘冲鸭！
へ　　　　　／|
　　/＼7　　　 ∠＿/
　 /　│　　 ／　／
　│　Z ＿,＜　／　　 /`ヽ
　│　　　　　ヽ　　 /　　〉
　 Y　　　　　`　 /　　/
　ｲ●　､　●　　⊂⊃〈　　/
　()　 へ　　　　|　＼〈
　　>ｰ ､_　 ィ　 │ ／／
　 / へ　　 /　ﾉ＜| ＼＼
　 ヽ_ﾉ　　(_／　 │／／
　　7　　　　　　　|／
　　＞―r￣￣`ｰ―＿
*/
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#define mod 1000000007
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f
#define MEM(x,y) memset(x,y,sizeof(x))
#define pb push_back
#define mk make_pair
#define pi pair<int, int>
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i,a,b) for(int i=a;i>=(b);--i)
using namespace std;
int dt[][2]= {{1,0},{-1,0},{0,1},{0,-1},{0,0}};
//typedef pair<int, int> P;
//priority_queue<int, vector<int>, greater<int> > q;

int a[110][110];

int main()
{
   //freopen("in.txt","r",stdin);
   //freopen("out.txt","w",stdout);
   IOS;
   int h,w;
   cin >> h >> w;
   int mi = INF;
   rep(i,1,h)
   {
      rep(j,1,w)
      {
         cin >> a[i][j];
         mi = min(mi,a[i][j]);
      }
   }
   ll ans = 0;
   rep(i,1,h)
   {
      rep(j,1,w)
      {
         ans += a[i][j] - mi;
      }
   }
   cout << ans;
}

/**
*    author:  shu8Cream
*    created: 13.05.2021 00:15:07
**/

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i=0; i<(n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using ll = long long;
using P = pair<ll,ll>;
using vi = vector<ll>;
using vvi = vector<vi>;

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    ll t,n;
    cin >> t >> n;
    cout << ((100+t)*n-1)/t << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define inf 1e15
#define ll long long int

void solve(ll n, ll k){
     vector<ll>a(n);
         vector<ll>mp(n,0);

        for(int i=0;i<n;++i){
            cin>>a[i];
            ++mp[a[i]];
        }
        ll i=0;
        ll res = 0;

        while(i<n){
            if(mp[i]==0){
                break;
            }
            if(mp[i]<k){
                res += (k-mp[i])*i;
                k = mp[i];
            }
            ++i;
        }
        res += k*i;
        cout<<res;

}
int main() {

        int n,k;
        cin>>n>>k;
        solve(n,k);

}

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

typedef vector <int> vi;
typedef pair< int ,int > pii;
#define endl "\n"
#define sd(val) scanf("%d",&val)
#define ss(val) scanf("%s",&val)
#define sl(val) scanf("%lld",&val)
#define debug(val) printf("check%d\n",val)
#define all(v) v.begin(),v.end()
#define PB push_back
#define MP make_pair
#define FF first
#define SS second
#define ll long long
#define MOD 1000000007
#define clr(val) memset(val,0,sizeof(val))
#define what_is(x) cerr << #x << " is " << x << endl;
#define OJ \
    freopen("input.txt", "r", stdin); \
    freopen("output.txt", "w", stdout);
#define FIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

int main()
{

    int N;
    int x;

    cin >> N;

    int a[N], b[N];

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

    for (int i = 0; i < N; i++){
        cin >> b[i];
    }

    int* a_max = max_element(a, a + N);
    int* b_min = min_element(b, b + N);

    x = *b_min - *a_max;

    if (x < 0){
        cout << 0;
    }else{
        cout << x + 1<< endl;
    }

    return 0;

}


/* Aditya0412 */
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define vii vector < int >
#define pii pair < int , int >
#define fi first
#define all(a)      (a).begin(),(a).end()
#define si(x)       (int)((x).size())
#define debug(x)    cout<<#x<<" is "<<(x)<<endl;cout.flush();
#define se second
#define endl        '\n'
#define f(i,a,b)    for(int i=a;i<b;i++)
#define mem(a,x)          memset(a,x,sizeof(a))
#define fastIO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
const int  mod=998244353;
// const int  mod = 1000000000+7;
const int  N=1000000+6;
#define M_PI           3.14159265358979323846
// In C++, comparator should return false if its arguments are equal.
// === Debug macro starts here ===
int recur_depth = 0;
#ifdef DEBUG
#define dbg(x) {++recur_depth; auto x_=x; --recur_depth; cerr<<string(recur_depth, '\t')<<"\e[91m"<<__func__<<":"<<__LINE__<<"\t"<<#x<<" = "<<x_<<"\e[39m"<<endl;}
#else
#define dbg(x)
#endif
template<typename Ostream, typename Cont>
typename enable_if<is_same<Ostream,ostream>::value, Ostream&>::type operator<<(Ostream& os,  const Cont& v){
    os<<"[";
    for(auto& x:v){os<<x<<", ";}
    return os<<"]";
}
template<typename Ostream, typename ...Ts>
Ostream& operator<<(Ostream& os,  const pair<Ts...>& p){
    return os<<"{"<<p.first<<", "<<p.second<<"}";
}
// === Debug macro ends here ===
int inv[3000000+5],fac_inv[3000000+5],fac[3000000+5];
void initialize()
{
	int i;
	inv[1]=1;
	for(i=2;i<=3000000;i++)
		inv[i]=(mod-mod/i)*inv[mod%i]%mod;
	fac[0]=fac[1]=1;
	for(i=2;i<=3000000;i++)
		fac[i]=i*fac[i-1]%mod;
	fac_inv[0]=fac_inv[1]=1;
	for(i=2;i<=3000000;i++)
		fac_inv[i]=inv[i]*fac_inv[i-1]%mod;
}

int ncr(int n,int r)
{
	if(n<r) return 0;
	return (fac[n]*fac_inv[r]%mod)*fac_inv[n-r]%mod;
}
int spf[N];
void smallest_prime()
{
    mem(spf,0);
    for(int i=2;i<N;i++)
    {
        if(spf[i]==0)
        {
            spf[i]=i;
            for(int j=i*i;j<N;j+=i)
            if(spf[j]==0)
            spf[j]=i;
        }
    }

}
int n,m;
int32_t main()
{
	// _ _| | _ _ _
    fastIO;smallest_prime();initialize();
  	cin>>n>>m;int ans=0;
  	for(int i=1;i<=m;i++){
  		int t=1;
  		int x=i;
  		while(x!=1)
  		{
  			int y=spf[x],z=0;
  			while(x%y==0)
  			{
  				x/=y;z++;
  			}
  			t*=ncr(z+n-1,n-1);
  			t%=mod;
  		}
  		ans+=t;ans%=mod;
  	}
  	cout<<ans;

}

#include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>
#include<vector>
#include<math.h>
#include<stdio.h>
#include<stack>
#include<queue>
#include<tuple>
#include<cassert>
#include<set>
#include<functional>
//#include<bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define rep(i, x) for(ll i = 0; i < x; i++)
#define rep2(i, x) for(ll i = 1; i <= x; i++)
#define all(a) (a).begin(),(a).end()
using ll = long long;
using ld = long double;
using namespace std;
const ll INF = 10000000000000000;
const int intINF = 1000000000;
const ll mod = 1000000007;
const ll MOD = 998244353;
const ld pi = 3.141592653589793238;

bool isprime(int p) {
	if (p == 1) return false;
	for (int i = 2; i < p; i++) {
		if (p % i == 0) return false;
	}
	return true;
}
ll gcd(ll a, ll b) {
	if (a < b)swap(a, b);
	if (a % b == 0)return b;
	return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
	return a / gcd(a, b) * b;
}
ll keta(ll n) {
	ll res = 0;
	while (n >= 1) {
		res += n % 10; n /= 10;
	}
	return res;
}
ll modpow(ll x, ll y) {
	ll res = 1;
	while (y) {
		if (y % 2) { res *= x; res %= mod; }
		x = x * x % mod; y /= 2;
	}
	return res;
}
ll nCk(ll n, ll k) {
	ll a = 1, b = 1;
	for (int h = n - k + 1; h <= n; h++) { a *= h; a %= mod; }
	for (int h = 1; h <= k; h++) { b *= h; b %= mod; }
	return a * modpow(b, mod - 2) % mod;
}
//printf("%.10f\n", n);
typedef pair <ll, ll> P;
ll dx[4] = { 1, 0, -1, 0 }, dy[4] = { 0, 1, 0, -1 };
struct edge { ll to, cost; };
struct status {
	ll y;
	ll high;
	ll cost;

	bool operator<(const status& rhs) const { return cost < rhs.cost; };
	bool operator>(const status& rhs) const { return cost > rhs.cost; };
};

ll a[123456], w1[1234567], w2[1234567];
signed main() {
	ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
	//cout << fixed << setprecision(15);

	ll n, x; cin >> n >> x;
	rep(i, n) {
		cin >> a[i];
	}
	for (int i = 0; i < (1 << (n / 2)); i++) {
		ll sum = 0;
		for (int j = 0; j < (n / 2); j++) {
			if ((i / (1 << j)) % 2 == 0) sum += a[j];
		}
		w1[i] = sum;
	}

	// 後半 N - N/2 個を bit 全探索で全列挙する
	for (int i = 0; i < (1 << (n - (n / 2))); i++) {
		ll sum = 0;
		for (int j = 0; j < n - (n / 2); j++) {
			if ((i / (1 << j)) % 2 == 0) sum += a[j + (n / 2)];
		}
		w2[i] = sum;
	}

	// ソートしたうえで半分全列挙をする
	ll ans = 0;
	sort(w1, w1 + (1 << (n / 2)));
	sort(w2, w2 + (1 << (n - (n / 2))));
	for (int i = 0; i < (1 << (n / 2)); i++) {
		ll rem = x - w1[i];
		ll pos1 = upper_bound(w2, w2 + (1 << (n - (n / 2))), rem) - w2;
		if (pos1 == 0) { continue; }
		pos1--;
		ans = max(ans, w1[i] + w2[pos1]);
	}
	cout << ans << endl;
	return 0;
}


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

int main(){
    ios_base::sync_with_stdio(false);cin.tie(0);
    int n;
    cin>>n;
    string s;
    cin>>s;
    vector<int> min_rem(n+1, n);
    min_rem[n]=0;
    vector<int> ending_with_char(26, -1);
    for(int i=n-1;i>=0;i--){
        if(i==n-1){
            min_rem[i]=-1;
            ending_with_char[(int)(s.at(i)-'a')]=1;
        }
        else{
            min_rem[i]=n;
            for(int ch=0;ch<26;ch++){
                if(ch!=(int)(s.at(i)-'a')){
                    if(ending_with_char[ch]!=-1){
                        min_rem[i]=min(min_rem[i], ending_with_char[ch]);
                    }
                }
            }
            min_rem[i]=(min_rem[i]==n?-1:min_rem[i]);
            if(min_rem[i+1]!=-1){
                ending_with_char[(int)(s.at(i)-'a')]=min(ending_with_char[(int)(s.at(i)-'a')]==-1?n:ending_with_char[(int)(s.at(i)-'a')], min_rem[i+1]+1);
            }
        }
    }
    cout<<min_rem[0]<<"\n";
}

#include<bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define F first
#define S second
#define pss pair<string,string>
#define pcc pair<char,char>
#define pll pair<ll,ll>
#define pii pair<int,int>
#define piii pair<int,pii>
#define vi vector<int>
#define vii vector<pii>
#define pb push_back
#define vs vector<string>
#define vl vector<ll>
#define vs vector<string>
#define vll vector<pll>
#define vss vector<pss>
#define vcc vector<pcc>
#define MP make_pair
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,n) for(int i=n-1;i>=0;i--)
#define Very_fast std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
const int N = 1e5+5;
const int mod=1e9+7;
const int INF=0x3f3f3f3f;
inline ll gcd(ll a,ll b){if(b==0)return a;else return gcd(b,a%b);}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll lowbit(ll x){return (x&-x);}
inline ll kissme(ll x,ll y)
{
	ll res=1;
	if(x==0)return 1;
	if(x==1)return y;
	res=kissme(x/2,y);
	res=res*res%mod;
	if(x%2==1)res=res*y%mod;
	return res;
}
int T,n,d[N],p[N];
string s;
signed main()
{
	Very_fast;
	cin>>n;
	cin>>s;
	if(s[0]!=s[s.size()-1])cout<<"1\n";
	else{
		for(int i=0;i<s.size()-1;i++)
			if(s[i]!=s[0]&&s[i+1]!=s[s.size()-1])
			{
				cout<<"2\n";
				return 0;
			}
		cout<<"-1\n";
	}
	return 0;
}


#pragma region head
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
template <class T>
using vv = vector<vector<T>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rrepi(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define bit(n) (1LL << (n))
template <class T>
inline bool chmax(T& a, const T& b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
inline bool chmin(T& a, const T& b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
const int INF = 1002003004;
const ll LINF = 1002003004005006007ll;
struct preprocess {
    preprocess() {
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(20);
    }
} ____;
#pragma endregion head

#pragma region library
const int MOD = 1000000007;
struct mint {
    ll x;  // typedef long long ll;
    mint(ll x = 0) : x((x % MOD + MOD) % MOD) {}
    mint operator-() const { return mint(-x); }
    mint& operator+=(const mint a) {
        if ((x += a.x) >= MOD) x -= MOD;
        return *this;
    }
    mint& operator-=(const mint a) {
        if ((x += MOD - a.x) >= MOD) x -= MOD;
        return *this;
    }
    mint& operator*=(const mint a) {
        (x *= a.x) %= MOD;
        return *this;
    }
    mint operator+(const mint a) const {
        mint res(*this);
        return res += a;
    }
    mint operator-(const mint a) const {
        mint res(*this);
        return res -= a;
    }
    mint operator*(const mint a) const {
        mint res(*this);
        return res *= a;
    }
    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t >> 1);
        a *= a;
        if (t & 1) a *= *this;
        return a;
    }
    // for prime MOD
    mint inv() const {
        return pow(MOD - 2);
    }
    mint& operator/=(const mint a) {
        return (*this) *= a.inv();
    }
    mint operator/(const mint a) const {
        mint res(*this);
        return res /= a;
    }
    bool operator==(const mint a) const {
        return x == a.x;
    }
    bool operator!=(const mint a) const {
        return x != a.x;
    }
    friend ostream& operator<<(ostream& os, const mint& value) {
        os << value.x;
        return os;
    }
    friend istream& operator>>(istream& is, mint& value) {
        ll t;
        is >> t;
        value = mint(t);
        return is;
    }
};
struct combination {
    vector<mint> fact, ifact;
    combination(int n) : fact(n + 1), ifact(n + 1) {
        assert(n < MOD);
        fact[0] = 1;
        for (int i = 1; i <= n; ++i) fact[i] = fact[i - 1] * i;
        ifact[n] = fact[n].inv();
        for (int i = n; i >= 1; --i) ifact[i - 1] = ifact[i] * i;
    }
    mint operator()(int n, int k) {
        if (k < 0 || k > n) return 0;
        return fact[n] * ifact[k] * ifact[n - k];
    }
};

#pragma endregion library

int main() {
    int n;
    cin >> n;
    vector<char> c(4);
    rep(i, 4) cin >> c[i];
    if (n <= 3 || (c[0] == 'A' && c[1] == 'A') || (c[1] == 'B' && c[3] == 'B')) {
        cout << 1 << '\n';
        return 0;
    } else {
        if (c[1] != c[2]) {
            mint two = 2;
            cout << two.pow(n - 3) << '\n';
        } else {
            mint ans = 0;
            combination cb(2000);
            rep(i, n) {
                int cnta = n - 3 - i;
                if (cnta < 0) break;
                ans += cb(cnta + 1, i);
            }
            cout << ans << '\n';
        }
    }
}

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

const int MAX_LEN = 50;
string DIR[5] = {"U", "D", "L", "R", ""};
int dir2int(char c) {
  if ( c == 'U' ) { return 0; }
  if ( c == 'D' ) { return 1; }
  if ( c == 'L' ) { return 2; }
  if ( c == 'R' ) { return 3; }
  return 4;
}
void move(int* i, int* j, int dir) {
  if ( dir == 0 && *i > 0 ) { *i -= 1; }
  if ( dir == 1 && *i < MAX_LEN-1 ) { *i += 1; }
  if ( dir == 2 && *j > 0 ) { *j -= 1; }
  if ( dir == 3 && *j < MAX_LEN-1 ) { *j += 1; }
}
int si, sj;
int t[MAX_LEN][MAX_LEN], p[MAX_LEN][MAX_LEN];

const double TIME_LIMIT = 1.3;
double now_time(clock_t start, clock_t now) {
  return ((double)(now - start) / CLOCKS_PER_SEC);
}

int next_dir(int i, int j, bool visited[]) {
  int ret = 4;
  int next_p = -1;

  if ( i > 0 && (! visited[t[i-1][j]]) && p[i-1][j] > next_p ) { ret = 0; next_p = p[i-1][j]; }
  if ( i < MAX_LEN-1 && (! visited[t[i+1][j]]) && p[i+1][j] > next_p ) { ret = 1; next_p = p[i+1][j]; }
  if ( j > 0 && (! visited[t[i][j-1]]) && p[i][j-1] > next_p ) { ret = 2; next_p = p[i][j-1]; }
  if ( j < MAX_LEN-1 && (! visited[t[i][j+1]]) && p[i][j+1] > next_p ) { ret = 3; next_p = p[i][j+1]; }

  return ret;
}

int next_dir_2(int i, int j, bool visited[]) {
  int ret = 4;
  int next_p = -1;

  if ( i > 0 && (! visited[t[i-1][j]]) && p[i-1][j] > next_p ) { ret = 0; next_p = p[i-1][j]; }
  if ( i < MAX_LEN-1 && (! visited[t[i+1][j]]) && p[i+1][j] > next_p ) { ret = 1; next_p = p[i+1][j]; }
  if ( j > 0 && (! visited[t[i][j-1]]) && p[i][j-1] > next_p ) { ret = 2; next_p = p[i][j-1]; }
  if ( j < MAX_LEN-1 && (! visited[t[i][j+1]]) && p[i][j+1] > next_p ) { ret = 3; next_p = p[i][j+1]; }

  return ret;
}

struct STATE {
  string route;
};

void init(STATE& state) {
  state.route = "";
  int i = si, j = sj;
  bool visited[2500] = {};
  visited[t[i][j]] = true;

  // 貪欲
  while ( true ) {
    int next = next_dir(i, j, visited);
    if ( next == 4 ) { break; }
    state.route += DIR[next];
    move(&i, &j, next);
    visited[t[i][j]] = true;
  }
}

void modify(STATE& state) {
  string mod_route = "";
  int i = si;
  int j = sj;
  bool visited[2500] = {};
  visited[t[i][j]] = true;

  int use_len = rand() % state.route.size();
  for ( int k = 0; k < use_len; k++ ) {
    mod_route += state.route[k];
    move(&i, &j, dir2int(state.route[k]));
    visited[t[i][j]] = true;
  }

  // randに変更
  int r = rand() % 4;
  mod_route += DIR[r];
  move(&i, &j, r);
  visited[t[i][j]] = true;

  // それ以降貪欲
  while ( true ) {
    int next = next_dir(i, j, visited);
    if ( next == 4 ) { break; }
    mod_route += DIR[next];
    move(&i, &j, next);
    visited[t[i][j]] = true;
  }

  state.route = mod_route;
}

int calc_score(STATE& state) {
  int point = p[si][sj];
  int i = si;
  int j = sj;
  bool visited[2500] = {};
  visited[t[i][j]] = true;
  for ( int k = 0; k < state.route.size(); k++ ) {
    move(&i, &j, dir2int(state.route[k]));
    point += p[i][j];
    if ( visited[t[i][j]] ) { return 0; }
    visited[t[i][j]] = true;
  }
  return point;
}

void mountain(STATE& state) {
  init(state);

  clock_t start = clock();
  while ( true ) {
    clock_t now = clock();
    if ( now_time(start, now) > TIME_LIMIT ) { break; }

    STATE new_state = state;
    modify(new_state);
    int new_score = calc_score(new_state);
    int pre_score = calc_score(state);

    if ( new_score > pre_score ) {
      state = new_state;
    }
  }
}

int main() {
  srand(time(NULL));
  cin >> si >> sj;

  for ( int i = 0; i < MAX_LEN; i++ ) {
    for ( int j = 0; j < MAX_LEN; j++ ) {
      cin >> t[i][j];
    }
  }
  for ( int i = 0; i < MAX_LEN; i++ ) {
    for ( int j = 0; j < MAX_LEN; j++ ) {
      cin >> p[i][j];
    }
  }

  STATE ans;
  mountain(ans);
  cout << ans.route << endl;
  return 0;
}


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

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> ii;
typedef tuple<ll, ll, ll> iii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
#define REP(i,n) for (ll i = 0; i < n; ++i)
#define REPR(i,n) for (ll i = n-1; i >= 0; --i)
#define FOR(i,m,n) for (ll i = m; i < n; ++i)
#define FORR(i,m,n) for (ll i = n-1; i >= m; --i)
#define FORE(x,xs) for (const auto& x : xs)
#define FORI(i,v) for (auto i = v.begin(); i != v.end(); i++)
#define ALL(v) v.begin(), v.end()
#define SORT(v) sort(ALL(v))
#define REV(v) reverse(ALL(v))
#define UNIQ(v) sort(ALL(v)); v.erase(unique(ALL(v)),end(v))
#define CHMIN(x,y) x = min(x, y)
#define CHMAX(x,y) x = max(x, y)
#define YES(b) cout << ((b) ? "YES" : "NO") << endl
#define Yes(b) cout << ((b) ? "Yes" : "No") << endl

const int MAX = 20;

int N, M;
vii X[MAX];
ll dp[1<<MAX];

int main() {
    cout << fixed << setprecision(15);
    cin >> N >> M;
    REP (i, M) {
        int x, y, z;
        cin >> x >> y >> z;
        X[x].push_back(make_pair(y, z));
    }

    vector<bool> visit(1<<N, false);
    queue<int> q;
    q.push(0);
    dp[0] = 1;
    while (q.size()) {
        int state = q.front(); q.pop();
        int b = __builtin_popcount(state) + 1;
        REP (i, N) if ((state & (1<<i)) == 0) {
            int next = state | (1<<i);
            bool check = true;
            FORE (x, X[b]) {
                if (__builtin_popcount(next&((1<<(x.first))-1)) > x.second) check = false;
            }
            if (check) {
                dp[next] += dp[state];
                if (!visit[next]) {
                    q.push(next);
                    visit[next] = true;
                }
            }
        }
    }
    cout << dp[(1<<N)-1] << endl;
}

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

typedef long long ll;
typedef vector<ll> vi;
ll n, m;
vi x(100), y(100), z(100);

ll cntmask(ll n) {
    ll ans = 0;
    while (n) {
        ans += n%2;
        n /= 2;
    }
    return ans;
}

bool checkrule(ll mask, ll rule) {
    ll cnt = 0;
    for (ll i = 0; i < y[rule]; ++i)
        if (mask & (1<<i))
            ++cnt;
    return cnt <= z[rule];
}

bool check(ll mask) {
    ll elems = cntmask(mask);
    for (ll i = 0; i < m; ++i) {
        if (x[i] != elems) continue;
        if (not checkrule(mask, i)) return false;
    }
    return true;
}

ll solve() {
    vi memo(1<<n);
    memo[0] = 1;

    queue<ll> q;
    q.push(0);

    while (not q.empty()) {
        ll mask = q.front();
        q.pop();

        for (ll i = 0; i < n; ++i) {
            if (mask & (1<<i)) continue;
            if (check(mask + (1<<i))) {
                memo[mask + (1<<i)] += memo[mask];
                if (memo[mask + (1<<i)] == memo[mask])
                    q.push(mask + (1<<i));
            }
        }
    }

    return memo[(1<<n) - 1];
}

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

    cin >> n >> m;
    for (ll i = 0; i < m; ++i)
        cin >> x[i] >> y[i] >> z[i];

    cout << solve() << '\n';
    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define st first
#define nd second
#define pii pair<int,int>
#define mp make_pair
#define pll pair<long long,long long>

using namespace std;

const int nax = 205;
vector<pii> parki;
int dp[nax];
int n;

void solve(){
    cin >> n;
    for(int i=1;i<=n;i++){
        int x, y; cin>>x>>y;
        parki.pb(mp(x, y));
    }

    dp[0] = 0;
    for(int i=1;i<=2*n;i++){
        dp[i] = 1e9;
        if(i & 1) continue;

        for(int lo=1;lo<i;lo+=2){
            int hi = i;
            bool ok = true;
            set<int> xd;
            int len = (hi - lo + 1);
            for(pii cur : parki){
                if(cur.st != - 1){
                    if(cur.st >= lo && cur.st < lo + len / 2){
                        if(xd.count(cur.st)) ok = false;
                        xd.insert(cur.st);
                        if(cur.nd != -1){
                            if(cur.nd != cur.st + len / 2) ok = false;
                        }
                    }
                }
                else if(cur.nd != -1){
                    // cur.st = -1
                    if(cur.nd >= lo + len / 2 && cur.nd <= hi){
                        if(xd.count(cur.nd - len / 2)) ok = false;
                        xd.insert(cur.nd - len / 2);
                    }
                }
            }

            if(ok){
                dp[i] = min(dp[i], dp[lo - 1] + (int)(len / 2 - xd.size()));
            }
        }
    }

    int cnt = 0;
    for(pii cur : parki){
        if(cur == mp(-1, -1)) cnt++;
    }
    if(dp[n * 2] == cnt) cout << "Yes" << "\n";
    else cout << "No" << "\n";

}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    int tt = 1;
    // cin >> tt;
    while(tt--) solve();
    return 0;
}

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

using ll = long long;
using pii = pair<int, int>;
template <class T>
using V = vector<T>;
template <class T>
using VV = V<V<T>>;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define per(i, b) per2(i, 0, b)
#define per2(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define ALL(c) (c).begin(), (c).end()
#define SZ(x) ((int)(x).size())

constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }

template <class T, class U>
void chmin(T& t, const U& u) {
    if (t > u) t = u;
}
template <class T, class U>
void chmax(T& t, const U& u) {
    if (t < u) t = u;
}

template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
    os << "(" << p.first << "," << p.second << ")";
    return os;
}

template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    os << "{";
    rep(i, v.size()) {
        if (i) os << ",";
        os << v[i];
    }
    os << "}";
    return os;
}

#ifdef LOCAL
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cerr << " " << H;
    debug_out(T...);
}
#define debug(...) \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif

int main() {
    int N;
    cin >> N;
    V<int> A(N), B(N);
    int sz = N * 2;
    V<int> tp(sz), com(sz, -1);

    bool ng = false;

    rep(i, N) {
        cin >> A[i] >> B[i];
        if (A[i] != -1) {
            --A[i];
            if (tp[A[i]]) ng = true;
            tp[A[i]] = i + 1;
        }
        if (B[i] != -1) {
            --B[i];
            if (tp[B[i]]) ng = true;
            tp[B[i]] = -(i + 1);
        }

        if (A[i] != -1 && B[i] != -1) {
            com[A[i]] = i;
            com[B[i]] = i;
        }
    }
    if (ng) {
        puts("No");
        return 0;
    }

    V<bool> dp(sz + 1);
    dp[0] = true;

    rep(i, sz) {
        if (!dp[i]) continue;
        for (int j = i + 1; j < sz; ++j) {
            int w = j - i + 1;
            if (w & 1) continue;
            w /= 2;
            bool ok = true;

            rep(k, w) {
                int p = i + k, q = i + k + w;
                if(com[p] != com[q])ok = false;
                if (tp[p] != 0 && tp[q] != 0) {
                    if (tp[p] < 0 || tp[p] + tp[q] != 0) {
                        ok = false;
                    }
                }
                if (tp[p] < 0 || tp[q] > 0) {
                    ok = false;
                }
            }
            if (ok) {
                dp[j + 1] = true;
            }
        }
    }
    puts(dp[sz] ? "Yes" : "No");

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define f(i,a,n) for(ll i=a;i<n;i++)
#define w(x) ll _t;cin>>_t;f(x,1,_t+1)
#define br cout<<"\n"
#define N 10000000
ll md=1e9+7,n;
ll h,w,m;
//l,d,r,u
ll x[]={0,1,0,-1};
ll y[]={1,0,-1,0};
ll mat[1501][1501];
bool isSafe(ll i,ll j){
    return i>=0 && i<h && j>=0 && j<w;
}

map<pair<ll,ll>,vector<ll>> mp;

void dfs(ll i,ll j,ll cur_dir){
    if(!isSafe(i,j)) return ;
    if(mat[i][j]==2) return ;
    if(mat[i][j]==-1){
        if(cur_dir ==0 || cur_dir ==2) mp[{i,j}][0]=mp[{i,j}][2]=0;
        if(cur_dir ==1 || cur_dir ==3) mp[{i,j}][1]=mp[{i,j}][3]=0;
    }
    else
        mat[i][j]=1;
    dfs(i+x[cur_dir],j+y[cur_dir],cur_dir);
}

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

    //w(_x)
    {
        ll ans=0;
        cin>>h>>w>>n>>m;
        vector<pair<ll,ll>> bulb;
        ll bi,bj;
        f(i,0,h) f(j,0,w) mat[i][j]=0;
        f(ti,0,n){
            cin>>bi>>bj;
            mat[bi-1][bj-1]=-1;
            bulb.push_back({bi-1,bj-1});
            mp[{bi-1,bj-1}].push_back(1);
            mp[{bi-1,bj-1}].push_back(1);
            mp[{bi-1,bj-1}].push_back(1);
            mp[{bi-1,bj-1}].push_back(1);
        }
        f(i,0,m){
            cin>>bi>>bj;
            mat[bi-1][bj-1]=2;
        }
        f(i,0,n){
            bi=bulb[i].first;
            bj=bulb[i].second;
            if(mp[{bi,bj}][0]) dfs(bulb[i].first+x[0],bulb[i].second+y[0],0);
            if(mp[{bi,bj}][1]) dfs(bulb[i].first+x[1],bulb[i].second+y[1],1);
            if(mp[{bi,bj}][2]) dfs(bulb[i].first+x[2],bulb[i].second+y[2],2);
            if(mp[{bi,bj}][3]) dfs(bulb[i].first+x[3],bulb[i].second+y[3],3);
        }
        f(i,0,h) f(j,0,w) if(mat[i][j]==1 || mat[i][j]==-1) ans++;
        cout<<ans;
    }
    return 0;
}

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

#define REP(i,m,n) for(int i=(m); i<(int)(n); i++)
#define RREP(i,m,n) for(int i=(int)((n)-1); i>=m; i--)
#define rep(i,n) REP(i,0,n)
#define rrep(i,n) RREP(i,0,n)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define fi first
#define se second
#define debug(...) {cerr<<"[L"<<__LINE__<<"] "; _debug(__VA_ARGS__);}

template<typename T>
string join(const vector<T>&v, string del=", "){ stringstream s;
    for(auto x : v) s << del << x; return s.str().substr(del.size());
}
template<typename T>
ostream& operator<<(ostream& o, const vector<T>&v){
    if(v.size()) o << "[" << join(v) << "]"; return o;
}
template<typename T>
ostream& operator<<(ostream& o, const vector<vector<T> >&vv){
    int l = vv.size();
    if(l){ o<<endl; rep(i,l) o << (i==0 ? "[ " : ",\n  " ) << vv[i] << (i==l-1 ? " ]" : ""); }
    return o;
}
template<typename T1, typename T2>
ostream& operator<<(ostream& o, const pair<T1, T2>& p){
    return o << "(" << p.first << ", " << p.second << ")";
}
inline void _debug(){cerr<<endl;}
template<class First, class... Rest>
void _debug(const First& first, const Rest&... rest){cerr<<first<<" ";_debug(rest...);}

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;

const double PI = (1*acos(0.0));
const double EPS = 1e-9;
const int INF = 0x3f3f3f3f;
const ll INFL = 0x3f3f3f3f3f3f3f3fLL;
const ll mod = 1e9 + 7;

inline void finput(string filename) {
    freopen(filename.c_str(), "r", stdin);
}

int main(){
    ios_base::sync_with_stdio(0);
    // finput("./input");
    int t; cin >> t;
    while(t--){
        string s; cin >> s;
        int ans = -1;
        if("atcoder" < s){
            ans = 0;
        }else{
            rep(j,s.size()){
                if('a' < s[j]){
                    if('t' < s[j]){
                        ans = j-1;
                    }else{
                        ans = j;
                    }
                    break;
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}


#include <cstdio>
#include <cmath>
#include <iostream>
#include <set>
#include <algorithm>
#include <vector>
#include <map>
#include <cassert>
#include <string>
#include <cstring>
#include <queue>

using namespace std;

#define rep(i,a,b) for(int i = a; i < b; i++)
#define S(x) scanf("%d",&x)
#define S2(x,y) scanf("%d%d",&x,&y)
#define P(x) printf("%d\n",x)
#define all(v) v.begin(),v.end()
#define FF first
#define SS second
#define pb push_back
#define mp make_pair

typedef long long int LL;
typedef pair<int, int > pii;
typedef vector<int > vi;

const int N = 505;
const int mod = 998244353;

string s, t;

int main() {
  int n,m;
  S2(n,m);
  int ways = 1;
  rep(i,0,n+m-1) t += ".";
  rep(i,0,n) {
    cin >> s;
    rep(j,0,m) {
      if(s[j] == '.') continue;
      if(s[j] == 'B') {
        if(t[i + j] == 'R') ways = 0;
        t[i + j] = 'B';
      }

      if(s[j] == 'R') {
        if(t[i + j] == 'B') ways = 0;
        t[i + j] = 'R';
      }
    }
  }

  rep(i,0,n+m-1) if(t[i] == '.') {
    ways = ways * 2;
    if(ways >= mod) {
      ways -= mod;
    }
  }
  P(ways);
  return 0;
}

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define all(c) (c).begin(),(c).end()
#define endl "\n"
#define ff first
#define ss second
#define allr(c) (c).rbegin(),(c).rend()
#define fr(x,in,n,r) for(ll x=in;x<n;x+=r)
#define ifr(x,n) for(ll x=0;x<n;x++)
#define dfr(x,n) for(ll x=n-1;x>=0;x--)
#define pb(a) push_back(a)
#define pf(a) push_front(a)
#define pof(a) pop_front(a)
#define pob(a) pop_back(a)
#define eb(a) emplace_back(a)
#define ef(a) emplace_front(a)
#define fstm(m,n,r) m.reserve(n);m.max_load_factor(r)
#define os tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
typedef long long ll;
typedef map<ll,ll> mll;
typedef map<string,ll> msll;
typedef unordered_map<ll,ll> umap;
typedef vector<ll> vll;
typedef pair<ll,ll> pll;
typedef long double ld;
#define mod 1000000007
#define N 200001

vector<pll> v[N];ll a[N];
bool vis[N];

void dfs(ll u){
    vis[u]=1;
    ifr(i,v[u].size()){
        if(!vis[v[u][i].ff]){
            a[v[u][i].ff]=a[u]^v[u][i].ss;
            dfs(v[u][i].ff);
        }
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll n,x,y,w;
    cin>>n;
    ifr(i,n-1){
        cin>>x>>y>>w;
        v[x].pb(pll(y,w));
        v[y].pb(pll(x,w));
    }
    a[1]=0;
    dfs(1);
    ll ans=0;
    ifr(i,60){
        ll cnt =0,c=0,r=1ll<<i;
        fr(j,1,n+1,1){
            if(r&(a[j]))cnt++;
            else c++;
        }
        ans =(ans + ((r)%mod*(cnt*c)%mod)%mod )%mod;
    }
    cout<<ans<<endl;
    return 0;
}


//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,avx512f")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <stack>
#include <queue>
#include <bitset>
#include <numeric>
#include <cassert>
#include <memory>
#include <random>
#include <functional>
#include <complex>
#include <immintrin.h>
#ifdef DEBUG
#include "./Lib/debug_VC.hpp"
#include "./Lib/Timer.hpp"
#include "./Lib/sample.hpp"
#else
#define dump(...)
#endif

/* macro */
#define FOR(i, b, e) for(ll i = (ll)(b); i < (ll)(e); ++i)
#define RFOR(i, b, e) for(ll i = (ll)(e-1); i >= (ll)(b); --i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define REPC(x,c) for(const auto& x:(c))
#define REPI2(it,b,e) for(auto it = (b); it != (e); ++it)
#define REPI(it,c) REPI2(it, (c).begin(), (c).end())
#define RREPI(it,c) REPI2(it, (c).rbegin(), (c).rend())
#define REPI_ERACE2(it, b, e) for(auto it = (b); it != (e);)
#define REPI_ERACE(it, c) REPI_ERACE2(it, (c).begin(), (c).end())
#define ALL(x) (x).begin(),(x).end()
#define cauto const auto&
/* macro func */
template<class T>
inline auto sort(T& t) { std::sort(ALL(t)); }
template<class T>
inline auto rsort(T& t) { std::sort((t).rbegin(), (t).rend()); }
template<class T>
inline auto unique(T& t) { (t).erase(unique((t).begin(), (t).end()), (t).end()); }
template<class T, class S>
inline auto chmax(T& t, const S& s) { if (s > t) { t = s; return true; } return false; }
template<class T, class S>
inline auto chmin(T& t, const S& s) { if (s < t) { t = s; return true; } return false; }
inline auto BR() { std::cout << "\n"; }

/* type define */
using ll = long long;
using PAIR = std::pair<ll, ll>;
using VS = std::vector<std::string>;
using VL = std::vector<long long>;
using VVL = std::vector<VL>;
using VVVL = std::vector<VVL>;
using VD = std::vector<double>;
template<class T>
using V = std::vector<T>;

/* using std */
using std::cout;
constexpr char endl = '\n';
using std::cin;
using std::pair;
using std::string;
using std::stack;
using std::queue;
using std::vector;
using std::list;
using std::map;
using std::unordered_map;
using std::multimap;
using std::unordered_multimap;
using std::set;
using std::unordered_set;
using std::unordered_multiset;
using std::multiset;
using std::bitset;
using std::priority_queue;

/* Initial processing  */
struct Preprocessing { Preprocessing() { std::cin.tie(0); std::ios::sync_with_stdio(0); }; }_Preprocessing;

/* Remove the source of the bug */
signed pow(signed, signed) { assert(false); return -1; }

/* define hash */
namespace std {
template <>	class hash<std::pair<ll, ll>> { public:	size_t operator()(const std::pair<ll, ll>& x) const { return hash<ll>()(1000000000 * x.first + x.second); } };
}

/* input */
template<class T> std::istream& operator >> (std::istream& is, vector<T>& vec) { for (T& x : vec) is >> x; return is; }

/* constant value */
//constexpr ll MOD = 1000000007;
constexpr ll MOD = 998244353;

//=============================================================================================;

auto d(ll n, ll p) {
    while (n > 0) {
        ll k = n % p;
        if (k == 7) { return false; }
        n /= p;
    }
    return true;
}

signed main() {
    ll n;
    cin >> n;

    ll ans = 0;
    FOR(i, 1, n + 1) {
        ans += (d(i, 10) && d(i, 8));
    }
    cout << ans << endl;
}

#include <bits/stdc++.h>

#define endl '\n'
#define fi first
#define se second
#define MOD(n,k) ( ( ((n) % (k)) + (k) ) % (k))
#define forn(i,n) for (int i = 0; i < int(n); i++)
#define forr(i,a,b) for (int i = a; i <= b; i++)
#define all(v) v.begin(), v.end()
#define pb push_back

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> ii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;

vvi res;

vvi comb (vvi &a, vvi &b) {
	vvi res;
	forn (i, a.size()) {
		vi v;
		for (int x : a[i]) v.pb(x);
		for (int x : b[i]) v.pb(x);
		res.pb(v);
	}
	return res;
}

void go (int u) {
	if (!u) return;
	go(u - 1);

	vvi a = res, b = res;

	res = {{}};
	forn (i, (1 << u - 1)) res[0].pb(1);
	forn (i, (1 << u - 1)) res[0].pb(0);

	for (vi v : comb(a, b))
		res.pb(v);

	for (vi &v : b)
		for (int &x : v)
			x ^= 1;

	for (vi v : comb(a, b))
		res.pb(v);
}

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

	int n;
	cin >> n;
	go(n);

	cout << res.size() << endl;
	for (vi &v : res) {
		for (int x : v)
			cout << "AB"[x];
		cout << endl;
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	string s;
	cin >> s;
	int t = s.size();
	int flag=0;
	for(int i=0;i<t;i++){
		if(i%2==0){
			if((s[i]-'Z')<=0){
				flag++;
			}
		}
		else{
			if((s[i]-'Z')>0){
				flag++;
			}
		}
	}
	if(flag>0){
		cout << "No" << endl;
	}
	else{
		cout << "Yes" << endl;
	}
}

#include <bits/stdc++.h>
using namespace std;
int main(){
    string s;
    cin>>s;
    for (unsigned int i{};i<s.length()/2;i++){
        char temp = s.at(i);
        s.at(i) = s.at(s.length() - i - 1);
        s.at(s.length() - i - 1) = temp;
    }
    for (unsigned int i{};i<s.length();i++){
        if (s.at(i) == '9'){
            s.at(i) = '6';
        }else if(s.at(i) == '6'){
            s.at(i) = '9';
        }
    }
    cout<<s<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin>>N;
  if(N%2==1){
    cout<<"Black"<<endl;
  }else{
    cout<<"White"<<endl;
  }

}

#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 20000;
int Max[MAXN<<2];
int S[MAXN];
void build(int l,int r,int rt) {
    if(l == r) {
        Max[rt] = S[l];
        return ;
    }
    int m = (l+r)>>1;
    build(l,m,rt<<1);
    build(m+1,r,rt<<1|1);
    Max[rt] = max(Max[rt<<1],Max[rt<<1|1]);
}
int query(int L,int R,int l,int r,int rt) {
    if(l>=L && r<=R) {
        return Max[rt];
    }
    int m = (l+r)>>1;
    int Max = -1;
    if(L<=m) Max = max(Max,query(L,R,l,m,rt<<1));
    if(R > m) Max = max(Max,query(L,R,m+1,r,rt<<1|1));
    return Max;
}
int main() {
    int n;scanf("%d",&n);
    if(n%2==0) {
        cout<<"White"<<endl;
    }else {
        cout<<"Black"<<endl;
    }
    return 0;
}


// Qg3
// //g++ -std=c++17 -O2 -Wall a.cpp -o test
// _()_
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define vll vector<ll>
#define vi vector<int>
#define vb vector<bool>
#define pi pair<int,int>
#define pll pair<ll,ll>
#define vp vector<pi>
#define vpll vector<pll>
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
#define For(i,a,b) for(ll i=a;i<b;i++)
//#define endl "\n"
#define debug2(x,y) cout<<"This side ----> "<<#x<<" -> "<<x<<" | "<<#y<<" -> "<<y<<endl;
#define debug(x) cout<<"This side    ----> "<<#x<<" -> "<<x<<endl
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(), x.rend()
#define mint map<int,int>
#define mall map<ll,ll>
#define ciN cin
#define gu(a,s) get<a>(s)
#define tin tuple<ll,ll,ll>
#define ter(x,y,z) ((x)?y:z)
#define ul ll
/////////////
const ll N = 1e6 + 3;
const ll maxn = 2e5 + 5;
const ll max_val = 5e4 + 10;
ll mod = 1e9 + 7;
const ll bits = 20;
ll caseNumber = 1;
////////////////////////////////////////////////////////////////
vi adj[maxn];
vb vis(maxn, 0);
ll cnt = 0;
void dfs(int st) {
	vis[st] = 1;
	cnt++;
	for (auto x : adj[st]) {
		if (!vis[x])dfs(x);
	}
}
void jabru() {
	ll n; cin >> n;
	vll v(n);
	for (int i = 0; i < n; i++)cin >> v[i];

	for (int i = 0; i < n / 2; i++) {
		if (v[i] != v[n - i - 1]) {
			adj[v[i]].pb(v[n - i - 1]);
			adj[v[n - i - 1]].pb(v[i]);
		}
	}
	ll ans = 0;
	for (int i = 1; i < maxn; i++) {
		if (!vis[i]) {
			cnt = 0;
			dfs(i);
			ans += cnt - 1;
		}
	}
	cout << ans << endl;
}
bool TestCase1 = 0;
bool isGoogles = 0;

//////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////

int main() {
	// freopen("input.txt", "r", stdin);
	// freopen("output.txt", "w", stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	ll t;
	t = 1;

	if (TestCase1) {
		cin >> t;
	}
	while (t--) {
		if (isGoogles) {cout << "Case #" << caseNumber << ": ";} caseNumber++;
		jabru();

	}
	return 0;
}
/* stuff you should look for
	* int overflow, array bounds
	* special cases (n=1?)
	* do smth instead of nothing and stay organized
	* WRITE STUFF DOWN
	* DON'T GET STUCK ON ONE APPROACH

*/

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

int N, A[200000], ufds[200000], rnk[200000];
map<int,int> m;

int findSet(int i) {
    return ufds[i] == i ? i : ufds[i] = findSet(ufds[i]);
}

void unionSet(int i, int j) {
    int a = findSet(i), b = findSet(j);
    if (a == b) return;
    if (rnk[a] < rnk[b]) ufds[a] = b;
    if (rnk[a] > rnk[b]) ufds[b] = a;
    if (rnk[a] == rnk[b]) ufds[a] = b, rnk[b]++;
}

int main() {
    scanf("%d", &N);
    for (int i = 0; i < N; ++i) scanf("%d", &A[i]);
    for (int i = 0; i < N; ++i) ufds[i] = i, rnk[i] = 1;
    for (int i = 0; i < N; ++i) {
        if (m[A[i]] != 0) unionSet(i,m[A[i]]-1);
        m[A[i]] = i+1;
    }
    int ans = 0;
    for (int i = 0; i*2 < N; ++i) if (findSet(i) != findSet(N-i-1)) {
        ans++;
        unionSet(i,N-i-1);
    }
    printf("%d", ans);

    return 0;
}