#include<bits/stdc++.h>
#define ll long long
using namespace std;
int read()
{
	char c;
	int w=1;
	while((c=getchar())>'9'||c<'0')if(c=='-')w=-1;
	int ans=c-'0';
	while((c=getchar())>='0'&&c<='9')ans=(ans<<1)+(ans<<3)+c-'0';
	return ans*w;
}
int n;
ll X;
int a[105];
int b[105];
int f[105][105][105];
int vis[105][105][105];
int mod;
int red(int x)
{
	return x>=mod?x-mod:x;
}
void check(int a,int b,int c)
{
	if(vis[a][b][c]!=mod)
	{
		vis[a][b][c]=mod;
		f[a][b][c]=-1e9;
	}
}
int main(){
//	freopen("a.in","r",stdin);
	cin>>n>>X;
	for(int i=1;i<=n;i++)a[i]=read();
	ll ans=1e18;
	for(mod=1;mod<=n;mod++)
	{
		for(int i=1;i<=n;i++)b[i]=a[i]%mod;
		vis[0][0][0]=mod;
		for(int x=0;x<n;x++)
		{
			for(int i=max(0,mod-n+x);i<=x;i++)
			{
				for(int j=0;j<mod;j++)
				{
					if(vis[x][i][j]!=mod)continue;
//					cout<<x<<" "<<i<<" "<<j<<" "<<f[x][i][j]<<endl;
					int o=red(j+b[x+1]);
					check(x+1,i,j);
					check(x+1,i+1,o);
					f[x+1][i][j]=max(f[x+1][i][j],f[x][i][j]);
					f[x+1][i+1][o]=max(f[x+1][i+1][o],f[x][i][j]+a[x+1]);
				}
			}
		}
		int s=X%mod;
		if(vis[n][mod][s]!=mod)continue;
//		cout<<mod<<" "<<f[n][mod][s]<<endl;
		ans=min(ans,(X-f[n][mod][s])/mod);
	}
	cout<<ans<<endl;
	return 0;
}

#include<bits/stdc++.h>

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

#define mod 1000000007
#define IO ios_base::sync_with_stdio(false);cin.tie(NULL);
#define fo(i,a,b) for(i=a;i<b;i++)
#define forr(i,a,b) for(i=a;i>=b;i--)
#define mp make_pair
#define pb(x) push_back(x)
#define fi first
#define se second
#define print(vec,a,b) for(ll i=a;i<b;i++) cout<<vec[i]<<" ";cout<<endl;
#define all(a) a.begin(),a.end()
#define input(vec,a,b) for(ll i = a;i<b;i++) cin>>vec[i];
#define ms(a,val) memset(a,val,sizeof(a))
using namespace std;

const int N = 2e5+ 5;

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

ll modInverse(ll a, ll m = mod) {
	return expo_pow(a, m - 2);
}

using matrix = vector<vector<ll>>;

matrix matMul(matrix A, matrix B) {
	//assert((int)A[0].size() == (int)B.size());
	matrix res((ll)A.size(), vector<ll>((ll)B[0].size(), 0));
	for (int i = 0; i < (int)A.size(); ++i) {
		for (int j = 0; j < (int)B[0].size(); ++j) {
			for (int k = 0; k < (int)A[0].size(); ++k) {
				res[i][j] += (A[i][k] * B[k][j]) % mod;
				res[i][j] %= mod;
			}
		}
	}
	return res;
}

matrix matPow(matrix M, int p) {
	if (p == 1) return M;
	if (p % 2) return matMul(M, matPow(M, p - 1));
	matrix X = matPow(M, p / 2);
	return matMul(X, X);
}

void solve(){
            ll i,j,n,m,k;
            cin>>n>>m>>k;

            vv a(1,vi(n,0));
            input(a[0],0,n);
            vv adj(n,vector<ll>(n,0));

            vi deg(n,0);
            fo(i,0,m)
            {
				ll x,y;
				cin>>x>>y;
				x--,y--;
				adj[x][y] = 1;
				adj[y][x] = 1;
				deg[x]++;
				deg[y]++;
			}

			ll inv = modInverse(2*m,mod);

			vv M(n,vi(n,0));

			if(k == 0)
			{
				print(a[0],0,n);
				return ;
			}
			fo(i,0,n)
			{
				fo(j,0,n)
				{
					if(i == j)
					{
						M[i][j] = (1 - (deg[i]*inv)%mod + mod)%mod;
					}
					else
					{
						M[i][j] = inv*adj[i][j];
					}
				}
			}
			M = matPow(M, k);
			a = matMul(a,M);
			fo(i,0,n)
			{
				cout<<a[0][i]<<endl;
			}
}

int main()
{
     IO;
      ll t=1,i;
      //cin>>t;

      while(t--)
      {

          solve();
      }
    return 0;
}


//#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>

using namespace std;

//vector string deque break continue
#define forn(i, s, f) for (int i = (int)s; i < (int)f; i++)
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair <int, int>
#define fs first
#define sc second
#define pf push_front
#define pb push_back
#define pop_f pop_front
#define pop_b pop_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()

#ifdef DEBUG
#else
    #define cerr if (false) cerr
#endif

template <typename T> istream& operator>>(istream& in, vector <T>& a) {for (auto& i : a) in >> i; return in;}
template <typename T> ostream& operator<<(ostream& out, vector <T>& a)  {for (auto& i : a) out << i << " "; return out;}
template <typename T, typename U> void chkmin(T& a, U b) {if (a > b) a = b;}
template <typename T, typename U> void chkmax(T& a, U b) {if (a < b) a = b;}

const int MOD = 998244353;

int add(int x, int y) {
    x += y;
    if (x >= MOD)
        return x - MOD;
    return x;
}

int sub(int x, int y) {
    x -= y;
    if (x < 0)
        return x + MOD;
    return x;
}

int mul(int x, int y) {
    return (1LL * x * y) % MOD;
}

int bin_pow(int a, int p) {
    if (!p)
        return 1;
    if (p % 2)
        return (1LL * bin_pow(a, p - 1) * a) % MOD;
    int t = bin_pow(a, p / 2);
    return (1LL * t * t) % MOD;
}

int inv(int x) {
    return bin_pow(x, MOD - 2);
}

class Dsu {
private:
    int n;
    vector <int> p, sz;
public:
    Dsu(int _n) {
        n = _n;
        p.resize(n, -1);
        sz.resize(n, 1);
    }

    int get(int v) {
        if (p[v] == -1)
            return v;
        return p[v] = get(p[v]);
    }

    void unite(int v, int u) {
        v = get(v), u = get(u);
        if (v == u)
            return;
        if (sz[v] > sz[u])
            swap(v, u);
        p[v] = u;
        sz[u] += sz[v];
    }

    int get_size(int comp) const {
        return sz[comp];
    }
};

vector <int> f;

int n, limit;

int solve(vector <vector <int>> a) {
    Dsu gr(sz(a));
    forn (i, 0, n) {
        forn (j, i + 1, n) {
            bool ok = true;
            forn (k, 0, n) {
                if (a[i][k] + a[j][k] > limit)
                    ok = false;
            }
            if (ok)
                gr.unite(i, j);
        }
    }
    set <int> same;
    forn (i, 0, n)
        same.insert(gr.get(i));
    int ans = 1;
    for (int i : same) {
        ans = mul(ans, f[gr.get_size(i)]);
        cerr << i << " ";
    }
    cerr << "\n";
    return ans;
}

int main() {
    ios_base::sync_with_stdio(0), cin.tie(0);
    cin >> n >> limit;
    vector <vector <int>> a(n, vector <int> (n));
    cin >> a;
    f.resize(1e5);
    f[0] = 1;
    forn (i, 1, sz(f))
        f[i] = mul(f[i - 1], i);
    int t = solve(a);
    forn (i, 0, n)
        forn (j, i + 1, n)
            swap(a[i][j], a[j][i]);
    cerr << t << "\n";
    cout << mul(t, solve(a));
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

// using mint = long double;
// using mint = modint998244353;
// using mint = modint1000000007;

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<P, ll> T;
typedef pair<ll, vector<ll>> Pd;

const ll INF = 2e18;
const ll fact_table = 1200008;

priority_queue<ll> pql;
priority_queue<P> pqp;
// big priority queue
// priority_queue <ll, vector<ll>, greater<ll> > pqls;
priority_queue<P, vector<P>, greater<P>> pqps;
// small priority queue
// top pop

ll dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
ll dy[8] = {0, 1, 0, -1, -1, 1, 1, -1};
//↓,→,↑,←

/*
#define endl "\n"
#ifdef ENJAPMA
#undef endl
#endif
*/

#define p(x) cout << x << endl;
#define el cout << endl;
#define pe(x) cout << x << " ";
#define ps(x) cout << fixed << setprecision(25) << x << endl;
#define pu(x) cout << (x);
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define pc(x) cout << x << ",";
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, a, b) for (ll i = a; i <= (b); i++)
#define rep3(i, a, b) for (ll i = a; i >= (b); i--)
#define all(c) begin(c), end(c)

typedef vector<ll> vec;
typedef vector<vector<ll>> mat;

// vec v(n) -> 長さnのベクトルを宣言
// mat dp(h, vec(w)) -> h * w の行列を宣言

const ll mod = 998244353ll;
// const ll mod = 1000000007ll;

ll mypow(ll a, ll b, ll m = mod) {
    ll x = 1;
    while (b) {
        while (!(b & 1)) {
            (a *= a) %= m;
            b >>= 1;
        }
        (x *= a) %= m;
        b--;
    }
    return x;
}
vec rv(ll read) {
    vec res(read);
    for (int i = 0; i < read; i++) {
        cin >> res[i];
    }
    return res;
}

/*
ll fact[fact_table + 5], rfact[fact_table + 5];

void c3_init() {
    fact[0] = rfact[0] = 1;
    for (ll i = 1; i <= fact_table; i++) {
        fact[i] = (fact[i - 1] * i) % mod;
    }
    rfact[fact_table] = mypow(fact[fact_table], mod - 2, mod);
    for (ll i = fact_table; i >= 1; i--) {
        rfact[i - 1] = rfact[i] * i;
        rfact[i - 1] %= mod;
    }
    return;
}
ll c3(ll n, ll r) {
    return (((fact[n] * rfact[r]) % mod) * rfact[n - r]) % mod;
}
*/
bool icpc = false;
bool multicase = false;

ll n, k;
ll dist = 0, cnt = 0;

vec G[500005];

P dfs(int v, int p) {
    // first  : 一番近い黒までの距離
    // second : 一番遠い白までの距離
    ll black = INF, white = 0;

    for (auto nv : G[v]) {
        if (nv == p) continue;
        auto [b, w] = dfs(nv, v);
        b++;
        w++;
        black = min(black, b);
        white = max(white, w);
    }

    if (white + black <= dist) {
        white = -1;
    }
    if (white == dist) {
        cnt++;
        white = -1, black = 0;
    }
    if (p == -1 && white >= 0) {
        cnt++;
    }
    return P(black, white);
}

int calc(ll mid) {
    dist = mid;
    cnt = 0;
    P res = dfs(1, -1);
    return cnt;
}

