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

ll mod = 1000000007;

int main() {
    ll N,M,X,Y; cin >> N >> M >> X >> Y;
    Graph G(N+1);
    rep(i,M){
        ll a,b,t,k; cin >> a >> b >> t >> k;
        G[a].push_back(make_tuple(b,t,k));
        G[b].push_back(make_tuple(a,t,k));
    }
    //Dijkstra (普通のダイクストラ)
    priority_queue<Pr, vector<Pr>, greater<Pr>> go;
    ll x,y,a,t,k; ll inf = 2e18;
    ll dist[N+1]; //始点からの距離
    rep(j,N+1){
        dist[j] = inf;
    }
    go.push(make_pair(0,X)); dist[X] = 0; //始点座標:s
    Pr p;
    while(!go.empty()){
        p = go.top(); go.pop();
        x = p.first; y = p.second;
        if(x>dist[y]) continue;
        for(auto q:G[y]){ //G[y]:Graph
            a = get<0>(q); t = get<1>(q); k = get<2>(q);
            ll m = x%k;
            ll st = 0;
            if(m!=0) st = k-m;
            if(x+st+t<dist[a]){
                dist[a] = x+st+t;
                go.push(make_pair(x+st+t,a));
            }
        }
    }
    if(dist[Y]==inf) cout << -1 << endl;
    else cout << dist[Y] << endl;
}

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

typedef long long ll;
typedef unsigned long long ull;
typedef long long int lli;
typedef vector<ll> vl;

#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define SQR(x) ((x) * (x))

#define loop(i, y) for (int i = 0; i < int(y); i++)
#define FOR(i, x, y) for (int i = int(x); i <= int(y); i++)
#define ROF(i, y, x) for (int i = int(y); i >= int(x); i--)

#define ALL(c) c.begin(), c.end()
#define CLR(x, y) memset(x, y, sizeof(x))

#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)

#define si(x) scanf("%d", &x)
#define sii(x, y) scanf("%d %d", &x, &y)
#define siii(x, y, z) scanf("%d %d %d", &x, &y, &z)
#define sl(x) scanf("%lld", &x)
#define sll(x, y) scanf("%lld %lld", &x, &y)
#define slll(x, y, z) scanf("%lld %lld %lld", &x, &y, &z)
#define sllll(a, b, c, d) scanf("%lld %lld %lld %lld", &a, &b, &c, &d)

#define FileIO  freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#define FastIO  ios::sync_with_stdio(false); cin.tie(0);

#define FixedPrecision(x) ios::sync_with_stdio(false);cout.setf(ios::fixed); cout.precision(x);cin.tie(0);
#define bitCheck(N, in) ((bool)(N & (1LL << (in))))
#define bitOff(N, in) (N & (~(1LL << (in))))
#define bitOn(N, in) (N | (1LL << (in)))
#define bitFlip(a, k) (a ^ (1LL << (k)))

#define start_time clock_t tStart = clock();
#define execution_time printf("\n>>Time taken: %.10fs\n", (double)(clock() - tStart) / CLOCKS_PER_SEC);

#define popCount(x) __builtin_popcount(x)
#define int2Bin(x, y); bitset<y> z = x; cout << z.to_string() << endl;
#define tr(it, container) for (auto it = container.begin(); it != container.end(); it++)
#define unq(v) (v).erase(unique(ALL(v)), v.end())
#define common(a, b) sort(ALL(a)), sort(ALL(b)), a.erase(set_intersection(ALL(a), ALL(b), a.begin()), a.end())
#define uncommon(a, b) sort(ALL(a)), sort(ALL(b)), a.erase(set_symmetric_difference(ALL(a), ALL(b), a.begin()), a.end())

#define TE(t, n) cout << "Case " << t << ": " << n << endl;

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

/// Constants

#define MAXN 1000005
#define MAX 200005
#define MOD 1000000007
#define BASE 1000000009
#define eps 1e-9
#define INF 1llu << 61 // 2,305,843,009,213,693,952
#define inf 1 << 29    // 536,870,912
#define PI acos(-1.0)  // 3.1415926535897932
#define E 2.71828182845904523536

int main()
{

    ll T = 1;          //sl(T);

    FOR(t,1,T)
    {
        ll n ; sl (n);

        unordered_map< ull,ull > m1,m2;
        vector < ull > v1,v2;

        ull val = 1;

        for(ull i = 0; i <= 37 ; i++){
            val*=3;
            v1.pb(val);
            m1[val]=i+1;
        }
        val = 1;
        for(ull i = 0; i <= 25 ; i++){
            val*=5;
            v2.pb(val);
            m2[val]=i+1;
        }

        for(int i = 0 ; i <=37 ; i++){
            if( m2[n-v1[i]] ){
                cout << i+1 << " " << m2[n-v1[i]];
                return 0;
            }
        }

        cout << "-1" <<endl;
    }

        return 0;
}


//ye moh moh ke dhaage,tere ungliyon se ja uljhe

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

#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define ll long long
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep(i,n) for(i=0;i<n;i++)
#define forn(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define for1(i, n) for (ll i = 1; i <= (ll)(n); ++i)
#define ford(i, n) for (ll i = (ll)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (ll i = (ll)(a); i <= (ll)(b); ++i)
#define fora(it,x) for(auto it:x)
#define PI 3.14159265
#define sync ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define endl "\n"

typedef pair<ll, ll> pii;

typedef vector<ll> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef long double ld;

template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

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

    if(c==1){
        if(b>a) cout<<"Aoki";
        else cout<<"Takahashi";
    }
    else{
        if(a>b) cout<<"Takahashi";
        else cout<<"Aoki";
    }
}


#include <bits/stdc++.h>
using namespace std;
int _ = (cout << fixed << setprecision(9), cin.tie(0), ios::sync_with_stdio(0));
using Int = long long;
vector<int> divisor(int n) {
    vector<int> ans;
    for (int t = 1; t * t <= n; t++) {
        if (n % t != 0) continue;
        if (t * t == n) {
            ans.push_back(t);
        } else {
            ans.push_back(t);
            ans.push_back(n / t);
        }
    }
    return ans;
}
int main() {
    Int L, R; cin >> L >> R;
    vector<Int> A(500001);
    Int ans = 0;
    for (int n = 500000; n >= 2; n--) {
        Int t = (R / n) - ((L + n - 1) / n) + 1;
        A[n] += t * (t - 1) / 2;
        for (int d : divisor(n)) {
            if (d == 1 || d == n) continue;
            A[d] -= A[n];
        }
        Int a = A[n];
        if (L <= n && n <= R) {
            a -= t - 1;
        }
        ans += a;
    }
    cout << ans * 2 << '\n';
    return 0;
}

#include<bits/stdc++.h>
#define            pb            push_back
#define            pl            pair<ll,ll>
#define            pll           pair<ll,pair<ll,ll>>
#define            ll            long long
#define            vl            vector<ll>
#define            fastio        ios_base::sync_with_stdio(false); cin.tie(NULL);
#define            mp            make_pair
#define            w(t)          int t;cin>>t;while(t--)
#define            inf           1e18
#define            fi(a,n)       for(ll i=a;i<n;i++)
#define            fr(i,a,b)     for(ll i=a;i>=b;i--)
#define            fj(a,n)       for(ll j=a;j<n;j++)
#define            k(a,n)        for(ll k=a;k<n;k++)
#define            endl          "\n"
#define            all(v)        (v).begin(),(v).end()
#define            set1(x)       __builtin_popcount(x)
#define            gcd(a,b)      __gcd(a,b)
#define mem1(a)           memset(a,-1,sizeof(a))
#define mem0(a)           memset(a,0,sizeof(a))
using namespace std;
const ll mod= 1000000007;
const ll N =1000005;
ll v[N];
vl m(N,1);
ll f[N];
void seive()
{
  m[1]=1;
  fi(2,N)
  {
    if(!v[i])
    {
      for(ll j=i;j<N;j+=i)
      {

        if(j%(i*i)==0)m[j]=0;
        m[j]=-m[j];
        v[j]=1;
      }
    }
  }
}
void solve()
{
  ll l,r;
  cin>>l>>r;
  fi(l,r+1)
  {
    f[i]++;
  }
  ll ans=0;
  // fi(1,10)cout<<m[i]<<endl;
  fi(1,r+1)
  {
    if(m[i]==0)continue;
    ll temp=0;
    for(ll j=i;j<r+1;j+=i)
    {
       temp+=(f[j]);
    }
    // cout<<i<<" "<<temp<<endl;
    ans+=(temp*(temp-1)/2*m[i]);
  }
  ll x=r-l+1;
  ans=((x*(x-1))/2)-ans;
  // cout<<ans<<endl;
  ll kk=0;
  fi(max(2LL,l),r+1)
  {
    for(ll j=i;j<r+1;j+=i)
    {
       if(i==j)continue;
       else kk++;
    }
  }

  cout<<2*(ans-kk)<<endl;
}
int main()
{
  ios_base::sync_with_stdio(false);
    cin.tie(NULL);
  #ifndef ONLINE_JUDGE
    // for getting input from input.txt
    freopen("input.txt", "r", stdin);
    // for writing output to output.txt
    freopen("output.txt", "w", stdout);
#endif
      // w(t)
       seive();
      solve();

}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int INF = (1<<30)-1;
const ll LINF = (1LL<<60)-1;

#define rep(i, n) for (int i = 0; i < n; i++)
#define sz(a) (int)(a.size())

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

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

//コーナーケースに気をつけろ！
int main() {
    ll n;   cin >> n;
    vector<ll> a(n+1, 0);
    rep(i, n)   cin >> a[i+1];
    vector<vector<ll> > s(n+1, vector<ll>(n+2, 0));
    vector<vector<int> > pre(n+1, vector<int>(n+1, 0));
    for (int i = 1; i <= n; i++){
        vector<int> last(i, 0);
        ll s = 0;
        for (int j = 1; j <= n; j++) {
            s = (s + a[j]) % i;
            pre[i][j] = last[s];
            last[s] = j;
        }
    }
    vector<vector<ll> > dp(n+1, vector<ll>(n+1, 0));
    dp[0][0] = 1;
    ll mod = 1e9+7;
    for (int r = 1; r <= n; r++) {
        for (int k = 1; k <= n; k++) {
            dp[r][k] = (dp[pre[k][r]][k] + dp[pre[k][r]][k-1]) % mod;
        }
    }
    ll ans = 0;
    for (int k = 1; k <= n; k++)
        ans = (ans + dp[n][k]) % mod;
    cout << ans << endl;
    return 0;
}

//小数点精度
//cout << fixed << std::setprecision(15) << y << endl;

#include <iostream>
#include <cmath>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <set>
#include <algorithm>
#include <iomanip>
#include <string.h>
#include <bitset>

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define ALL(a)  (a).begin(),(a).end()

typedef long long lint;

using namespace std;

int main(){
  int N;
  cin>>N;
  int A[N][5];
  REP(i,N)REP(j,5)cin>>A[i][j];
  int OK=0;
  int NG=1e9+1;
  while(NG-OK>1){
    int c=(OK+NG)/2;
    bool dp[4][1<<5]={0};
    dp[0][0b00000]=1;
    // cerr<<c<<endl;
    int pi[N];
    REP(i,N){
      pi[i]=0;
      REP(j,5)if(A[i][j]>=c)pi[i]|=1<<j;
      // cerr<<bitset<5>(pi[i])<<endl;
    }
    REP(i,N){
      REP(n,3)REP(p,1<<5)if(dp[n][p]){
        dp[n+1][p|pi[i]]=true;
      }
    }
    if(dp[3][0b11111])OK=c;
    else NG=c;
  }
  cout<<OK<<endl;
  return 0;
}


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

constexpr double eps = 1e-9;
double o;
double pi2 = 2 * acos(-1);

struct P {
    bool operator!=(const P &p) const {
        double z = t - p.t - o;
        z = z - round(z / pi2) * pi2;
        return !(abs(z) < eps && r2 == p.r2);
    }
    bool operator<(const P &p) const {
        //return r2 != p.r2 ? r2 < p.r2 : t < p.t;
        return abs(t - p.t) > eps ? t < p.t : r2 < p.r2;
    }
    int x, y;
    int r2;
    double t;
};

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

    vector<P> p(n), q(n);
    for (int i = 0; i < n; i++) {
        int x, y;
        cin >> x >> y;
        p[i] = { x, y, 0, 0 };
    }
    for (int i = 0; i < n; i++) {
        int x, y;
        cin >> x >> y;
        q[i] = { x, y, 0, 0 };
    }

    p[0].r2 = 0;
    p[0].t = -100;
    for (int i = 1; i < n; i++) {
        int x = p[i].x - p[0].x;
        int y = p[i].y - p[0].y;
        p[i].r2 = x * x + y * y;
        p[i].t = atan2(y, x);
    }
    sort(p.begin(), p.end());

    for (int j = 0; j < n; j++) {
        vector<P> r = q;
        swap(r[j], r[0]);
        r[0].r2 = 0;
        r[0].t = -100;
        for (int i = 1; i < n; i++) {
            int x = r[i].x - r[0].x;
            int y = r[i].y - r[0].y;
            r[i].r2 = x * x + y * y;
            r[i].t = atan2(y, x);
        }
        sort(r.begin(), r.end());

        for (int h = 0; h < n; h++) {
            int f = 1;
            int i1 = 1 + h;
            if (i1 >= n) i1 -= n - 1;
            o = p[1].t - r[i1].t;
            for (int i = 1; i < n; i++) {
                int i1 = i + h;
                if (i1 >= n) i1 -= n - 1;
                if (p[i] != r[i1]) {
                    f = 0;
                    break;
                }
            }
            if (f) {
                //cout << j << ' ' << h << endl;
                cout << "Yes" << endl;
                exit(0);
            }
        }
    }
    cout << "No" << endl;

    return 0;
}