bool solve() {
    cin >> n >> k;
    rep(i, n - 1) {
        int a, b;
        cin >> a >> b;
        G[a].pb(b);
        G[b].pb(a);
    }

    ll ok = n, ng = -1;
    while (abs(ok - ng) > 1) {
        ll mid = (ok + ng) / 2;
        if (calc(mid) <= k) {
            ok = mid;
        } else {
            ng = mid;
        }
    }
    p(ok);
    return true;
}

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

    if (icpc) {
        while (solve())
            ;
        return 0;
    }
    ll q, testcase = 1;
    if (multicase) {
        cin >> q;
    } else {
        q = 1;
    }
    while (q--) {
        solve();
        testcase++;
    }

    // solve();
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i,m,n) for(int i=(m);i<(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(c) (c).rbegin(),(c).rend()
#define mp make_pair
#define endl '\n'
//#define vec vector<ll>
//#define mat vector<vector<ll> >
#define fi first
#define se second
#define double long double
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
//typedef long double ld;
typedef complex<double> Complex;
const ll INF=1e9+7;
const ll MOD=998244353;
const ll inf=INF*INF;
const ll mod=MOD;
const ll MAX=1000010;
const double PI=acos(-1.0);
typedef vector<vector<ll> > mat;
typedef vector<ll> vec;

//dijkstra(O(ElogV))
//0-indexed
void dijkstra(ll n,ll s,vector<vector<pair<ll,pll> > >&G,vector<ll> &d){
    priority_queue<pll,vector<pll>,greater<pll> >pq;
    rep(i,n){
        d[i]=inf;
    }
    d[s]=0;
    pq.push(mp(0,s));
    while(!pq.empty()){
        pll k=pq.top();pq.pop();
        ll v=k.second;
        if(d[v]<k.first)continue;
        for(auto e:G[v]){
            ll x=e.se.fi,y=e.se.se;
            ll nxt=x+y/(d[v]+1);
            if(y>0){
            ll z=sqrt(y);
                if(z>=d[v]){
                    nxt=min(nxt,x+min(y/(z+1)+z-d[v],y/(z+2)+1+z-d[v]));
                }
                if(z-1>=d[v]){
                    nxt=min(nxt,x+y/z+z-1-d[v]);
                }
            }
            if(d[e.first]>d[v]+nxt){
                d[e.first]=d[v]+nxt;
                pq.push(mp(d[e.first],e.first));
            }
        }
    }
}

signed main(){
    ll n,m;cin>>n>>m;
    vector<vector<pair<ll,pll> > >G(n);
    rep(i,m){
        ll a,b,c,d;cin>>a>>b>>c>>d;
        a--;b--;
        G[a].pb(mp(b,mp(c,d)));
        G[b].pb(mp(a,mp(c,d)));
    }
    vector<ll>d(n);
    dijkstra(n,0,G,d);
    ll ans=d[n-1];
    if(ans==inf)ans=-1;
    cout<<ans<<endl;
}

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+100;
typedef long long ll;
const int inf = 0x3f3f3f3f;
typedef pair<int,int> pii;
#define fr(i,a,b) for(int i = a;i <= b; ++i)
#define nfr(i,a,b) for(int i = a;i >= b; --i)

int n,l;
int a[N],b[N];
int pos[N];
set<pii> s1;
int main(){
   scanf("%d%d",&n,&l);
   fr(i,1,n)scanf("%d",a+i);
   fr(i,1,n)scanf("%d",b+i);
   fr(i,1,n){
      s1.insert(pii(a[i]-i,i));
   }
   a[n+1] = l+1;
   s1.insert(pii(0,0)); s1.insert(pii(a[n+1]-n-1,n+1));

   ll ans = 0;
   bool flag = true;
   fr(i,1,n){
      if(b[i] <= a[i]){
         pii cj = *(--s1.upper_bound(pii(b[i]-i,i)));
         if(cj.first != b[i]-i){
            flag = false;
         } else pos[i] = cj.second;
      } else {
         pii cj = *s1.lower_bound(pii(b[i]-i,i));
         if(cj.first != b[i]-i){
            flag = false;
         } else pos[i] = cj.second;
      }
   }
   if(!flag){
      puts("-1"); return 0;
   }

   pos[n+1] = pos[0] = 1145141919;
   fr(i,1,n){
      if(pos[i] == i)continue ;
      if(pos[i] < i){
         if(pos[i] == pos[i+1])continue ;
         else ans += i-pos[i];
      } else {
         if(pos[i] == pos[i-1])continue ;
         else ans += pos[i]-i;
      }
   }

   printf("%lld\n",ans);
   return 0;
}


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

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; }
long long mod= 1000000007;

vector<pair<int, int> > prime_factorize(int N) {
    vector<pair<int, int> > res;
    for (int a = 2; a * a <= N; ++a) {
        if (N % a != 0) continue;
        int ex = 0; // 指数

        // 割れる限り割り続ける
        while (N % a == 0) {
            ++ex;
            N /= a;
        }

        // その結果を push
        res.push_back({a, ex});
    }

    // 最後に残った数について
    if (N != 1) res.push_back({N, 1});
    return res;
}

int main(){
    int N;
    cin >> N;
    unsigned long long ans = 1;
    int prime[] = {2,3,5,7,11,13,17,19,23,29};

    unordered_map<int, int> um;

    for(int i=2; i <=N; ++i){
        vector<pair<int, int> > v = prime_factorize(i);
        for(int j=0; j<v.size(); ++j){
            pair<int, int> p = v[j];
            int prime = p.first;
            int num = p.second;
            if(um.find(prime) == um.end()){
                um[prime] = num;
            }
            else{
                if(um[prime] < num){
                    um[prime] = num;
                }
            }

        }
    }

    for(auto a:um){
        for(int i=0; i<a.second; ++i){
            ans *= a.first;
        }
    }
    ans +=1;

    cout << ans << endl;
    return 0;

}

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define bg begin()
#define en end()
#define endl "\n"
#define vvl(n,m) vector<vector<ll> > a( n , vector<ll> (m))
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define mod 1000000007
using namespace std;
ll power(ll x, ll y, ll m)
{
    if (y == 0)
        return 1;
    ll p = power(x, y / 2, m) % m;
    p = (p * p) % m;

    return (y % 2 == 0) ? p : (x * p) % m;
}
ll modInverse(ll a, ll m)
{
    return power(a, m - 2, m);
}
ll knapsack(vector<vector<ll>>&dp,ll x,ll i,ll n,ll s[],ll h[]){
    ll r,l;
    if(i>=n || x==0)
        return 0;
    if(dp[i][x]!=-1)
        return dp[i][x];
    if(x>=h[i]){
        l=s[i]+knapsack(dp,x-h[i],i+1,n,s,h);
        r=knapsack(dp,x,i+1,n,s,h);
        return dp[i][x]=max(l,r);
    }
    else
        return dp[i][x]=knapsack(dp,x,i+1,n,s,h);
}
void solve()
{
    ll r=1;
    r*=16*27*25*7*11*13;
    r*=(17*19*23*29);
    r++;
    cout<<r;
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    fast
    solve();
    #ifndef ONLINE_JUDGE
    cout << "\nTime Elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " sec\n";
    #endif
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef vector<int> VI;
typedef vector<LL> VLL;
typedef vector<LD> VLD;
typedef vector<string> VS;
#define rep(i, max) for (LL i = 0; i < (LL)(max); i++)
#define repm(i, min, max) for (LL i = (LL)(min); i < (LL)(max); i++)
#define rev(i, max) for (LL i = (LL)(max) - 1; i >= 0; i--)
#define revm(i, min, max) for (LL i = (LL)(max) - 1; i >= (LL)(min); i--)

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

  LL a = 3;
  int p = 1;
  while(a < N){
    LL b = 5;
    int q = 1;
    while(b < N){
      if(a + b == N){
        cout<<p<<" "<<q<<endl;
        return 0;
      }
      q++;
      b *= 5;
    }
    p++;
    a *= 3;
  }
  cout<<-1<<endl;
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
#define rep(i,n) for (int i=0; i<(n);++i)
ll n,p,q,mod=pow(10,9)+7;

int main(){
    cin >> n;
    p = n%5;
    if(p==0){
        cout << -1;
        return 0;
    }
    if(p==1) q=4;
    else if(p==2) q=3;
    else if(p==3) q=1;
    else q=2;
    ll t=1,ans=q,ans2=0;
    set<ll> s;
    while(true){
        if(t<pow(10,18)){
            t*=5;
            s.insert(t);
        }
        else break;
    }
    q = pow(3,q);
    rep(i,20){
        if(n>q){
            if(s.count(n-q)){
                ll r=n-q;
                while(true){
                    if(r%5==0){
                        ans2++;
                        r/=5;
                    }
                    else break;
                }
                cout << ans << ' ' << ans2 << endl;
                return 0;
            }
        }
        else{
            cout << -1;
            return 0;
        }
        q*=81;
        ans+=4;
    }
}


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

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

const int Nmax=2e5+9;
int N,M,A[109],B[109],C[19],D[19],K,ans,Ball[109],tmp;

int main() {
    ios_base::sync_with_stdio(0);    cin.tie(0);
    cin>>N>>M;
    REP(i,1,M) cin>>A[i]>>B[i];
    cin>>K;
    REP(i,1,K) cin>>C[i]>>D[i];
    REP(j,0,(1<<K)-1) {
        fill(Ball+1,Ball+N+1,0);
        REP(k,1,K) {
            if ((j>>(k-1))&1) Ball[C[k]]=1;
            else Ball[D[k]]=1;
        }
        tmp=0;
        REP(i,1,M) {
            if (Ball[A[i]] && Ball[B[i]]) ++tmp;
        }
        ans=max(ans,tmp);
    }
    cout<<ans;
    return 0;
}

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

mt19937 rng((unsigned int) chrono::steady_clock::now().time_since_epoch().count());
#define ll           long long
#define int          long long
#define vi           std::vector<int>
#define setbits(x)   __builtin_popcountll(x)
#define pb           push_back
#define fn           for(int i=0;i<n;i++)
#define rep(i,a,b)   for(int i=a;i<b;i++)
#define all(x)       x.begin(), x.end()
#define  ret(x) {cout << #x << endl; return;}
#define PI           3.14159265358979323846
#define pii          pair<ll,ll>
#define fr           first
#define sc           second

const int mod = 1e+7;
void getio();
ll ceill(ll a, ll b) { return (a + b - 1) / b; }

ll fastpow(ll x, ll n)
{
    if (n == 0)
        return 1;
    ll res = 1;
    while (n)
    {
        if (n % 2)
            res = res * x;
        x = x * x;
        n = n >> 1;
    }
    return res;
}

// Function to generate all binary strings
vector<vector<int>> store;
void generateAllBinaryStrings(int n, vi &arr, int i)
{
    if (i == n) {
        // printTheArray(arr, n);
        store.pb(arr);
        return;
    }

    // First assign "0" at ith position
    // and try for all other permutations
    // for remaining positions
    arr[i] = 0;
    generateAllBinaryStrings(n, arr, i + 1);

    // And then assign "1" at ith position
    // and try for all other permutations
    // for remaining positions
    arr[i] = 1;
    generateAllBinaryStrings(n, arr, i + 1);
}

void solve()
{
    int n, m;
    cin >> n >> m;
    vector<pair<int, int>> use, chos;
    vector<pair<int, int>> ar;
    rep(i,0,m)
    {
        int a, b;
        cin >> a >> b;
        ar.push_back({a, b});
    }

    int k;
    cin >> k;
    rep(i,0,k)
    {
        int c, d;
        cin >> c >> d;
        chos.pb({c, d});
    }

    vi per(k);
    generateAllBinaryStrings(k, per, 0);
    // cout << store.size();
    int ans = 0;
    int cnt = 0;
    for(int i=0; i<store.size(); i++)
    {
        cnt=0;
        vector<int> tem(n+1,0);
        for(int j=0; j<k; j++)
        {
            if(store[i][j] == 0)
                tem[chos[j].first]++;
            else tem[chos[j].second]++;
        }
        for(int i=0; i<m; i++)
        {
            if(tem[ar[i].first]>=1 and tem[ar[i].second]>=1)
                {
                    // cout << ar[i].first << ' ' << ar[i].second << endl;
                    cnt++;
                }
        }
        ans = max(ans, cnt);
    }
    cout << ans << endl;
}

int32_t main()
{
    ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    getio();
    // int t; cin >> t; while (t--)
    solve();
}

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


#include <bits/stdc++.h>
// #include <atcoder/all>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rep1(i,n) for(int i = 1; i <= (n); ++i)
// #define MOD 998'244'353
// #define MOD 1'000'000'007
// #define INF 1'000'000'000'000'000'000
using namespace std;
// using namespace atcoder;
typedef long long ll;

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

    if (M == 0){
        cout << 1 << endl;
        return 0;
    }

    vector<ll> A(M+1);
    A[0] = 0;
    rep1(i, M) cin >> A[i];

    if (N == M){
        cout << 0 << endl;
        return 0;
    }

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

    vector<ll> dif(M+1);
    rep(i, M){
        dif[i] = A[i+1] - A[i] - 1;
    }
    dif[M] = N - A[M];

    sort(dif.begin(), dif.end());
    int pos = 0;
    while(dif[pos] == 0){
        pos++;
    }
    ll k = dif[pos];

    // cout << k << endl;
    ll ans = 0;

    for(int i = pos; i <= M; i++){
        if (dif[i] % k == 0){
            ans += dif[i] / k;
        }else{
            ans += (dif[i] / k + 1);
        }
    }

    cout << ans << endl;

    return 0;
}

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <initializer_list>
#include <cmath>

#define REP(i, n)       for(int i = 0; i < (n); i++)
#define REPS(i, a, b)   for(int i = (a); i <= (b); i++)
#define REPR(i, n)      for(int i = (n) - 1; i >= 0; i--)
#define REPRS(i, b, a)  for(int i = (b) ; i >= (a); i--)
#define ALL(x)          (x).begin(), (x).end()
#define ALLR(x)         (x).rbegin(), (x).rend()

constexpr static int MOD = 1e8 + 7;

inline void scan() {}

template <typename T, typename... U>
inline void scan(T&& t, U&&... u) {
  std::cin >> t;
  scan(std::forward<U>(u)...);
}

template <typename T>
inline void scana(T t, int n) {
  for (int i = 0; i < n; i++) {
    std::cin >> t[i];
  }
}

template <typename T>
inline void scana(T t, int n, int m) {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      std::cin >> t[i][j];
    }
  }
}

inline void print() {
  std::cout << "\n";
}

template <typename T, typename... U>
inline void print(T&& t, U&&... u) {
  std::cout << t;
  print(std::forward<U>(u)...);
}

using graph = std::vector<std::vector<int>>;
using ll = long long;
using ull = unsigned long long;

ll pow(ll x, ll n) {
  if (n == 0) return 1;
  ll val = pow(x, n / 2);
  val *= val;
  if (n % 2 == 1) val *= x;
  return val;
}

int main() {
  int n;
  scan(n);

  int ans = static_cast<int>(std::sqrt(2 * n));
  while (true) {
    if ((ans * (ans + 1)) >= 2 * n) {
      break;
    } else {
      ans++;
    }
  }

  print(ans);

  return 0;
}


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

#define mkp make_pair
#define mkt make_tuple
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
template <class T> void chmin(T &a, const T &b) {
    if (a > b) a = b;
}
template <class T> void chmax(T &a, const T &b) {
    if (a < b) a = b;
}

void add(ll &a, ll b) {
    a = (a + b) % MOD;
}

void mul(ll &a, ll b) {
    a %= MOD;
    b %= MOD;
    a = a * b % MOD;
}

void solve() {
    int N;
    cin >> N;
    vector<ll> A(N);
    rep(i, N) cin >> A[i];

    vector<ll> num_dp(2, 0);
    num_dp[0] = 1;
    vector<ll> sum_dp(2, 0);
    sum_dp[0] = A[0];
    for (int i = 1; i < N; i++) {
        vector<ll> nex_num_dp(2, 0);
        vector<ll> nex_sum_dp(2, 0);
        {
            add(nex_num_dp[1], num_dp[0]);
            add(nex_num_dp[0], num_dp[0]);
            add(nex_num_dp[0], num_dp[1]);
        }
        {
            add(nex_sum_dp[1], ((sum_dp[0] - A[i] * num_dp[0] % MOD) % MOD + MOD) % MOD);
            add(nex_sum_dp[0], ((sum_dp[0] + A[i] * num_dp[0] % MOD) % MOD + MOD) % MOD);
            add(nex_sum_dp[0], ((sum_dp[1] + A[i] * num_dp[1] % MOD) % MOD + MOD) % MOD);
        }
        swap(nex_num_dp, num_dp);
        swap(nex_sum_dp, sum_dp);
    }

    ll ans = 0;
    add(ans, sum_dp[0]);
    add(ans, sum_dp[1]);
    cout << ans << endl;
}

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

    solve();

    return 0;
}

#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;

const int mod = 998244353;

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

void solve() {
    ll n;
    cin >> n;
    ll g = 1;
    for (ll i = 2; i <= n; i++) {
        g = g * i / __gcd(i, g);
    }
    cout << g + 1 << '\n';
}

int32_t main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T = 1;
    // cin >> T;
    for (int tc = 1; tc <= T; tc++) {
        // cout << "Case " << tc << ": ";
        solve();
    }
    return 0;
}


/**
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡀⠀⠀⠀⠀⢀⡀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⡖⠁⠀⠀⠀⠀⠀⠀⠈⢲⣄
⠀⠀⠀⠀⠀⠀⠀⠀⣼⡏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⣧
⠀⠀⠀⠀⠀⠀⠀⣸⣿⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣿⣇
⠀⠀⠀⠀⠀⠀⠀⣿⣿⡇⠀⢀⣀⣤⣤⣤⣤⣀⡀⠀⢸⣿⣿
⠀⠀⠀⠀⠀⠀⠀⢻⣿⣿⣔⢿⡿⠟⠛⠛⠻⢿⡿⣢⣿⣿⡟
⠀⠀⠀⠀⣀⣤⣶⣾⣿⣿⣿⣷⣤⣀⡀⢀⣀⣤⣾⣿⣿⣿⣷⣶⣤⡀
⠀⠀⢠⣾⣿⡿⠿⠿⠿⣿⣿⣿⣿⡿⠏⠻⢿⣿⣿⣿⣿⠿⠿⠿⢿⣿⣷⡀
⠀⢠⡿⠋⠁⠀⠀⢸⣿⡇⠉⠻⣿⠇⠀⠀⠸⣿⡿⠋⢰⣿⡇⠀⠀⠈⠙⢿⡄
⠀⡿⠁⠀⠀⠀⠀⠘⣿⣷⡀⠀⠰⣿⣶⣶⣿⡎⠀⢀⣾⣿⠇⠀⠀⠀⠀⠈⢿
⠀⡇⠀⠀⠀⠀⠀⠀⠹⣿⣷⣄⠀⣿⣿⣿⣿⠀⣠⣾⣿⠏⠀⠀⠀⠀⠀⠀⢸
⠀⠁⠀⠀⠀⠀⠀⠀⠀⠈⠻⢿⢇⣿⣿⣿⣿⡸⣿⠟⠁⠀⠀⠀⠀⠀⠀⠀⠈
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣼⣿⣿⣿⣿⣧⡀
⠀⠀⠀⠐⢤⣀⣀⢀⣀⣠⣴⣿⣿⠿⠋⠙⠿⣿⣿⣦⣄⣀⠀⠀⣀⡠⠂
⠀⠀⠀⠀⠀⠈⠉⠛⠛⠛⠛⠉⠀⠀⠀⠀⠀⠈⠉⠛⠛⠛⠛⠋⠁
 *    author:  Daredevil666
 *    institution: IIT Patna
**/
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define nl cout<<"\n";
#define ll unsigned long long int
#define REP(i,a,n) for(i=a;i<=n;i++)
#define F(i,a,b) for(i=a;i<b;i++)
#define RF(i,b,a) for(i=b;i>=a;i--)
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
const ll M=1e9+7;
const ll MAXN=100200;
ll i,j,mask;
ll power(ll p,ll x)
{
    ll res=1;
    while(x>0)
    {
        if(x%2==1)
        {
            res*=p;
        }
        x/=2;
        p*=p;
    }
    return res;
}
int main()
{
IOS
/*#ifndef ONLINE_JUDGE
   freopen("input.txt","r",stdin);
   freopen("output.txt","w",stdout);
#endif // ONLINE_JUDGE*/
ll n;
cin>>n;
for(i=1;power(3,i)<=n;i++)
{
    for(j=1;power(5,j)<=n;j++)
    {
        if(power(3,i)+power(5,j)==n)
        {
            cout<<i<<" "<<j;
            return 0;
        }
    }
}
cout<<-1;
return 0;
}

// Author : ps41

#include <bits/stdc++.h>
using namespace std;
#define ll int64_t
const ll M = 1e9 + 7, N = 1e6 + 5;

int main()
{
    ios::sync_with_stdio(false), cin.tie(nullptr);
    string s;
    cin >> s;
    int n = s.size();
    int ch = 1;
    for (int i = 0; i < n; i++)
    {
        if (i % 2)
        {
            if (s[i] < 'A' or s[i] > 'Z')
            {
                ch = 0;
                break;
            }
        }
        else
        {
            if (s[i] < 'a' or s[i] > 'z')
            {
                ch = 0;
                break;
            }
        }
    }
    if (ch)
        cout << "Yes\n";
    else
        cout << "No\n";
}


#include<bits/stdc++.h>

using namespace std;
#define int long long
// typedef long long int;

int const N = 4e6 + 10, mod = 1e9 + 7;
int fact[N], infact[N];
int n, m, k;

int qmi(int a, int k, int p) {
    int res = 1;
    while (k) {
        if (k & 1) res = res * a % p;
        k >>= 1;
        a = a * a % p;
    }
    return res;
}

// 预处理
void init() {
    fact[0] = infact[0] = 1;
    for (int i = 1; i < N; ++i) {
        fact[i] = (int)fact[i - 1] * i % mod;
        infact[i] = (int)infact[i - 1] * qmi(i, mod - 2, mod) % mod;
    }
}

int cnm(int a, int b) {
    return (int)fact[a] * infact[b] % mod * infact[a - b] % mod;
}

signed main() {
    init();  // 预处理
    cin >> n >> m >> k;  // c(n + m, n) - c(n + m, n - k - 1)
    if (n >= m + k + 1) cout << 0;
    else cout << ((cnm(n + m, n) % mod - cnm(n + m, m + k + 1) % mod) % mod + mod) % mod ;
    return 0;
}

// title

#include <cstdint>

template <std::uint_fast64_t Modulus> class modint {
  using u64 = std::uint_fast64_t;

public:
  u64 a;

  constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {}
  constexpr u64 &value() noexcept { return a; }
  constexpr const u64 &value() const noexcept { return a; }
  constexpr modint operator+(const modint rhs) const noexcept {
    return modint(*this) += rhs;
  }
  constexpr modint operator-(const modint rhs) const noexcept {
    return modint(*this) -= rhs;
  }
  constexpr modint operator*(const modint rhs) const noexcept {
    return modint(*this) *= rhs;
  }
  constexpr modint operator/(const modint rhs) const noexcept {
    return modint(*this) /= rhs;
  }
  constexpr modint &operator+=(const modint rhs) noexcept {
    a += rhs.a;
    if (a >= Modulus) {
      a -= Modulus;
    }
    return *this;
  }
  constexpr modint &operator-=(const modint rhs) noexcept {
    if (a < rhs.a) {
      a += Modulus;
    }
    a -= rhs.a;
    return *this;
  }
  constexpr modint &operator*=(const modint rhs) noexcept {
    a = a * rhs.a % Modulus;
    return *this;
  }
  constexpr modint &operator/=(modint rhs) noexcept {
    u64 exp = Modulus - 2;
    while (exp) {
      if (exp % 2) {
        *this *= rhs;
      }
      rhs *= rhs;
      exp /= 2;
    }
    return *this;
  }
};

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

#define prime 1000000007

modint<prime> path(int x, int y){
  if(x<0 || y<0){
    return 0;
  }
  modint<prime> ans = 1;
  rep(i,x+y){
    ans *=i+1;
  }
  rep(i,x){
    ans /=i+1;
  }
  rep(i,y){
    ans /=i+1;
  }
  return ans;
}

int main() {
  int N,M,K;
  cin >> N >> M >> K;
  if(N > M+K){
    cout << "0\n";
    return 0;
  }

  int ans = (path(N,M) - path(N-(K+1), M+(K+1))).value();
  cout << ans << endl;
}


#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define all(A) A.begin(), A.end()
typedef long long ll;

int main(){
  int n;
  cin >> n;
  cout << n-1 << endl;
}

#include <iostream>
using namespace std;
int main() {
  int n;
  while(cin>>n){
    cout<<n-1<<endl;
  }
}


#include<iostream>
#include<algorithm>
#include<ctime>
#include<vector>
#include<string>
#include<cmath>
#include<map>
#include<iomanip>
#include<numeric>
#include<queue>
#include<deque>
#include<cfloat>
#include<functional>
#include<tuple>
#include<math.h>
#include<bitset>
#include<stack>
#include<set>
#include<random>
#include<stdlib.h>
#define rip(i,n) for(int i=0;i<n;i++)
#define Rip(i,n) for(int i=1;i<=n;i++)
#define RIP(i,a,b) for(int i=a;i<b;i++)
#define all(V) V.begin(),V.end()
#define sec  setprecision;
#define _CRT_SECURE_NO_WARNINGS
#pragma target("avx")
#pragma optimize("O3")
#pragma optimize("unroll-loops");
constexpr double eps = 1e-9;
constexpr double pi = 3.141592653589793238462643383279;
using namespace std;
using ll = long long;
using ld = long double;
using Pa = pair<ll, ll>;
const ll MOD = 1000000007;
ll gcd(ll a, ll b)
{
	if (b == 0) return a;
	else return gcd(b, a % b);
}
ll lcm(ll a, ll b)
{
	return a * b / gcd(a, b);
}
ll pow(ll a, ll b) {
	ll ans = 1;
	if (b <= 0)return 1;
	else if (b % 2 == 0)return(pow(a * a, b / 2));
	else return (a * pow(a, b - 1));
}
ll modpow(ll x, ll n, ll mod) {
	x %= mod;
	if (n == 0)
		return 1;
	if (x == 1)return 1;
	if (n % 2 == 0)
		return (modpow((x * x) % mod, n / 2, mod) % mod);
	else
		return (x * modpow(x, n - 1, mod)) % mod;
}
struct UnionFind {
	vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2
	UnionFind(int N) : par(N) { //最初は全てが根であるとして初期化
		for (int i = 0; i < N; i++) par[i] = i;
	}

	int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
		if (par[x] == x) return x;
		return par[x] = root(par[x]);
	}

	void unite(int x, int y) { // xとyの木を併合
		int rx = root(x); //xの根をrx
		int ry = root(y); //yの根をry
		if (rx == ry) return; //xとyの根が同じ(=同じ木にある)時はそのまま
		par[rx] = ry; //xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
	}

	bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
		int rx = root(x);
		int ry = root(y);
		return rx == ry;
	}
};
void solveA() {
	int x, y, z; cin >> x >> y >> z;
	int num = 0;
	for (int i = 1000000; i >= 0; i--)if (y * z > x* i) {
		num = i; break;
	}
	cout << num << endl;

}
void solveB() {
	int n, m; cin >> n >> m;
	vector<int>a(1005, 0);
	vector<int>b(1005, 0);
	vector<int>ab(1005, 0);
	rip(i, n) {
		int d; cin >> d;
		a[d]++, ab[d]++;
	}
	rip(i, m) {
		int e; cin >> e;
		b[e]++, ab[e]++;
	}
	for (int i = 1; i <= 1000; i++) {
		if (ab[i] != 0 && (ab[i] == a[i] || ab[i] == b[i]))cout << i << endl;
	}
}
void solveC() {
	int a, b; cin >> a >> b;
	if (2 * a <= b)cout << b / 2 << endl;
	else {
		int c = b - a;
		for (int i = c; i >= 1; i--) {
			if (a % i == 0 || b % i == 0) {
				cout << i << endl;
				break;
			}
			else if (a/i + 1 <b/i - 1) {
				cout << i << endl;
				break;
			}
		}
	}
}
void solveD() {
	int N,P; cin >> N>>P;
	ll ans = modpow(P - 2, N-1, MOD)*(P-1);
	cout << ans % MOD << endl;
}
void solveE() {
}
void solveF() {
}
int main() {
	solveD();
}

#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define X first
#define Y second
#define nl '\n'
#define AC return 0
#define pb(a) push_back(a)
#define mst(a,b) memset(a, b, sizeof a)
#define rep(i,n) for(int i = 0; (i)<(n); i++)
#define rep1(i,n) for(int i = 1; (i)<=(n); i++)
#define scd(a) scanf("%lld", &a)
#define scdd(a,b) scanf("%lld%lld", &a, &b)
#define scs(s) scanf("%s", s)
//#pragma GCC optimize(2)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
const ll INF = (ll)0x3f3f3f3f3f3f3f, MAX = 9e18, MIN = -9e18;
const int N = 1e6+10, M = 2e6+10, mod = 1e9+7, inf = 0x3f3f3f;
ll a[N];

int main()
{
	IOS;
	string s, t;
	cin>>s>>t;
	if(s[0] == 'Y')
		rep(i, t.size())
			cout<<char(t[i] >= 'a'&&t[i] <= 'z' ? t[i]-32 : t[i]);
	else
		rep(i, t.size())
			cout<<t[i];
	AC;
}


#include "bits/stdc++.h"
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
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; }

struct State{
    int at;
    ll cost;
    State(int a, ll c) : at(a), cost(c){}
    bool operator>(const State& s) const{
        return cost > s.cost;
    }
};

struct Edge{
    int to;
    ll cost;
    Edge(int t, ll c) : to(t), cost(c){}
};

using Graph = vector<vector<Edge> >;
const ll INF = 1e15;