#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
int n;
struct point{
	int x,y;
}a[110],b[110];
double dx[110],dy[110];
double abss(double x){return (x<0)?-x:x;}
int main()
{
	scanf("%d",&n);
	if (n==1) {printf("Yes");return 0;}
	for (int i=1;i<=n;i++)
		scanf("%d%d",&a[i].x,&a[i].y);
	for (int i=1;i<=n;i++)
		scanf("%d%d",&b[i].x,&b[i].y);
	int len=(a[1].x-a[2].x)*(a[1].x-a[2].x)+(a[1].y-a[2].y)*(a[1].y-a[2].y);
	int ax=a[1].x-a[2].x,ay=a[1].y-a[2].y;
	for (int i=1;i<=n;i++)
		for (int j=1;j<=n;j++)
		{
			if (i==j) continue;
			if (((b[i].x-b[j].x)*(b[i].x-b[j].x)+(b[i].y-b[j].y)*(b[i].y-b[j].y))!=len) continue;
			int bx=b[i].x-b[j].x,by=b[i].y-b[j].y;
			double sina=1.0*(1.0*ax*by-ay*bx)/len;
			double cosa=sqrt(1.0-sina*sina);
			for (int m=-1;m<=1;m+=2)
			{

				//cout<<i<<" "<<j<<endl;
				if (ax!=bx||ay!=by)
				{
					for (int k=1;k<=n;k++)
					{
						dx[k]=a[k].x*cosa-a[k].y*sina;
						dy[k]=a[k].y*cosa+a[k].x*sina;
					//	cout<<k<<" :: "<<dx[k]<<" "<<dy[k]<<endl;
					}
				}
				else
				{
					for (int k=1;k<=n;k++)
					{
						dx[k]=a[k].x;
						dy[k]=a[k].y;
					//	cout<<k<<": "<<dx[k]<<" "<<dy[k]<<endl;
					}
				}
				double cx=b[i].x-dx[1],cy=b[i].y-dy[1];
				for (int k=1;k<=n;k++)
				{
					dx[k]+=cx;
					dy[k]+=cy;
				//	cout<<dx[k]<<" "<<dy[k]<<endl;
				}

				//judge
				bool yes=1;
				for (int k=1;k<=n;k++)
				{
					bool can=0;
					for (int q=1;q<=n;q++)
						if (abss(dx[k]-b[q].x)<=1e-6&&abss(dy[k]-b[q].y)<=1e-6)
							{can=1;break;}
					if (!can) {yes=0;break;}
				}
				if (yes) {printf("Yes\n");return 0;}
				cosa*=-1;
			//printf("%d %d %f %f %d %d\n",i,j,sina,cosa,rx,ry);
			}
		}
	printf("No\n");
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 10;
vector <pair <int, int>> a;
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int a, b, x, y;
    cin >> a >> b >> x >> y;
    int ans = 0;
    if (a > b) {
        --a;
    }
    y = min(y, x + x);
    cout << x + y * abs(a - b);
}

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

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;

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

#define fast			ios_base::sync_with_stdio(0);cin.tie(0)
#define f0(a,b,c) 		for(int a=b;a<c;a++)
#define re(a,b,c) 		for(int a=b;a>=c;a--)
#define trav(a,x)		for(auto &a:x)
#define ini(a,x)		memset(a,x,sizeof(a))
#define pb 				push_back
#define eb				emplace_back
#define fi 				first
#define se 				second
#define all(con) 		con.begin(),con.end()
#define rall(con)		con.rbegin(),con.rend()
#define ll 				long long
#define vi 				vector<int>
#define vll 			vector<ll>
#define MOD 			1000000007
#define pi 				pair<int,int>
#define pll 			pair<ll,ll>
#define mi 				map<int,int>
#define mll				map<ll,ll>
#define umi 			unordered_map<int,int>
#define umll 			unordered_map<ll,ll>
#define lb 				lower_bound
#define ub 				upper_bound
#define pq 				priority_queue<ll,ll>
#define st				stack<int>
#define qu				queue<int>
#define endl            "\n"
#define sz(a)			(int)a.size()
using ull = unsigned long long;

template<class T> int chkmax(T &a, T b) {
	if (b > a) {
		a = b;
		return 1;
	}
	return 0;
}

template<class T> int chkmin(T &a, T b) {
	if (b < a) {
		a = b;
		return 1;
	}
	return 0;
}

template<class iterator> void output(iterator begin, iterator end, ostream& out = cerr) {
	while (begin != end) {
		out << (*begin) << " ";
		begin++;
	}
	out << endl;
}

template<class T> void output(T x, ostream& out = cerr) {
	output(x.begin(), x.end(), out);
}

ll powm(ll a , ll n)
{
	ll res = 1;

	while(n)
	{
		if(n & 1)
		res = (res * a);

		n >>= 1;
		a = (a * a);
	}

	return res;
}

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
	return (ull)rng() % B;
}

ll add(ll x, ll y) {
	x += y;
	if (x >= MOD) return x - MOD;
	return x;
}
ll sub(ll x, ll y) {
	x -= y;
	if (x < 0) return x + MOD;
	return x;
}
ll mult(ll x, ll y) {
	return (x * y) % MOD;
}
ll bin_pow(ll x, ll p) {
	if (p == 0) return 1;
	if (p & 1) return mult(x, bin_pow(x, p - 1));
	return bin_pow(mult(x, x), p / 2);
}
ll rev(ll x) {
	return bin_pow(x, MOD - 2);
}

int main()
{
	fast;
	int t=1;
	//cin>>t;
	while(t--)
	{
		int a,b,x,y;
		cin>>a>>b>>x>>y;
		if(a>b)
		    cout<<min((a-b-1)*y+x,(a-b-1)*2*x+x);
		else
		    cout<<min((b-a)*y+x,(b-a)*2*x+x);
	}
	return 0;
}


// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
#define all(x) (x).begin(),(x).end()
template<typename T1,typename T2> bool chmin(T1 &a,T2 back){if(a<=back)return 0; a=back; return 1;}
template<typename T1,typename T2> bool chmax(T1 &a,T2 back){if(a>=back)return 0; a=back; return 1;}
int dx[4]={-1,1,0,0}, dy[4]={0,0,-1,1};
long double eps = 1e-9;
long double pi = acos(-1);

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

    int a;
    cin>>a;
    if(a >= 0)cout << a << endl;
    else cout << 0 << endl;

}

#include<bits/stdc++.h>

using namespace std;

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

	if(s[0] != s[1]) cout<<"Lost\n";
	else if(s[1] != s[2]) cout<<"Lost\n";
	else cout<<"Won\n";

	return 0;
}


#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
using namespace std;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
using ll = long long;
constexpr int inf=1e9+7;
constexpr ll longinf=1LL<<60 ;
constexpr ll mod=1e9+7 ;

int gcd(int x, int y){
	return y ? gcd(y, x%y) : x;
}

int main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	int n;
	cin>>n;
	vector<int> a(n);
	rep(i,n)cin>>a[i];
	map<int,int> dp;
	rep(i,n){
		REP(j,1,32000){
			if(j*j>a[i])break;
			if(a[i]%j==0){
				if(!dp.count(j))dp[j]=a[i];
				else dp[j]=gcd(dp[j],a[i]);
				int k = a[i]/j;
				if(!dp.count(k))dp[k]=a[i];
				else dp[k]=gcd(dp[k], a[i]);
			}
		}
	}
	int mi = inf;
	rep(i,n)mi=min(a[i],mi);
	int ans = 0;
	for(auto e: dp){
		ans += e.first <= mi && e.first == e.second;
	}
	cout<<ans<<endl;
	return 0;
}

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

const int N = 2005;

int n;
int a[N];
map<int , int> mp;

int main()
{
    // freopen(".inp","r",stdin);
    // freopen(".out","w",stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin >> n;
    for (int i = 1 ; i <= n ; ++i) cin >> a[i];
    sort(a + 1 , a + n + 1);

    for (int i = 1 ; i <= n ; ++i)
    {
        for (int j = 1 ; j * j <= a[i] ; ++j)
        {
            if (a[i] % j == 0)
            {
                mp[j] = __gcd(mp[j] , a[i]);
                mp[a[i] / j] = __gcd(mp[a[i] / j] , a[i]);
            }
        }
    }

    int ans = 0;
    for (auto x : mp)
    {
        if (x.first > a[1]) break;
        if (x.first == x.second) ++ans;
    }
    cout << ans;

}


#include <bits/stdc++.h>
#define PB push_back
#define MP(x,y) make_pair(x,y)
#define F first
#define S second
#define LOOP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define lp(i,n) LOOP(i,0,n)
using namespace std;
typedef long long int lli;
typedef pair<int,int> pii;
char grid[2005][2005];
int dp[2005][2005];

int s(char c) {
    return c=='+' ? 1 : -1;
}
int main(){
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
    int h, w;
    scanf("%d%d", &h, &w);
    LOOP(i, 1, h+1) scanf(" %s", grid[i]+1);
    for(int i = h-1; i > 0; --i) dp[i][w] = -dp[i+1][w] + s(grid[i+1][w]);
    for(int j = w-1; j > 0; --j) dp[h][j] = -dp[h][j+1] + s(grid[h][j+1]);
    for(int i = h-1; i > 0; --i)
        for(int j = w-1; j > 0; --j)
            dp[i][j] = max(-dp[i+1][j] + s(grid[i+1][j]), -dp[i][j+1] + s(grid[i][j+1]));
    if (dp[1][1] > 0) printf("Takahashi");
    else if (dp[1][1] < 0) printf("Aoki");
    else printf("Draw");
    return 0;
}

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

using ll = long long;
vector<ll> prime = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71};

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

  ll a, b;
  cin >> a >> b;

  vector<ll> dp((1 << 20), 0);
  dp[0] = 1;
  for (ll i = a; i <= b; i++) {
    ll bit_i = 0;
    for (int j = 0; j < prime.size() /* = 20 */ ; j++) {
      if (i % prime[j] == 0) bit_i |= (1 << j);
    }
    for (int bit = 0; bit < (1 << 20); bit++) {
      if ((bit & bit_i) > 0) continue;
      dp[bit | bit_i] += dp[bit];
    }
  }

  ll ans = 0;
  for (int bit = 0; bit < (1 << 20); bit++) ans += dp[bit];

  cout << ans << '\n';
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define endl "\n"
#define mod 1000000007
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
const int N = 2e5 + 5;
vector<int> adj[N], depth[N];
vector<int> in(N), out(N);
int timer = 0;

void dfs(int node, int par, int d)
{
    in[node] = timer++;
    depth[d].pb(in[node]);
    for (auto child : adj[node])
    {
        if (child != par)
            dfs(child, node, d + 1);
    }
    out[node] = timer++;
}

void solve()
{
    int n, q, x, y;
    cin >> n;
    for (int i = 2; i <= n; i++)
    {
        cin >> x;
        adj[i].pb(x);
        adj[x].pb(i);
    }
    dfs(1, 0, 0);
    cin >> q;
    rep(i, q)
    {
        cin >> x >> y;
        vector<int> v = depth[y];
        auto it = lower_bound(v.begin(), v.end(), out[x]) - lower_bound(v.begin(), v.end(), in[x]);
        cout << it<<endl;
    }
}

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

    return 0;
}

#include <bits/stdc++.h>
#define ll long long
#define V vector<long long>
#define VV vector<vector<long long>>
#define VVV vector<vector<vector<long long>>>
#define P pair<ll,ll>
#define rep(i,n) for(ll (i)=0;(i)<(n);++(i))
#define per(i,n) for(ll (i)=(n)-1;(i)>=0;--(i))
#define all(x) (x).begin(),(x).end()
using namespace std;

std::vector<int> Eratosthenes( const int N )
{
    std::vector<bool> is_prime( N + 1 );
    for( int i = 0; i <= N; i++ )
    {
        is_prime[ i ] = true;
    }
    std::vector<int> p;
    for( int i = 2; i <= N; i++ )
    {
        if( is_prime[ i ] )
        {
            for( int j = 2 * i; j <= N; j += i )
            {
                is_prime[ j ] = false;
            }
            p.emplace_back( i );
        }
    }
    return p;
}

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

int main(){
    ll n;
    cin>>n;
    V x(n);
    rep(i,n)cin>>x[i];
    vector<int> pri=Eratosthenes(50);
    ll N=pri.size();
    ll ans=1e18;
    ll tmp=1;
    for(ll bit=0;bit<(1<<N);bit++){
        ll num=1;
        rep(k,N){
            if(1<<k&bit){
                num*=pri[k];
            }
        }
        if(num==1)continue;
        bool ok=true;
        rep(i,n){
            if(gcd(num,x[i])==1){
                ok=false;
                break;
            }
        }
        if(ok)ans=min(ans,num);
    }
    cout<<ans<<endl;
}


#include <bits/stdc++.h>
#include <iostream>
#include <fstream>
#include <numeric>
#define ll long long
#define mod  1000000007
#define mod1 998244353
using namespace std;
//vector<int>adj[200000+1];
int cnt=0;
vector<int>cand;
int prime[1000001];
  void SieveOfEratosthenes(int n)
{
    memset(prime, 0, sizeof(prime));
    for (int p=2; p*p<=n; p++)
    {
        if (prime[p] == 0 )
        {
            for (int i=p*p; i<=n; i += p)
                if(prime[i]==0)
                prime[i] = p;
        }
    }

}

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

long long mul(long long x, long long y)
{
    return (x * 1ll * y) % mod;
}

long long binpow(long long x, long long y)
{
    long long z = 1ll;
    while(y)
    {
        if(y & 1) z = mul(z, x);
        x = mul(x, x);
        y >>= 1ll;
    }
    return z;
}