void dijkstra(int s, const Graph& G, vector<ll>& minc){
    minc.assign(G.size(), INF);
    priority_queue<State, vector<State>, greater<State> > pq;
    pq.emplace(s, 0);
    minc[s] = 0;
    while(!pq.empty()){
        State cur = pq.top();
        pq.pop();
        if(minc[cur.at] < cur.cost) continue;
        for(const Edge& e : G[cur.at]){
            ll cost = cur.cost + e.cost;
            if(minc[e.to] <= cost) continue;
            minc[e.to] = cost;
            pq.emplace(e.to, cost);
        }
    }
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n, m;
	cin >> n >> m;
	vector<int> a(m), b(m);
	Graph g1(n);
	rep(i,m){
		cin >> a[i] >> b[i];
		--a[i];
		--b[i];
		g1[a[i]].emplace_back(b[i], 1);
        g1[b[i]].emplace_back(a[i], 1);
	}
	int k;
	cin >> k;
	vector<int> c(k);
	vector<vector<ll>> minc(k);
	rep(i,k){
		cin >> c[i];
		--c[i];
		dijkstra(c[i], g1, minc[i]);
	}
	ll ans = INF;
	vector<vector<ll>> dp((1<<k), vector<ll>(k, INF));
	rep(i,k) dp[0][i] = 1;
	rep(S,(1<<k)){
		rep(i,k){
			rep(j,k){
				if(S>>j & 1) continue;
				int nextS = S + (1<<j);
				ll cost = dp[S][i] + minc[i][c[j]];
				chmin(dp[nextS][j], cost);
			}
		}
	}
	rep(j,k) chmin(ans, dp[(1<<k)-1][j]);
	if(ans == INF) ans = -1;
	cout << ans << endl;
	return 0;
}

#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int INF = (1u<<30) - 1;

struct BFS
{
	int V;
	const vector<vector<int>> &g;
	int s;
	vector<int> dist;
	vector<int> previous;

	BFS(const vector<vector<int>> &g) : V(g.size()), g(g) {}

	void calc(int start) {
		s = start;
		dist.assign(V, INF);
		previous.assign(V, -1);
		dist[s] = 0;
		queue<int> que;
		que.push(s);
		while (!que.empty()) {
			int v = que.front(); que.pop();
			for (int nv : g[v]) {
				if (dist[nv] != INF) continue;
				dist[nv] = dist[v] + 1;
				previous[nv] = v;
				que.push(nv);
			}
		}
	}
};

int main() {
	int n, m;
	cin >> n >> m;
	vector<vector<int>> g(n);
	for (int i = 0; i < m; i++) {
		int a, b;
		scanf("%d%d", &a, &b);
		a--; b--;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	int k;
	cin >> k;
	vector<int> C(k);
	for (int i = 0; i < k; i++) {
		cin >> C[i];
		C[i]--;
	}
	vector<vector<int>> dmatrix(k, vector<int>(k, INF));
	BFS bfs(g);
	for (int i = 0; i < k; i++) {
		bfs.calc(C[i]);
		for (int j = 0; j < k; j++) {
			dmatrix[i][j] = bfs.dist[C[j]];
		}
	}
	int p = 1 << k;
	vector<vector<int>> dp(p, vector<int>(k, INF));
	for (int v = 0; v < k; v++) dp[0][v] = 0;
	for (int i = 0; i < p; i++) {
		for (int v = 0; v < k; v++) {
			for (int u = 0; u < k; u++) {
				dp[i][v] = min(dp[i][v], dp[i & (~(1<<v))][u] + dmatrix[v][u]);
			}
		}
	}
	int ans = INF;
	for (int i = 0; i < k; i++) {
		ans = min(ans, dp[p-1][i]);
	}
	ans++;
	if (ans >= INF) ans = -1;
	cout << ans << endl;
	return 0;
}


#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(ri,n) for(int ri = (int)(n-1); ri >= 0; ri--)
#define rep2(i,x,n) for(int i = (int)(x); i < (int)(n); i++)
#define rrep2(ri,x,n) for(int ri = (int)(n-1); ri >= (int)(x); ri--)
#define repit(itr,x) for(auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr,x) for(auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(x) x.begin(), x.end()
using ll = long long;
using namespace std;

int main() {
  int n;
  cin >> n;
  if(n == 3) {
    cout << "6 10 15" << endl;
    return 0;
  }
  set<int> se;
  int now = 6;
  while(now <= 100000) {
    se.insert(now);
    now += 6;
  }
  now = 10;
  while(now <= 100000) {
    se.insert(now);
    now += 10;
  }
  now = 15;
  while(now <= 100000) {
    se.insert(now);
    now += 15;
  }
  auto itr = se.begin();
  bool f = false;
  vector<int> v;
  while(n--) {
    if(f) cout << " ";
    cout << *itr;
    itr++;
    f = true;
    v.push_back(*itr);
  }
  cout << endl;
  // rep(i, v.size()) {
  //   rep2(j, i+1, v.size()) {
  //     int g = gcd(v.at(i), v.at(j));
  //     if(g == 1) {
  //       cout << "!!!!" << v.at(i) << " " << v.at(j) << endl;
  //       return 1;
  //     }
  //   }
  // }
  // int g = v.front();
  // rep2(i, 1, v.size()) g = gcd(g, v.at(i));
  // cerr << g << endl;
  return 0;
}

#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <sstream>
#include <algorithm>
#include <deque>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <list>
#include <cstdio>
#include <iostream>
#include <cmath>
#include <climits>
#include <bitset>
#include <functional>
#include <numeric>
#include <ctime>
#include <cassert>
#include <cstring>
#include <fstream>

using namespace std;

int main() {
    int n;
    cin >> n;
    int m = 1;
    for (int i = 0; i < n; i++) {
        m *= 2;
    }
    vector<int> a(m);
    for (int i = 0; i < m; i++) {
        cin >> a[i];
    }
    int x = 0, y = m/2;
    for (int i = 0; i*2 < m; i++) {
        if (a[i] > a[x]) x = i;
        if (a[i + m / 2] > a[y]) y = i + m / 2;
    }
    if (a[x] > a[y])
        cout << y+1 << endl;
    else
        cout << x+1 << endl;

    return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>

int main(void){
    int n, t;
    std::cin >> n >> t;
    std::vector<long long> vec1, vec2;
    vec1 = vec2 = {0};
    for(int i = 0; i < n; i++){
        int a;
        std::cin >> a;
        for(int j = vec1.size()-1; j >= 0; --j){
            vec1.push_back(a+vec1[j]);
        }
        std::swap(vec1, vec2);
    }
    std::sort(vec1.begin(), vec1.end());
    long long ans = 0;
    for(int i = 0; i < vec2.size(); i++){
        if(vec2[i] > t) continue;
        int vec1_i = std::distance(vec1.begin(), std::upper_bound(vec1.begin(), vec1.end(), t - vec2[i]));
        ans = std::max(ans, vec1[vec1_i-1]+vec2[i]);
    }

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

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <bitset>
#include <vector>
#include <cstdio>
#include <string>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define PB push_back
#define MP make_pair
#define SZ(v) ((int)(v).size())
#define abs(x) ((x) > 0 ? (x) : -(x))
#define FOREACH(e,x) for(__typeof(x.begin()) e=x.begin();e!=x.end();++e)
typedef long long LL;

const int MAXN = 45;
LL n, T;
LL a[MAXN];

set<LL> get_sum(vector<int> &num) {
    set<LL> ret;
    for (int i = 0; i < (1 << num.size()); i++) {
        LL sum = 0;
        for (int j = 0; j < num.size(); j++) {
            if (i & (1 << j)) {
                sum += num[j];
            }
        }
        ret.insert(sum);
    }
    return ret;
}

void solve() {
    cin >> n >> T;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    vector<int> A, B;
    for (int i = 0; i < n / 2; i++) {
        A.push_back(a[i]);
    }
    for (int i = n / 2; i < n; i++) {
        B.push_back(a[i]);
    }
    set<LL> X = get_sum(A), Y = get_sum(B);

    LL res = 0;
    for (auto x : X) {
        auto it = Y.upper_bound(T - x);
        if (it == Y.begin()) continue;
        it--;
        res = max(res, x + *it);
    }
    cout << res << endl;
}

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


#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (ll i = 0; i < (n); ++i)
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
using ll = long long;

ll countFull(ll n) {
    string n_str = to_string(n);
    ll dig_n_2 = n_str.length() - 2;
    if (dig_n_2 == 0) return 0;

    //ll dig_n_2_m1 = dig_n_2 - 1;
    ll dig_n_2_m1 = (dig_n_2 - 1)/2;
    ll ret = 0;
    ll ten = 1;
    //rep(i, dig_n_2_m1 - 1) ten *= 10;
    rep(i, dig_n_2_m1 ) ten *= 10;
    ret = 9 * ten;
    return ret + countFull(n / 100);
}
int main() {
    ll n;
    cin >> n;

    string ns =to_string(n);
    if (ns.size() % 2 != 0){
        n=9;
        if (ns.size() != 1) rep(i, ns.size() - 2) {
                n *= 10;
                n += 9;
            }
        else {
            cout << 0 <<endl;
            return 0;
        }
    }

    ll cnt_fra;
    string n_str = to_string(n);
    string bef, aft;
    rep(i, n_str.length()) {
        if (i < n_str.length() / 2) bef.push_back(n_str[i]);
        else aft.push_back(n_str[i]);
    }
    ll be = stoll(bef);
    ll af = stoll(aft);

    ll ten = 1;
    rep(i, (n_str.length() / 2) - 1) ten *= 10;
    cnt_fra = min(be, af) - (ten - 1);
    if (to_string(min(be, af)).length() < n_str.length() / 2) {
        cnt_fra = 0;
    }
    if (be > af) {
        ten =1;
        rep(i,bef.size()-1) ten *= 10;
        cnt_fra = be - 1 - (ten-1) ;
    }

    cout << cnt_fra + countFull(n) << endl;

}

/*** author: yuji9511 ***/
#include <bits/stdc++.h>
// #include <atcoder/all>
// using namespace atcoder;
using namespace std;
using ll = long long;
using lpair = pair<ll, ll>;
using vll = vector<ll>;
const ll MOD = 1e9+7;
const ll INF = 1e18;
#define rep(i,m,n) for(ll i=(m);i<(n);i++)
#define rrep(i,m,n) for(ll i=(m);i>=(n);i--)
ostream& operator<<(ostream& os, lpair& h){ os << "(" << h.first << ", " << h.second << ")"; return os;}
#define printa(x,n) for(ll i=0;i<n;i++){cout<<(x[i])<<" \n"[i==n-1];};
void print() {}
template <class H,class... T>
void print(H&& h, T&&... t){cout<<h<<" \n"[sizeof...(t)==0];print(forward<T>(t)...);}
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

void solve(){
    ll a[3];
    rep(i,0,3) cin >> a[i];
    sort(a, a+3);
    if(a[1] - a[0] == a[2] - a[1]){
        print("Yes");
    }else{
        print("No");
    }

}

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


#include<iostream>
#include<string>
using namespace std;
int main(){
  string n;
  cin >> n;
  int a;
  a=n.length();
  a--;
  int i=a,j;
  while(i>=0 && n[i]=='0'){
    if(n[i]=='0') a--;
    i--;
  }
  j=a;
  int k=0,flag=0;
  while(k<=j){
    if(n[k]!=n[j]) flag=1;
    k++;
    j--;
  }
  if(flag==0) cout << "Yes";
  else cout << "No";
  return 0;
}

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

#define endl '\n'
#define int long long

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

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

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

	int x; cin >> x;
	int ans = 1;
	for(int i = 1; i <= x; i ++) {
		ans = lcm(i, ans);
	}
	ans ++;
	cout << ans << endl;

	return 0;
}


// ahc002_a
#pragma GCC optimize ("O3")

#include <bits/stdc++.h>
#ifdef LOCAL
#include "../../debug_util/cxx-prettyprint/prettyprint.hpp"
#include "../../debug_util/rng.hpp"
#endif
using namespace std;

using ll = long long;
using ull = unsigned long long;
using P = pair<int, int>;

#define REP(i, n) for (int i = 0 ; i < (int)(n) ; ++i)
#define REPN(i, m, n) for (int i = m ; i < (int)(n) ; ++i)
#define REP_REV(i, n) for (int i = (int)(n) - 1 ; i >= 0 ; --i)
#define REPN_REV(i, m, n) for (int i = (int)(n) - 1 ; i >= m ; --i)
#define ALL(x) x.begin(), x.end()

#define INF (ll)(1e15)
#define MOD (1000000007)

#define print2D(h, w, arr) REP(i, h) { REP(j, w) cout << arr[i][j] << " "; cout << endl; }
#define print_line(vec, n) {for(int idx=0;idx<(n-1);idx++) cout << (vec)[idx] << " "; cout << (vec)[(n)-1] << endl;}
template<class T> void print(const T& x){cout << x << "\n";}
template<class T, class... A> void print(const T& first, const A&... rest) { cout << first << " "; print(rest...); }
//struct PreMain {PreMain(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(20);}} premain;

#include <chrono>
struct Timer {
    std::chrono::time_point<std::chrono::system_clock> begin;

    void init() {
        begin = std::chrono::system_clock::now();
    }

    int get_ms() {
        auto cur = std::chrono::system_clock::now();
        return std::chrono::duration_cast<std::chrono::milliseconds>(cur - begin).count();
    }
};

struct Grid {

    const int H, W;
    const vector<vector<int>> S, T;
    Timer timer;

    const int di[8]={ 1, 0, -1, 0, -1, 1, -1, 1};
    const int dj[8]={ 0, 1, 0, -1, -1, -1, 1, 1};
    bool bound_check(int i, int j) const {return (0 <= i) && (i < H) && (0 <= j) && (j < W);}

    const char order[4] = {'D', 'R', 'U', 'L'};

    [[nodiscard]] int hash_func(int i, int j) const {
        return i * W + j;
    }

    [[nodiscard]] tuple<int, int> rev_hash(int h) const{
        int i = h / W;
        int j = h % W;
        return tie(i, j);
    }

    Grid(int H, int W, vector<vector<int>>& S, vector<vector<int>>& T): H(H), W(W), S(S), T(T) {}

    vector<bool> used;
    int score = 0;
    int max_score = 0;

    string sequence;
    string ans;

    void dfs(int i, int j){

        used[S[i][j]] = true;
        score += T[i][j];

        if (score > max_score){
            max_score = score;
            ans = sequence;
        }

        if (timer.get_ms() > 1900){
            throw 1;
        }

        REP(k, 4){
            int ni = i + di[k];
            int nj = j + dj[k];
            if (not bound_check(ni, nj)) continue;
            if (used[S[ni][nj]]) continue;

            sequence += order[k];
            dfs(ni, nj);
            sequence.pop_back();
        }

        score -= T[i][j];
        used[S[i][j]] = false;

    }

    string dfs_wrap(int si, int sj){

        // init
        int mx = 0;
        REP(i, H) REP(j, W) mx = max(mx, S[i][j]);
        used.assign(mx+1, false);

        // exec
        timer.init();
        try{
            dfs(si, sj);
        } catch(int& n){
            ;
        }
        return ans;
    }

};

int main() {
#ifdef LOCAL
    ifstream in("../arg.txt"); cin.rdbuf(in.rdbuf());
#endif

    int si, sj;
    cin >> si >> sj;

    vector<vector<int>> S(50, vector<int>(50)), T(50, vector<int>(50));
    REP(i, 50) REP(j, 50) cin >> S[i][j];
    REP(i, 50) REP(j, 50) cin >> T[i][j];

    Grid g(50, 50, S, T);

    string ans = g.dfs_wrap(si, sj);
    print(ans);

#ifdef LOCAL
    print(g.max_score);
#endif

    return 0;
}

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

#define MOD 1000000007
#define SWAP(a,b) ((a != b)? (a += b,b = a - b,a -= b) : 0 ) //数値のみ
#define SWAP(type,a,b) { type temp = a; a = b; b = temp; } //ポインタ, 構造体, 文字列
#define rng(i, a, b) for(int i = int(a); i < int(b); i++)
#define rep(i, b) rng(i, 0, b)
#define pb push_back
#define eb emplace_back
#define bg begin()
#define ed end()
#define all(x) x.bg,x.ed

ll jo(ll a, ll n){
  ll tmp = 1;
  rep(i, n) {
    tmp *= a;
    if(tmp <= 0) return -1;
  }
  return tmp;
}

ll jom(ll a, ll n){
  ll tmp = 1;
  rep(i, n) {
    tmp *= a;
    tmp %= MOD;
  }
  return tmp;
}

unsigned digit(unsigned num){
    return std::to_string(num).length();
}

void yn(bool Yes){
  cout << ((Yes)? "Yes" : "No") << endl;
}

void ny(bool No){
  cout << ((No)? "No" : "Yes") << endl;
}

void YN(bool YES){
  cout << ((YES)? "YES" : "NO") << endl;
}

void NY(bool NO){
  cout << ((NO)? "NO" : "YES") << endl;
}

void Yay(bool y){
  cout << ((y)? "Yay!" : ":(") << endl;
}

string move(int &si, int &sj, vector<vector<int>> &t, vector<vector<int>> &p){
  int mi = -1, mj = -1, i = 0;
  if(si < 25) mi = 1;
  if(sj < 25) mj = 1;
  string ans = "";
  while(si+mi >= 0 && si+mi < 50 && sj+mj >= 0 && sj+mj < 50 && i != 50){
    i++;
    t[si][sj] += 10000;
    if(t[si+mi][sj] % 10000 != t[si][sj] % 10000 && t[si+mi][sj] <= 10000){
      if(mi == 1) ans += 'D';
      else ans += 'U';
      si += mi;
    }
    else if(t[si][sj+mj] % 10000 != t[si][sj] % 10000 && t[si][sj+mj] <= 10000){
      if(mj == 1) ans += 'R';
      else ans += 'L';
      sj += mj;
    }
    else {
      break;
      cout << si << " " << sj << endl;
    }
  }
  return ans;
}

int main() {
  int n, si, sj, b, ans = 0, tmp = 0;
  string s = "", s2 = "";
  vector<vector<int>> t(50, vector<int>(50, 0)), p(50, vector<int>(50, 0));
  cin >> si >> sj;
  rep(i, 50) rep(j, 50) cin >> t[i][j];
  rep(i, 50) rep(j, 50) cin >> p[i][j];
  s = move(si, sj, t, p);
  //s2 = move(si, sj, t, p);
  cout << s << endl;
  //printf("%.10lf\n", ans);
}


#pragma warning(disable : 4996)
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;
typedef tuple<ll,ll,ll> tl3;
#define FOR(a, b, c) for (int(a) = (b); (a) < (c); ++(a))
#define FORN(a, b, c) for (int(a) = (b); (a) <= (c); ++(a))
#define rep(i, n) FOR(i, 0, n)
#define repn(i, n) FORN(i, 1, n)
#define tc(t) while (t--)
// https://atcoder.jp/contests/hhkb2020/tasks/hhkb2020_c
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    ll n;
    cin >> n;
    vll v(n);
    map<ll,ll> isUsed;
    ll min = 0;
    rep(i,n){
        cin >> v[i];
        isUsed[v[i]]++;
        for(;;min++){
            if(isUsed[min] == 0){
                break;
            }
        }
        cout << min << "\n";
    }

    return 0;
}

#include <stdio.h>
#define MOD 3

int fact[400010],count[400010];
char x[400010];
int comb(int a, int b)
{
	if(count[a]>count[b]+count[a-b]) return 0;
	else return (fact[a]*fact[b]*fact[a-b])%MOD;
}
int main()
{
	fact[0] = 1;
	count[0] = 0;
	for(int i=1;i<=400000;i++)
	{
		int c = i;
		count[i] = count[i-1];
		while(c%MOD==0)
		{
			c/=MOD;
			count[i]++;
		}
		fact[i] = (fact[i-1]*c)%MOD;
	}

	int a;
	scanf("%d",&a);
	scanf("%s",x+1);

	int sum = 0;
	for(int i=1;i<=a;i++)
	{
		int c;
		if(x[i]=='W') c = 0;
		if(x[i]=='B') c = 1;
		if(x[i]=='R') c = 2;
		sum += comb(a-1,i-1)*c;
		sum %= MOD;
	}
	if(a%2==1)
	{
		if(sum==0) printf("W");
		if(sum==1) printf("B");
		if(sum==2) printf("R");
	}
	else
	{
		if(sum==0) printf("W");
		if(sum==1) printf("R");
		if(sum==2) printf("B");
	}
}


#include <bits/stdc++.h>
#define ALL(A) (A).begin(), (A).end()
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)

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

int dx[] = { 0, 1, -1, 0, 1, -1, 1, -1 };  // i<4:4way i<8:8way
int dy[] = { 1, 0, 0, -1, 1, -1, -1, 1 };

const ll mod = 998244353;
const ll INF = -1 * ((1LL << 63) + 1);
const int inf = -1 * ((1 << 31) + 1);
struct UnionFind {
  vector< int > data;

  UnionFind(int sz) {
    data.assign(sz, -1);
  }

  bool unite(int x, int y) {
    x = find(x), y = find(y);
    if(x == y) return (false);
    if(data[x] > data[y]) swap(x, y);
    data[x] += data[y];
    data[y] = x;
    return (true);
  }

  int find(int k) {
    if(data[k] < 0) return (k);
    return (data[k] = find(data[k]));
  }

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

  int size(int k) {
    return (-data[find(k)]);
  }
};

ll f(ll x){
    ll ret = 1;
    for(int i=0;i<x;i++){
        ret *= i+1;
        ret %= mod;
    }
    return ret % mod;
}

int main(void){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    int n,m;
    cin >> n >> m;
    vector<vector<int>> a(n,vector<int>(n));
    rep(i,n)rep(j,n)cin >> a[i][j];
    ll ans = 1;
    // まずは行についてスワップできる組み合わせを見つけていく
    UnionFind unitate(n),uniyoko(n);
    set<int> tate,yoko;
    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            bool ok = true;
            for(int k=0;k<n;k++){
                if(a[i][k] + a[j][k] > m)ok = false;
            }
            if(ok){
                unitate.unite(i,j);
            }
        }
    }
    for(int i=0;i<n;i++){
        int size = unitate.size(i);
        int now = unitate.find(i);
        if(tate.count(now))continue;
        else{
            tate.insert(now);
            ans *= f(size);
            ans %= mod;
        }
    }

    // 次は列についてスワップできる組み合わせを見つける
    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            bool ok = true;
            for(int k=0;k<n;k++){
                if(a[k][i] + a[k][j] > m)ok = false;
            }
            if(ok){
                uniyoko.unite(i,j);
            }
        }
    }
    for(int i=0;i<n;i++){
        int size = uniyoko.size(i);
        int now = uniyoko.find(i);
        if(yoko.count(now))continue;
        else{
            yoko.insert(now);
            ans *= f(size);
            ans %= mod;
        }
    }
    cout << ans << endl;
}

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
const int M=1e6+5;
const int N=1e3+5;
#define int long long
int Max(int x,int y){return x>y?x:y;}
int Min(int x,int y){return x>y?y:x;}
int Abs(int x){return x>0?x:-x;}
int read(){
	int k=1,num=0;
	char s=getchar();
	while(s>'9'||s<'0'){
		if(s=='-') k=-1;
		s=getchar();
	}
	while(s>='0'&&s<='9'){
		num=(num<<1)+(num<<3)+(s-'0');
		s=getchar();
	}
	return num*k;
}
int cnt[M];
signed main(){
	int L=read(),R=read();
	for(int i=2;i<=R;i++){
		if(cnt[i]==0){
			for(int j=i;j<=R;j+=i) cnt[j]++;
			for(int j=i*i;j<=R;j+=i*i) cnt[j]=-(1ll<<60);
		}
	}
	int ans=0;
	for(int k=2;k<=R;k++){
		if(cnt[k]<0) continue;
		int val=R/k-(L-1)/k;
		val=val*(val-1)/2;
		if(cnt[k]%2) ans+=val;
		else ans-=val;
	}
	for(int i=Max(2ll,L);i<=R;i++){
		ans-=R/i-1;
	}
	printf("%lld",ans*2);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fast_io ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define all(v) v.begin(),v.end()

const int mod = 1e9 + 7;

int main (){
    fast_io;
    int x, y, z;
    cin >> x >> y >> z;
    int ans = (y * z)/ x + ((y * z) % x > 0);
    cout << ans - 1 << endl;
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int a,b,c;
    cin>>a>>b>>c;
    cout<<a+b+c-min(a,min(b,c));

	return 0;
}


#include<bits/stdc++.h>
#define LL long long
#define pb push_back
#define SZ(x) ((int)x.size()-1)
#define ms(a,b) memset(a,b,sizeof a)
#define F(i,a,b) for (int i=(a);i<=(b);++i)
#define DF(i,a,b) for (int i=(a);i>=(b);--i)
using namespace std;
inline int read(){
	char ch=getchar(); int w=1,c=0;
	for(;!isdigit(ch);ch=getchar()) if (ch=='-') w=-1;
	for(;isdigit(ch);ch=getchar()) c=(c<<1)+(c<<3)+(ch^48);
	return w*c;
}
int main(){
	int n=read();
	cout<<(1<<n)-1<<"\n";
	F(i,1,(1<<n)-1){
		F(j,0,(1<<n)-1){
			cout<<((__builtin_popcount(i&j)&1)&1 ? 'A' : 'B');
		}
		cout<<"\n";
	}
	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>

#define div /

/*
ID: farukde1
TASK: beads
LANG: C++
*/

using namespace std;

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

    /*
    freopen("input.txt","r",stdin);
    freopen("beads.out","w",stdout);
    */

    string s;
    cin >> s;
    int ans=0;
    for(int i=0; i<=9999; i++){
        bool flag[10]={false};
        int x=i;
        for(int j=0; j<4; j++){
            flag[x%10] = true;
            x=x div 10;
        }
        bool flag2 = true;
        for(int j=0; j<10; j++){
            if(s[j]=='o' && !flag[j]) flag2 = false;
            if(s[j]=='x' && flag[j]) flag2 = false;
        }
        ans+=flag2;
    }
    cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
int n,ans=1e18;
int a[100001];
signed main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	for(int i=0;i<1<<(n-1);i++)
	{
		int x=0,o=0;
		for(int j=1;j<=n;j++)
		{
			o|=a[j];
			if(1<<(j-1)&(i))x^=o,o=0;
		}
		x^=o;
		ans=min(ans,x);
	}
	cout<<ans;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MOD = 998244353;
int N, a[200010], vis[200010];
int in[200010];
int main()
{
	scanf("%d",&N);
	for (int i=1; i<=N; i++)
	{
		scanf("%d",&a[i]);
		in[a[i]]++;
	}
	queue<int>q;
	for (int i=1; i<=N; i++)
		if (!in[i])
			q.push(i);
	while(!q.empty())
	{
		int now = q.front();
		q.pop();
		vis[now] = true;
		if (--in[a[now]] == 0)
			q.push(a[now]);
	}
	int cnt = 0;
	for (int i=1; i<=N; i++)
	{
		if (!vis[i])
		{
			cnt++;
			for (int j=i; !vis[j]; j=a[j])
			{
				vis[j] = true;
			}
		}
	}

	LL ans = 1;
	for (int i=1; i<=cnt; i++)
		ans = ans * 2LL % MOD;
	ans = (ans + MOD - 1) % MOD;
	printf("%lld\n",ans);
}


#include <iostream>
using namespace std;
int a, b, c, d;
int main() {
  cin >> a >> b >> c >> d;
  cout << b - c;
  return 0;
}

// coded by- mrNOBODY_

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ld long double
#define in(x) cin>>x;
#define print(x) cout<<x;
#define println(x) cout<<x<<"\n";
#define vec(x) vector<x>
#define endl "\n"
#define all(x) x.begin(),x.end()
#define inf 1e15
#define sqr(x) ((x) * (x))
#define fr(i,a,b) for(ll i=a;i<b;i++)
void OJ();
ll const M=1e9 +7;

using namespace std;

/*ll binpow(ll a,ll n){
    ll res=1;
    while (n>0)
    {
        if(n&1)
            res =res*a;
        a=sqr(a);n>>=1;
    }
    return res;
}

ll binpow(ll a,ll n,ll m){
    ll res=1;
    while (n>0)
    {
        if(n&1)
            res=((res%m)*(a%m))%m;
        a=(a*a)%m;n>>=1;
    }
    return res;
}

bool comp(const pair<int,int> &a , const pair<int,int> &b){
     return a.first < b.second;
}*/
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin.sync();

    ll a,b,c,d;
    in(a>>b>>c>>d)
    print(max(a,b)-min(c,d))
    return 0;
}

void OJ()
{
#ifndef ONLINE_JUDGE
    freopen("inputf.in", "r", stdin);
    freopen("outputf1.in", "w", stdout);
#endif
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
// --------------------------------------------------------
template<class T> bool chmax(T& a, const T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> bool chmin(T& a, const T b) { if (b < a) { a = b; return 1; } return 0; }
#define FOR(i,l,r) for (ll i = (l); i < (r); ++i)
#define RFOR(i,l,r) for (ll i = (r)-1; (l) <= i; --i)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define MIN(c) *min_element(ALL(c))
#define MAX(c) *max_element(ALL(c))
#define SUMLL(c) accumulate(ALL(c), 0LL)
#define COUNT(c,v) count(ALL(c),(v))
#define SZ(c) ((ll)(c).size())
#define BIT(b,i) (((b)>>(i)) & 1)
#define PCNT(b) __builtin_popcountll(b)
#define CIN(c) cin >> (c)
#define COUT(c) cout << (c) << '\n'
#define debug(x) cerr << "l." << __LINE__ << " : " << #x << " = " << (x) << '\n'
ll llceil(ll a, ll b) { return (a + b - 1) / b; }
ll bitlen(ll b) { if (b <= 0) { return 0; } return (64LL - __builtin_clzll(b)); }
string toupper(const string& S) { string T(S); REP(i,SZ(T)) T[i] = toupper(T[i]); return T; }
string tolower(const string& S) { string T(S); REP(i,SZ(T)) T[i] = tolower(T[i]); return T; }
using P = pair<ll,ll>;
using VP = vector<P>;
using VVP = vector<VP>;
using VS = vector<string>;
using VVS = vector<VS>;
using VLL = vector<ll>;
using VVLL = vector<VLL>;
using VVVLL = vector<VVLL>;
using VB = vector<bool>;
using VVB = vector<VB>;
using VVVB = vector<VVB>;
using VD = vector<double>;
using VVD = vector<VD>;
using VVVD = vector<VVD>;
using VLD = vector<ld>;
using VVLD = vector<VLD>;
using VVVLD = vector<VVLD>;
static const double EPS = 1e-10;
static const double PI  = acos(-1.0);
static const ll MOD = 1000000007;
// static const ll MOD = 998244353;
static const ll INF = (1LL << 62) - 1;  // 4611686018427387904 - 1
// --------------------------------------------------------
// #include <atcoder/all>
// using namespace atcoder;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    ll N; cin >> N;
    VVLL C(N,VLL(N)); REP(i,N) REP(j,N) cin >> C[i][j];

    VLL A(N),B(N);
    A[0] = 0;
    REP(i,N) B[i] = C[0][i];
    FOR(i,1,N) A[i] = C[i][0] - B[0];

    FOR(i,1,N) FOR(j,1,N) {
        if (A[i] + B[j] != C[i][j]) {
            COUT("No"); return 0;
        }
    }

    ll min_a = MIN(A);
    ll min_b = MIN(B);
    if (min_a + min_b < 0) {
        COUT("No"); return 0;
    } else if (min_a < 0) {
        REP(i,N) A[i] += (-min_a);
        REP(i,N) B[i] += min_a;
    } else if (min_b < 0) {
        REP(i,N) B[i] += (-min_b);
        REP(i,N) A[i] += min_b;
    }

    COUT("Yes");
    REP(i,N) {
        if (i) cout << " ";
        cout << A[i];
    }
    cout << endl;
    REP(i,N) {
        if (i) cout << " ";
        cout << B[i];
    }
    cout << endl;

    return 0;
}

#pragma GCC optimize ("Ofast")
#pragma GCC optimization ("unroll-loops, no-stack-protector")
#pragma GCC target ("avx")
#pragma GCC target ("avx2")
#pragma GCC target ("fma")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair <int, int>
#define pll pair <ll, ll>
#define pci pair <char, int>
#define pld pair <ld, ld>
#define ppld pair <pld, pld>
#define ppll pair <pll, pll>
#define pldl pair <ld, ll>
#define vll vector <ll>
#define vvll vector <vll>
#define vpll vector <pll>
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define mll map <ll, ll>
#define fastmap gp_hash_table
#define cd complex <double>
#define vcd vector <cd>
#define PI 3.14159265358979
#define ordered_set tree <ll, null_type, less <ll>, rb_tree_tag, tree_order_statistics_node_update>
#pragma 03
using namespace std;
using namespace __gnu_pbds;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll a[505], b[505];
ll c[505][505];
int main(){
	fastio;
	ll n; cin >> n;
	for (ll i = 1; i <= n; i++){
		for (ll j = 1; j <= n; j++) cin >> c[i][j];
	}
	for (ll i = 1; i <= n; i++){
		a[i] = 1e9;
		for (ll j = 1; j <= n; j++) a[i] = min(a[i], c[i][j]);
	}
	for (ll j = 1; j <= n; j++) b[j] = c[1][j] - a[1];
	for (ll i = 2; i <= n; i++){
		for (ll j = 1; j <= n; j++){
			if (c[i][j] != a[i] + b[j]){
				cout << "No\n"; return 0;
			}
		}
	}
	cout << "Yes\n";
	for (ll i = 1; i <= n; i++) cout << a[i] << " ";
	cout << "\n";
	for (ll i = 1; i <= n; i++) cout << b[i] << " ";
	cout << "\n";
}


#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
#define rep(i,n) for(ll i=0; i<n; i++)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define rrep(i,n) for(ll i=n-1; i>=0; i--)
#define fi first
#define se second
long long mo = 1000000007;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;
typedef pair<ll,Pll> PlP;
template<class T, class S> void cmin(T &a, const S &b) { if (a > b)a = b; }
template<class T, class S> void cmax(T &a, const S &b) { if (a < b)a = b; }
template<class A>void PR(A a,ll n){rep(i,n){if(i)cout<<' ';cout<<a[i];}cout << "\n";}
string zero_padding(int val, int nf){ ostringstream sout;sout << setfill('0') << setw(nf) << val; return sout.str();};
ld PI=3.14159265358979323846;
//using namespace atcoder;
vector<ll> pickdiv(ll n){
    vector<ll> res;
    for(ll i=1; i*i <=n;i++){
        if(n%i)continue;
        res.emplace_back(i);
        ll q = n/i;
        if(q != i){
            res.emplace_back(q);
        }
    }
    return res;
}

int main(){
    ll S,P;
    cin >> S >> P;
    auto v = pickdiv(P);
    bool f = 0;
    for(auto& e:v){
        ll q = P/e;
        if(q+e == S){
            f = 1;
        }
    }
    cout << (f?"Yes":"No") << endl;
}

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

#define eb emplace_back
#define pb push_back
#define INF LLONG_MAX
#define FLT_INF pow(10.0, 16)

using ll = long long;
using vll = vector<ll>;
using ull = unsigned long long;
using int2 = pair<ll, ll>;
using int3 = tuple<ll, ll, ll>;

template <typename A>
string to_string(const A &v);
string to_string(const string &s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(char c) { return string(1, c); }
string to_string(bool b) { return (b ? "true" : "false"); }

template <typename A, typename B>
string to_string(const pair<A, B> &p)
{
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(const tuple<A, B, C> &p)
{
  return "(" + to_string(std::get<0>(p)) + ", " + to_string(std::get<1>(p)) + ", " + to_string(std::get<2>(p)) + ")";
}

template <typename A>
string to_string(const A &v)
{
  bool first = true;
  string res = "{";
  for (const auto &x : v)
  {
    if (!first)
    {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

template <typename T>
void sort(vector<T> &val)
{
  sort(val.begin(), val.end());
}

template <typename T>
void reverse(vector<T> &val)
{
  reverse(val.begin(), val.end());
}

template <typename T>
void sort_b(vector<T> &val)
{
  sort(val.begin(), val.end(), greater<T>());
}

template <typename T>
T pair_sum(pair<T, T> &val)
{
  return val.first + val.second;
}

template <typename T>
T min(T a, T b, T c)
{
  return min(min(a, b), c);
}

template <typename T>
T max(T a, T b, T c)
{
  return max(max(a, b), c);
}

template <typename T>
T mul_mod(T a, T b, T mod)
{
  T res = 0;
  a %= mod;
  while (b)
  {
    if (b & 1)
    {
      res = (res + a) % mod;
    }
    a = (2 * a) % mod;
    b >>= 1;
  }
  return res;
}

template <typename T>
T fac_mod(T v, T mod)
{
  ll res = 1;
  for (ll i = 1; i <= v; i++)
  {
    res *= i;
    res = res % mod;
  }
  return res;
}

template <typename T>
T pow_mod(T v, T p, T mod)
{
  if (p == 1)
    return v;

  if (p % 2 == 0)
  {
    T t = pow_mod(v, p / 2, mod);
    return (t * t) % mod;
  }
  else
  {
    return (v * pow_mod(v, p - 1, mod)) % mod;
  }
}
void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T)
{
  cerr << " " << to_string(H);
  debug_out(T...);
}

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

#define COMMON_MOD static_cast<ll>(pow(10, 9) + 7)
#define PI 3.14159265

// sort(a.begin(), a.end(), less<ll>()); greater<ll>()
// cout<<fixed<<setprecision(20)<<(s/kl)*2;
/* printf("%lld %lld \n", cur_n, m / 2 + 1);
sort(a.begin(), a.end(), [ ](ll& lhs, ll& rhs) {return abs(lhs) < abs(rhs);});
*/

class mytask
{
public:
  mytask() {}

  int solve()
  {
    ll n;
    cin >> n;
    string s;
    cin >> s;
    n = s.size();
    vll stack;
    ll res = n;
    for (ll i = 0; i < n; i++)
    {
      if (s[i] == 'f')
        stack.pb(1);
      else if (s[i] == 'o' && stack.size() && stack.back() == 1)
      {
        stack[stack.size() - 1] = 2;
      }
      else if (s[i] == 'x' && stack.size() && stack.back() == 2)
      {
        stack.pop_back();
        res -= 3;
      }
      else
      {
        stack.clear();
      }
    }

    cout << res;

    return 0;
  }
};

int main()
{

  auto task = mytask();
  task.solve();

  return 0;
}


#include <bits/stdc++.h>

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)
#define unless(p) if(!(p))
#define until(p) while(!(p))

#ifdef LOCAL
#define log(val) std::cerr << std::setw(3) << __LINE__ << "| " << #val << " = " << (val) << std::endl
#define dbg(proc) proc
#else
#define log(val) 0
#define dbg(proc) 0
#endif

template <typename T>
std::ostream& operator<<(std::ostream& os,const std::vector<T>& v){os << "{";if(!v.empty()){os << v[0];for(std::size_t i=1;i<v.size();++i){os << ", " << v[i];}}return os << "}";}

using ll = std::int64_t;
using P = std::tuple<int,int>;

int N, M;
P YX[200100];
std::set<int> se;

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

    std::cin >> N >> M;

    for(int i=0;i<M;++i){
        int X, Y;
        std::cin >> X >> Y;

        YX[i] = std::make_tuple(X, Y);
    }

    std::sort(YX, YX + M);

    se.emplace(N);

    for(int i=0,j=0;i<M;){
        while(j < M && fst(YX[j]) == fst(YX[i])){
            j += 1;
        }

        std::set<int> ok, ng;

        while(i < j){
            auto [Y, X] = YX[i];

            if(se.count(X) > 0){
                ng.emplace(X);
            }

            if(se.count(X - 1) > 0 || se.count(X + 1) > 0){
                ok.emplace(X);
            }

            i += 1;
        }

        for(int x : ng){
            auto it = se.find(x);

            if(it != se.end()){
                se.erase(it);
            }
        }

        for(int x : ok){
            se.emplace(x);
        }
    }

    int res = se.size();
    std::cout << res << std::endl;
}

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

#define fr(i,n) for(int i = 0; i<n; i++)
#define sz(v) (int)(v.size())
#define prin(a) cout << #a << " = " << a << endl
#define prinv(v) cout << #v << " = "; for(auto it : v) cout << it << ", "; cout << endl
#define all(v) (v).begin(),(v).end()

typedef long long ll;

#define rmin(a,b) a = min<ll>(a,b)
#define rmax(a,b) a = max<ll>(a,b)

#define fi first
#define se second

const int N = 4e5+10;

int main(){
	ios::sync_with_stdio(0); cin.tie(0);
	int npos, npawns; cin >> npos >> npawns;
	map<int,vector<int>> mp;
	fr(i,npawns){
		int x, y; cin >> x >> y;
		mp[x].push_back(y);
	}
	set<int> at;
	at.insert(npos);
	for(auto &[x,v] : mp){
		vector<int> por, tirar;
		for(auto &i : v){
			tirar.push_back(i);
			if(at.count(i-1) or at.count(i+1)) por.push_back(i);
		}
		for(auto &i : tirar) at.erase(i);
		for(auto &i : por) at.insert(i);
	}
	cout << sz(at) << "\n";
}


//#include <boost/multiprecision/cpp_int.hpp>
#include "bits/stdc++.h"
//using namespace boost::multiprecision;
using namespace std;
#define MODULO 1000000007
#define PI 3.14159265359
typedef long long int ll;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin>>n;
     cout<<"6 10 15 ";
    int x=n-3;
    int xd=0;
    if(x==0)
    return 0;
    for(int i=16;i<=10000;i++)
    {
      if(i%6==0 || i%10==0 || i%15==0)
      {
          cout<<i<<" ";
          xd++;
      }
      if(xd==x)
      break;
    }
    return 0;
}


#include <bits/stdc++.h>
#define all(nums) nums.begin(), nums.end()
#define allr(nums) nums.rbegin(), nums.rend()
using namespace std;
typedef long long int ll;
typedef vector<vector<int>>graph;
const int imax = INT32_MAX;
const int imin = INT32_MIN;
const int mx=1e5+1;
const ll inf=1e18;
template<class T>void print(vector<T>vec){for(T i:vec)cout<<i<<" ";cout<<"\n";}
template<class T>void alloc(vector<vector<T>*>vec, T n){for(auto v:vec)v->resize(n+1);}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    ll t = 1;
    // cin>>t;
    while(t--) {
        int n;
        cin>>n;
        cout<<(n&1?"Black":"White");
	}
    return 0;
}

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

int main()
{
  int T;
  cin >> T;
  if (T % 2 == 1)
    cout << "Black";
  else
    cout << "White";
  return 0;
}


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

long long n, k, m, sum=0;

int main(){
	cin >> n >> k >> m;
	for (int i=0;i<n-1;i++){
		long long a;
		cin >> a;
		sum+=a;
	}
	m*=n;
	cout << (m-sum<=k ? max((long long)0,m-sum) : -1) << "\n";
}

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

using namespace std;
using ll = long long;

void _cin(){} template <class Head, class... Tail> void _cin(Head&& head, Tail&&... tail){ cin >> head; _cin(forward<Tail>(tail)...); }
void _cout(){ cout << "\n"; } template <class Head, class... Tail> void _cout(Head&& head, Tail&&... tail){ cout << head; _cout(forward<Tail>(tail)...); }
int gcd(int a, int b){ return (b == 0) ? a : gcd(b, a % b); }
template<typename S, typename T> ostream& operator<<(ostream &os, const pair<S, T> &p){ cout << "[" << p.first << ", " << p.second << "]"; return os; }

#define Sq(x) (x)*(x)
#define For(i, n) for(int i = 0; i < (n); i ++)
#define Rep(n) For(_, n)
#define Range(c) c.begin(), c.end()
#define RevRange(c) c.rbegin(), c.rend()
#define Contains(c, x) (find(Range(c), x) != c.end())
#define Search(rb, re, x) distance(rb, find(rb, re, x))
#define Sort(a) sort(Range(a))
#define DeSort(a) sort(RevRange(a))
#define Reverse(c) reverse(Range(c))
#define Unique(a) a.erase(unique(Range(a)), a.end())
#define Cusum(T, xs, sxs) vector<T> sxs(xs.size()+1); For(i, (int)xs.size()) sxs[i+1] = sxs[i] + xs[i]
#define Cin(T, ...) T __VA_ARGS__; _cin(__VA_ARGS__)
#define Cins(T, n, xs) vector<T> xs(n); For(i, n) cin >> xs[i]
#define Cins2(T, n, xs, ys) vector<T> xs(n), ys(n); For(i, n) cin >> xs[i] >> ys[i]
#define Cins3(T, n, xs, ys, zs) vector<T> xs(n), ys(n), zs(n); For(i, n) cin >> xs[i] >> ys[i] >> zs[i]
#define Cinss(T, n, m, xs) Vec2(T, n, m, xs); For(i, n) For(j, m) cin >> xs[i][j]
#define Cinm(T, n, map) unordered_map<T, int> map; Rep(n){ Cin(T, x); map[x] ++; }
#define Cout(...) _cout(__VA_ARGS__)
#define Couts(xs) { for(const auto &e : xs) cout << e << " "; cout << "\n"; }
#define Coutyn(cond) Cout((cond) ? "yes" : "no")
#define CoutYn(cond) Cout((cond) ? "Yes" : "No")
#define CoutYN(cond) Cout((cond) ? "YES" : "NO")
#define Return(expr) { Cout(expr); return 0; }
#define vc vector
#define Mini(a, x) a = min(a, x)
#define Maxi(a, x) a = max(a, x)

// constexpr int MOD = 1e9+7;

int main(void){
    Cin(int, n, k, m);
    int need = m*n;
    Rep(n-1){
        Cin(int, x);
        need -= x;
    }
    Cout(need < 0 ? 0 : need <= k ? need : -1);
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ull = unsigned long long;
using P = pair<int, int>;

int gc = 0;
void br() {
  cout << "(" << gc++ << ") "
       << "======" << endl;
}

template <typename T>
void print(vector<T> &a) {
  for (int i = 0; i < a.size(); i++) {
    cout << a[i];
    if (i + 1 < a.size()) {
      cout << " ";
    }
  }
  cout << endl;
}
void print(int n) { cout << n << endl; }
void print(ll n) { cout << n << endl; }
void print(ull n) { cout << n << endl; }
void print(double n) { cout << n << endl; }
void print(long double n) { cout << n << endl; }
void print(char n) { cout << n << endl; }
void print(string n) { cout << n << endl; }
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
double toRadian(double deg) { return deg * M_PI / 180; }

ll gcd(ll a, ll b) {
  if (a < b) {
    a ^= b;
    b ^= a;
    a ^= b;
  }
  return b ? gcd(b, a % b) : a;
}

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

int main() {
  ll n;
  cin >> n;
  vector<P> v(n);

  for (int i = 0; i < n; i++) {
    ll a, b;
    cin >> a >> b;

    v[i].first = a;
    v[i].second = b;
  }

  ll ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      double dx = v[j].first - v[i].first;
      double dy = v[j].second - v[i].second;
      if (dx != 0) {
        double k = dy / dx;

        if (-1 <= k && k <= 1) {
          ans++;
        }
      }
    }
  }

  print(ans);

  return 0;
}

#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <climits>
#include <string>
#include <cmath>
#include <bitset>
#include <complex>
#include <functional>
#include <ctime>
#include <cassert>
#include <fstream>
#include <stack>
#include <random>
#include <iomanip>

#include <fstream>

using namespace std;
typedef long long ll;
typedef long double dd;
#define i_7 (ll)(1E9+7)
//#define i_7 998244353
#define i_5 i_7-2
ll mod(ll a){
    ll c=a%i_7;
    if(c>=0)return c;
    return c+i_7;
}
typedef pair<ll,ll> l_l;
ll inf=(ll)1E18;
#define rep(i,l,r) for(ll i=l;i<=r;i++)
#define pb push_back
ll max(ll a,ll b){if(a<b)return b;else return a;}
ll min(ll a,ll b){if(a>b)return b;else return a;}
dd EPS=1E-9;
#define endl "\n"
#define fastio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

int main(){fastio
    ll n;cin>>n;
    if(n==1){
        cout<<0<<endl;
        return 0;
    }
    ll x[n],y[n];
    rep(i,0,n-1){
        cin>>x[i]>>y[i];
    }
    ll ans=0;
    rep(i,0,n-2){
        rep(j,i+1,n-1){
            //abs(y[j]-y[i])/abs(x[j]-x[i])が1以下
            if(abs(y[j]-y[i])<=abs(x[j]-x[i])){
                ans++;
            }
        }
    }
    cout<<ans<<endl;

    return 0;

}


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

#define TRACE(x) cerr << #x << " :: " << x << endl
#define _ << " " <<
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(),(x).end()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for (int i=(a);i>=(b);--i)
typedef long long ll;

const int mxN = 5e3+5;
const int mxM = 5e3+5;
const int mod = 998244353;

int N, M;
int powtbl[mxM][mxN], powsum[mxN];

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

    // observation 1: X will contribute if min(prv[X], pos[X], thing) < X
    // <=> X will not contribute if min(prv[X], pos[x], thing) > X
    //  consider prv[X] = 0 (DNE), 1, 2...pos[X]-1;
    //      (M-X) ** (pos[X]-1-prv[X])
    //
    //      rest: M ** (prv[X]-1)    *    M ** (N - pos[X]);
    //
    // for each i < j:
    //      for each X:
    //          (M-X) ** (j-1-i)   *   M ** (i-1)   *   M ** (N-j);
    //  -------
    //
    //          (M-X)**(j-1-i) * M**(N-j+i-1)
    //          precompute 0...M ** 0..N
    //          precompute add (0 to M-1) ** 0...N
    cin >> N >> M;

    FOR(i,0,M){
        powtbl[i][0] = 1;
        if (i<M) powsum[0] += powtbl[i][0];
        FOR(j,1,N){
            powtbl[i][j] = (ll)powtbl[i][j-1]*i % mod;
            if (i<M) powsum[j] = (powsum[j]+powtbl[i][j]) % mod;
        }
    }

    int ans = 1;
    FOR(i,1,N) ans = (ll)ans*M % mod;
    ans = (ll)ans*N % mod;

    FOR(i,1,N){
        FOR(j,i+1,N){
            int x = (ll)powsum[j-i-1] * powtbl[M][N-j+i-1] % mod;
            ans = ((ll)ans-x+mod) % mod;
        }
    }
    cout << ans << '\n';
}