long long inv(long long x)
{
    return binpow(x, mod - 2);
}

long long divide(long long x, long long y)
{
    return mul(x, inv(y));
}

long long fact[100001];

void precalc()
{
    fact[0] = 1;
    for(long long i = 1; i < 100001; i++)
        fact[i] = mul(fact[i - 1], i);
      //fact[i]=fact[i-1]*i;
}

long long C(long long n, long long k)
{

    if(n==0)
    return 1;
    return divide(fact[n], mul(fact[k], fact[n - k]));
    //return (fact[n]/(fact[n-k]))/fact[k];
}
long long sub(long long A,long long B)
{
    return (A-B+mod)%mod;
}
// ll segtree[4*200010+1];
// int build(ll A[], ll tl, ll tr, ll v)
// {
//     if(tl==tr)
//     {segtree[v]=A[tl-1]; return 1;}
//     else
//     {
//         ll tm=(tl+tr)/2;
//         int x= build(A,tl,tm,v*2);
//         x= build(A,tm+1,tr,2*v+1);
//         if(x%2==1)
//         segtree[v]=(segtree[2*v]|segtree[2*v+1]);
//         else
//         segtree[v]=(segtree[2*v]^segtree[2*v+1]);
//         return x+1;
//     }
// }
// ll maxival(ll v, ll tl, ll tr, ll l, ll r)
// {
//     if(l>r)
//     return LLONG_MAX;
//     if(l==tl && r==tr)
//     return segtree[v];
//     ll tm=(tl+tr)/2;
//     return min(maxival(2*v,tl,tm,l,min(r,tm)), maxival(2*v+1,tm+1,tr,max(l,tm+1),r));
// }
// int updateval(ll v, ll tl, ll tr, ll pos, ll newval)
// {
//     if(tl==tr)
//     {segtree[v]=newval; return 1;}
//     else
//     {
//         ll tm=(tl+tr)/2;
//         int x;
//         if(pos<=tm)
//         x=updateval(2*v,tl,tm,pos,newval);
//         else
//         x=updateval(2*v+1,tm+1,tr,pos,newval);
//         if(x%2==1)
//         segtree[v]=(segtree[2*v]|segtree[2*v+1]);
//         else
//         segtree[v]=(segtree[2*v]^segtree[2*v+1]);
//         return x+1;
//     }

//  }
// int matrixdim;
// struct M {
//     vector<vector<int>> t;
//     M() {
//         t.resize(matrixdim,vector<int>(matrixdim));
//     }
//     M operator* (const M& b) const {
//         M c = M();
//         int n=matrixdim;
//         for(int i = 0; i < n; ++i) {
//             for(int j = 0; j < n; ++j) {
//                 for(int k = 0; k < n; ++k) {
//                     c.t[i][k]=add(c.t[i][k], mul(b.t[i][j], t[j][k]));
//                 }
//             }1
//         }
//         return c;
//     }
// };

int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cout<<fixed<<setprecision(10);
    cerr<<fixed<<setprecision(5);
    int t=1;
    //SieveOfEratosthenes(1000001);
    //precalc();
    //freopen("input.txt","r",stdin);
    //freopen("output.text","w",stdout);
    // cin>>t;
    while(t--)
    {
        double a,b;
        cin>>a>>b;
        cout<<(a-b)/a*100.0<<"\n";
    }

    return 0;

}

#include<bits/stdc++.h>
using namespace std;
long long arr[1000000];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    long double x,a,b,c;
    cin>>a>>b;
    x=(a-b)*100;
    x=x/a;
    cout<<x<<endl;
}


#include<bits/stdc++.h>
#define gc getchar
#define pc putchar
#define N 200005
#define ll long long
using namespace std;
template<class T>void read(T&x) {
	T f=1;x=0;char c=gc();
	while(c<'0'||c>'9'){if(c=='-')f=-1;c=gc();}
	while('0'<=c&&c<='9'){x=x*10+c-'0';c=gc();}
	x*=f;
}
template<class T>void print(T x,char c) {
	if(x<0)pc('-'),x=-x;int s[20],top=0;
	while(s[++top]=x%10,x/=10);
	while(top)pc(s[top--]+'0');pc(c);
}
int n,m,Q,a[N],b[N],tot,lis[N];
int T[N],X[N],Y[N];
ll sum[N<<2];
int root1,root2,all,lc[N<<2],rc[N<<2],siz[N<<2];
void Insert(int &rt,int l,int r,int pos,int d) {
	if(!rt)rt = ++all;
	sum[rt] += d*lis[pos];siz[rt] += d;
	if(l == r)return;
	int mid = l+r>>1;
	if(pos <= mid)Insert(lc[rt],l,mid,pos,d);
	else Insert(rc[rt],mid+1,r,pos,d);
}
ll moresum(int rt,int l,int r,int pos) {
	if(!rt)return 0;
	if(pos <= l)return sum[rt];
	if(r < pos)return 0;
	int mid = l+r>>1;
	return moresum(lc[rt],l,mid,pos)+moresum(rc[rt],mid+1,r,pos);
}
int lesssiz(int rt,int l,int r,int pos) {
	if(!rt)return 0;
	if(r <= pos)return siz[rt];
	if(pos < l)return 0;
	int mid = l+r>>1;
	return lesssiz(lc[rt],l,mid,pos)+lesssiz(rc[rt],mid+1,r,pos);
}
int main() {
	read(n),read(m),read(Q);lis[tot = 1] = 0;
	for(int i=1; i<=Q; ++i)read(T[i]),read(X[i]),read(Y[i]),lis[++tot] = Y[i];
	sort(lis+1,lis+tot+1);tot = unique(lis+1,lis+tot+1)-lis-1;
	for(int i=1; i<=n; ++i)Insert(root1,1,tot,1,1);
	for(int i=1; i<=m; ++i)Insert(root2,1,tot,1,1);
	ll ans = 0;
	for(int i=1; i<=Q; ++i) {
		int x = X[i],y = lower_bound(lis+1,lis+tot+1,Y[i])-lis;
		if(T[i] == 1) {
			ans -= moresum(root2,1,tot,a[x]);
			ans -= 1ll*lis[a[x]]*lesssiz(root2,1,tot,a[x]-1);
			Insert(root1,1,tot,a[x],-1);Insert(root1,1,tot,a[x] = y,1);
			ans += moresum(root2,1,tot,a[x]);
			ans += 1ll*lis[a[x]]*lesssiz(root2,1,tot,a[x]-1);
		}
		else {
			ans -= moresum(root1,1,tot,b[x]);
			ans -= 1ll*lis[b[x]]*lesssiz(root1,1,tot,b[x]-1);
			Insert(root2,1,tot,b[x],-1);Insert(root2,1,tot,b[x] = y,1);
			ans += moresum(root1,1,tot,b[x]);
			ans += 1ll*lis[b[x]]*lesssiz(root1,1,tot,b[x]-1);
		}
		print(ans,'\n');
	}
}

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

int n,q;
int p[200010],in[200010],out[200010],dep[200010],cnt;
int ver[200010],nxt[200010],head[200010],tot;

vector<int> g[200010];

bool cmp(int x,int y)
{
	return in[x]<in[y];
}

template<typename Tp>
void read(Tp &x)
{
	x=0;int f=1;char c=getchar();
	while(c<'0'||c>'9'){if(c=='-') f=-1;c=getchar();}
	while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}x*=f;
}

void add(int u,int v)
{
	ver[++tot]=v;
	nxt[tot]=head[u];
	head[u]=tot;
}

void dfs(int x,int deep)
{
	in[x]=++cnt;
	dep[x]=deep;
	g[deep].push_back(x);
	for(int i=head[x];i;i=nxt[i])
	{
		int y=ver[i];
		dfs(y,deep+1);
	}
	out[x]=cnt;
}

signed main()
{
	read(n);
	for(int i=2;i<=n;i++)
	{
		read(p[i]);
		add(p[i],i);
	}
	dfs(1,1);
	for(int i=1;i<=n;i++) g[i].push_back(0);
	for(int i=1;i<=n;i++) sort(g[i].begin(),g[i].end(),cmp);
	read(q);
	for(int i=1;i<=q;i++)
	{
		int u,d;
		read(u),read(d);
		if(dep[u]>d+1) printf("0\n");
		else
		{
			d++;
			int l=1,r=g[d].size()-1,ans1=0,ans2=0;
			while(l<=r)
			{
				int mid=(l+r)>>1;
				if(in[g[d][mid]]<in[u]) ans1=mid,l=mid+1;
				else r=mid-1;
			}
			ans1++;
			l=1,r=g[d].size()-1;
			while(l<=r)
			{
				int mid=(l+r)>>1;
				if(in[g[d][mid]]<=out[u]) ans2=mid,l=mid+1;
				else r=mid-1;
			}
			printf("%d\n",ans2-ans1+1);
		}
	}
	return 0;
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_set>
using namespace std;
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define _rep(i, n) _rep2(i, 0, n)
#define _rep2(i, a, b) for(int i = (int)(a); i < (int)(b); i++)
#define rep(...) GET_MACRO(__VA_ARGS__, _rep2, _rep)(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using i64 = long long;
template<class T, class U>
bool chmin(T& a, const U& b) { return (b < a) ? (a = b, true) : false; }
template<class T, class U>
bool chmax(T& a, const U& b) { return (b > a) ? (a = b, true) : false; }

template<typename T>istream& operator>>(istream&i,vector<T>&v){rep(j,v.size())i>>v[j];return i;}
template<typename T>string join(vector<T>&v){stringstream s;rep(i,v.size())s<<' '<<v[i];return s.str().substr(1);}
template<typename T>ostream& operator<<(ostream&o,vector<T>&v){if(v.size())o<<join(v);return o;}
template<typename T>string join(vector<vector<T>>&vv){string s="\n";rep(i,vv.size())s+=join(vv[i])+"\n";return s;}
template<typename T>ostream& operator<<(ostream&o,vector<vector<T>>&vv){if(vv.size())o<<join(vv);return o;}

int main()
{
    i64 k, n, m;
    cin >> k >> n >> m;
    vector<pair<i64, int>> a(k);
    rep(i, k)
    {
        i64 x;
        cin >> x;
        a[i] = {x, i};
    }
    rep(i, k) a[i].first *= m;
    i64 cnt = 0;
    rep(i, k) cnt += a[i].first / n;
    auto x = m - cnt;
    sort(all(a), [&](auto aa, auto b){return aa.first%n > b.first%n;});
    rep(i, k) a[i].first /= n;
    rep(i, x) a[i].first++;
    sort(all(a), [](auto t, auto s){return t.second < s.second;});
    rep(i, k) cout << a[i].first << " ";
}

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

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vll = vector<ll>;
using vvll = vector<vll>;

template <typename T> constexpr int inof(const T x) {
    return static_cast<int>(x);
}
template <typename T> constexpr ll llof(const T x) {
    return static_cast<ll>(x);
}
template <typename T> constexpr double doof(const T x) {
    return static_cast<double>(x);
}

template <typename T> constexpr int sz(const T &xs) { return inof(xs.size()); }

template <typename T> constexpr T div_ceil(const T x, const T y) {
    return x ? (1 + (x - 1) / y) : 0;
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    cin.exceptions(cin.failbit);
    cout << setprecision(9) << fixed;

    int x, y, z;
    cin >> x >> y >> z;

    int ans = div_ceil(z * y, x) - 1;
    cout << ans << '\n';

    return 0;
}


//g++ t.cpp -o t && t < p.txt
//d>p.txt&&t<p.txt&&t2<p.txt

#include <iostream> // プログラムの耳と口
#include <vector> // 超有能配列秘書
#include <algorithm> // みんな大好きソートと二分探索
#include <queue> // きゅーちゃん、だいすき
#include <string> // to_string
#include <functional> // 関数を変数に入れる子
#include <set> // 値の取得・挿入・削除を高速に
#include <map> // setの妹 これまた優秀
#include <random> // 乱択さん
#include <ctime> // へびになりたい
#include <bitset> // へびになった ｼｬｰ
#include <cassert>

// #include <atcoder/all>
// using namespace atcoder; // コンパイル重い
// g++ t.cpp -o t -I .

#define DB cerr<<"D"<<endl

using namespace std; using ll=long long; using ld=long double; const int INF=1e9; const ll LINF=1e18; const double dINF = 1e18; const ld ldINF = 1e18; const double EPS = 1e-6;
using P=pair<ll,ll>;
const ll M = 998244353;
ll mod_pow(ll x, ll a) { ll an = 1; while(a > 0) { if (a&1) an = an * x % M; x = x * x % M; a >>= 1;} return an;}
ll mod_pow(ll x, ll a, ll m) { ll an = 1; while(a > 0) { if (a&1) an = an * x % m; x = x * x % m; a >>= 1;} return an;}
void add(ll& x, ll y) {x+=y; x%=M;}; void mul(ll& x, ll y) {x*=y; x%=M;}; template<typename T, typename U> void chmax(T& x, U y) {if (x<y) x=y;}; template<typename T, typename U> void chmin(T& x, U y) {if (x>y) x=y;} bool vaild(int x, int y, int hh, int ww){return 0<=x&&x<hh&&0<=y&&y<ww;}
ll gcd(ll a, ll b) {if (b==0) return a; else return gcd(b, a%b);}
int keta(ll a) {int res=0; while(a>0) res+=a%10, a/=10; return res;}
const int up[]={1,-1,0,0}, lf[]={0,0,1,-1};

int main() {
  ll a,b,x,y;cin>>a>>b>>x>>y;
  ll p = abs(b-a);
  ll an=x+y*p;
  if (a<b) {
    chmin(an,(2*p+1)*x);
  } else if (a>b) {
    chmin(an,(2*p-1)*x);
    chmin(an,x+y*(p-1));
  }
  cout<<an<<endl;
}

// ・配列の大きさok？　・priority_queueはgreater？　・debug消した？
// ・落ち着いて。提出まで4分待ってね……WJ……1/10……2/10……

// Thank you for making problems and running the contest

#include <cstdio>
#include <iostream>
using namespace std;
#define REP(i, n) for(int i = 0; i < (int)(n); ++i)
typedef long long ll;

const int nQuery = 1000;

int main(void) {
  REP(iQuery, nQuery) {
    int r1, c1, r2, c2;
    scanf("%d%d%d%d", &r1, &c1, &r2, &c2);
    --r1;
    --c1;
    --r2;
    --c2;
    while(r1 < r2) {
      printf("D");
      ++r1;
    }
    while(r1 > r2) {
      printf("U");
      --r1;
    }
    while(c1 < c2) {
      printf("R");
      ++c1;
    }
    while(c1 > c2) {
      printf("L");
      --c1;
    }
    printf("\n");
    fflush(stdout);
    int d = 0;
    scanf("%d", &d);
  }
  return 0;
}


// https://atcoder.jp/contests/abc203/editorial/2008
#include <bits/stdc++.h>
using namespace std;

#define N 805
#define MAX_A 1000000000
#define rep(i, n) for(int i = 0; i < n; ++i)

int main(void) {
	int n, k, lim;
	int a[N][N];
	int s[N][N];
	rep(i, N) {
		s[i][0] = 0;
		s[0][i] = 0;
	}
	int ng = -1;
	int ok = MAX_A;
	int mid;
	bool ext;
	cin >> n >> k;
	lim = ((k*k) / 2) + 1;
	rep(i, n) {
		rep(j, n)cin >> a[i][j];
	}
	while ((ng + 1) < ok) {
		mid = (ng + ok) / 2;
		rep(i, n) {
			rep(j, n) {
				s[i + 1][j + 1] = s[i + 1][j] + s[i][j + 1] - s[i][j];
				if (a[i][j] > mid)s[i + 1][j + 1]++;
			}
		}
		ext = false;
		rep(i, n - k + 1) {
			rep(j, n - k + 1) {
				if ((s[i + k][j + k] + s[i][j] - s[i][j + k] - s[i + k][j]) < lim)ext = true;
			}
		}
		if (ext)ok = mid;
		else ng = mid;
	}
	cout << ok << endl;
	return 0;
}

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

int main() {

	int a, b, as = 0, bs = 0;
	cin >> a >> b;

	as += a / 100;
	bs += b / 100;
	a = a % 100;
	b = b % 100;
	as += a / 10;
	as += a % 10;
	bs += b / 10;
	bs += b % 10;

	if (as > bs)
		cout << as;
	else
		cout << bs;

	return 0;
}


#include <iostream>
#include <vector>

using namespace std;

typedef long long LL;

LL process(vector<pair<int, LL>>& a, int n) {
  if (n == 2) {
    return min(a[0].second, a[1].second) == a[0].second ? a[0].first : a[1].first;
  }
  for (int i = 0, j = 0; i < n - 1; i += 2, j++) {
    a[j] = max(a[i].second, a[i + 1].second) == a[i].second ? a[i] : a[i + 1];
  }
  n /= 2;
  return process(a, n);
}

int main() {
  int n;
  vector<pair<int, LL>> a;
  LL val;

  cin >> n;
  n = 1 << n;

  for (int i = 0; i < n; i++) {
  	cin >> val;
    a.push_back(make_pair(i + 1, val));
  }

  cout << process(a, n) << endl;

  return 0;
}

#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
using namespace std;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
#define square(x) pow(x,2.0)
#define all(x) (x).begin(),(x).end()
#define F first
#define S second
#define max_queue(x) priority_queue<x>
#define min_queue(x) priority_queue<x, vector<x>, greater<x>>
#define big_sort(v) sort(all(v), [](int a, int b) { return a > b; })//大きい順にソート
using ll = long long;
using vi = vector<ll>;
using vvi = vector<vi>;
using vs = vector<string>;
using vd = vector<long double>;
#define co(x) cout << x << endl;
#define cins(n,v) rep(i,n){cin >> v.at(i);}
#define couts(v) rep(i,v.size()){cout << v.at(i) << endl;}
// aよりもbが大きいならばaをbで更新する
// (更新されたならばtrueを返す)
template <typename T>
bool chmax(T &a, const T& b) {
  if (a < b) {
    a = b;  // aをbで更新
    return true;
  }
  return false;
}
// aよりもbが小さいならばaをbで更新する
// (更新されたならばtrueを返す)
template <typename T>
bool chmin(T &a, const T& b) {
  if (a > b) {
    a = b;  // aをbで更新
    return true;
  }
  return false;
}

/*
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
*/

int main() {
  // srand((unsigned)time(NULL));
  // set<int> st;
  ll N,n=1,a,b;
  cin >> N;
  rep(i, N){
    n*=2;
  }
  queue<ll> surv;
  std::map<ll, ll> mp;
  rep(i,n){
    cin >> a;
    mp[a]=i;
    surv.push(a);
  }
  while(surv.size()!=1){
    a=surv.front();
    surv.pop();
    b=surv.front();
    surv.pop();
    surv.push(max(a,b));
  }

  cout << mp[min(a,b)]+1 << endl;
  return 0;
}


/*NEVER GIVE UP*/
/*First try brute force and check if it is in the range of time complexity*/
/*Then try to solve the problem in mathematical form*/
/*Then try to imagine how the final ans will look like*/
/*Write on paper if still not able to get*/
/*Try to think diff. algorithms and data structures that could solve the problem*/
#include <bits/stdc++.h>
using namespace std;
long long power(long long x, long long y, long long p)
{
    long long res = 1;     // Initialize result

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

    if (x == 0) return 0; // In case x is divisible by p;

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

        // y must be even now
        y = y>>1; // y = y/2
        x = (x*x) % p;
    }
    return res;
}
int main()
{long long n=0;
    cin>>n;
    cout<<n-1;
}


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