// Problem: C - IPFL
// Contest: AtCoder - AtCoder Beginner Contest 199（Sponsored by Panasonic）
// URL: https://atcoder.jp/contests/abc199/tasks/abc199_c
// Memory Limit: 1024 MB
// Time Limit: 2000 ms

#include<bits/stdc++.h>
// #define int long long
// #define maxn
// #define mod
using namespace std;
// template<class T>T ABS(T x) {return x<0?-x:x;}
//int ksm(int x,int y) {int r=1;while(y) y&1?r=1ll*r*x%mod:0,x=1ll*x*x%mod,y>>=1;return r;}
int read()
{
	int s=0,w=1;
	char ch=getchar();
	while(ch<'0'||ch>'9') {if(ch=='-')w=-1;ch=getchar();}
	while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
	return s*w;
}
char S[2][1000010];int n,Q;
// struct Edge{int to,next;}a[maxn];
// int h[maxn],cnt;
// void add(int x,int y) {a[++cnt]=(Edge){y,h[x]},h[x]=cnt,a[++cnt]=(Edge){x,h[y]},h[y]=cnt;}
signed main()
{
	n=read();
	scanf("%s",S[0]+1);
	for(int i=1;i<=n;i++) S[1][i]=S[0][i+n];
	int fr=0;Q=read();
	for(int i=1;i<=Q;i++)
	{
		if(read()==2) fr^=1,read(),read();
		else {
			int x=read(),y=read();
			swap(S[fr^(x>n)][(x>n?x-n:x)],S[fr^(y>n)][(y>n?y-n:y)]);
		}
	}
	for(int i=1;i<=n;i++) cout<<S[fr][i];
	for(int i=1;i<=n;i++) cout<<S[fr^1][i];
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mit map<int,int>::iterator
#define sit set<int>::iterator
#define itrm(g,x) for(mit g=x.begin();g!=x.end();g++)
#define itrs(g,x) for(sit g=x.begin();g!=x.end();g++)
#define ltype int
#define rep(i,j,k) for(ltype(i)=(j);(i)<=(k);(i)++)
#define rap(i,j,k) for(ltype(i)=(j);(i)<(k);(i)++)
#define per(i,j,k) for(ltype(i)=(j);(i)>=(k);(i)--)
#define pii pair<int,int>
#define fi first
#define se second
#define mpr make_pair
#define pb push_back
#define fastio ios::sync_with_stdio(false)
#define check(x) if(x>=mod) x-=mod
const int inf=0x3f3f3f3f,mod=998244353;
const double pi=3.1415926535897932,eps=1e-6;
void chmax(int &x,int y){if(x < y) x = y;}
void chmin(int &x,int y){if(x > y) x = y;}
int qpow(int x,int y){
    int ret = 1;
    while(y) {
        if(y & 1) ret = (ll)ret * x % mod;
        x = (ll)x * x % mod;
        y >>= 1;
    }
    return ret;
}
int n,p[105],fac[105];
int dp[105][5005][105];
int main()
{
    scanf("%d",&n);
    fac[0] = 1;rep(i,1,n) fac[i] = (ll)fac[i - 1] * i % mod;
    int sum = 0;
    rep(i,1,n) scanf("%d",p+i), sum += p[i];
    if(sum & 1) {
        puts("0");
        return 0;
    }
    dp[0][0][0] = 1;
    rep(i,1,n) {
        rep(j,0,sum/2) {
            rep(k,0,i) {
                dp[i][j][k] = dp[i - 1][j][k];
                if(p[i] <= j && k) dp[i][j][k] += dp[i - 1][j - p[i]][k - 1];
                check(dp[i][j][k]);
            }
        }
    }
    int ans = 0;
    rep(i,1,n) {
        int num = dp[n][sum/2][i];
        ans += (ll)num * fac[i] % mod * fac[n - i] % mod;
        check(ans);
    }
    printf("%d\n",ans);
    return 0;
}

#include <bits/stdc++.h>
const int mod=998244353;
using namespace std;
int T,n,m,fac[110],a[110],f[110][5010];
void init(int N){
	fac[0]=1;
	for(int i=1;i<=N;i++) fac[i]=1ll*fac[i-1]*i%mod;
}
void qmo(int &x){
	x+=(x>>31)&mod;
}
int main(){
	scanf("%d",&n),init(n);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]),m+=a[i];
	if(m&1) return puts("0"),0;
	m/=2,f[0][0]=1;
	for(int i=1;i<=n;i++) for(int j=i;j>=1;j--) for(int k=m;k>=a[i];k--){
		qmo(f[j][k]+=f[j-1][k-a[i]]-mod);
	}
	int res=0;
	for(int i=0;i<=n;i++) res=(res+1ll*fac[i]*fac[n-i]%mod*f[i][m])%mod;
	printf("%d\n",res);
}


#include <iostream>
#include <vector>
#include <string>
#include <stdint.h>
#include <math.h>
#include <algorithm>
#include <cmath>

#define rep(i, j) for (int i = 0; i < (j); ++i)
#define fep(i, j) for (i = 0; i < (j); ++i)
#define pb push_back
#define ll long long
#define dp vector<pair<ll, int> >
#define dn vector<vector<int> >
#define ds vector<int>

//template <class RandomAccessIterator>
  //void sort (RandomAccessIterator first, RandomAccessIterator last);

using namespace std;

int sol()
{
    int N;
    int64_t S, D, X, Y;
    cin>>N>>S>>D;
    vector<vector<int> > p;
    string ans = "No";

    rep(i, N)
    {
       vector<int> spells;
       rep(i, 1)
       {
           cin >> X >> Y;
           spells.pb(X);
           spells.pb(Y);
       }
       p.pb(spells);
    }

    rep(i, N)
    {
       for(unsigned int j = 0; j < p[i].size(); ++j)
       {
           if(p[i][0] < S && p[i][1] > D)
           {
               ans = "Yes";
           }
       }
    }

    cout << ans;
    return 0;
}

int main()
{
   return sol();
}

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
int n,s,d;
int main(){
	scanf("%d%d%d",&n,&s,&d);
	bool flag=false;
	for(int i=1;i<=n;i++){
		int x,y;
		scanf("%d%d",&x,&y);
		if(x<s&&y>d)	flag=true;
	}
	if(flag)	puts("Yes");
	else	puts("No");
	return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define MV 200001
#define LMV 21
#define ff first
#define ss second
#define pb push_back
#define eb emplace_back
#define emp emplace
#define mp make_pair
#define ins insert
#define sz(x) (int)x.size()
#define whoami(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); whi(_it, args); }

void whi(istream_iterator<string> it) { cerr<<"\n"; }

template<typename T, typename... Args>
void whi(istream_iterator<string> it, T a, Args... args) { cerr<<*it<<" "<<a<<" "; whi(++it, args...); }

void FLASH() {ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);}
void SETF() {cout.ios_base::setf(ios_base::fixed); cerr.ios_base::setf(ios_base::fixed);}
void UNSETF() {cout.ios_base::unsetf(ios_base::fixed); cerr.ios_base::unsetf(ios_base::fixed);}