ll ll_pow(ll a, ll n) {
  ll ans = 1;
  for(ll i = 0; i < n; i++) ans *= a;
  return ans;
}

int main() {
  string s;
  int ans = 0;
  cin >> s;

  vi num(10);
  vi num_n(10);

  rep(i,10){
    if(s[i] == 'o') num[i]=2;
    else if(s[i] == '?') num[i]=1;
    else num[i] = 0;
  }

  rep(i,10000){

    rep(k,10) num_n[k] = 0;

    num_n[(i/1000)%10]++;
    num_n[(i/100)%10]++;
    num_n[(i/10)%10]++;
    num_n[i%10]++;

    int flag = 0;
    flag = 0;

  rep(k,10){
        if(num_n[k] > 0 && num[k] == 0) flag = 1;
        if(num_n[k] == 0 && num[k] == 2) flag = 1;
    }
     if(flag == 0) ans++;
  }
  cout << ans << endl;
  return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define MOD 1000000007
#define pd push_back
#define sort(v) sort(v.begin(),v.end())
#define Sort(v) sort(v.begin(),v.end(),greater<int>())
#define display(s) for(auto val:s) cout<<val<<" "; cout<<endl;
#define displayMap(mymap) for(auto val:mymap) cout<<val.first<<" -> "<<val.second<<endl;
#define Sortcom(v) sort(v.begin(),v.end(),mycompare)
#define f(a,b,c) for(int i=a;i<b;i+=c)

bool mycompare(int a,int b)
{
    return a<b;
}

int mod(int x)
{
    return (x%MOD+MOD)%MOD;
}
int mul(int a,int b)
{return mod(mod(a)*mod(b));}
int add(int a,int b)
{return mod(mod(a)+mod(b));}

int power(int a,int b)
{
    if(b==0)
    return 1;
    int smallans = power(a,b/2);
    smallans = smallans * smallans;
    if(b&1)
    smallans = smallans * a;
    return smallans;
}

bool check(string s, int i,int j,int k, int l)
{
    map<int,int> mymap;
    mymap[i]++; mymap[j]++; mymap[k]++; mymap[l]++;
    for(int i=0;s[i];i++)
    {
        if(s[i] == 'o' && mymap[i]<=0)
        return false;
        if(s[i] == 'x' && mymap[i]>0)
        return false;
    }
    return true;
}

int solve(){
    string s; cin>>s;
    int a=0,b=0,c=0;
    for(int i=0;s[i];i++)
    {
        if(s[i] == 'o') a++;
        else if(s[i] == 'x') b++;
        else c++;
    }
    int ans = 0;
    if(a>4) return ans;
    for(int i=0;i<10;i++)
    {
        for(int j=0;j<10;j++)
        {
            for(int k=0;k<10;k++)
            {
                for(int l = 0;l<10;l++)
                {
                    if(check(s,i,j,k,l))
                    ans++;
                }
            }
        }
    }
    return ans;
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;  t=1;
    // int t; cin>>t;
    while(t--)
    {
        // solve();
        cout<<solve()<<endl;
    }
    return 0;
}
/*
if((s[i] == 'o' || s[i] == '?') && (s[j] == 'o' || s[j] == '?') &&
                    (s[k] == 'o' || s[k] == '?') && (s[l] == 'o' || s[l] == '?'))
*/


#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#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;
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;}

struct Edge{
    int to,id;
    ll dist;
    Edge(int to,ll dist=1,int id=0):to(to),dist(dist),id(id){}
};

#include <functional>

template< typename T, typename S >
struct LazySegmentTree{
    int n;
    vector<T> data;
    vector<S> lazy;
    T te;
    S se;

    inline void merge_functions(S& lazy,S& val){
        lazy+=val;
        //lazy=val;
    }
    inline void operate(T& data,S& val,int len){
        data+=val*len;
        //data=val*len;
        //data+=val;
        //data=val;
    }
    inline T merge_values(T& x,T& y){
        return x+y;
        //return min(x,y);
        //return max(x,y);
    }

    LazySegmentTree(){}
    LazySegmentTree(int sz,T te,S se):te(te),se(se){
        n=1;
        while(n<sz) n*=2;
        data.resize(2*n-1,te);
        lazy.resize(2*n-1,se);
    }

    void build(const vector<T> &A){
        for(int k=0;k<int(A.size());k++) data[k+n-1]=A[k];
        for(int k=n-2;k>=0;k--) data[k]=merge_values(data[2*k+1],data[2*k+2]);
    }

    void eval(int k,int l,int r){
        if(lazy[k]==se) return;
        operate(data[k],lazy[k],r-l);
        if(r-l>1){
            merge_functions(lazy[2*k+1],lazy[k]);
            merge_functions(lazy[2*k+2],lazy[k]);
        }
        lazy[k]=se;
    }

    void update(int a,int b,S val,int k=0,int l=0,int r=-1){
        if(r<0) r=n;
        eval(k,l,r);
        if(b<=l||r<=a) return;
        if(a<=l&&r<=b){
            merge_functions(lazy[k],val);
            eval(k,l,r);
        }else{
            update(a,b,val,2*k+1,l,(l+r)/2);
            update(a,b,val,2*k+2,(l+r)/2,r);
            data[k]=merge_values(data[2*k+1],data[2*k+2]);
        }
    }

    T query(int a,int b,int k=0,int l=0,int r=-1){
        if(r<0) r=n;
        eval(k,l,r);
        if(b<=l||r<=a) return te;
        if(a<=l&&r<=b) return data[k];

        T vl=query(a,b,2*k+1,l,(l+r)/2);
        T vr=query(a,b,2*k+2,(l+r)/2,r);
        return merge_values(vl,vr);
    }

    T get(int p){
        return query(p,p+1);
    }
};

/*
  LazySegmentTree<ll,ll> seg(N,te,se);
  vector<ll> init(N,0);
  seg.build(init);
*/

vector<int> in,out;
vector<int> dep;

void dfs(int now,int par,int &cnt,vector<vector<Edge>> &g,int d){
  in[now]=cnt++;
  dep[now]=d;
  for(auto e:g[now]){
    int nex=e.to;
    if(nex==par) continue;
    dfs(nex,now,cnt,g,d+1);
  }
  out[now]=cnt;
}

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

  int N;
  cin>>N;
  vector<int> A(N-1),B(N-1);
  rep(i,N-1) cin>>A[i]>>B[i];
  rep(i,N-1){
    A[i]--;B[i]--;
  }

  vector<vector<Edge>> g(N);
  rep(i,N-1){
    g[A[i]].emplace_back(Edge{B[i],1,i});
    g[B[i]].emplace_back(Edge{A[i],1,i});
  }

  LazySegmentTree<ll,ll> seg(N,0,0);
  seg.build(vector<ll> (N,0));
  int cnt=0;
  in.resize(N,0);
  out.resize(N,0);
  dep.resize(N,0);
  dfs(0,-1,cnt,g,0);

  int Q;
  cin>>Q;
  rep(q,Q){
    int T,E;
    ll X;
    cin>>T>>E>>X;
    E--;
    int a=A[E];
    int b=B[E];
    if(T==2) swap(a,b);
    if(true){
      if(dep[a]<dep[b]){
        seg.update(0,in[b],X);
        seg.update(out[b],N,X);
      }else{
        seg.update(in[a],out[a],X);
      }
    }/*else{
      if(hld.dep[a]<hld.dep[b]){
        seg.update(in[b],out[b],X);
      }else{
        seg.update(0,in[a],X);
        seg.update(out[a],N,X);
      }
    }*/
  }

  vector<ll> C(N);
  rep(i,N) C[i]=seg.query(in[i],in[i]+1);
  rep(i,N) cout<<C[i]<<"\n";

  return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ff first
#define ss second
#define mp make_pair
typedef long long int ll;
vector<ll> v[200000+5],par(200000+5),val(200000+5,0);
ll vis1[200000+5]={0};
ll vis[200000+5]={0};
void pdfs(int p){
    vis1[p]=1;
    for(auto i:v[p]){
        if(vis1[i]==0){
            par[i]=p;
            pdfs(i);
        }
    }
}
void vdfs(int p){
    vis[p]=1;
    for(auto i:v[p]){
        if(vis[i]==0){
            val[i]+=val[p];
            vdfs(i);
        }
    }
}

int main()
{
    ll n;cin>>n;
    vector<pair<ll,ll>> v0;
    for(int i=1;i<n;i++){
        ll a,b;cin>>a>>b;
        v0.pb(mp(a,b));
        v[a].pb(b);
        v[b].pb(a);
    }
    pdfs(1);
    par[1]=0;
    ll q;cin>>q;
    while(q--){
        ll t,e,x;cin>>t>>e>>x;
        ll a=v0[e-1].ff;
        ll b=v0[e-1].ss;
        if(a==par[b]){
            if(t==1){
                val[b]-=x;
                val[1]+=x;
            }
            else{
                val[b]+=x;
            }
        }
        else{
            if(t==1){
                val[a]+=x;
            }
            else{
                val[1]+=x;
                val[a]-=x;
            }
        }
    }
    vdfs(1);
    for(int i=1;i<=n;i++){
        cout<<val[i]<<endl;
    }
}


/*
“The only way that we can live is if we grow.
 The only way we can grow is if we change.
The only way we can change is if we learn.
 The only way we can learn is if we are exposed.
 And the only way that we are exposed is if we throw ourselves into the open.”
 ***************************************   ************************   **********************************
*/
#include <bits/stdc++.h>
using namespace std;

//---------------------mishra ka idea------------------
#define int         long long
#define double      long double
#define pb          push_back
#define pf          push_front
#define pii         pair<int,int>
#define vi          vector<int>
#define vii         vector<pii>
#define all(a)      (a).begin(),(a).end()
#define rall(a)     (a).rbegin(),(a).rend()
#define x           first
#define y           second
#define endl        '\n'
#define sz(x)     (int)(x).size()
#define ull       unsigned long long
//------------------------mishra ka idea-------------------

//-------------------------alsi hu mai-----------------------
#define fo(i,l,u)   for(i=l;i<u;i++)
#define rfo(i,l,u)  for(i=l;i>=u;i--)
#define allfo(s)    for(auto it=(s).begin();it!=(s).end();it++)
#define _init(b)     memset(b,-1,sizeof(b))
#define _init0(b)     memset(b,0,sizeof(b))
#define MOD         1000000007
#define hell        998244353
#define output(x) cout << (x ? "YES" : "NO")<<endl;
#define Output(x) cout << (x ? "Yes" : "No")<<endl;
//-------------------------alsi hu mai-----------------------

//--------------------het se uthaya---------------------
#define MOD2 (998244353)
#define MOD3 (1000000009)
#define PI acos(-1)
#define eps (1e-8)
#define INF (1e18)

template<class A,class B>ostream&operator<<(ostream&out,const pair<A,B>&a){return out<<"("<<a.first<<","<<a.second<<")";}
template<class A>ostream&operator<<(ostream&out,const vector<A>&a){for(const A &it:a)out<<it<<" ";return out;}
template<class A,class B>istream&operator>>(istream&in,pair<A,B>&a){return in>>a.first>>a.second;}
template<class A>istream&operator>>(istream&in,vector<A>&a){for(A &i:a)in>>i;return in;}
ifstream cinn("in.txt");ofstream coutt("out.txt");
int poww(const int &a,int b,const int &m=MOD){if(b==0)return 1;int x=poww(a,b/2,m);x=x*x%m;if(b&1)x=x*a%m;return x;}

//--------------------het se uthaya-------------------------------------------------------------------

int gcd(int a, int b) {if(a>b)swap(a,b) ;  if (a == 0)  return b; return gcd(b % a, a); }
int rint(int  l , int r){return rand()%(r-l+1) + l ;}

//---------------------------aab code dekho bahut template dekh liya---------------------------------

signed main()
{
   //freopen("input.txt", "r", stdin);
   //freopen("output.txt", "w", stdout);
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
   int i,j,x;
   vi v;
   vi vv;
   fo(i,0,4){
    cin>>x;
    v.pb(x);
    vv.pb(x);
   }
   sort(all(v));
   int sum = 0;
   fo(i,0,(1<<4)){
    int a = 0 , b =0 ;
    fo(j,0,4){
      if(i&(1<<j)){
        a+=v[j];
      }
      else{
        b+=v[j];
      }
      if(a==b){
        cout<<"Yes"<<endl;
        return 0;
      }
    }
   }

cout<<"No"<<endl;

    return 0;
}

//----------------abc tez kar liya iska matlab ye nahi tum legend ho kya pata hagdo aage ------------------

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

using namespace std;

#define ll long long

// const ll p = 998244353;
const ll nax = 100001;

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

bool find_any_solution (ll a, ll b, ll c, ll & x0, ll & y0, ll & g) {
	g = gcd (abs(a), abs(b), x0, y0);
	if (c % g != 0)
		return false;
	b /= g;
	x0 *= c / g;
	y0 *= c / g;
	x0 %= b;
	if (x0 < 0) x0 += b;
	if (a < 0)   x0 *= -1;
	if (b < 0)   y0 *= -1;
	return true;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int t;
	cin >> t;
	while (t--) {
		ll x, y, p, q, ans = INT64_MAX;
		cin >> x >> y >> p >> q;
		for (int i = 0; i < y; ++i) {
			for (int j = 0; j < q; ++j) {
				ll f, s, g;
				if (find_any_solution(2 * (x + y), p + q, p + j - x - i, f, s, g)) {
					ans = min(ans, 2 * f * (x + y) + x + i);
				}
			}
		}
		if (ans == INT64_MAX) cout << "infinity\n";
		else cout << ans << '\n';
	}
}


#include <bits/stdc++.h>
using namespace std;
typedef bool boool;
typedef long long ll;
#define vl vector<ll>
#define vb vector<boool>
#define vs vector<string>
#define vp vector<pair<ll, ll>>
#define vvl vector<vector<ll>>
#define vvp vector<vector<pair<ll, ll>>>
#define mod 1000000007
#define all(x) x.begin(), x.end()
#define rep1(i, n) for (long long i=0; i<(long long)(n); i++)
#define rep2(i, s, e) for (long long i=(s); i<(long long)(e); i++)
#define GET_MACRO(_1,_2,_3,NAME,...) NAME
#define rep(...) GET_MACRO(__VA_ARGS__, rep2, rep1)(__VA_ARGS__)

int main() {
  int h, w;
  cin >> h >> w;
  vector<string> vec(h, "");
  vector<string> a(h, "");
  rep(i, h){
    rep(j, w){
      vec[i]+=".";
      a[i]+=".";
    }
  }
  int n, m;
  cin >> n >> m;
  rep(i, n){
    int y, x;
    cin >> y >> x;
    x--; y--;
    vec[y][x] = 'o';
  }
  rep(i, m){
    int y, x;
    cin >> y >> x;
    x--; y--;
    vec[y][x] = 'x';
  }

  rep(i, h){
    bool light = false;
    int num = 0;
    rep(j, w){
       if(vec[i][j]=='o'){
         light = true;
         num++;
       }else if(vec[i][j]=='x'){
         if(light){
           rep(k, 1, num+1) a[i][j-k] ='p';
           light = false;
           num = 0;
         }else{
           num = 0;
         }
       }else{
         num++;
       }
    }
    if(light) rep(k, 1, num+1) a[i][w-k] ='p';
  }
  rep(i, w){
    bool light = false;
    int num = 0;
    rep(j, h){
       if(vec[j][i]=='o'){
         light = true;
         num++;
       }else if(vec[j][i]=='x'){
         if(light){
           rep(k, 1, num+1) a[j-k][i] ='p';
           light = false;
           num = 0;
         }else{
           num = 0;
         }
       }else{
         num++;
       }
    }
    if(light) rep(k, 1, num+1) a[h-k][i] ='p';
  }

  int ans = 0;
  rep(i, h){
    rep(j, w){
      if(a[i][j]=='p') ans++;
    }
  }
  cout << ans << endl;
  return 0;
}

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

// bitでフラグ管理
enum Flag{
	NONE   = 0b000, // なし
	BRIGHT = 0b001, // 明るい
	BULB   = 0b010, // 電球
	BLOCK  = 0b100, // ブロック
};

int main(){
	int H, W, N, M; cin>>H>>W>>N>>M;
	vector<vi> grid(H, vi(W));
	rep(i, N){
		int a, b; cin>>a>>b;
		grid[a-1][b-1] = BULB;
	}
	rep(i, M){
		int c, d; cin>>c>>d;
		grid[c-1][d-1] = BLOCK;
	}
	// 座標変換で回転
	vi I(H); rep(i, H) I[i] = i;
	vi J(W); rep(i, W) J[i] = i;
	rep(t, 4){
		for(int i:I){ // 90° 回転 * 4
			int f = NONE; // flag
			for(int j:J){
				int h = i, w = j;
				if(t%2) swap(h, w); // (i,j) -> (h,w)
				// grid 設定 / フラグ更新
				if(grid[h][w] & BULB) f = BRIGHT;
				if(grid[h][w] & BLOCK) f = NONE;
				grid[h][w] |= f;
			}
		}
		// 90° 回転
		swap(I, J); // 45° 線対称
		reverse(J.begin(), J.end()); // 軸線対称
	}
	int ans = 0;
	for(vi v:grid) for(int x:v) if(x & BRIGHT) ans++;
	cout<< ans <<endl;
}


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

using namespace std;

#define pb push_back
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
const int seed=233;
const int N=4e4+10;

char win[200][200];

int main()
{
    win['R']['R'] = win['R']['S'] = win['S']['R'] = 'R';
    win['S']['S'] = win['S']['P'] = win['P']['S'] = 'S';
    win['P']['P'] = win['P']['R'] = win['R']['P'] = 'P';
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int n,m;
    string s,t;
    cin>>n>>m>>s;
    while(m--){
        t=s+s;
        for(int i=0;i<n;i++) s[i]=win[t[i*2]][t[i*2+1]];
    }
    cout<<s[0]<<endl;
    return 0;
}

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

#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()
#define pb push_back
#define f first
#define s second
#define nl "\n"
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int MOD = 1e9+7;
template<class T> using pqg = priority_queue<T,vector<T>,greater<T>>;

char w[101];
int n, k;
string s;

char win(char a, char b){ // P R S
  if(b<a) swap(a, b);
  if(a=='R') return 'R';
  if(a=='P' && b=='R') return 'P';
  else if(a=='P' && b=='S') return 'S';
  return a;
}

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

  cin >> n >> k >> s;
  rep(i, 0, n-1) w[i]=s[i];
  int pt=1;
  rep(len, 1, k){
    vi v(n);
    rep(i, 0, n-1){
      v[i]=win(w[i], w[(i+pt+n)%n]);
    }
    rep(i, 0, n-1) w[i]=v[i];
    pt=(pt*2)%n;
  }
  cout << w[0];
}


#define _CRT_SECURE_NO_WARNINGS
#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 <string> vS; typedef vector <vector <int>> vv; typedef map<int, int> mi; typedef map<string, int> ms; typedef map<char, int> mc; typedef string S; typedef map <ll, ll> ml; typedef map <int, bool> mb; typedef vector <ld> vld; typedef vector <bool> vb; typedef map<ll, ll> mll;
#define FOR(i,N) for(int i = 0 ; i < N;i++)
#define eFOR(i,a,b) for(int i = a; i <=b;i++)
#define dFOR(i,N) for(int i = N - 1; i>=0;i--)
#define edFOR(i,a,b) for(int i = b ; i >=a;i--)
#define all(x) x.begin(),x.end()
#define SORT(x) sort(all(x))
#define RSORT(x) sort(x.rbegin(),x.rend())
#define mine(x) min_element(all(x))
#define maxe(x) max_element(all(x))
#define pb push_back
#define PI 3.14159265359
const int mod = 1e9 + 7;
int CASE = 1;
ll __gcd(ll a, ll b)
{
	return !b ? a : __gcd(b, a % b);
}
void solve()
{
	int n;
	cin >> n;
	mb mp;
	int k = 0;
	FOR(i, n)
	{
		int x;
		cin >> x;
		mp[x] = true;
		while (mp[k])
			k++;
		cout << k << endl;
	}
}
int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int T = 1;
	while (T--)
	{
		solve();
	}
}