typedef long long ll;
typedef long double ld;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef pair<PII, int> PPII;
typedef pair<PLL, ll> PPLL;
typedef map<int, int> MII;

const int MOD = 1000000007;
const ll INF = 4e18;

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

struct h_llint {
    static uint64_t splitmix64(uint64_t x) {    // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }
    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
}
};

struct h_pair{
    size_t operator()(const PLL&x)const{
        return hash<ll>()(((ll)x.ff)^(((ll)x.ss)<<32));
    }
};

typedef map<ll, ll> MLL;
typedef map<PII, int> MPII;
typedef map<PLL, ll> MPLL;
typedef set<int> SI;
typedef set<ll> SL;
//ordered_set = order_of_key(.)
//ordered_set = find_by_order(.)
typedef ordered_set<int> OSI;
typedef ordered_set<ll> OSL;
typedef ordered_multiset<int> MOSI;
typedef ordered_multiset<ll> MOSL;
typedef unordered_map<ll, int, h_llint> UMLI;
typedef unordered_map<ll, ll, h_llint> UMLL;
typedef unordered_map<PLL, int, h_pair> UMPI;
typedef unordered_map<PLL, ll, h_pair> UMPL;

int ar[MV];
ll arr[MV];

void solve(int T)
{
    int n;
    cin>>n;
    VI ax(n), bx(n), cx(n), cc(n+1);
    for(auto &&u : ax)
    {
        cin>>u;
        cc[u]++;
    }
    for(auto &&u : bx)
    cin>>u;
    for(auto &&u : cx)
    cin>>u;
    ll tt = 0;
    for(auto &&u : cx)
    tt += cc[bx[u-1]];
    cout<<tt<<"\n";
    return;
}

int main(void)
{
    FLASH();
    //freopen("cowjog.in", "r", stdin);
    //freopen("cowjog.out", "w", stdout);
    int T;
    T = 1;

#ifndef ONLINE_JUDGE
    time_t time_t1, time_t2;
    time_t1 = clock();
#endif

    //cin>>T;
    while(T--)
    solve(T);

#ifndef ONLINE_JUDGE
    time_t2 = clock();
    SETF();
    cerr<<"Time taken: "<<setprecision(7)<<(time_t2 - time_t1)/(double)CLOCKS_PER_SEC<<"\n";
    UNSETF();
#endif

    return 0;
}

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

#define ll long long
#define pb push_back
#define sz(x) int(x).size()

typedef pair<int,int>ii;
typedef vector<int> vi;
const int mxn=2e5+5;
int n,a[mxn],me=1e9+10;
int32_t main(){
	ios_base::sync_with_stdio(0);cin.tie(0);
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a[i];
		me=min(me,a[i]);
	}
	unordered_map<int,int>mp;
	for(int i=0;i<n;i++){
		for(int j=1;j*j<=a[i];j++){
			if(a[i]%j==0){
				if(j<me){
				 	if(mp.find(j)==mp.end())mp[j]=a[i];
				 	mp[j]=__gcd(mp[j],a[i]);
				}
				if(a[i]/j<me){
				 	if(mp.find(a[i]/j)==mp.end())mp[a[i]/j]=a[i];
				 	mp[a[i]/j]=__gcd(mp[a[i]/j],a[i]);
				}

			}
		}
	}
	int ans=0;
	for(auto it=mp.begin();it!=mp.end();it++){
		if(it->first==it->second)ans++;
	}
	cout<<ans+1<<"\n";
	return 0;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
#define lowbit(x) (x & (-x))

const int maxn = 2e5+8, inf = 1e18+9, mod = 1e9+7;

int n, m, a[maxn], b[maxn], bit[maxn], pos[maxn];
map<int, set<int>> mp;

void update(int x, int v) {
    while (x < maxn) bit[x] += v, x += lowbit(x);
}

int ask(int x) {
    int ans = 0;
    while (x) ans += bit[x], x -= lowbit(x);
    return ans;
}

void solve() {
    int i, j, ans = 0;
    cin >> n;
    for (i = 1; i <= n; i++) cin >> a[i], a[i] += i;
    for (i = 1; i <= n; i++) cin >> b[i], b[i] += i;
    for (i = 1; i <= n; i++) mp[a[i]].insert(i);
    for (i = 1; i <= n; i++) {
        if (mp[b[i]].empty()) return (void)(cout << -1 << endl);
        auto it = mp[b[i]].begin();
        pos[i] = *it;
        mp[b[i]].erase(it);
    }
    for (i = 1; i <= n; i++) {
        ans += ask(n) - ask(pos[i]);
        update(pos[i], 1);
    }
    cout << ans << endl;
}

signed main() {
    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); //cout << fixed << setprecision(15);
    int t = 1; //cin >> t;
    while (t--) solve();
    return 0;
}

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

#define e1 first
#define e2 second
#define pb push_back
#define mp make_pair
#define boost {ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); }
#define eb emplace_back
#define OUT(x) {cout << x; exit(0); }
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define scanf(...) scanf(__VA_ARGS__)?:0
typedef long long int ll;
typedef unsigned long long ull;
typedef pair <int, int> PII;
typedef pair <ll, ll> PLL;
typedef pair <PLL, int> PLLI;
typedef pair <PII, PII> PP;
typedef pair <PII, int> PPI;
typedef pair <int, PII> PIP;
typedef pair <ll, int> PLI;
typedef unsigned int ui;
const int inf = 1e9+9;
const ll MOD = 1e9+696969;
#ifdef DEBUG
template<class T> int size(T &&x) {
	return int(x.size());
}
template<class A, class B> ostream& operator<<(ostream &out, const pair<A, B> &p) {
	return out << '(' << p.first << ", " << p.second << ')';
}
template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) {
	out << '{';
	for(auto it = x.begin(); it != x.end(); ++it)
		out << *it << (it == prev(x.end()) ? "" : ", ");
	return out << '}';
}
void dump() {}
template<class T, class... Args> void dump(T &&x, Args... args) {
	cerr << x << ";  ";
	dump(args...);
}
#endif
#ifdef DEBUG
  struct Nl{~Nl(){cerr << '\n';}};
# define debug(x...) cerr << (strcmp(#x, "") ? #x ":  " : ""), dump(x), Nl(), cerr << ""
#else
# define debug(...) 0 && cerr
#endif
mt19937_64 rng(0);
int random(int l, int r) {
	return uniform_int_distribution<int>(l, r)(rng);
}

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long LL;
typedef pair<int, int> pii;
typedef vector<int> vi;
const LL INF = 1e18;
const int maxn = 300100;
const int mod = 998244353;
int dr[maxn];

void add(int p, int v) {
	for (; p < maxn; p += p & (-p)) dr[p] += v;
}

int get(int p) {
	int res = 0;
	for (; p > 0; p -= p & (-p)) res += dr[p];
	return res;
}

ll inv(vector <int> &perm) {
	int n = perm.size();
	ll res = 0;
	for (int i=n-1; i>=0; --i) {
		res += get(perm[i]);
		add(perm[i], 1);
	}

	return res;
}

void solve() {
	int n;
	cin >> n;
	set <PII> q;
	FOR(i, 0, n-1) {
		int a;
		cin >> a;
		q.insert({a + i, i + 1});
	}

	vector <int> perm;
	vector <int> B(n + 1);
	FOR(i, 1, n) {
		cin >> B[i];
	}

	FOR(i, 1, n) {
		int a = B[i];
		int co = a + i - 1;
		set <PII> :: iterator szukane = q.lower_bound(make_pair(co, -1));
		if (szukane == q.end() || szukane->first != co) {
			cout << "-1\n";
			return;
		}

		perm.push_back(szukane -> e2);
		q.erase(szukane);
	}
	debug(perm);
	cout << inv(perm) << "\n";
}

int main() {
	boost;
	int tests;
	tests = 1;
	//cin >> tests;
	while (tests--) {
		solve();
	}
}


#include <bits/stdc++.h>
using namespace std;
int par[110];
int depth[110];
int siz[110];
void init(){
	for(int i = 0; i < 110; i++){
		par[i] = i; depth[i] = 0; siz[i] = 1;
	}
}
int find(int x){
	if(par[x] == x) return x;
	else return par[x] = find(par[x]);
}
void unite(int x, int y){
	x = find(x);
	y = find(y);
	if(x == y) return;
	else if(depth[x] < depth[y]){
		par[x] = y;
		siz[y] += siz[x];
	}else{
		par[y] = x;
		siz[x] += siz[y];
		if(depth[x] == depth[y]) depth[x] ++;
	}
}
bool same(int x, int y){
	return find(x) == find(y);
}
int sz(int x){
	return siz[find(x)];
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int N; cin >> N;
	int X[110], Y[110];
	for(int i = 1; i <= N; i++){
		cin >> X[i] >> Y[i];
	}
	double ok = 0; double ng = 114.514;
	int T = 64;
	while(T--){
		init();
		double mid = (ok + ng) * 0.5;
 		init();
		for(int i = 1; i <= N; i++){
			if(Y[i] + mid * 2 >= 100) unite(0, i);
			if(Y[i] - mid * 2 <= -100) unite(N+1, i);
		}
		if(mid >= 100) unite(0, N+1);
		for(int i = 1; i <= N; i++){
			for(int j = i+1; j <= N; j++){
				double d = pow((X[j] - X[i]) * (X[j] - X[i]) + (Y[j] - Y[i]) * (Y[j] - Y[i]), 0.5);
				if(d <= mid * 2) unite(i, j);
			}
		}
		if(same(0, N+1)) ng = mid;
		else ok = mid;
	}
	cout.precision(12);
	cout << ok << "\n";
}

#include <bits/stdc++.h>
#define rep(i, n) for (lli i = 0; i < (n); i++)
#define rrep(i, n) for (lli i = (n)-1; i >= 0; i--)
#define vall(x) (x).begin(), (x).end()
using namespace std;
namespace atcoder {}
using namespace atcoder;
using lli = long long int;
void YESNO(bool), YesNo(bool);
template <class T1, class T2> bool chmin(T1 &l, const T2 &r);
template <class T1, class T2> bool chmax(T1 &l, const T2 &r);
#define int long long int
int help(int st, int dst, int last) { return abs(st - dst) + abs(last - dst); }
signed main() {
  int n;
  cin >> n;
  map<int, vector<int>> m;
  rep(i, n) {
    int x, c;
    cin >> x >> c;
    m[c].push_back(x);
  }
  vector<vector<int>> dp(1e6, vector<int>(2, 1e18));
  vector<vector<int>> mem;

  for (auto &e : m) {
    sort(e.second.begin(), e.second.end());
    int f = e.second.front();
    int en = e.second.back();
    mem.push_back({f, en});
  }
  rep(i, mem.size()) {
    if (i) {
      chmin(dp[i][1], dp[i - 1][0] + help(mem[i - 1][0], mem[i][0], mem[i][1]));
      chmin(dp[i][0], dp[i - 1][0] + help(mem[i - 1][0], mem[i][1], mem[i][0]));
      chmin(dp[i][1], dp[i - 1][1] + help(mem[i - 1][1], mem[i][0], mem[i][1]));
      chmin(dp[i][0], dp[i - 1][1] + help(mem[i - 1][1], mem[i][1], mem[i][0]));
    } else {
      dp[i][0] = help(0, mem[i][1], mem[i][0]);
      dp[i][1] = help(0, mem[i][0], mem[i][1]);
    }
  }
  cout << min(dp[mem.size() - 1][0] + abs(mem.back()[0]),
              dp[mem.size() - 1][1] + abs(mem.back()[1]))
       << endl;

  return 0;
}

// -- lib
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }

template <class T1, class T2> bool chmin(T1 &l, const T2 &r) {
  return (l > r) ? (l = r, true) : false;
}

template <class T1, class T2> bool chmax(T1 &l, const T2 &r) {
  return (l < r) ? (l = r, true) : false;
}

template <class T1, class T2> void vadd(vector<T1> &v, T2 x) {
  for (auto &s : v)
    s += T2(x);
}