#include<algorithm>
#include<cstdio>
#define MaxN 200005
using namespace std;
struct Data{int u,v;}ans[MaxN];
int tot;
bool vis[MaxN];
int a[MaxN],b[MaxN],p[MaxN];
int st[MaxN],tn;
bool cmp(int A,int B)
{return a[A]>a[B];}
void solve(int u)
{
  st[tn=1]=u;
  for (int v=p[u];v!=u;v=p[v])st[++tn]=v;
  sort(st+1,st+tn+1,cmp);
  for (int k=1;k<=tn;k++){
    int i=st[k];
    if (p[i]==i)continue;
    while(p[i]!=i){
      ans[++tot]=(Data){i,p[i]};
      swap(p[i],p[p[i]]);
    }
  }
}
int n;
int main()
{
  scanf("%d",&n);
  for (int i=1;i<=n;i++)scanf("%d",&a[i]);
  for (int i=1;i<=n;i++)scanf("%d",&b[i]);
  for (int i=1;i<=n;i++)scanf("%d",&p[i]);
  for (int i=1;i<=n;i++)
    if (b[p[i]]>=a[i]&&p[i]!=i)
      {puts("-1");return 0;}
  for (int i=1;i<=n;i++){
    if (vis[i])continue;
    solve(i);
  }
  printf("%d\n",tot);
  for (int i=1;i<=tot;i++)
    printf("%d %d\n",ans[i].u,ans[i].v);
  return 0;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<bits/stdc++.h>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<bitset>
#include<unordered_map>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fio ios_base::sync_with_stdio(false)
#define li long long int
#define ll long long int
//#define readi(x) scanf("%lld",&x)
#define  reads(x)  scanf("%s", x)
#define readl(x) scanf("%lld",&x)
#define rep(i,n) for(i=0;i<n;i++)
#define revp(i,n) for(i=(n-1);i>=0;i--)
#define myrep1(i,a,b) for(i=a;i<=b;i++)
#define myrep2(i,a,b) for(i=b;i>=a;i--)
#define pb push_back
#define mp make_pair
#define fi first
#define sec second
#define MAXN 100000000000000005
#define MINN -10000000000000000
#define INTMAX 1000001000
#define pii pair

#define pdd pair<double,double>
#define pic pair<int,char>
//#define N 5005
#define lgn 20
#define ddouble long double
#define minus minu
#define PI 3.1415926535
#define lgn 20
using namespace std;
//li bit[200004]={0};
li mod=1000000007;
li mod2=998244353;

/*
void update(li x,li n,li val){//cout<<(x-1)<<" "<<val<<endl;
    while(x<=n){bit[x]+=val;x=x+((-x)&x);}}

li query(li x,li n){li c=0;while(x>0){c+=bit[x];x=x-((-x)&x);}return(c);}

li pp(li a,li b,li modd){
    if(b==0) return(1);
    li ans=pp(a,b/2,modd);ans=(ans*ans)%modd;if(b%2) ans=(ans*a)%modd;return(ans);
}
*/
li gcd(li a,li b){
    if(a<b) return(gcd(b,a));
    if(b==0) return(a);
    return(gcd(b,a%b));
}

vector
 z_function(string s)
{
     li n=s.size();
     vector
z(n);
     for(li i=1,l=0,r=0;i<n;i++)
     {
         if(i<=r)
         {
             z[i]=min(r-i+1,z[i-l]);
         }
         while((i+z[i])<n && s[z[i]]==s[i+z[i]]){++z[i];}
         if((i+z[i]-1)>r){l=i;r=i+z[i]-1;}
     }
     return(z);
}

/*vector
p(2005,0);
li parent(li a){
    if(p[a]==0 || p[a]==a){p[a]=a;return(a);}
    return(p[a]=parent(p[a]));
}
void unionn(li a,li b){p[a]=b;}*/
/*vector<pii>MM;li N;*/
/*li ff(li i,li j,vector<vector
>& dp)
{
    if(i==0 && j==0)
    {
        return(1);
    }
    if(dp[i][j]!=-1) return(dp[i][j]);
    li ans=0;
    if(i)
    {
        ans+=ff(i-1,j,dp);
    }
    if(j)
    {
        ans+=ff(i,j-1,dp);
    }
    dp[i][j]=ans;
    return(ans);
}*/

li DFS(li i,li p,vector<vector
>& M,vector
& D,vector
& C)
{
    if(p==0) D[i]=1;
    else D[i]=D[p]+1;
    li ans=0,y=1;
    for(li c=0;c<3;c++)
    {
        if(p!=0 && C[p]==c) continue;
        C[i]=c;
        y=1;
        for(auto x:M[i])
        {
            if(x!=p)
            {
                if(D[x]==-1 || D[x]==(D[i]+1))
                {
                    y=y*(DFS(x,i,M,D,C));
                }
                else if(D[x]<D[i])
                {
                    if(C[i]==C[x]) {y=0;break;}
                }
            }
        }
        //C[i]=-1;
        //if(i==1) cout<<y<<" "<<c<<endl;
        ans=ans+y;
    }
    return(ans);
}

int main()
{
li i,j,a,b,c,d,g,h,k,m,n,q,s,t,x,y,z,X,Y,sz,st,end,mid,ans,W,pa,pb,l,r,p,M;
    string T,SN,S,TN;
    //char S1[100004];

    n=1000005;
    /*vector
v(n+2,0);
    for(i=2;i<=n;i++)
    {
        if(v[i]==0)
        {
            for(j=i;j<=n;j+=i)
            {
                v[j]=i;
            }
            //cout<<v[i]<<" ";
        }
    }*/
    t=1;
    //readl(t);
    x=0;
    while(t--)
    {
        readl(n);readl(m);
        vector<vector
>M(n+2);
        vector
D(n+2,-1);
        vector
C(n+2,-1);
        for(i=0;i<m;i++)
        {
            readl(a);readl(b);
            M[a].pb(b);
            M[b].pb(a);
        }
        ans=1;
        for(i=1;i<=n;i++)
        {
            //cout<<D[i]<<endl;
            if(D[i]==-1)
            {
                //cout<<i<<endl;
                ans=ans*DFS(i,0,M,D,C);
            }
        }
        cout<<ans<<endl;
        //return(0);
        //x++;
        //cout<<"Case #"<<x<<": "<<
    }
}

///Given threee color R,G,B .Now find the number of ways of coloring the n nodes and m edges graph using these color
///where no adjacent nodes can be same color
///Here n<=10
///Idea : As n<=10 so ideal backtracking can be done complexity:3*(2^10)

#include<bits/stdc++.h>
using namespace std;
#define   ll long long int
#define pb push_back
ll lvl[21];
ll color[21];
vector<ll>adj[21];
vector<ll>vis(21,0);
ll solve(ll src)
{
    vis[src]=1;
    ll ret=1;
        for(auto x:adj[src])
        {
            if(lvl[x]!=0&&lvl[x]!=lvl[src]+1)
            {
                if(color[src]==color[x])
                {
                  ret=0;
                }
                continue;
            }

            if(lvl[x]==0)
            lvl[x]=lvl[src]+1;
            ll tot=0;
            for(ll i=1;i<=3;i++)
            {
                if(i==color[src])
                    continue;

                color[x]=i;
                tot+=solve(x);
                color[x]=0;
            }
            ret=ret*tot;
     }

   return ret;

}
int main()
{
  ll n,m;
  cin>>n>>m;

  for(ll i=0;i<m;i++)
  {
      ll u,v;
      cin>>u>>v;
      adj[u].pb(v);
      adj[v].pb(u);
  }

ll ans=1;

for(ll i=1;i<=n;i++)
{
    if(vis[i])
        continue;
    lvl[i]=1;
    color[i]=1;
    ans=3*ans*solve(i);
}

cout<<ans;

return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define ll long long
#define forn(i,a,n,b) for(ll i=a;i<n;i+=b)
#define forr(i,a,n,b) for(ll i=a;i>=n;i-=b)
#define MOD 1000000007
#define vll vector<ll>
using namespace std;

int main()
{
    fastio;
    ll t = 1;
    //cin>>t;
    while(t-->0)
    {
         ll h, w, sum = 0, ans = 0, m = LONG_LONG_MAX;
         cin>>h>>w;
         ll arr[h][w];
         forn(i,0,h,1)
         {
             forn(j,0,w,1)
             {
                 cin>>arr[i][j];
                 sum += arr[i][j];
                 m = min(m, arr[i][j]);
             }
         }

         cout<<sum - h * w * m;

    }
    return 0;
}

#ifdef __LOCAL
#define _GLIBCXX_DEBUG
#endif

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

#pragma region macros

using str=string;
using ll=long long;
using vl=vector<ll>;
using vc=vector<char>;
using pl=pair<ll,ll>;
using ml=map<ll,ll>;
using sl=set<ll>;
template<class T> using V=vector<T>;
template<class T,class U> using P=pair<T,U>;

#define FOR(n) for(ll i=0;i<n;i++)
#define rep(i,n) for(ll i=0;i<n;i++)
#define reps(i,n) for(ll i=1;i<n;i++)
#define REP(i,m,n) for(ll i=m;i<n;i++)
#define drep(i,n) for(ll i=n-1;i>=0;i--)
#define fore(n) for(auto&& i:n)
#define fors(n) for(auto&&[i,j]:n)
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define sor(v) sort(all(v))
#define rsor(v) sort(rall(v))
#define rev(v) reverse(all(v))
#define low(v,x) lower_bound(all(v),x)-v.begin()
#define up(v,x) upper_bound(all(v),x)-v.begin()
#define acc(v) accumulate(all(v),0LL)
#define ef(x) emplace_front(x)
#define eb(x) emplace_back(x)
#define pf() pop_front()
#define pb() pop_back()
#define mp(a,b) make_pair(a,b)
#define ceil(a,b) (a+b-1)/b
#define bit(n) (1LL<<n)

#define fi first
#define se second
#define fr front()
#define ba back()
#define be begin()
#define en end()
#define br break
#define cn continue
#define wh while
#define el else if
#define re return

const ll inf=1LL<<60;
const ll mod=1000000007;
const ll MOD=998244353;
const double pi=3.1415926535;
const double eps=1e-10;
const ll dx[4]={1,0,-1,0};
const ll dy[4]={0,1,0,-1};

void input(){}
void inputs(){}
void output(){}
template<class T,class...U> void input(T& x,U&...y){cin>>x;input(y...);}
template<class...T> void in(T&...x){input(x...);}
template<class T> void inputs(const ll t,T& x){cin>>x[t];}
template<class T,class...U> void inputs(const ll t,T& x,U&...y){cin>>x[t];inputs(t,y...);}
template<class T,class...U> void inputs(T& x,U&...y){rep(t,size(x))inputs(t,x,y...);}
template<class...T> void ins(T&...x){inputs(x...);}
template<class T,class...U> void output(T x,U...y){cout<<x<<"\n";output(y...);}
template<class...T> void out(T...x){output(x...);}
template<class...T> void fin(T...x){out(x...);exit(0);}
template<class T> istream&operator>>(istream& i,V<T>& v){for(T& x:v)i>>x;re i;}
template<class T,class U> istream&operator>>(istream& i,P<T,U>& p){re i>>p.fi>>p.se;}
template<class T> ostream&operator<<(ostream& o,V<T>& v){for(T& x:v)o<<x<<" ";re o;}
template<class T,class U> ostream&operator<<(ostream& o,P<T,U>& p){re o<<p.fi<<" "<<p.se;}

void yn(bool b){fin((b?"Yes":"No"));}
void YN(bool b){fin((b?"YES":"NO"));}
void pos(bool b){fin((b?"POSSIBLE":"IMPOSSIBLE"));}
template<class T,class U> void chmax(T& a,U b){if(a<b)a=b;}
template<class T,class U> void chmin(T& a,U b){if(a>b)a=b;}
template<class T,class U> auto max(T a,U b){re a>b?a:b;}
template<class T,class U> auto min(T a,U b){re a<b?a:b;}
ll qp(ll x,ll n){ll r=1;wh(n>0){if(n&1)r*=x;x*=x;n>>=1;}re r;}
ll mdp(ll x,ll n){ll r=1;wh(n>0){if(n&1)r=r*x%mod;x=x*x%mod;n>>=1;}re r;}
ll MDp(ll x,ll n){ll r=1;wh(n>0){if(n&1)r=r*x%MOD;x=x*x%MOD;n>>=1;}re r;}

struct UnionFind{
    vl par,sz;
    UnionFind(ll n):par(n),sz(n,1){rep(i,n) par[i]=i;}
    ll rot(ll x){
        if(par[x]==x) re x;
        re par[x]=rot(par[x]);
    }
    void uni(ll x,ll y){
        x=rot(x),y=rot(y);
        if(x==y) re;
        if(sz[x]<sz[y]) swap(x,y);
        sz[x]+=sz[y];
        par[y]=x;
    }
    bool eql(ll x,ll y){re rot(x)==rot(y);}
    ll siz(ll x){re sz[rot(x)];}
};

struct IOSetup{
    IOSetup(){
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        cout<<fixed<<setprecision(10);
    }
}iosetup;

#pragma endregion

signed main(){
    ll x,y; in(x,y);

    if(x==y) out(x);
    else{
        if(x==1){
            if(y==0) out(2);
            else out(0);
        }
        if(x==2){
            if(y==0) out(1);
            else out(0);
        }
        if(x==0){
            if(y==1) out(2);
            else out(1);
        }
    }
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long

int main()
{
   int t=1;
 //  cin>>t;

   while(t--)
   {

      int a,b,x,y;

      cin>>a>>b>>x>>y;

      cout<<(b-x)<<endl;

   }

  return 0;

}

// written by c++(gcc)
// -*- cording: utf-8 -*-
#include <stdio.h>

// 最大値を返す関数
int max(int a, int b){
    int maxNum;

    if(a < b){
        maxNum = b;
    }else{
        maxNum = a;
    }

return maxNum;
}

int main(void){
    int a, b, c, d;
    int ans;

    scanf("%d %d", &a, &b);
    scanf("%d %d", &c, &d);

    ans = max(a-c, a-d);
    ans = max(ans, b-c);
    ans = max(ans, b-d);

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

    return 0;
}


#include <iostream>
#include <set>
#include <string>
#include <random>
#include <unordered_map>
#include <vector>
using namespace std;

int main(){
    int N, M;
    cin >> N >> M;
    set<string> Set;
    unordered_map<string, int> count;
    unordered_map<string, bool> done;
    int Lmin = 10000, Lmax = 0;
    while(M--){
        string S;
        cin >> S;
        Set.insert(S);
        done[S] = true;
        Lmin = min(Lmin, (int)S.size());
        Lmax = max(Lmax, (int)S.size());
    }
    for(string S : Set){
        for(int k = 0; k < S.size(); k++){
            for(int l = 1; k + l <= S.size(); l++){
                count[S] += done[S.substr(k, l)];
            }
        }
    }
    for(auto [key, val] : count){
        if(key.size() != Lmin){
            count[key] = 0;
            Set.erase(key);
        }
    }
    string ans[N];
    for(int i = 0; i < N; i++){
        string last;
        int m = 0;
        for(auto [key, val] : count){
            if(m < val){
                m = val;
                last = key;
            }
        }
        ans[i] = "";
        int cnt = 0;
        while(true){
            bool finish = true;
            for(int j = 0; j < last.size(); j++){
                string temp = last.substr(j);
                string temp_next = temp;
                temp_next.back()++;
                if(Set.lower_bound(temp) != Set.lower_bound(temp_next)){
                    string in = *Set.lower_bound(temp);
                    if(cnt == 0){
                        finish = false;
                        cnt += in.size();
                        ans[i] = ans[i] + in;
                    }else{
                        int start = last.size() - j;
                        int arg = in.size() - start;
                        if(cnt + arg > N + 30){
                            finish = true;
                            break;
                        }
                        for(int k = start; k < in.size(); k++){
                            ans[i].push_back(in[k]);
                            cnt++;
                        }
                        finish = false;
                    }
                    last = in;
                    for(int k = 0; k < last.size(); k++){
                        for(int l = 1; k + l <= last.size(); l++){
                            Set.erase(last.substr(k, l));
                            count[last.substr(k, l)] = 0;
                        }
                    }
                    break;
                }
            }
            if(finish) break;
        }
        for(int j = ans[i].size(); j < N; j++) ans[i].push_back('A' + (rand() % 8));
    }
    for(int i = 0; i < N; i++){
        int r = rand();
        ans[i].resize(N);
        for(int j = 0; j < N; j++){
            cout << ans[i][(j + r) % N];
        }
        cout << endl;
    }
    return 0;
}

/*
    /\ In The Name Of Allah /\
    Author : Jawahiir Nabhan
*/
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
typedef long long ll;
const char nl = '\n';
const int NM = 3e6 + 10;

ll dp[4][NM];
int main()
{
    ll N,K; cin>> N >> K;
    dp[0][0] = 1;

    for (ll i = 0; i < 3; i++) {
        for (ll j = 0; j < 3 * N; j++){
           dp[i + 1][j + 1] += dp[i][j];
           if (j + N + 1 <= NM) dp[i + 1][j + N + 1] -= dp[i][j];
        }
        for (int j = 1; j <= 3 * N; j++) {
           dp[i + 1][j] += dp[i + 1][j - 1];
        }
    }

    ll S = 3;
    while(K > dp[3][S]){
        K -= dp[3][S];
        S++;
    }
    // S -> Sum of BTP of the K-th position.

    ll X = 1;
    while(K > dp[2][S - X]){
        K -= dp[2][S - X];
        X++;
    }

    ll Y = max(1ll, S - X - N) + K - 1;
    ll Z = S - X - Y;
    cout<< X << ' ' << Y << ' ' << Z << nl;
}


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

typedef int_fast32_t int32;
typedef int_fast64_t int64;

const int32 inf = 1e9+7;
const int32 MOD = 1000000007;
const int64 llinf = 1e18;

#define YES(n) cout << ((n) ? "YES\n" : "NO\n"  )
#define Yes(n) cout << ((n) ? "Yes\n" : "No\n"  )
#define POSSIBLE(n) cout << ((n) ? "POSSIBLE\n" : "IMPOSSIBLE\n"  )
#define ANS(n) cout << (n) << "\n"
#define REP(i,n) for(int64 i=0;i<(n);++i)
#define FOR(i,a,b) for(int64 i=(a);i<(b);i++)
#define FORR(i,a,b) for(int64 i=(a);i>=(b);i--)
#define all(obj) (obj).begin(),(obj).end()
#define rall(obj) (obj).rbegin(),(obj).rend()
#define fi first
#define se second
#define pb(a) push_back(a)
typedef pair<int32,int32> pii;
typedef pair<int64,int64> pll;

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

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int32 n;
  cin >> n;
  n = 1.08 * n;
  if(n < 206){
    ANS("Yay!");
  }else if(n == 206){
    ANS("so-so");
  }else{
    ANS(":(");
  }
  return 0;
}

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

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

    int price = n * 1.08;
    if (price < 206) {
        cout << "Yay!" << endl;
    } else if (price == 206) {
        cout << "so-so" << endl;
    } else {
        cout << ":(" << endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f
#define swap(a, b) (a ^= b ^= a ^= b)
#define max(x,y) ((x)>(y)?(x):(y))
// #define min(x,y) ((x)<(y)?(x):(y))
#define endl '\n'
const int maxn = 1e6 + 5;
inline int pow(int a, int b, int mod) { int ans = 1;while (b) { if (b & 1)        ans = ans * a % mod;    a = a * a % mod;    b >>= 1; }    return ans % mod; }
inline void print(double x, int d) { cout << fixed << setprecision(d) << x; }
inline int read() { int x = 0, s = 1;   char c = cin.get();   while (c < 48 || c > 57) { if (c == '-')            s = -1;        c = cin.get(); }   while (c <= 57 && c >= 48) { x = (x << 1) + (x << 3) + c - '0';       c = cin.get(); }    return x * s; }
int a[maxn];
signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int a, b, c;cin >> a >> b >> c;
    cout << a + b + c - min({ a,b,c });
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define endl '\n'
#define all(x) (x).begin(),(x).end()
#define mp(x,y) make_pair(x,y)
const int INF=1000000000+5;
const int N=100+5;
const ll oo=1e18+5;
const ll mod=1e9+7;

double dp[N][N][N];

double solve(int a,int b,int c){
	if(a>=100||b>=100||c>=100) return 0;
	double& ans=dp[a][b][c];
	if(ans>=0) return ans;
	double sum=a+b+c;
	ans=1+a/sum*solve(a+1,b,c)+b/sum*solve(a,b+1,c)+c/sum*solve(a,b,c+1);
	return ans;
}

int main(){
	ios::sync_with_stdio(0); cin.tie(0);
	int a,b,c;
	cin>>a>>b>>c;
	for(int i=0;i<N;++i){
		for(int j=0;j<N;++j){
			for(int d=0;d<N;++d) dp[i][j][d]=-1;
		}
	}
	cout<<fixed<<setprecision(9)<<solve(a,b,c);
	return 0;
}


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

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

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
	string s;
	cin >> s;
	if(s[0]==s[1]&&s[1]==s[2]) cout << "Won" << endl;
	else cout << "Lost" << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
using ll = long long;
int main(){
  int n,k,ans=0;
  cin>>n>>k;
  int grid[n][n];
  rep(i,n)rep(j,n) cin>>grid[i][j];
  vector<int> v;
  rep(i,n) v.push_back(i+1);
  do {
      ll loop = 0;
      rep(i,n-1){
        loop += grid[v[i]-1][v[i+1]-1];
      }
      loop += grid[v[n-1]-1][v[0]-1];
      if(loop == k) ans++;
    } while(next_permutation(v.begin(), v.end()));
  ans /= n;
  cout << ans << endl;
}

#include <bits/stdc++.h>
// clang-format off
using namespace std; using ll = int64_t; using ull = uint64_t; using pll = pair<ll,ll>; const ll INF = 4e18;
void print0() {}
template<typename Head,typename... Tail> void print0(Head head,Tail... tail){cout<<fixed<<setprecision(15)<<head;print0(tail...);}
void print() { print0("\n"); }
template<typename Head,typename... Tail>void print(Head head,Tail... tail){print0(head);if(sizeof...(Tail)>0)print0(" ");print(tail...);}
// clang-format on

vector<vector<ll>> route(9, vector<ll>(9, -1));
ll N;
ll cnt = 0;
ll K;
vector<bool> done(9, false);
void recursive(ll c, ll ttl) {
    //print(c, ttl, K);
    done[c] = true;
    bool all = true;
    for (ll j = 0; j < N; j++) {
        if (!done[j]) {
            all = false;
        }
    }
    if (all) {
        // print(ttl, c);
        if (ttl + route[c][0] == K) {
            cnt++;
        }
        done[c] = false;
        return;
    }
    for (ll j = 0; j < N; j++) {
        if (!done[j]) {
            //    print(c, j);
            recursive(j, ttl + route[c][j]);
        }
    }
    done[c] = false;
}

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

    for (ll i = 0; i < N; i++) {
        for (ll j = 0; j < N; j++) {
            ll t;
            cin >> t;
            route[i][j] = t;
        }
    }
    recursive(0, 0);
    print(cnt);
}


/*
  AUTHOR:SOURABH
  CREATED:11:10:20
*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define ordered_set tree<ll , null_type, less< ll >, rb_tree_tag, tree_order_statistics_node_update>
#define ll long long int
#define ld long double
#define fi first
#define se second
#define pb push_back
#define pob pop_back
#define W while
#define fn(i,x) for(i=0;i<x;i++)
#define fs(i,s,x) for(i=s;i<x;i++)
#define fr(i,x) for(i=x;i>=0;i--)
#define fit(it,s) for(it=s.begin();it!=s.end();it++)
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vii vector<int>
#define vll vector<ll>
#define mii map<int,int>
#define mll map<ll,ll>
#define mod 1000000007
#define MAXN 10000001
#define M 32

using namespace std;
using namespace __gnu_pbds;

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

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

    ll t,n,m,i,j,k,w,ans;
    pll p;
    t=1;
    //cin>>t;
    W(t--)
    {
        cin>>n>>m;
        vll a(n);
        vector<pll> b(m);
        fn(i,n)
        cin>>a[i];
        fn(i,m)
        cin>>b[i].se>>b[i].fi;
        sort(b.begin(),b.end());
        sort(a.begin(),a.end());
        if(a[n-1]>b[0].fi)
        {cout<<-1;return 0;}
        vll pre(m);
        pre[0]=b[0].se;
        fs(i,1,m)
        pre[i]=max(pre[i-1],b[i].se);
        ans=LLONG_MAX;
        ll dis[n][n];
        do
        {
            fn(i,n)
            {
                fn(j,n)
                dis[i][j]=-1e15;
            }
            fn(i,n-1)
            {
                w=a[i];
                dis[i][i]=0;
                fs(j,i+1,n)
                {
                    w+=a[j];
                    p= {w,-1};
                    k=lower_bound(b.begin(),b.end(),p)-b.begin();
                    k--;
                    if(k>=0)
                        dis[i][j]=pre[k];
                }
            }
            fn(k,n)
            {
                fn(i,n)
                {
                    fn(j,n)
                    dis[i][j]=max(dis[i][j],dis[i][k]+dis[k][j]);
                }
            }
            ans=min(ans,dis[0][n-1]);
        }
        W(next_permutation(a.begin(),a.end()));
        cout<<ans;
    }
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

// clang-format off
/* accelration */
// 高速バイナリ生成
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
// cin cout の結びつけ解除, stdioと同期しない(入出力非同期化)
// cとstdの入出力を混在させるとバグるので注意
struct Fast {Fast() {std::cin.tie(0); ios::sync_with_stdio(false);}} fast;

/* alias */
using ull = unsigned long long;
using ll = long long;
using vi = vector<int>;
using vl = vector<long>;
using vll = vector<long long>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using vvll = vector<vll>;
using vs = vector<string>;
using pii = pair<int, int>;

/* define short */
#define pb push_back
#define mp make_pair
#define all(obj) (obj).begin(), (obj).end()
#define YESNO(bool) if(bool){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}
#define yesno(bool) if(bool){cout<<"yes"<<endl;}else{cout<<"no"<<endl;}
#define YesNo(bool) if(bool){cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}

/* REP macro */
#define reps(i, a, n) for (ll i = (a); i < (ll)(n); ++i)
#define rep(i, n) reps(i, 0, n)
#define rrep(i, n) reps(i, 1, n + 1)
#define repd(i,n) for(ll i=n-1;i>=0;i--)
#define rrepd(i,n) for(ll i=n;i>=1;i--)

/* debug */
// 標準エラー出力を含む提出はrejectされる場合もあるので注意
#define debug(x) cerr << "\033[33m(line:" << __LINE__ << ") " << #x << ": " << x << "\033[m" << endl;

/* func */
inline int in_int() {int x; cin >> x; return x;}
inline ll in_ll() {ll x; cin >> x; return x;}
inline string in_str() {string x; cin >> x; return x;}
// search_length: 走査するベクトル長の上限(先頭から何要素目までを検索対象とするか、1始まりで)
template <typename T> inline bool vector_finder(std::vector<T> vec, T element, unsigned int search_length) {
    auto itr = std::find(vec.begin(), vec.end(), element);
    size_t index = std::distance( vec.begin(), itr );
    if (index == vec.size() || index >= search_length) {return false;} else {return true;}
}
template <typename T> inline void print(const vector<T>& v, string s = " ")
    {rep(i, v.size()) cout << v[i] << (i != (ll)v.size() - 1 ? s : "\n");}
template <typename T, typename S> inline void print(const pair<T, S>& p)
    {cout << p.first << " " << p.second << endl;}
template <typename T> inline void print(const T& x) {cout << x << "\n";}
template <typename T, typename S> inline void print(const vector<pair<T, S>>& v)
    {for (auto&& p : v) print(p);}
// 第一引数と第二引数を比較し、第一引数(a)をより大きい/小さい値に上書き
template <typename T> inline bool chmin(T& a, const T& b) {bool compare = a > b; if (a > b) a = b; return compare;}
template <typename T> inline bool chmax(T& a, const T& b) {bool compare = a < b; if (a < b) a = b; return compare;}

int main(){
    rep(I, 1000){
        int si, sj, ti, tj;
        cin >> si >> sj >> ti >> tj;

        int down = si - ti;
        int right = sj - tj;

        string s = "";
        if(down != 0){
            rep(i, abs(down)){
                if(down < 0){
                    s += 'D';
                }
                else
                {
                    s += 'U';
                }

            }
        }
        if(right != 0){
            rep(i, abs(right)){
                if(right < 0){
                    s += 'R';
                }
                else
                {
                    s += 'L';
                }

            }
        }
        printf("%s\n", s.c_str());
        fflush(stdout);
        ll points;
        cin >> points;
    }
}


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

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

    vector<pair<int64_t, int64_t>> piece(n);
    for (auto &[x, y] : piece)
        cin >> x >> y;

    int64_t m;
    cin >> m;

    Matrix E({{1, 0, 0}, {0, 1, 0}, {0, 0, 1}});
    Matrix Clockwise({{0, 1, 0}, {-1, 0, 0}, {0, 0, 1}});
    Matrix AntiClockwise({{0, -1, 0}, {1, 0, 0}, {0, 0, 1}});

    Matrix now = E;
    vector<Matrix> MX;
    MX.emplace_back(E);

    auto matmul = [](Matrix A, Matrix B) {
        // 列ベクトルと行列積をとるのに3x3決め打ちで書いていた
        Matrix ret((int64_t)A.size(), vector<int64_t>((int64_t)B[0].size(), 0));
        rep(i, (int64_t)A.size()) rep(j, (int64_t)B[0].size()) rep(k, (int64_t)A[0].size())
            ret[i][j] += A[i][k] * B[k][j];
        return ret;
    };

    auto line_sym = [](char axis, int64_t p) {
        Matrix ret;
        if (axis == 'x')
            ret = {{-1, 0, p * 2}, {0, 1, 0}, {0, 0, 1}};
        else
            ret = {{1, 0, 0}, {0, -1, p * 2}, {0, 0, 1}};
        return ret;
    };

    while (m--)
    {
        int64_t op, p;
        cin >> op;
        if (op == 1)
        {
            now = matmul(Clockwise, now);
        }
        else if (op == 2)
        {
            now = matmul(AntiClockwise, now);
        }
        else if (op == 3)
        {
            cin >> p;
            now = matmul(line_sym('x', p), now);
        }
        else if (op == 4)
        {
            cin >> p;
            now = matmul(line_sym('y', p), now);
        }
        MX.emplace_back(now);
    }

    int64_t q;
    cin >> q;
    Matrix v;
    while (q--)
    {
        int64_t a, b;
        cin >> a >> b;
        b--;
        v = {{piece[b].first}, {piece[b].second}, {1}}; // 1を忘れてdebug
        v = matmul(MX[a], v);
        cout << v[0][0] << " "s << v[1][0] << "\n"s; // 行と列を間違えてdebug
    }
    return 0;
}


// Created by Kshitij Anand NSIT
#include <bits/stdc++.h>
//#include <ext/numeric>
//using namespace __gnu_cxx;

using namespace std;

#define int long long
#define pb push_back
#define P pair<int,int>
#define F first
#define S second
#define vi vector<int>
#define vc vector<char>
#define vb vector<bool>
#define all(x) x.begin(),x.end()
#define sz(x) (int)x.size()
#define mp(a, b) make_pair(a, b)
#define min3(a, b, c) min(min(a, b), c)
#define min4(a, b, c, d) min(min(a, b), min(c, d))
#define max3(a, b, c) max(max(a, b), c)
#define max4(a, b, c, d) max(max(a, b), max(c, d))
#define fill(arr,val) memset(arr,val,sizeof(arr))
#define inf 1e18
#define ps(x,y) fixed<<setprecision(y)<<x
#define db(x)  cout<<#x<<" : "<<x<<endl

const int N = 1000000007;

void solve(){

    int n,a,b,c, ans=0, count=0, sum=0;
    cin>>n;
    int val = 1;

    while(val*3 < n){

        val*=3;
        count++;
    }

    // db(n - val);

    int n2 = n - val;
    // n-= val;
    int val2 = 1;

    while(val2*5 <= n2){

        val2*=5;
        sum++;
    }

   if(val2 == n2 && count && sum){

    cout<<count<<" "<<sum<<endl;

    return;
   }

   else{
    // cout<<-1<<endl;

    val = 1;

    count = 0;

    while(val*5 < n){

        val*=5;
        count++;
    }

    // db(n - val);

    n2 = n - val;
    // n-= val;
    val2 = 1;
    sum = 0;

    while(val2*3 <= n2){

        val2*=3;
        sum++;
    }

    if(val2 == n2 && count && sum){

    cout<<sum<<" "<<count<<endl;
   }

   else{
    cout<<-1<<endl;
   }

   }

}

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

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

  return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, k, n) for (int i = k; i < (int)(n); i++)
#define repd(i, n) for (int i = n-1; i >= 0; i--)
#define rrepd(i, k, n) for (int i = n-1; i >= (int)(k); i--)
#define all(x) (x).begin(),(x).end()
#define chmax(x,y) x=max(x,y)
#define chmin(x,y) x=min(x,y)
#define F first //pairの一つ目の要素
#define S second //pairの二つ目の要素
#define PB push_back //挿入
#define MP make_pair //pairのコンストラクタ
//V,Pは大文字i,l,bは小文字
using ll = long long;
using Vi = vector<int>;
using VVi = vector<Vi>;
using Vl = vector<ll>;
using VVl = vector<Vl>;
using Vb = vector<bool>;
using VVb = vector<Vb>;
using P = pair<int,int>;
using Pl = pair<ll, ll>;
using Vs = vector<string>;
const ll mod = 1000000007;
const ll inf = 1000000000000000000;//10の18乗
#define yn {puts("Yes");}else{puts("No");}
#define dame { puts("-1"); return 0;}

int main() {
    ll n;
    cin >> n;
    ll now=1;
    ll mn=1,mx=9;
    chmin(mx,n);
    ll ans=0;
    rep(i,17){
        if(mn>mx) continue;
        ans+=(mx-mn+1)*((now-1)/3);
        now++;
        mn=mn*10;
        mx=(mx+1)*10-1;
        chmin(mx,n);
    }
    cout << ans << endl;

}


/* In the name of Anton */

/*
* Motto :)
*
*
*
*
*
*/

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

#define rep(i,n) for(int i=0;i<n;i++)
#define repa(i,a,n) for(int i=a;i<=n;i++)
#define repb(i,a,n) for(int i=a;i>=n;i--)
#define trav(x,a) for(auto &x: a)
#define all(x) x.begin(),x.end()
#define sz(x) (int)(x).size()
#define fst first
#define snd second
#define pb push_back
#define mp make_pair
#define vt vector
typedef long double ld;
typedef pair <int,int> pii;
typedef vector <int> vi;
typedef long long ll;

template<class A> void read(vt<A>& v);
template<class T> void read(T& x){
  cin>>x;
}

void read(double &d){
  string t;
  read(t);
  d=stod(t);
}
void read(long double &d){
  string t;
  read(t);
  d=stold(t);
}

template<class H, class... T> void read(H &h, T&...t){
  read(h);
  read(t...);
}

template <class A> void read(vt<A> &x){
  trav(a,x)
    read(a);
}

string to_string(char c){
  return string(1,c);
}

string to_string(bool b){
  return b?"true":"false";
}

string to_string(const char* s){
  return string(s);
}

string to_string(string s){
  return string(s);
}

string to_string(vt<bool> v){
  string res;
  rep(i,sz(v)){
    res+=char('0'+v[i]);
  }
  return res;
}

template<class T> string to_string(T v){
  bool f=1;
  string res;
  trav(x,v){
    if(!f)
      res+=' ';
    f=0;
    res+=to_string(x);
  }
  return res;
}

template<class A> void write(A x){
  cout<<to_string(x);
}

template<class H, class...T> void write(const H& h, const T&...t){
  write(h);
  write(t...);
}

void print(){
  write("\n");
}

template<class H, class...T> void print(const H& h, const T&...t){
  write(h);
  if(sizeof...(t))
    write(' ');
  print(t...);
}

/* -----------------------------------------------------------------------------------------------*/

void pre(){

}

void solve(){
  int n; read(n);
  vector <ll> a(n); read(a);

  sort(all(a));

  vector <ll> suff(n);

  for(int i=n-1;i>=0;i--){
    if(i==n-1) suff[i]=a[i];
    else{
      suff[i]=suff[i+1]+a[i];
    }
  }

  ll ans=0;

  ll tot=n-1;

  for(int i=0;i<n-1;i++){
    ll temp=suff[i+1];
    ll kemp=(tot*a[i]);

    temp-=kemp;

    ans+=temp;
    tot--;
  }

  print(ans);
}

int main(){
  ios_base::sync_with_stdio(0); cin.tie(0);
  pre();
  int t=1;
  // read(t);
  rep(i,t) solve();
  return 0;
}

#include <algorithm>
#include <numeric>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <numeric>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#define DEBUG(x) std::cout << '>' << #x << ':' << x << '\n';
#define PRINTVEC(x) for (int i = 0; i < (int)x.size(); i++) std::cout << x[i] << ' '; std::cout << '\n';
#define PRINTPAIR(x) for (int i = 0; i < (int)x.size(); i++) std::cout << x[i].first << ' ' << x[i].second << '\n'; std::cout << '\n';
#define PRINTSET(x) for (auto it = x.begin(); it != x.end(); it++) { std::cout << *it << ' '; } std::cout << '\n';
#define io std::ios_base::sync_with_stdio(false);std::cin.tie(NULL);
const int INF = 1<<29;
const double PI = acos(-1.0);
typedef long long ll;
const std::vector<int> GRID = {0,1,0,-1,0}; // for(k < 4) r=x+GRID[k],c=y+GRID[k+1]; if(0<=r&&r<m&&0<=c&&c<n) do smthg;
using namespace std;

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

    ll n; cin >> n;
    vector<ll> v(n);
    for (auto &it : v) cin >> it;
    sort(v.begin(),v.end());
    ll sum = 0;
    for (ll i=n-1; i>=0; i--)
        sum += i*v[i] - (n-1-i)*v[i];
     cout << sum;

    #ifndef ONLINE_JUDGE
    std::cout << '\n' << "Time elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " sec \n";
    #endif
    return 0;
}


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

const int INF = 1001001001;

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

    vector<int> dp(n, 2*INF);
    vector<vector<int>> to(n);
    rep(i, m) {
        int x, y;
        cin >> x >> y;
        x--; y--;
        to[x].push_back(y);
    }
    int ans = -INF;
    rep(i, n) {
        ans = max(ans, a[i] - dp[i]);
        for (auto j : to[i]) {
            dp[j] = min(dp[j], dp[i]);
            dp[j] = min(dp[j], a[i]);
        }
    }
    cout << ans << endl;

    return 0;
}

/// Down with The Sickness
#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#pragma GCC optimization ("unroll-loops")

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

using ll = long long;
using ii = pair<int, int>;

#define pb push_back
#define F first
#define S second
#define f(i,a,b)  for(int i = a; i < b; i++)
#define all(a)  a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()

const int N = 2e5 + 5, LG = 19, MOD = 998244353;
int n;
vector<int> adj[N];
int dist[N];
int depth[N];
void dfs1(int node, int p) {

    for(auto x : adj[node]) {
        if(x == p)continue;
        dist[x] = dist[node] + 1;
        dfs1(x, node);
    }

}
void dfs2(int node, int p) {

    for(auto x : adj[node]) {
        if(x == p)continue;
        dfs2(x, node);
        depth[node] = max(depth[node], depth[x] + 1);
    }

}
int cnt = 1;
int E[N];
void dfs3(int node, int p) {

    vector<ii> ans;
    E[node] = cnt;

    for(auto to : adj[node])
    if(to != p) {
        ans.pb(ii(depth[to], to));
    }

    sort(ans.begin(), ans.end());
    for(auto to : ans) {
        cnt++;
        dfs3(to.S,node);
        cnt++;
    }

}
int main(){
#ifdef ONLINE_JUDGE
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#endif // ONLINE_JUDGE

    cin >> n;

    f(i,1,n) {
        int u, v;
        cin >> u >> v;
        adj[u].pb(v);
        adj[v].pb(u);
    }

    dfs1(1,1);
    int mx = 1;
    f(i,2,n+1)
        if(dist[mx] < dist[i])
            mx = i;
    dfs2(mx,mx);
    dfs3(mx,mx);
    f(i,1,n+1)
        cout << E[i] << ' ';

    return 0;
}


#include <stdio.h>
#include <iostream>
#include <vector>
// #include <bits/stdc++.h>
#include <queue>
#include <algorithm>
#include <string>
#include <iomanip>
#include <cmath>

using namespace std;
typedef long long ll;

#define rep(i,k,n) for(ll i=k; i<(ll)(n); i++)

int main(){
    ll n; cin >> n;
    ll a,b;
    ll ans = 0;
    rep(i,0,n) {
        cin >> a >> b;
        ll cnt = b-a+1;
        ans += (a+b)*cnt / 2;
    }

    cout << ans << endl;

    return 0;
}

#include <bits/stdc++.h>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<string.h>
using namespace std;
#define pb push_back
#define all(v) v. begin(),v. end()
#define  rep(i,n,v) for(i=n;i<v;i++)
#define per(i,n,v) for(i=n;i>v;i--)
#define ff first
#define ss second
#define pp pair<ll,ll>
#define ll  long long
#define endl '\n'
void solve()
{
  ll n, a,m=0,b=0, c=0,k=0, i, j,l=998244353;
  string s, r, y;
  cin>>n;
  while(n--)
  {
    cin>>a>>m;
    b=m-a+1;
    k+=(b*(2*a+b-1))/2;
   // rep(i, a, m+1) k+=i;
  }
  cout<<k;
}
int main()
{

 ios_base::sync_with_stdio(false);
  cin. tie(0);cout. tie(0);
    ll t=1;
  // cin>>t;
    while(t--)
    {
      solve();
    }
    return 0;
}