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

template<typename T, T MOD = 1000000007>
struct Mint{
  static constexpr T mod = MOD;
  T v;
  Mint():v(0){}
  Mint(signed v):v(v){}
  Mint(long long t){v=t%MOD;if(v<0) v+=MOD;}

  Mint pow(long long k){
    Mint res(1),tmp(v);
    while(k){
      if(k&1) res*=tmp;
      tmp*=tmp;
      k>>=1;
    }
    return res;
  }

  static Mint add_identity(){return Mint(0);}
  static Mint mul_identity(){return Mint(1);}

  Mint inv(){return pow(MOD-2);}

  Mint& operator+=(Mint a){v+=a.v;if(v>=MOD)v-=MOD;return *this;}
  Mint& operator-=(Mint a){v+=MOD-a.v;if(v>=MOD)v-=MOD;return *this;}
  Mint& operator*=(Mint a){v=1LL*v*a.v%MOD;return *this;}
  Mint& operator/=(Mint a){return (*this)*=a.inv();}

  Mint operator+(Mint a) const{return Mint(v)+=a;}
  Mint operator-(Mint a) const{return Mint(v)-=a;}
  Mint operator*(Mint a) const{return Mint(v)*=a;}
  Mint operator/(Mint a) const{return Mint(v)/=a;}

  Mint operator-() const{return v?Mint(MOD-v):Mint(v);}

  bool operator==(const Mint a)const{return v==a.v;}
  bool operator!=(const Mint a)const{return v!=a.v;}
  bool operator <(const Mint a)const{return v <a.v;}

  static Mint comb(long long n,int k){
    Mint num(1),dom(1);
    for(int i=0;i<k;i++){
      num*=Mint(n-i);
      dom*=Mint(i+1);
    }
    return num/dom;
  }
};
template<typename T, T MOD> constexpr T Mint<T, MOD>::mod;
template<typename T, T MOD>
ostream& operator<<(ostream &os,Mint<T, MOD> m){os<<m.v;return os;}

const ll MOD = (int)(1e9 + 7);

constexpr ll factorial(int n){
  ll res = 1;
  for (int i = 1; i <= n; i++){
    res *= i;
    res %= MOD;
  }
  return res;
}

int main(){
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  int sum = 0;
  for (int i = 0; i < n; i++){
    cin >> a[i];
    sum += a[i];
  }
  using M = Mint<int>;
  M res = 1;
  for (int i = 1, j = n + m; i <= sum + n; i++, j--){
    res *= j;
    res /= i;
  }
  cout << res << endl;
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template<class T> using vc = vector<T>;
template<class T> using vvc = vector<vector<T>>;

#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)
#define repa(i,n) for (auto& i: n)

template<class T1, class T2> inline bool chmax(T1 &a, const T2 &b) {if (a<b) { a=b; return 1;} return 0;}
template<class T1, class T2> inline bool chmin(T1 &a, const T2 &b) {if (b<a) { a=b; return 1;} return 0;}
struct init{init(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}}init_;

#ifdef DEBUG
template <class T, class N> void verr(const T& a, const N& n) { rep(i, n) cerr << a[i] << " "; cerr << "\n" << flush; }
ll dbgt = 1; void err() { cerr << "passed " << dbgt++ << "\n" << flush; }
template<class H, class... T> void err(H&& h,T&&... t){ cerr<< h << (sizeof...(t)?" ":"\n") << flush; if(sizeof...(t)>0) err(forward<T>(t)...); }
#endif

const ll INF = 4e18;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
//--------------------------------------------------------------------------------//
ll modpow(ll a, ll n, ll mod_) {
    ll res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod_;
        a = a * a % mod_;
        n >>= 1;
    }
    return res;
}

const ll MAX_SIZE = 4005005;
array<ll, MAX_SIZE> fac, inv, finv;
struct finit{
    finit(){
        // combination init
        fac[0] = 1;
        for (ll i = 1; i < MAX_SIZE; i++) fac[i] = fac[i - 1] * i % MOD;
        finv[MAX_SIZE - 1] = modpow(fac[MAX_SIZE - 1], MOD - 2, MOD);
        for(ll i = MAX_SIZE - 1; i > 0; i--) finv[i - 1] = finv[i] * i % MOD;

        //inv init
        repe(i, 1, MAX_SIZE) inv[i] = modpow(i, MOD - 2, MOD);
    }
} finit_;

ll perm(ll a, ll b) { return fac[a] * finv[a - b] % MOD; }

ll comb(ll a, ll b) { return fac[a] * finv[b] % MOD * finv[a - b] % MOD; }

int main() {
    ll N, M;
    cin >> N >> M;
    vl A(N);
    rep(i, N) cin >> A[i];

    ll sum = accumulate(all(A), 0ll);
    if(sum > M){
        cout << 0 << endl;
        return 0;
    }

    ll ans = finv[sum + N];
    rep(i, sum + N)(ans *= (M + N - i)) %= MOD;

    cout << ans << endl;
}


///////////////////////////////
////ACくれなきゃ悪戯しちゃうぞ!////
///////////////////////////////
//include
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cmath>
#include <iomanip>
#include <math.h>
#include <utility>
#include <functional>
#include <cstdlib>
#include <map>
#include <utility>
#include <set>
#include <deque>
//using
using namespace std;
using vi = vector <int>;
using vl = vector <long long>;
using vs = vector <string>;
using vc = vector <char>;
using ll= long long;
using vvl = vector<vector<ll> >;
using vvc = vector<vector<char> >;
using vd = vector <double>;
using vpl = vector <pair<ll,ll> >;
//define
#define rep(i,r) for(ll i=0;i<(r);i++)
#define Rep(i,l,r) for(ll i=(l);i<(r);i++)
#define print(n) cout<<n<<endl;
#define sortp(d) sort(d.begin(),d.end()) //1234
#define sortm(d) sort(d.rbegin(),d.rend()) //4321
#define all(n) n.begin(),n.end()
//素数判定　O(√A)
bool is_prime(ll x){
 if(x<=1) return false;
 for(int i=2;i*i<=x;i++)
 if(x%i==0) return false;
 return true;
 }
 //各位の和
int wa(ll x)
{
ll sum = 0;
while(x!=0){
  sum += x% 10;
  x /= 10;
}
return sum;
}
/*順列生成
do {
  // 順列に対する処理
} while (next_permutation(配列変数.begin(), 配列変数.end()));*/
//最大公約数　ユークリッドの互除法
ll gcd(ll a, ll b) {
    if (b==0) return a;
    else return gcd(b, a%b);
}
//最小公倍数　a*b/gcd
ll lcm(ll a, ll b) {
    return a/gcd(a, b)*b;
}
//10進→２進
ll binary(ll bina){
    ll ans = 0;
    for (ll i = 0; bina>0 ; i++)
    {
        ans = ans+(bina%2)*pow(10,i);
        bina = bina/2;
    }
    return ans;
}
//'A'65,'a'97
//reverse 回文
/*for(const auto& [x,y]:m)    mapの範囲for
  {
    q+=y*(y-1)/2;
  }*/
/*小数点精度指定
cout<<fixed<<setprecision(9)<<;
*/
//文字列の削除 erase(n)

//グローバル変数宣言等
ll p=0,q=0,r=0;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
#define inf 1000000000000 //10^12:∞
#define mod 1000000007 //10^9+7:合同式の法
int main()
{
  unsigned long long n;cin>>n;
  if(n==1||n==2||n==3)
  {
    print(n/3+1)return 0;
  }
  for (unsigned long long i = 0; i < n; i++)
  {
    if(i%2==0)
    {
      p=i/2*(i+1);
    }else
    p=(i+1)/2*(i);
    if(1+n<p)
    {
      print(n-i+2);return 0;
    }
  }
}

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

int main(){
  int64_t n;
  cin>>n;
  int64_t cnt=0,sum=0;
  while(cnt+sum<=n)sum+=++cnt;
  cout<<n-cnt+1<<'\n';
}


/**
 * @author: adityasonani
 * */
#include <bits/stdc++.h>

// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")

#define     ll          long long
#define     ld        	long double
#define     ln          "\n"
#define     fastio      ios_base::sync_with_stdio(0); cin.tie(nullptr)
#define     MOD         (int) 1000000007
#define     rep(i, n)   for (int i = 0; i < (int)n; i++)
#define     all(x)      (x).begin(), (x).end()
#define     debug(x)    cerr << #x << " = " << x << endl;
#define     debugV(x)   cerr << #x << " = ["; for(auto i: x){ cerr << i << ", ";} cerr << "]" << endl;
#define     precise(x)  cout << fixed << setprecision(x)
#define     MAX         (int) 100007 // 10^5
std::vector<int> gv(int n){std::vector<int> v(n);for(auto &x: v){std::cin>>x;}return v;}

using namespace std;

/*
*/

void solve()
{
    int n, d, h;
    cin>>n>>d>>h;

    int mxH = INT_MIN, mxD = 1;
    double ans = 0.0;
    while(n--){
        int da, ha;
        cin>>da>>ha;
        double x = ((double)(da*h)-(d*ha))/(double)(da-d);
        ans = max(ans, x);
    }

    precise(10);
    cout << ans;
}

signed main()
{
    fastio;

    auto start = std::chrono::high_resolution_clock::now();

    int t=1;
    // cin>>t;

    while(t--)
        solve();

    auto stop = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start);

    // cerr << "\nTime taken : " << ((long double)duration.count())/((long double) 1e9) <<" s "<< endl;

    return 0;
}

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

int main(){
    Int n,s,d; cin>>n>>s>>d;
    vector<Int> x(n);
    vector<Int> y(n);

    rep(i,n) cin>>x[i]>>y[i];

    rep(i,n){
        if(x[i]<s && y[i]>d){
            cout<<"Yes"<<endl;
            return 0;
        }
    }

    cout<<"No"<<endl;

}


#include <bits/stdc++.h>

using namespace std;

int main() {
    int T;  cin >> T;
    string ss = "atcoder";
    while (T--) {
        string s = ss;
        string t;   cin >> t;
        string tt = t;
        sort(tt.rbegin(), tt.rend());
        if (t > s) {
            cout << 0 << '\n';
        }
        else if (tt <= s) {
            cout << -1 << '\n';
        }
        else {
            int ans = INT_MAX;
            for (int i = 0; i < (int)t.size(); i++) {
                if (t[i] > 't') {
                    ans = i - 1;
                    break;
                }
            }
            for (int i = 0; i < (int)t.size(); i++) {
                if (t[i] > 'a') {
                    ans = min(ans, i);
                    break;
                }
            }
            cout << ans << '\n';
        }

    }
    return 0;
}

#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <algorithm>
#define rep(i, n) for(i = 0; i < (n); i++)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define MOD 1000000007
#define PI 3.14159265358979323846
#define INF 1 << 30

using namespace std;
typedef long long ll;
typedef pair<int, int> pp;

int Solve(string & s) {
	if (s == string(s.size(), 'a'))
		return -1;
	if ("atcoder" < s)
		return 0;
	int i;
	rep(i, s.size()) {
		if (s[i] != 'a')
			break;
	}
	if (s[i] > 't')
		return i - 1;
	return i;
}

int main(void) {
	int num, i;
	string s;
	cin >> num;
	rep(i, num) {
		cin >> s;
		cout << Solve(s) << "\n";
	}
	return 0;
}


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

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

  if(x>=0) cout<<x;
  else if(x<0) cout<<0;

}

#include<bits/stdc++.h>
#define ll          long long int
#define nl          '\n'
#define pi          acos(-1)
#define GCD(a, b)       __gcd(a, b)
#define boost       ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define inout       freopen("input.txt","r",stdin);freopen("output.txt","w",stdout)

using namespace std;

int main(void){
    //boost;

    ll t,k,n,m,mi=0,mx=0,ans=0,po=0,ne=0,cnt=0,sum=0;

      cin>>t;

      //vector<ll>a(t);
       ll ar[1001];
      map<char,int>mp;
      vector<string>str(105);
      bitset<6>set_t;

read:
   if(t>=0) cout<<t;
   else cout<<0;
}


//Code by Ritik Agarwal
#include<bits/stdc++.h>
using namespace std;
#define sz(x) (int)(x).size()
#define int long long int
#define loop(i,a,b) for(int i=a;i<b;i++)
#define scan(arr,n) for (int i = 0; i < n; ++i) cin >> arr[i]
#define vi vector<int>
#define si set<int>
#define pii pair <int, int>
#define sii set<pii>
#define vii vector<pii>
#define mii map <int, int>
#define pb push_back
#define ff first
#define ss second
#define all(aa) aa.begin(), aa.end()
#define rall(a) a.rbegin() , a.rend()
#define read(a,b) int a,b;  cin>>a>>b
#define readt(a,b,c) int a,b,c; cin>>a>>b>>c
#define readf(a,b,c,d) int a,b,c,d; cin>>a>>b>>c>>d;
#define print(v) for(auto x:v) cout<<x<<" ";cout<<endl
#define printPair(res) for(pair<int,int>& p:res) cout<<p.first<<" "<<p.second<<endl;
#define faster ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
//*****  Constants  *****
const int mod = 1000000007; /* 1e9 + 7*/
const int MAXN = 1500005; /*1e6 +5 */
const int mod1= 998244353;
const int NAX=2E5+5;
int vis[MAXN]={0};
int fac[300001];
void fact(int m){fac[0]=1;for(int i=1;i<=300000;i++) fac[i]=(fac[i-1]%m * i%m)%m;}
/* Iterative Function to calculate (x^y)%p in O(log y)*/
long long power( long long x,  int y, int p){   long long res = 1;
x = x % p; while (y > 0) { if (y & 1)  res = (res * x) % p;  y = y >> 1; x = (x * x) % p;} return res;}
// Returns n^(-1) mod p
long long modInverse( long long n, int p) {  return power(n, p - 2, p); }
long long nCr( long long n,int r, int p) { if (r == 0) return 1; if(n<r) return 0;
return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p; }

int binarySearch(vi arr, int l, int r, int x) {
if (r >= l) { int mid = l + (r - l) / 2; if (arr[mid] == x) return mid;
if (arr[mid] > x)  return binarySearch(arr, l, mid - 1, x);
return binarySearch(arr, mid + 1, r, x); } return -1; }
bool sortGrt(const pair<int,int> &a,
              const pair<int,int> &b)
{

    return (a.second > b.second);

}
void solve(int test)
{
  string s;cin>>s;
  if(s[0]==s[1] && s[1]==s[2])
    cout<<"Won\n";
  else
    cout<<"Lost\n";
}

int32_t main()
{
      #ifndef ONLINE_JUDGE
          freopen("input.txt", "r", stdin);
          freopen("output.txt", "w", stdout);
      #endif
      faster;
      fact(mod);
      int t=1;
      //cin>>t;
      for(int test=1;test<=t;test++)
      {
          solve(test);
      }
}

#include<iostream>
using namespace std;

int main()
{

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

	if ((a[0] ==  a[1]) && (a[1] == a[2]))
	{
		cout << "Won" << endl;
	}
	else
	{
		cout << "Lost" << endl;
	}
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fore(b,c) for(int val0=b;val0<c;val0++)
#define forr(k,c,s) for(int k=c;k<s;k++)
#define pb push_back
#define mmp make_pair
using namespace __gnu_pbds;
using namespace std;
template<typename T>
using oset = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef pair<int,int> ii;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef long double ld;
typedef vector<vii> al;
typedef vector<ll> vl;
const int INF = 1e9;
const ll INFL = 1LL<<61;
const ll MN = 3030;
const ll LG = 13;
const ll M = 998244353;
ll dp[MN][MN];
ll ds(ll n, ll k) {
	if(k > n) {return 0;}
	if(k == n) {return 1;}
	if(n == 0 || k == 0) {return 0;}
	if(dp[n][k] != -1) {return dp[n][k];
	}
	//cout << "roop " << n << " " << k << '\n';
	ll res = ds(n-1,k-1) + ds(n,2*k);
	res %= M;
	return dp[n][k] = res;
}
int main() {
	//ios::sync_with_stdio(0);cout.precision(20);cout.tie(0);cin.tie(0);
	memset(dp,-1,sizeof(dp));
	ll n,k;
	cin >> n >> k;
	cout << ds(n,k) << '\n';
}

//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid (tr[u].l+tr[u].r>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;

//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;

const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;

int n;
LL f[110][2];

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

    scanf("%d",&n);
    f[0][1]=f[0][0]=1;
    for(int i=1;i<=n;i++)
    {
        char s[20]; scanf("%s",s+1);
        if(s[1]=='A')
        {
            f[i][0]=2*f[i-1][0]+f[i-1][1];
            f[i][1]=f[i-1][1];
        }
        else
        {
            f[i][0]=f[i-1][0];
            f[i][1]=2*f[i-1][1]+f[i-1][0];
        }
    }
    printf("%lld\n",f[n][1]);

	return 0;
}
/*

*/


#include <bits/stdc++.h>

#define lowbit(x) ((x) & (-x))
using namespace std;

typedef long long LL;

const int N = 200005;

int n, e;
int a[N], b[N], c[N], d[N * 4];
LL m, f[N * 4];

void init() {
    memset(f, 0, sizeof(f));
}

void add(int p, int val) {
    for (; p < e; p += lowbit(p)) f[p] += val;
}

LL cal(int p) {
    LL ans = 0;
    for (; p > 0; p -= lowbit(p)) ans += f[p];
    return ans;
}

void gao() {
    e = 0;
    d[e++] = 0;
    for (int i = 0; i < n; i++) {
        d[e++] = a[i], d[e++] = a[i] + 1;
        d[e++] = b[i], d[e++] = b[i] + 1;
    }
    sort(d, d + e);
    e = unique(d, d + e) - d;
    for (int i = 0; i < n; i++) {
        a[i] = lower_bound(d, d + e, a[i]) - d;
        b[i] = lower_bound(d, d + e, b[i]) - d;
        // printf("l= %d, r= %d, c= %d\n", a[i], b[i], c[i]);
    }

    init();
    for (int i = 0; i < n; i++) {
        add(a[i], c[i]);
        add(b[i] + 1, -c[i]);
    }

    LL ans = 0;
    for (int i = 1; i < e - 1; i++) {
        LL tmp = cal(i);
        // printf("i= %d %d %lld %lld\n", i, d[i], tmp, tmp * (d[i + 1] - d[i]));
        ans += min(m, tmp) * (d[i + 1] - d[i]);
    }
    cout << ans << '\n';
}

inline bool scan_d(int &num)
{
        char in;bool IsN=false;
        in=getchar();
        if(in==EOF) return false;
        while(in!='-'&&(in<'0'||in>'9')) in=getchar();
        if(in=='-'){ IsN=true;num=0;}
        else num=in-'0';
        while(in=getchar(),in>='0'&&in<='9'){
                num*=10,num+=in-'0';
        }
        if(IsN) num=-num;
        return true;
}

int main() {
    scan_d(n);
    scanf("%lld", &m);
        for (int i = 0; i < n; i++) scan_d(a[i]), scan_d(b[i]), scan_d(c[i]);
        gao();
    return 0;
}

#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

using namespace std;

#define endl '\n'
#define int long long int
#define rep(i, a, b) for (int i = a; i < b; i++)
#define revrep(i, a, b) for (int i = a; i >= b; i--)
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))

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

ostream &operator<<( ostream &output, const pii &p ) { output << p.first << " " << p.second;return output; }
istream &operator>>( istream &input, pii &p ) { input >> p.first >> p.second;return input; }
template<typename T>
void inline println(vector<T> args){ for(T i: args)cout<<i<<" ";cout<<endl; }
void amax(int& a, int b) { a = max(a, b); }
void amin(int& a, int b) { a = min(a, b); }

const string YES = "Yes";
const string NO = "No";
int INF = 1e18;
int MOD = 998244353;

void solve()
{
    int a, b, x, y; cin>>a>>b>>x>>y;
    int dp[2][101];
    rep(i, 0, 2) rep(j, 0, 101) dp[i][j] = INF;
    set<pair<int, pii>> q;
    dp[0][a] = 0;
    q.insert({0, {0, a}});
    while(q.size()) {
        auto front = *q.begin();
        auto cor = front.second;
        q.erase(q.begin());
        vii nxt;
        int other = 1-cor.first;
        nxt.pb({cor.first, cor.second+1});
        nxt.pb({cor.first, cor.second-1});
        nxt.pb({other, cor.second});
        if(other == 0) nxt.pb({other, cor.second+1});
        else nxt.pb({other, cor.second-1});
        for(auto pr: nxt) {
            if(pr.second > 100) continue;
            if(pr.second < 0) continue;
            int dd = (pr.first == cor.first ? y:x);
            int dist = dd+dp[cor.first][cor.second];
            if(dp[pr.first][pr.second] > dist) {
                auto key = q.find({dp[pr.first][pr.second], pr});
                if(key != q.end()) q.erase(key);
                q.insert({dist, pr});
                dp[pr.first][pr.second] = dist;
            }
        }
    }
    cout<<dp[1][b]<<endl;
}

signed main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t = 1;
    // cin >> t;
    rep(i, 0, t)
    {
        solve();
    }
    return 0;
}


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

string n;
int k;
string res;
//-----------
bool cmp(char a, char b)
{
    return a>b;
}
//-----------
string inttostring(long long n)
{
    stringstream stream;
    stream<<n;
    string st=stream.str();
    return st;
}
//--------------------------------
long long stringtoint(string m)
{
    long long k=atoll(m.c_str());
    return k;
}
//------------------------------
int main()
{
    cin>>n>>k;
    res=n;
    while (k--)
    {
        string a=res,b=res;
        int x,y,z;
        sort(a.begin(),a.end());
        sort(b.begin(),b.end(),cmp);
        x=stringtoint(a);
        y=stringtoint(b);
        z=y-x;
        res=inttostring(z);
    }
    cout<<res;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
#define asort(a,n) sort(a,a+n)
#define vsort(v) sort(v.begin(),v.end())
#define all(v) v.begin(),v.end()
#define run ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0);
#define mod 1000000007
#define endl "\n"
#define LL_MAX LLONG_MAX
#define yes cout<<"Yes"<<endl
#define no cout<<"No"<<endl

//********************************* mekch ************************************//

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

}

ll binExp(ll x,ll n)
{
   ll res=1;
    while(n)
    {
        if(n&1) res=(res*x)%mod;
        x=(x*x)%mod;
        n>>=1;
    }
    return res;
}

bool isPerfectSquare(long double x)
{
  long double sr = sqrt(x);

  return ((sr - floor(sr)) == 0);
}

int main()
 {
  run;
  ll t;

  // ALWAYS UES LL_MAX
  long double  n,i,x,y,ok=0,sum=0,ans=0,res;
  ll res1,res2,res3,res4;

        cin>>x>>y;
        res=x*x-4*y;
    if (isPerfectSquare(res))
    {
        res1=sqrt(res);
        res2=x+res1;
        res3=x-res1;
       // cout<<"jdc";

        if(res2%2==0)
          res2=res2/2;
          else
          res2=-1;
          if(res3%2==0)
            res3=res3/2;
            else
            res3=-1;

            if(res2>0||res3>0)
            {
                if(res2>0)
                res4=x-res2;
                else
                res4=x-res3;

                if(res4>0)
                yes;
                else
                no;
            }
        else
        no;
    }
    else
    no;

 }


/*

  Author: Arnab Sinha

  ███████╗██╗  ██╗███████╗██████╗         ██╗      ██████╗  ██████╗██╗  ██╗███████╗██████╗  █████╗  █████╗
  ██╔════╝██║  ██║██╔════╝██╔══██╗        ██║     ██╔═══██╗██╔════╝██║ ██╔╝██╔════╝██╔══██╗██╔══██╗██╔══██╗
  ███████╗███████║█████╗  ██████╔╝        ██║     ██║   ██║██║     █████╔╝ █████╗  ██║  ██║╚██████║╚██████║
  ╚════ █║██╔══██║██╔══╝  ██╔══██╗        ██║     ██║   ██║██║     ██╔═██╗ ██╔══╝  ██║  ██║ ╚═══██║ ╚═══██║
  ███████║██║  ██║███████╗██║  ██║███████╗███████╗╚██████╔╝╚██████╗██║  ██╗███████╗██████╔╝ █████╔╝ █████╔╝
  ╚══════╝╚═╝  ╚═╝╚══════╝╚═╝  ╚═╝╚══════╝╚══════╝ ╚═════╝  ╚═════╝╚═╝  ╚═╝╚══════╝╚═════╝  ╚════╝  ╚════╝
*/

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define li long long int
#define ld long double
#define all(v) v.begin(),v.end()
#define rev(a) reverse(all(a))
#define sort(a) sort(all(a))
#define pb push_back
#define INF 1e18+10
#define MINF -1e18-10
#define rep(i,a,b) for(li i=a;i<b;i++)
#define vli vector<li>
#define MAXN 1e5+10
const li mod = 1e9+7;
using namespace std;

li power(li a, li b) { a%=mod; li ret = 1; while(b){ if(b&1) ret*=a; a*=a;; if(ret>=mod) ret%=mod; if(a>=mod) a%=mod; b>>=1; } return ret; }

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

template <class T, class... U> void read(T& x, U&... u){
	read(x);
	read(u...);
}

template <class A> void read(vector<A>& v){
	for(auto &it:v)
		read(it);
}

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

template <class A> void printl(A x){
	cout<<x<<endl;
}

template <class A> void print(vector<A>& v)
{
	for(auto &it:v){
		print(it);
		cout<<' ';
	}
	cout<<endl;
}

vector<li> v[200005];
bool visited[200005];
long int a[200005],b[200005];
li n,m;
li sum1=0, sum2=0;

void dfs(li num) {

	// component_nodes.pb(num);
	sum1+=a[num-1];
	sum2+=b[num-1];

	visited[num] = true;
	for(auto it:v[num]) {
		if(visited[it]==true)
			continue;
		dfs(it);
	}
	return;
}

void solve()
{
	cin>>n>>m;

	rep(i,0,200005){
		visited[i] = false;
	}

	rep(i,0,n)cin>>a[i];
	rep(i,0,n)cin>>b[i];

	rep(i,0,m){
		li c,d;
		cin>>c>>d;

		v[c].pb(d);
		v[d].pb(c);
	}

	rep(i,1,n+1){

			if(visited[i]==false){
				// cout<<"Yes\n";
				sum1=sum2=0;
				dfs(i);
			}
			if(sum1!=sum2){
				cout<<"No\n";
				return;
			}
	}

	cout<<"Yes\n";
	// dfs(1,0);
}

int main()
{
	IOS;

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

	solve();

	return 0;
}

#include <iostream>
#include <string>
#include <stdio.h>
#include <math.h>
#include <queue>
#include <algorithm>
#include <utility>
#include <vector>
#include <tuple>
#include <numeric>
using namespace std;

int main(int argc, char* argv[]){
int N,M;
cin >> N >> M;
vector<long long> A(N);
vector<long long> B(N);
vector <pair<int,int>> P(M);

long long sum_a = 0;
long long sum_b = 0;
for(int i=0;i<N;i++){
	cin >> A[i];
	sum_a += A[i];
}
for(int i=0;i<N;i++){
	cin >> B[i];
	sum_b += B[i];
}
vector<int> num(N,0);
for(int i=0;i<M;i++){
	int c,d, tmp;
	cin >> c >> d;
	if(c>d){
		tmp = c;
		c = d;
		d = tmp;
	}
	num[c-1]++;
	num[d-1]++;
	P[i] = make_pair(c, d);
}
bool flag = true;
for(int i=0; i<N; i++){
	if(num[i]==0 && A[i]!=B[i]){
		flag = false;
	}
}

if(sum_a != sum_b || flag==false){
	cout << "No" << endl;
}else{
	cout << "Yes" << endl;
}

return 0;

}


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

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

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

#include <string>  // ヘッダファイルインクルード
#include <typeinfo>
using namespace std;  //  名前空間指定
using namespace std;
// using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;

template <class T>

#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i, a, b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i, a) for (int i = (a)-1; i >= 0; i--)

#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define shandom_ruffle random_shuffle

const int MOD = 1000000007;
const ll INF = 1e18;
const int MX = 100001;  // check the limits, dummy

const ll infl = 1LL << 60;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

template <class T>
void print(const vector<T> v) {
  cout << "[ ";
  F0R(i, v.size()) { cout << v[i] << ' '; }
  cout << "]" << '\n';
}

int mod(int i, int j) {
  return (i % j) < 0 ? (i % j) + 0 + (j < 0 ? -j : j) : (i % j + 0);
}

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

/*  lcm (a, b) : 2整数版
    入力：整数 a, b
    出力：aとbの最小公倍数
*/
long long lcm(long long a, long long b) {
  long long d = gcd(a, b);
  return a / d * b;
}

// --------------------------------------------------------
// This is tool
// --------------------------------------------------------
void Main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n;
  cin >> n;
  // pは与えられる配列
  vector<int> p(n);
  FOR(i, 0, n) cin >> p[i];

  vector<int> ans(n + 1000);
  vi done_swap(n - 1, 0);
  int count = 0;
  bool done_sort = true;
  int number = 1;
  int get_key = 0;

  FOR(i, 0, n) {
    if (p[i] == number) {
      for (int j = i - 1; j >= get_key; j--) {
        swap(p[j], p[j + 1]);
        ans[count] = j + 1;
        count++;
        done_swap[j]++;
        if (done_swap[j] != 1) {
          cout << -1 << endl;
          return;
        }
      }
      number = i + 1;
      get_key = i;
    }
  }
  FOR(i, 0, n - 1) {
    if (p[i] > p[i + 1]) {
      done_sort = false;
      break;
    }
  }
  if (done_sort and count == n - 1) {
    FOR(i, 0, n - 1) { cout << ans[i] << endl; }
  } else {
    cout << -1 << endl;
  }
}

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  Main();
}

#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define vec vector
using namespace std;

const int MOD = 3;
const int INV[3] = {0, 1, 2};

int main() {
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	#ifdef LOCAL
//		freopen("in1.txt", "r", stdin);
//		freopen("in1.txt", "w", stdout);
	#endif
	int n;
	cin >> n;
	string c;
	cin >> c;
	vec<int> a(n), b(n);
	for (int i=0, p=1, q=0, x; i<(n+1)/2; i++) {
		a[i] = a[n-i-1] = (q==0 ? p : 0);
		x = n-i-1;
		while (x%MOD==0) {
			q++;
			x /= MOD;
		}
		p = (p * x) % MOD;

		x = i+1;
		while (x%MOD==0) {
			q--;
			x /= MOD;
		}
		p = (p * INV[x%MOD]) % MOD;
	}
//	cout << "a "; for (int x : a) cout << x << ' '; cout << '\n';
	int sum = 0;
	for (int i=0; i<n; i++) {
		if (c[i] == 'W') sum = (sum + a[i]*1) % MOD;
		if (c[i] == 'R') sum = (sum + a[i]*2) % MOD;
	}
	if (n%2 == 0) sum = -sum;
	sum = (sum%MOD + MOD) % MOD;
	if (sum == 0) cout << 'B';
	if (sum == 1) cout << 'W';
	if (sum == 2) cout << 'R';
	cout << '\n';
}


#include <bits/stdc++.h>

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

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

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

ll f(ll h) {
  if (h == 1) {
    return 1;
  } else {
    return 2 * f(h / 2) + 1;
  }
}

ll mod = 1000000007;

ll powmod(ll a, ll b) {
  ll ret = 1;
  for (int i = 0; i < b; i++) {
    ret = (ret * a) % mod;
  }
  return ret;
}

ll comb(ll n, ll k) {
  map<string, ll> memo;
  if (n == k || k == 0) {
    return 1;
  } else {
    string key = to_string(n) + to_string(k);
    if (memo.count(key) != 0) {
      return memo[key];
    } else {
      ll v = comb(n - 1, k - 1) + comb(n - 1, k);
      memo[key] = v;
      return v;
    }
  }
}

int f(int n, int k) {
  int ret = 0;

  while (n < k) {
    n *= 2;
    ret++;
  }

  return ret;
}

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

  vector<int> v(N), box(K, -1);
  for (int i = 0; i < N; i++) {
    cin >> v[i];
  }
  sort(v.begin(), v.end());

  int i = 0;
  int prev = -1;
  for (auto a : v) {
    if (prev != a) {
      i = 0;
    }

    if (box[i] + 1 == a) {
      box[i] = a;
      i++;
    }

    prev = a;
  }

  ll ans = 0;
  for (auto a : box) {
    if (a != -1) {
      ans += (a + 1);
    }
  }
  cout << ans << endl;
}

#include<bits/stdc++.h>
using namespace std;
long long in(){
    long long x;
    cin>>x;
    return x;
}
int main(){
    long long x=in(),y=in(),a=in(),b=in(),ans=0;
    while(x<b/(a-1) && x<y/a)x*=a,ans++;
    ans+=(y-1-x)/b;
    cout<<ans<<endl;
    return 0;
}


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

// Editorial AC

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

    ll N; cin >> N;
    VLL V(2*N); REP(i,2*N) cin >> V[i];

    priority_queue<ll,VLL,greater<ll>> pq;
    ll ans = SUMLL(V);
    REP(i,N) {
        pq.push(V[N-1-i]);
        pq.push(V[N+i]);
        ans -= pq.top(); pq.pop();
    }
    COUT(ans);

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,m;
int main(){
	cin>>n>>m;
	if(m==0){
		for(int i=1;i<=n;i++)printf("%d %d\n",i*2-1,i*2);
		return 0;
	}
	if(abs(m)>n-2||m<0)return puts("-1"),0;
	if(m>0){
		int now=2*m+4;
		printf("%d %d\n",1,2*m+3);
		printf("%d %d\n",2*m+2,2*m+4);
		for(int i=1;i<=m;i++)printf("%d %d\n",2*i,2*i+1);
		while(now<2*n)printf("%d %d\n",now+1,now+2),now+=2;
	}
	else {
		m=-m;
		int now=2*m+4;
		printf("%d %d\n",2,2*m+4);
		printf("%d %d\n",1,3);
		for(int i=1;i<=m;i++)printf("%d %d\n",2*i+2,2*i+3);
		while(now<2*n)printf("%d %d\n",now+1,now+2),now+=2;
	}
    return 0;
}


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define ll long long
#define endl "\n"
#define    rep(X,Y)     for(X=0;X<Y;X++)
#define    rep1(X,Y)    for(X=1;X<=Y;X++)
#define    rrep(X,Y)    for(X=Y-1;X>=0;X--)
#define    rrep1(X,Y)   for(X=Y;X>=1;X--)
#define ml ll T,g; cin>>T; for(g=0;g<T;g++)
#define case "Case "<<g+1<<": "
#define sf(T) scanf("%lld",&T)
#define pf(T) printf("%lld\n",T)
#define FastRead                      \
                                    ios_base::sync_with_stdio(false); \
                                    cin.tie(0);
#define pi acos(-1)
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
int main()
{
    FastRead
    char a,b,c;
    cin>>a>>b>>c;
    cout<<b<<c<<a;
    return 0;
}

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

#pragma GCC optimize ("O3")
#pragma GCC target ("avx,avx2,fma")

#ifdef LOCAL
    #define D(a) cerr << #a << " = " << a << endl
#else
    #define D(a) 8
#endif
#define fastio ios_base::sync_with_stdio(0); cin.tie(0)
#define dforsn(i,s,n) for(int i=int(n-1);i>=int(s);i--)
#define forsn(i,s,n) for(int i=int(s);i<int(n);i++)
#define all(a) (a).begin(),(a).end()
#define dforn(i,n) dforsn(i,0,n)
#define forn(i,n) forsn(i,0,n)
#define si(a) int((a).size())
#define pb emplace_back
#define mp make_pair
#define snd second
#define fst first
#define endl '\n'
using pii = pair<long long,long long>;
using vi = vector<int>;
using ll = long long;

pii puntos[200000];

struct momento {
  ll a,b,dx,dy;
  bool haySwap;
};

momento resultados[200000+1];

int main() {
    fastio;

    resultados[0].a = 1;
    resultados[0].b = 1;
    resultados[0].dx = 0;
    resultados[0].dy = 0;
    resultados[0].haySwap = false;

    ll n,m,q;

    cin >> n;
    forn(i,n) {
		cin >> puntos[i].fst;
		cin >> puntos[i].snd;
	}

	cin >> m;

	ll numop,p;

	forn(i,m) {
		cin >> numop;

		resultados[i+1] = resultados[i];

		switch(numop){
			case 1:
				swap(resultados[i+1].a,resultados[i+1].b);
				swap(resultados[i+1].dx,resultados[i+1].dy);
				resultados[i+1].b *= -1;
				resultados[i+1].dy *= -1;
				resultados[i+1].haySwap = !resultados[i+1].haySwap;
				break;
			case 2:
				swap(resultados[i+1].a,resultados[i+1].b);
				swap(resultados[i+1].dx,resultados[i+1].dy);
				resultados[i+1].a *= -1;
				resultados[i+1].dx *= -1;
				resultados[i+1].haySwap = !resultados[i+1].haySwap;
				break;
			case 3:
				cin >> p;
				resultados[i+1].dx *= -1;
				resultados[i+1].a *= -1;
				resultados[i+1].dx += 2*p;
				break;
			case 4:
				cin >> p;
				resultados[i+1].dy *= -1;
				resultados[i+1].b *= -1;
				resultados[i+1].dy += 2*p;
				break;
		}

	}

	cin >> q;
	ll A,B;
	forn(i,q){
		cin >> A >> B;
		B--;
		if(!resultados[A].haySwap)
			cout << puntos[B].fst * resultados[A].a + resultados[A].dx << " " << puntos[B].snd * resultados[A].b + resultados[A].dy << endl;
		else
			cout << puntos[B].snd * resultados[A].a + resultados[A].dx << " " << puntos[B].fst * resultados[A].b + resultados[A].dy << endl;

	}

    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<double, double> pd;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<vector<ll>> vvl;
typedef map<int, int> mapi;
typedef map<ll, ll> mapl;
double eps = 1e-12;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define per(i, a, b) for (int i = a; i > b; i--)
#define repl(i, a, b) for (ll i = a; i < b; i++)
#define perl(i, a, b) for (ll i = a; i > b; i--)
#define maploop(p, i) for (auto i = p.begin(); i != p.end(); i++)
#define Ceil(a, b) (((a % b) == 0) ? (a / b) : ((a / b) + 1))
#define stable [](const auto &a, const auto &b) { return a.fi < b.fi; }
#define ln cout << endl;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define INF 2e18
#define FASTIO                        \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define tsolve(t) \
    ll t;         \
    cin >> t;     \
    while (t--)   \
    {             \
        solve();  \
    }

// ==================== Solve =====================
void solve()
{
    string s;
    cin >> s;
    int n = sz(s);
    int idx = -1;
    rep(i, 0, n)
    {
        if (s[i] == '.')
        {
            idx = i;
            break;
        }
    }
    if (idx == -1)
    {
        cout << s << endl;
        return;
    }
    rep(i, 0, idx) cout << s[i];
    cout << endl;
}
// ================================================
signed main()
{
    FASTIO
    //tsolve(t)
    solve();
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e3+9;
const int MAX=1e2+9;
const double ep=1e-10;
const int mod=1e9+7;
const int INF=0x3f3f3f3f;
const double inf=1e20;
const double pi=acos(-1);
#define debug1 puts("?");
#define debug(x) cout<<"##"<<(x)<<endl;
#define mk make_pair
#define PII pair<int,int>
#define PDI pair<double,int>
#define PIII pair<int,PII >
#define PIII1 pair<PII,int>
#define PIIII pair<PII,PII >
#define PIL pair<int,ll>
#define PLI pair<ll,int>
#define PLIII pair<PLI,PII >
#define PLL pair<ll,ll>
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define dep(i,a,b) for(int i=(a);i>=(b);--i)
#define sd(x) scanf("%d",&(x))
#define slld(x) scanf("%lld",&(x))
#define sdd(x,y) scanf("%d%d",&(x),&(y))
#define sc(s) scanf("%s",(s))
#define pd(x) printf("%d\n",(x))
#define plld(x) printf("%lld\n",(x))
#define pdk(x) printf("%d ",(x))
#define plldk(x) printf("%lld ",(x))
#define pdd(x,y) printf("%d %d\n",(x),(y))
#define PR(x) printf("Case %d: ",(x))

/*
*/
int h,w,n,m;
int a[maxn][maxn],res;
bool vis[maxn][maxn];
void solve()
{
	sdd(h,w);
	sdd(n,m);
	rep(i,1,n)
	{
		int x,y;
		sdd(x,y);
		a[x][y]=1;
	}
	rep(i,1,m)
	{
		int x,y;
		sdd(x,y);
		a[x][y]=-1;
	}
	rep(i,1,h)rep(j,1,w)
	{
		if(a[i][j]==1)
		{
			if(!vis[i][j])res++;
			vis[i][j]=1;
			rep(ii,i+1,h)
			{
				if(a[ii][j])break;
				if(!vis[ii][j])res++;
				vis[ii][j]=1;
			}
			dep(ii,i-1,1)
			{
				if(a[ii][j])break;
				if(!vis[ii][j])res++;
				vis[ii][j]=1;
			}
			rep(jj,j+1,w)
			{
				if(a[i][jj])break;
				if(!vis[i][jj])res++;
				vis[i][jj]=1;
			}
			dep(jj,j-1,1)
			{
				if(a[i][jj])break;
				if(!vis[i][jj])res++;
				vis[i][jj]=1;
			}
		}
	}
	pd(res);
}
int main()
{
//    ios::sync_with_stdio(false);
//    cin.tie(0);
    int T=1;
//    sd(T);
    while(T--)solve();
}


#include<bits/stdc++.h>
#include<bits/extc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
using namespace __gnu_pbds;
#ifndef DEBUG
#define cerr while(false) cerr
#endif
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define priority_queue std::priority_queue
#define F first
#define S second

ll a[50];
signed main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int n, t;
	cin>>n>>t;
	int k=n/2;
	vector<int> v1, v2;
	set<ll> sum1, sum2;
	for(int i=0;i<n;i++){
		cin>>a[i];
		if(i<k) v1.push_back(a[i]);
		else v2.push_back(a[i]);
	}
	int sz1=v1.size(), sz2=v2.size();
	for(int i=0;i<(1<<sz1);i++){
		ll sum=0;
		for(int j=0;j<sz1;j++){
			if(i&(1<<j)){
				sum+=v1[j];
			}
		}
		sum1.insert(sum);
	}
	for(int i=0;i<(1<<sz2);i++){
		ll sum=0;
		for(int j=0;j<sz2;j++){
			if(i&(1<<j)){
				sum+=v2[j];
			}
		}
		sum2.insert(sum);
	}
	ll ans=0;
	for(ll i:sum1){
		if(i>t) continue;
		auto it=sum2.upper_bound(t-i);
		it--;
		ans=max(ans, i+(*it));
	}
	cout<<ans<<endl;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll; //int:2*10**9
typedef long double ld;
typedef pair<ll,ll> P;
#define REP(i,n) for(ll i = 0; i<(ll)(n); i++)
#define FOR(i,a,b) for(ll i=(a);i<=(b);i++)
#define FORD(i,a,b) for(ll i=(a);i>=(b);i--)
#define vec2(name,i,j,k) vector<vector<ll>> name(i,vector<ll>(j,k))
#define vec3(name,i,j,k,l) vector<vector<vector<ll>>> name(i,vector<vector<ll>>(j,vector<ll>(k,l)))
#define pb push_back
#define MOD 1000000007 //998244353
#define PI 3.141592653
#define INF 100000000000000 //14
//cin.tie(0);cout.tie(0);ios::sync_with_stdio(false);

int main(){
  ll n, t, a; cin >> n >> t;
  vector<ll> fa;
  vector<ll> la;
  REP(i,n) {
    cin >> a;
    if (i<20) fa.pb(a);
    else la.pb(a);
  }
  sort(fa.rbegin(), fa.rend());
  sort(la.rbegin(), la.rend());
  ll fsize = fa.size();
  ll lsize = la.size();
  vector<ll> fcan;
  vector<ll> lcan;

  REP(i,(1ll<<fsize)) {
    ll val = 0;
    REP(j,fsize){
      if ((i>>j)%2) val += fa[j];
    }
    fcan.pb(val);
  }
  REP(i,(1ll<<lsize)) {
    ll val = 0;
    REP(j,lsize){
      if ((i>>j)%2) val += la[j];
    }
    lcan.pb(val);
  }
  sort(fcan.rbegin(), fcan.rend());
  sort(lcan.begin(), lcan.end());
  ll ans = 0;
  if (lsize==0) {
    sort(fcan.begin(), fcan.end());
    auto ind = upper_bound(fcan.begin(), fcan.end(), t);
    if (ind==fcan.begin()){
      cout << 0 << endl;
    }
    else {
      ind--;
      cout << *ind << endl;
    }
    return 0;
  }
  REP(i,fcan.size()){
    ll nval = fcan[i];
    auto ind = upper_bound(lcan.begin(), lcan.end(), t-nval);
    if (ind!=lcan.begin()) {
      ind--;
      ll maxval = *ind;
      // cout << nval << " " << maxval << endl;
      ans = max(nval+maxval, ans);
    }
  }
  cout << ans << endl;
  return 0;
}


#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long double ld;
typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<string> vs;
// typedef vector<pair<int,int>> vpii;
// typedef vector<vector<int>> vvi;
// typedef vector<vector<char>> vvc;
// typedef vector<vector<string>> vvs;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repll(i, n) for (long long int i = 0; i < n; i++)
#define fin(ans) cout << (ans) << endl
#define P 1000000007
#define STI(s) atoi(s.c_str())  // string to int
#define mp(p, q) make_pair(p, q)
#define Sort(a) sort(a.begin(), a.end())
template <class T>
inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
const int INF = INT_MAX;
const long long LLINF = 1LL << 60;
template <typename T_char>
char ToUpper(char cX) {
    return toupper(cX);
}
// g++ -std=c++1z temp.cpp
//./a.exe
ll digit(ll a) {
    ll count = 0;
    for (ll i = 0; i < a; i++) {
        if (a == 0) {
            break;
        } else {
            a = a / 10;
            count++;
        }
    }
    return count;
}

uintmax_t comb(unsigned int n, unsigned int r) {
    if (r * 2 > n) r = n - r;
    uintmax_t dividend = 1;
    uintmax_t divisor = 1;
    for (unsigned int i = 1; i <= r; ++i) {
        dividend *= (n - i + 1);
        divisor *= i;
    }
    return dividend / divisor;
}
//桁数のカウント
// pow(x,n) == x^n
// sum = accumulate(v.begin(),v.end(),0);
// using Graph = vector<vector<int>>;

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

int digsum(int n) {
    int res = 0;
    while (n > 0) {
        res += n % 10;
        n /= 10;
    }
    return res;
}
#include <iomanip>

// vector vの中のn以下の数で最大のものを返す
int bs(vector<ll>& v, vector<ll>& fb, ll k) {
    int ok = 0;          //これがx以下
    int ng = 100000000;  // x以上
    // 問題によってokとngを入れ替える
    while (abs(ok - ng) > 1) {
        int mid = (ok + ng) / 2;
        if (mid <= k)
            ok = mid;
        else
            ng = mid;
    }
    return ok;
}

//約数全列挙
vector<long long> divisor_efficiency(long long n) {
    vector<long long> ret;
    for (long long i = 1; i * i <= n; i++) {
        if (n % i == 0) {
            ret.push_back(i);
            if (i != 1 && i * i != n) ret.push_back(n / i);
        }
    }
    sort(ret.begin(), ret.end());  // 昇順に並べる
    return ret;
}

//素数判定（効率前処理なし）
bool is_prime_efficiency(long long n) {  // is n prime or not
    for (long long i = 2; i * i <= n; i++) {
        if (n % i == 0) return false;
    }
    return true;
}

vector<pair<long long, long long> > prime_factor(long long n) {
    vector<pair<long long, long long> > ret;
    for (long long i = 2; i * i <= n; i++) {
        if (n % i != 0) continue;
        long long tmp = 0;
        while (n % i == 0) {
            tmp++;
            n /= i;
        }
        ret.push_back(make_pair(i, tmp));
    }
    if (n != 1) ret.push_back(make_pair(n, 1));
    return ret;
}

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

    string s;
    cin >> s;
    int count = 0;

    rep(i,9){
        if(s.substr(i,4) == "ZONe") count++;
    }
    cout  << count << endl;

    //////////////////////////////////////////////////////
    return 0;
}

#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) begin(x),end(x)
#define F(i,n) for (int i = 0; i < n; ++i)
#define F1(i,n) for (int i = 1; i <= n; ++i)
#define dbg(x) cerr << #x << " = " << x << endl
#define dbgg(x) cerr << #x << " = " << x << ' '
#define T(x) x[pool]
#define mineq(x,y) { if ((x) > (y)) (x) = (y); }
#define maxeq(x,y) { if ((x) < (y)) (x) = (y); }
#define MEOW cout << "meowwwww" << '\n'; system("pause");
#define int long long
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;

template<typename T>
ostream& operator <<(ostream &s, const vector<T> &c)
{
	s << "[ "; for (auto it : c) s << it << " "; s << "\b]\n";
	return s;
}

template<typename T>
ostream& operator <<(ostream &s, const pair<int, T> &c)
{
	s << "[ "; cout << c.fi << " , " << c.se << " ] ";
	return s;
}

const int maxn = 345678;
int n;
int a[maxn], pre[maxn];
map<int, int> cnt;

void input()
{
	ios::sync_with_stdio(false); cin.tie(0);
	cin >> n;
	F1 (i, n) cin >> a[i];
}

void solve()
{
	F1 (i, n) if (i & 1) pre[i] = pre[i - 1] + a[i];
		else pre[i] = pre[i - 1] - a[i];
	F (i, n + 1) ++cnt[pre[i]];
	int ans = 0;
	for (auto x : cnt) ans += x.se * (x.se - 1) / 2;
	//F (i, n + 1) dbgg(pre[i]);
	cout << ans << '\n';
}

main()
{
	input();
	solve();
}


#pragma GCC optimize("Ofast")
#pragma loop_opt(on)

#include<bits/stdc++.h>

#define Rushia_mywife ios::sync_with_stdio(0);cin.tie(0);
#define F first
#define S second
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define mp make_pair
#define mt make_tuple
#define FL cout.flush()
#define all(x) (x).begin(),(x).end()
#define mem(x,i) memset((x),(i),sizeof((x)))

using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<long long,long long>;
using ld = long double;

mt19937 mtrd(chrono::steady_clock::now().time_since_epoch().count());

const int mod = 1000000007;
const int mod2 = 998244353;
const ld PI = acos(-1);

#define Bint __int128
#define int long long
int qpow(int x,int powcnt,int tomod){
	int res = 1;
	for(;powcnt;powcnt>>=1,x=(x*x)%tomod)
		if(1&powcnt)res = (res*x)%tomod;
	return (res%tomod);
}
int inv(int x){ return qpow(x,mod-2,mod); }
// --------------------------------------**
const int N = 2e5+10;

string s;
int sum[26];

void solve(){
	cin >> s;
	int n = s.size(),ans = 0;
	sum[s[n-1]-'a']++;
	for(int i=n-2;i>=0;i--){
		if(s[i]==s[i+1]){
			ans += n-i-1-sum[s[i]-'a'];
			mem(sum,0);
			sum[s[i]-'a'] = n-i;
		}
		else
			sum[s[i]-'a']++;
	}
	cout << ans << '\n';
}

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

#include<bits//stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define req(i,n) for(int i = 1;i <=  n; i++)
#define rrep(i,n) for(ll i = n-1;i >= 0;i--)
#define ALL(obj) begin(obj), end(obj)
#define RALL(a) rbegin(a),rend(a)
typedef long long int ll;
typedef long double ld;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const ll INF = 1e18;
ll mod = 998244353;
int main(){
    ll n;cin >>n;
    ll ok = 0,ng = 2e9+7;
    if(n==1) {
        cout << 1 << endl;
        return 0;
    }
    while(ng -ok >1){
        ll mid = (ng+ok)/2;
        ll sum = mid*(mid+1)/2;
        if(sum <= n+1) ok = mid;
        else ng = mid;
    }cout << n+1-ok << endl;
}


// In the name of god
#include <bits/stdc++.h>

#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()
#define Sort(x) sort(all(x))
#define debug(x) cerr << #x << " : " << x << "\n"
#define use_file freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef string str;

const ll maxn = 3e5 + 5, mod = 1e9 + 7, inf = 1e18 + 7;
ll n, dp[maxn];
vector <ll> adj[maxn];
bool bl[maxn];
void dfs(ll u){
	dp[u] ++;
	bl[u] = 1;
	vector <ll> v0, v1;
	for(ll v : adj[u]){
		dfs(v);
		if(bl[v])bl[u] = 1 - bl[u];
		if(bl[v])v1.pb(dp[v]);
		else v0.pb(dp[v]);
	}
	if(adj[u].size() == 0){
		bl[u] = 1;
		dp[u] = 1;
		return;
	}
	if(v0.size())Sort(v0);
	if(v1.size())Sort(v1);
	if(v0.size())reverse(all(v0));
	if(v1.size())reverse(all(v1));
	while(v0.size()){
		if(v0.back() > 0)break;
		dp[u] += v0.back();
		v0.pop_back();
	}
	bool b = 1;
	while(v1.size()){
		if(b)dp[u] += v1.back();
		else dp[u] -= v1.back();
		v1.pop_back();
		b = 1 - b;
	}
	while(v0.size()){
		if(b)dp[u] += v0.back();
		else dp[u] -= v0.back();
		v0.pop_back();
	}

}
int main(){
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> n;
	for(ll i = 2; i <= n; i ++){
		ll p;
		cin >> p;
		adj[p].pb(i);
	}
	dfs(1);
	for(ll i = 0; i <= n; i ++){
		if(i - (n - i) == dp[1])return cout << i << endl, 0;
	}
}

#include<bits/stdc++.h>
#define LL long long
#define SZ(x) (int)x.size()-1
#define F(i,a,b) for (int i=a;i<=b;++i)
#define DF(i,a,b) for (int i=a;i>=b;--i)
#define pb push_back
#define ms(a,b) memset(a,b,sizeof a)
using namespace std;
int read(){
    char ch=getchar(); int w=1,c=0;
    for (;!isdigit(ch);ch=getchar()) if (ch=='-') w=-1;
    for (;isdigit(ch);ch=getchar()) c=(c<<3)+(c<<1)+(ch^48);
    return w*c;
}
const int M=2e5+10;
int n,dp[M],sz[M];
vector<int> v[M];
bool cmp(int x,int y){
	return 2*dp[x]-sz[x]<2*dp[y]-sz[y];
}
void dfs(int x){
	sz[x]=dp[x]=1;
	vector<int> t;
	int tmp=1,t0=0,t1=0;
	F(i,0,SZ(v[x])){
		int y=v[x][i];
		dfs(y);
		if (sz[y]&1){
			t.pb(y);
		}
		else{
			if (2*dp[y]-sz[y]<0){
				tmp+=dp[y];
			}
			else{
				t0+=dp[y];
				t1+=sz[y]-dp[y];
			}
		}
		sz[x]+=sz[y];
	}
	sort(t.begin(),t.end(),cmp);
	F(i,0,SZ(t)){
		int y=t[i];
		if (i&1) tmp+=sz[y]-dp[y];
		else tmp+=dp[y];
	}
	if ((SZ(t)+1)&1) tmp+=t1;
	else tmp+=t0;
	dp[x]=tmp;
//	cout<<x<<" "<<dp[x]<<" "<<sz[x]<<"\n";
}
int main(){
	n=read();
	F(i,2,n){
		int x=read();
		v[x].pb(i);
	}
	dfs(1);
	cout<<dp[1]<<"\n";
    return 0;
}


#include <stdio.h>
inline int read() {
    int b = getchar(), c = 0;
    int m = b == 45;
    if (m) b = getchar();
    while (b > 47) {
        c = c * 10 + b - 48;
        b = getchar();
    }
    return m ? - c : c;
}
int p[200001];
int main() {
    int a[400002];
    int n = read(), m = read();
    long s = 0;
    for (int i = 1; i <= n + n; i++) a[i] = read();
    for (int i = 0; i < m + m; i++) p[read()] = 1;
    p[1] = 1;
    for (int i = 1; i < 200001; i++) {
        if (p[i]) s += a[i] - a[i + n];
    }
    puts(s ? "No" : "Yes");
}

#include<bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
vector<long long int> diff;
vector<bool> seen;
long long int sum;

void dfs(const Graph &G,int v){
  sum += diff[v];
  seen[v] = true;
  for(auto next_v:G[v]){
    if(seen[next_v]) continue;
    else dfs(G,next_v);
  }
}

int main(){
  int N,M;
  cin >> N >> M;
  vector<long long int> A(N);
  for(int i=0;i<N;i++) cin >> A[i];
  vector<long long int> B(N);
  for(int i=0;i<N;i++) cin >> B[i];
  diff.assign(N,0);
  for(int i=0;i<N;i++) diff[i] = A[i]-B[i];
  Graph G(N,vector<int>(0));
  seen.assign(N,false);
  for(int i=0;i<M;i++){
    int c,d;
    cin >> c >> d;
    c--;
    d--;
    G[c].push_back(d);
    G[d].push_back(c);
  }
  bool flag = true;
  for(int i=0;i<N;i++){
    if(seen[i]) continue;
    else{
      sum=0;
      dfs(G,i);
      if(sum != 0){
        flag = false;
        break;
      }
    }
  }
  if(flag) cout << "Yes" << endl;
  else cout << "No" << endl;
}


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

#define rep(i,n) for (int i = 0; i < (n); ++i)
using ll = long long;

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

    ll ans = 0;
    while (1) {
        if (y / a <= x) break;
        if (a * x >= y) break;
        if (a * x > x + b) break;
        x *= a;
        ++ans;
    }

    ans += (y - 1 - x) / b;
    cout << ans << endl;

    return 0;
}

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

#define ll          long long int
#define pb          push_back
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (int)x.size()
#define endl        '\n'
#define rep(i,a,b)  for(i=a;i<b;i++)
#define mod         1000000007

template<typename T, typename U> static inline void amin(T &x, U y){ if(y<x) x=y; }
template<typename T, typename U> static inline void amax(T &x, U y){ if(x<y) x=y; }

const int N = 2e6 + 5;

void solve()
{
    ll n, i, aoki = 0, taki = 0, ans = 0;
    cin >> n;
    ll a[n], b[n];
    vector<pair<ll,int>> v(n);
    rep(i, 0, n)
    {
    	cin >> a[i] >> b[i];
    	v[i].F = 2*a[i] + b[i];
    	v[i].S = i;
    	aoki += a[i];
    }
    sort(all(v));
    reverse(all(v));
    for(i = 0 ; taki <= aoki && i < n; i++)
    {
    	int idx = v[i].S;
        taki += a[idx] + b[idx];
        aoki -= a[idx];
        ans++;
    }
    cout << ans;
}
int main()
{
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    //freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);
    int t = 1;
    //cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define x first
#define y second
#define pdd pair<double,double>
#define pii pair<int,int>
#define pll pair<LL,LL>
#define mp make_pair
#define LL long long
#define ULL unsigned long long
#define sqr(x) ((x)*(x))
#define pi acos(-1)
using namespace std;
#define MXN 200005
void solve(){
    int n;
    scanf("%d",&n);
    int a[200005],b[200005],p[200005];
    for(int i = 0;i<n;i++)scanf("%d",&a[i]);
    for(int i = 0;i<n;i++)scanf("%d",&b[i]);
    for(int i = 0;i<n;i++)scanf("%d",&p[i]),p[i]--;
    for(int i = 0;i<n;i++){
        if(a[i]<=b[p[i]]&&p[i]!=i){
            printf("-1\n");
            return ;
        }
    }
    priority_queue<pii> pq;
    for(int i = 0;i<n;i++){
        pq.push(mp(a[i],i));
    }
    vector<pii> ans;
    while(!pq.empty()){
        pii pp=pq.top();
        pq.pop();
        while(p[pp.y]!=pp.y){
            ans.pb(mp(p[pp.y],pp.y));
            swap(p[pp.y],p[p[pp.y]]);
        }
    }
    printf("%d\n",ans.size());
    for(auto it:ans){
        printf("%d %d\n",it.x+1,it.y+1);
    }
}
//a*10 b*10/m

//b*10%m
int main(){
    int t=1;
    //scanf("%d",&t);
    //int n;
    while(t--){
        solve();
    }
}
/*
the first two line contains string "x" followed by two space followed by number one followed by two space followed by string "y".
 */

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

int main() {

  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> b(n);
  for (int i=0;i<n;i++) cin >> a[i];
  b = a;
  sort(b.begin(),b.end());
  reverse(b.begin(),b.end());
  int ans = 0;
  for (int i=0;i<n;i++) {
    int len = 0;
    for (int j=0;j<n;j++) {
      if (a[j] >= b[i]) {
        len++;
      }
      else {
        ans = max(ans,b[i]*len);
        len = 0;
      }
    }
    ans = max(ans,b[i]*len);
  }
  cout << ans << endl;
}


#include <bits/stdc++.h>

using namespace std;

int decimaltoOctal(int deciNum)
{

    int octalNum = 0, countval = 1;
    int dNo = deciNum;

    while (deciNum != 0) {

        int remainder = deciNum % 8;

        octalNum += remainder * countval;

        countval = countval * 10;
        deciNum /= 8;
    }

    return octalNum;
}

int main() {

	int n;
	cin>>n;

	set<int> s;

	for(int i=1; i<=n; i++) {

		string deci = to_string(i);
		string oct = to_string(decimaltoOctal(i));

		for(int j=0; j<deci.length(); j++) {

			if(deci[j] == '7') s.insert(i);
		}

		for(int j=0; j<oct.length(); j++) {

			if(oct[j] == '7') s.insert(i);
		}
	}

	cout<<n-s.size()<<endl;

	return 0;
}

#include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <stdio.h>
#include <math.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define ll long long
#define be(x) x.begin(), x.end()
using namespace std;

int main()
{

    string s;
    cin  >> s;

    for (int i = 1; i < s.size(); i++)
    {
        cout<<s[i];
    }
    cout<<s[0]<<endl;

    return 0;
}


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

int main()
{
	int N, D, H;
	cin >> N >> D >> H;
	vector<pair<int, int>> O(N);
	for (size_t i = 0; i < N; i++)
	{
		cin >> O.at(i).first >> O.at(i).second;
	}
	double ret = 0;
	for (size_t i = 0; i < N; i++)
	{
		ret = max(ret, O.at(i).second - (H - O.at(i).second) * O.at(i).first / (double)(D - O.at(i).first));
	}
	cout << ret;
	return 0;
}

//FIRST THINK THEN CODE.

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define rep(i,a,b) for(ll i=a;i<b;++i)
#define rrep(i,a,b) for(ll i=a;i>b;--i)
#define FOR(i,n)  for(ll i=0;i<n;i++)
#define vi vector<int>
#define vl vector<ll>
#define ld long double
#define vld vector<ld>
#define vvi vector<vector<int>>
#define vvl vector<vector<long long>>
#define vvld vector<vector<ld>>
#define pii pair<int,int>
#define pll pair<long,long>
#define vpii vector<pii>
#define vpll vector<pll>
#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define d1(x) cout<<(x)<<endl
#define d2(x,y) cout<<(x)<<" "<<(y)<<endl
#define d3(x,y,z) cout<<(x)<<" "<<(y)<<" "<<(z)<<endl
#define d4(a,b,c,d) cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<endl
#define PI 3.1415926535897932384626433832795
#define fix(f,n) fixed<<setprecision(n)<<f
#define all(x) x.begin(),x.end()
#define rev(p) reverse(p.begin(),p.end());
#define endl "\n"
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define popcount(x) __builtin_popcountll(x)
#define sz(x) ((ll)x.size())
const ll M = 1000000007;
const ll MM = 998244353;
ll begtime = clock();
#define end_routine() cerr << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//#define trace(...)
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}

template<typename T, typename F>
void chmax( T &a, F b) {
  if (b > a)a = b;
}

template<typename T, typename F>
void chmin( T &a, F b) {
  if (b < a)a = b;
}

/* if you want to multiply 2 64 bit numbers mod c.
 i.e a*b%c.
 this function will help  in preventing "overflow".
*/

ll mulmod(ll a, ll b, ll c) {

  ll ans = 0;
  ll y = a % c;
  while (b) {
    if (b & 1) {
      (ans += y) %= c;
    }
    y = y * 2 % c;
    b >>= 1;
  }
  return ans;

}

ll powM(ll a, ll b, ll m)
{
  if (b < 0)return 0;
  if (a <= 0)return 0;
  a %= m;

  ll ans = 1LL;
  while (b)
  {
    if (b & 1)ans = ans * a % m;
    //ans = mulmod(ans, a, m);
    a = a * a % m;
    //a = mulmod(a, a, m);
    b >>= 1;
  }

  return ans;
}

ll powMbig(ll a, ll b, ll m)
{
  if (a <= 0)return 0;
  a %= m;

  ll ans = 1LL;
  while (b)
  {
    if (b & 1)//ans = ans * a % m;
      ans = mulmod(ans, a, m);
    //a = a * a % m;
    a = mulmod(a, a, m);
    b >>= 1;
  }

  return ans;
}

ll poww(ll a, ll b)
{

  ll ans = 1;
  while (b)
  {
    if (b & 1)ans = ans * a;
    a = a * a;
    b >>= 1;
  }

  return ans;

}

string tostring(ll x) {
  stringstream sss;
  sss << x;
  string ans = sss.str();
  return ans;
}

const ll N = 1e6 + 5;

ll gandu(ll c) {
  return (c * (c - 1) / 2);
}

int main() {

  IOS;

#ifndef ONLINE_JUDGE

  freopen("input1.txt", "r", stdin);
  freopen("output1.txt", "w", stdout);

#endif

  ll n, x;
  cin >> n >> x;
  string s; cin >> s;
  FOR(i, n) {
    if (s[i] == 'o')x++;
    else x = max(x - 1, 0ll);
  }

  cout << x;

  return 0;
}


#include <iostream>
#include <vector>
#include <deque>
#include <algorithm>
#include <numeric>
#include <string>
#include <cstring>
#include <list>
#include <unordered_set>
#include <tuple>
#include <cmath>
#include <limits>
#include <type_traits>
#include <iomanip>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <set>
#include <bitset>
#include <regex>
#include <random>
#include<bits/stdc++.h>
//#include<atcoder/all>

using namespace std;
//using namespace atcoder;

typedef long long ll;
#define rep(i,n)for(ll i=0;i<n;++i)
#define exout(x) printf("%.12f\n", x)
const double pi = acos(-1.0);
const ll MOD = 1000000007;
const ll INF = 1e18;
const ll MAX_N = 1010101;

//組み合わせの余りを求める
ll fac[MAX_N], finv[MAX_N], inv[MAX_N];
void COMinit() {
	fac[0] = fac[1] = 1;
	finv[0] = finv[1] = 1;
	inv[1] = 1;
	for (int i = 2; i < MAX_N; i++) {
		fac[i] = fac[i - 1] * i % MOD;
		inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
		finv[i] = finv[i - 1] * inv[i] % MOD;
	}
}

// 二項係数計算
long long COM(ll n, ll k) {
	if (n < k) return 0;
	if (n < 0 || k < 0) return 0;
	return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

// mod.m での a の逆元 a^ { -1 } を計算する
long long modinv(long long a, long long m) {
	long long b = m, u = 1, v = 0;
	while (b) {
		long long t = a / b;
		a -= t * b; swap(a, b);
		u -= t * v; swap(u, v);
	}
	u %= m;
	if (u < 0) u += m;
	return u;
}

//最大公約数
ll gcd(ll x, ll y) {
	return y ? gcd(y, x % y) : x;
}

ll lcm(ll x, ll y) {
	if (x == 0 || y == 0)return 0;
	return (x / gcd(x, y) * y);
}

//union-find木について
ll par[101010];
ll rank2[101010];

void init(ll n) {
	rep(i, n) {
		par[i] = i;
		rank2[i] = 0;
	}
}

ll find(ll x) {
	if (par[x] == x) {
		return x;
	}
	else {
		return par[x] = find(par[x]);
	}
}

void unite(ll x, ll y) {
	x = find(x);
	y = find(y);
	if (x == y)return;
	if (rank2[x] < rank2[y]) {
		par[x] = y;
	}
	else {
		par[y] = x;
		if (rank2[x] == rank2[y]) {
			rank2[x]++;
		}
	}
}

ll dx[4] = { 1,-1,0,0 };
ll dy[4] = { 0,0,1,-1 };
ll move[201010];

//long longしか使わない
//素数は1より大きい
//lower_boundは指定したkey以上の要素の一番左のイテレータをかえす
//upper_boundは指定したkeyより大きい要素の一番左のイテレータをかえす
int main() {
	ll ans = INF;
	rep(i, 4) {
		ll a;
		cin >> a;
		ans = min(ans, a);
	}
	cout << ans << endl;
	return 0;
}

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

using namespace std;

const int COUNT_A = 100;

int a[4];
int n = 0;

int min(int a, int b) {

    if (a > b) {
        return b;
    }
    return a;
}

int main() {

    cin >> a[0] >> a[1] >> a[2] >> a[3];
    n = min(a[0], min(a[1], min(a[2], a[3])));
    printf("%d",n);

}


//Author: Fuadul Hasan(fuadul202@gmail.com)
//BSMRSTU,Gopalganj
//#include<bits/stdc++.h>
#define _USE_MATH_DEFINES
#include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <random>
#include <cassert>
#include <cstring>
#include <sstream>
#include <complex>
#include <numeric>
#include <iostream>
#include <algorithm>
#include <functional>
#include <unordered_set>
#include <unordered_map>
using namespace std;

//int input........
template <typename T> inline void readline(T &n) {n = 0; int f = 1; register int ch = getchar();
 for (; !isdigit(ch); ch = getchar()) if (ch == '-') f = -1;for (; isdigit(ch);
ch = getchar()) n = (n << 3) + (n << 1) + ch - '0';n = n * f;}
template <typename T, typename TT> inline void readline(T &n, TT &m) { readline(n); readline(m); }
template <typename T, typename TT, typename TTT> inline void readline(T &n, TT &m, TTT &l) { readline(n, m); readline(l);}

// long long input........
template <typename T> inline void readlinel(T &n) {n = 0; long long f = 1; register long long ch = getchar();
 for (; !isdigit(ch); ch = getchar()) if (ch == '-') f = -1;for (; isdigit(ch);
ch = getchar()) n = (n << 3) + (n << 1) + ch - '0';n = n * f;}
template <typename T, typename TT> inline void readlinel(T &n, TT &m) { readlinel(n); readlinel(m); }
template <typename T, typename TT, typename TTT> inline void readlinel(T &n, TT &m, TTT &l) { readlinel(n, m); readlinel(l);}

//debug..........
#define error(args...) {vector<string>_v=split(#args,',');err(_v.begin(),args);cout<<endl;}
vector<string> split(const string &s, char c) {vector<string>v; stringstream ss(s); string x;
while (getline(ss, x, c))v.emplace_back(x); return move(v);} void err(vector<string>::iterator it) {}
template<typename T, typename... Args>void err(vector<string>::iterator it, T a, Args...args) {
cout << it->substr((*it)[0] == ' ', it->length()) << " = " << a << " "; err(++it, args...);}

//............ignore it..................//
#define F first
#define S second
#define Pi atan(1)*4
#define mp make_pair
#define pb  push_back
const int M	= 1e9 + 7;
#define ll  long long int
#define happy cin.tie(0);
#define point(x) setprecision(x)
void Print(int n){printf("%d ", n);}
void print(int n){printf("%d\n", n);}
#define Test printf("Case %d: ",tc++);
int length(string s){return s.size();}
void print(ll n){printf("%I64d\n", n);}
void Print(ll n){printf("%I64d ", n);}
#define coding ios::sync_with_stdio(false);
#define Unique(c) (c).resize(unique(all(c))-(c).begin())
void print(int n,int m){printf("%d ",n);printf("%d ",m);}
void print(ll n,ll m){printf("%I64d ",n);printf("%I64d ",m);}
#define vout(v) for (auto z: v) cout << z << " "; cout << endl;
int length(long long x){int l = 0;for(long long i=x;i;i/=10)l++;return l;}
int dx[8]= {1,0,-1,0,-1,-1,1,1};int dy[8]= {0,1,0,-1,-1,1,-1,1}; int tc = 1;
void print(int n,int m,int k){printf("%d ",n);printf("%d ",m);printf("%d ",k);}
void print(ll n,ll m,ll k){printf("%I64d ",n);printf("%I64d ",m);printf("%I64d ",k);}
long long pow(long long a,long long n){ll res = 1;while(n){if(n&1) res = ((res%M)*(a%M))%M;a = ((a%M)*(a%M))%M;n>>=1;}return res%M;}

template<class T> bool remin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
template<class T> bool remax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }

#define   pr 	pair<int, int>
#define   vpr 	vector<pr>
#define   vi   	std::vector<int>
#define   vll  	std::vector<ll>
#define   all(n) n.begin(),n.end()

const int Inf           = (int)2e9 + 5;
const ll  Lnf           = (ll)2e18 + 5;
const int N             = 5e3 + 5;
const int NN            = 1e6 + 5;

int solve()
{
	//happy coding

	int a,b,c,d;
	cin>>a>>b>>c>>d;
	cout<<min(a,min(b,min(c,d)))<<endl;

	return 0;
	//error();
}
int main(){
	int test = 1;
	//readline(test);//(void)getchar();
	while (test--)solve();return 0;
}

// Username: WillTheBill
// Time of creation: 2020-11-22 12:49
// Original filename: m.cp.cpp

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

// shortcuts
#define pb push_back
#define fi first
#define se second
#define all(_v) _v.begin(),_v.end()
#define ll long long

// IO
#define multitest signed __T; cin >> __T; while(__T--)
template<typename T>
void _read(T& t);
template<typename T>
void _read(vector<T>&v);
template<typename T1, typename T2>
void _read(pair<T1,T2>&p);
template<typename T>
void _read(T& t){cin >> t;}
template<typename T>
void _read(vector<T>&v){for(unsigned _i=0;_i<v.size();_i++)_read(v[_i]);}
template<typename T1, typename T2>
void _read(pair<T1,T2>&p){_read(p.first);_read(p.second);}
void _masterread(){}
template<typename T,typename... V>
void _masterread(T& t, V&... v){_read(t);_masterread(v...);}
#define re(...)_masterread(__VA_ARGS__)

template<typename T>
void _print(T t);
template<typename T>
void _print(vector<T>&v);
template<typename T1, typename T2>
void _print(pair<T1,T2>&p);
template<typename T>
void _print(T t){cout<<t;}
template<typename T>
void _print(vector<T>&v){for(unsigned _i=0;_i<v.size();_i++)_print(v[_i]),cout<<(_i==v.size()-1?"":" ");}
template<typename T1, typename T2>
void _print(pair<T1,T2>&p){_print(p.first);cout<<" ";_print(p.second);}
void _masterprint(){cout<<endl;}
template<typename T,typename... V>
void _masterprint(T t, V... v){_print(t);if(sizeof...(v))cout<<" ";_masterprint(v...);}
#define pr(...)_masterprint(__VA_ARGS__)

// DEBUG
// colored output???
template <typename T> // start forward declaration
void _debug(T t);
template<typename T1,typename T2>
void _debug(pair<T1,T2> p);
template<typename T>
void _debug(vector<T>v);
template <typename T> // end forward declaration
void _debug(T t){cerr<<t;}
template<typename T1,typename T2>
void _debug(pair<T1,T2> p){cerr<<"{";_debug(p.first);cerr<<", ";_debug(p.second);cerr<<"}";}
template<typename T>
void _debug(vector<T>v){cerr<<"(";for(unsigned _i=0;_i<v.size();_i++)_debug(v[_i]),cerr<<(_i==v.size()-1?"":", ");cerr << ")";}
void _masterdebug(){cerr<<"]\n";}
template<typename T,typename... V>
void _masterdebug(T t,V... v){_debug(t);if(sizeof...(v))cerr<<", ";_masterdebug(v...);}
#ifdef __local__
#define debug(...) cerr<<"["<<#__VA_ARGS__<<"] = [";_masterdebug(__VA_ARGS__)
#else
#define debug(...)
#endif

// TYPES
#define int long long

signed main(){
    ios_base::sync_with_stdio(false); cin.tie(NULL);
	int a,b,c,d; re(a,b,c,d);
	int res = 3;
	if(a==c&&b==d) res = 0;
	if(a + b == c + d) res = min(res, 1ll);
	if(a - b == c - d) res = min(res, 1ll);
	if(abs(a-c) + abs(b - d) <= 3) res = min(res, 1ll);
	for(int i = a - 10; i <= a + 10; i++){
		for(int j = b - 10; j <= b + 10; j++){
			if(!(i + j == a + b || i - j == a - b || abs(i - a) + abs(j - b) <= 3)) continue;
			if(i == c && j == d) res = min(res, 1ll);
			if(i + j == c + d) res = min(res, 2ll);
			if(i - j == c - d) res = min(res, 2ll);
			if(abs(i - c) + abs(j - d) <= 3) res = min(res, 2ll);
		}
	}
	if((a + b) % 2 == (c + d) % 2){
		res = min(res, 2ll);
	}
	/*int l = -1e10, r = 1e10;
	while(l <= r){
		int m = (l + r) / 2;
		int d1 = abs(a + m - c) + abs(b + m - d);
		if(d1 == 0){
			res = min(res, 1ll);
		}
		if(d1 <= 3){
			res = min(res, 2ll);
			break;
		}
		int d2 = abs(a + m + 1 - c) + abs(b + m + 1 - d);
		if(d2 < d1){
			l = m + 1;
		}else{
			r = m - 1;
		}
	}
	l = -1e10, r = 1e10;
	while(l <= r){
		int m = (l + r) / 2;
		int d1 = abs(a + m - c) + abs(b - m - d);
		if(d1 == 0){
			res = min(res, 1ll);
		}
		if(d1 <= 3){
			res = min(res, 2ll);
			break;
		}
		int d2 = abs(a + m + 1 - c) + abs(b - (m + 1) - d);
		if(d2 < d1){
			l = m + 1;
		}else{
			r = m - 1;
		}
	}*/
	pr(res);
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <math.h>
#include <queue>
#include <numeric>
using namespace std;

using ll = long long;

#define For(i,a,b) for(ll i=(a);i<(ll)(b);i++)
#define rFor(i,a,b) for(ll i=(a);i>=(ll)(b);i--)
#define co(a) cout<<a<<endl
#define Sort(a) sort((a).begin(),(a).end())
#define Rev(a) reverse((a).begin(),(a).end())
#define Unique(a) (a).erase(unique((a).begin(), (a).end()),(a).end())
#define cfs(a) cout<<fixed<<setprecision(a)

ll N;

vector<bool> ans(100005);
vector<ll> check(100005);
void dfs(vector<ll> &coler,vector<vector<ll>> &ki,ll now, ll bef) {
	if (0 == check[coler[now - 1]]) {
		ans[now] = true;
	}
	check[coler[now - 1]]++;
	For(i, 0, ki[now].size()) {
		if (ki[now][i] != bef) {
			dfs(coler, ki, ki[now][i], now);
		}
	}
	check[coler[now - 1]]--;
}

int main() {
	//ll N;
	cin >> N;
	vector<ll> coler(N); //colerだけ0から
	For(i, 0, N) {
		cin >> coler[i];
	}
	vector<vector<ll>> ki(N+1, vector<ll>(0));
	ll A, B;
	For(i, 0, N - 1) {
		cin >> A >> B;
		ki[A].push_back(B);
		ki[B].push_back(A);
	}
	check[coler[0]] = 1;
	ans[1] = true;
	For(i, 0, ki[1].size()) {
		dfs(coler, ki, ki[1][i], 1);
	}
	For(i, 0, N + 1) {
		if (ans[i]) {
			co(i);
		}
	}
}

#include <bits/stdc++.h>

#pragma GCC optimize(2)
using namespace std;
#define int long long
typedef long long LL;
typedef long long ll;
const int INF = 0x3f3f3f3f;
//const int inf = 1e18;
//const int mod = 998244353;
const int mod = 1e9 + 7;
int gcd(int a, int b) { return !b ? a : gcd(b, a % b); }
const int maxn = 1e6 + 10;
const int N = 6e6 + 100;

int c[maxn];
vector<int > e[maxn];
int vis[maxn];
int g[maxn];

void dfs(int x,int fa){
    if (vis[c[x]]==0) g[x]=1;
    vis[c[x]]++;
    for(auto v:e[x]) if (fa!=v) dfs(v,x);
    vis[c[x]]--;
}

void solve() {
    int n;
    cin>>n;
    for(int i=1;i<=n;i++) cin>>c[i];
    for (int i = 0; i < n - 1; ++i) {
        int u,v;
        cin>>u>>v;
        e[u].push_back(v);
        e[v].push_back(u);
    }
    dfs(1,0);
    for (int i = 1; i <=n; ++i)
        if (g[i]) cout<<i<<"\n";
}

signed main() {
    //ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int _ = 1;
//    cin >> _;
    while (_--) {
        solve();//cout<<"\n";
    }
    return 0;
}


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

#define    int               long long int
#define  ld         long double
 #define  f(i,j,n)            for(int i = j; i <= n; i++)
#define  r(i,n,j)       for(int i = n; i >= j; i--)
 #define  all(container)       container.begin() , container.end()
#define  sz(container)   (int)container.size()
 #define  ff             first
#define  ss             second
 #define  pii      pair <int , int>
#define  sp(x)       setprecision(x)
#define  mod  1000000007
#define  endl  "\n"
 #define  pb    push_back
#define  mp    make_pair
 #define  T     int ttt; cin >> ttt; while(ttt--)
#define fast ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);

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

int32_t main()
{
    fast
    ld x,y,a,b;
    cin>>x>>y>>a>>b;
    if(x>a) {
        swap(x,a);
        swap(y,b);
    }
    ld d=abs(x-a);
    ld m= b*d;
    m=m/(ld)(y+b);
    cout<<fixed<<sp(10)<<a-m;
}

#include<bits/stdc++.h>
using namespace std;
typedef int64_t ll;
typedef long double ld;
const ll MOD=1000000007;
const ll MODA=998244353;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
long long gcd(long long a,long long b){
    ll gcdmax=max(a,b);
    ll gcdmin=min(a,b);
    while(true){
        if(gcdmax%gcdmin==0)break;
        else gcdmax%=gcdmin;
        swap(gcdmin,gcdmax);
    }
    return gcdmin;
}
ll powerup(ll N,ll P,ll M){
    if(P==0)return 1;
    else if(P%2==0){
        ll t=powerup(N,P/2,M);
        return t*t%M;
    }
    else return N*powerup(N,P-1,M)%M;
}
vector<ll> find_divisor(ll N){
  ll k=1;
  while(k*k<=N){
    k++;
  }
  vector<ll> A(1);
  rep(i,k){
    if(i==1)A.at(0)=1;
    else if(i>=2){
      if(N%i==0)A.push_back(i);
    }
  }
  ll t=0;
  t=A.size();
  rep(i,t){
    if(A.at(t-i-1)*A.at(t-i-1)!=N)A.push_back(N/A.at(t-1-i));
  }
  return A;
}
vector<ll> fac;
vector<ll> finv;
vector<ll> inv;
void COMinit(ll N,ll P){
    rep(i,N+1){
        if(i==0){
            fac.push_back(1);
            finv.push_back(1);
            inv.push_back(1);
        }
        else if(i==1){
            fac.push_back(1);
            finv.push_back(1);
            inv.push_back(1);
        }
        else{
            fac.push_back(fac.at(i-1)*i%P);
            inv.push_back(P-inv.at(P%i)*(P/i)%P);
            finv.push_back(finv.at(i-1)*inv.at(i)%P);
        }
    }
}

ll COM(ll n,ll k,ll P){
    if(n<k)return 0;
    if(n<0||k<0)return 0;
    return fac.at(n)*(finv.at(k)*finv.at(n-k)%P)%P;
}

struct UnionFind {
    vector<ll> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2

    UnionFind(ll N) : par(N) { //最初は全てが根であるとして初期化
        for(ll i = 0; i < N; i++) par[i] = i;
    }

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

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

    bool same(ll x, ll y) { // 2つのデータx, yが属する木が同じならtrueを返す
        ll rx = root(x);
        ll ry = root(y);
        return rx == ry;
    }
};

int main(){
    ld sx,sy,gx,gy;
    cin>>sx>>sy>>gx>>gy;
    cout<<fixed<<setprecision(7)<<sx*(gy/(sy+gy))+gx*(sy/(sy+gy))<<endl;
}


#include<bits/stdc++.h>
typedef long long ll;
ll gi(){
	ll x=0,f=1;
	char ch=getchar();
	while(!isdigit(ch))f^=ch=='-',ch=getchar();
	while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
	return f?x:-x;
}
#define all(x) (x).begin(),(x).end()
template<class T>void cxk(T&a,T b){a=a>b?a:b;}
template<class T>void cnk(T&a,T b){a=a<b?a:b;}
int a[100010],b[100010];
std::vector<ll>A,B;
int main(){
#ifdef LOCAL
	freopen("in.in","r",stdin);
	//freopen("out.out","w",stdout);
#endif
	int n=gi();
	ll sa=0;
	for(int i=1;i<=n;++i)sa+=a[i]=gi();
	for(int i=1;i<=n;++i)b[i]=gi()-a[i];
	A.resize(n/2+1),B.resize(n/2+1);
	for(int i=1,j=1;i<=n;i+=2,++j)A[j]=b[i],B[j]=b[i+1];
	std::sort(A.begin()+1,A.end(),std::greater<ll>());
	std::sort(B.begin()+1,B.end(),std::greater<ll>());
	for(int i=1;i<=n/2;++i)A[i]+=A[i-1],B[i]+=B[i-1];
	ll ans=0;for(int i=0;i<=n/2;++i)cxk(ans,A[i]+B[i]);
	printf("%lld\n",ans+sa);
	return 0;
}

#include <bits/stdc++.h>
// ifブロックはインデントがいる
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
#define repp(i, l, r) for (long long i = (l); i < (r); i++)
#define rep(i, n) for (long long i = 0; i < (n); ++i)
#define per(i, n) for (long long i = (n); i >= 0; --i)
#define all(v) v.begin(), v.end()
const int INF = 1 << 30;
const long long LINF = 1LL << 60;
const long long int MOD = 1000000007;
using namespace std;
using ll = long long;
using P = pair<long long, long long>;
template <class T>
inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T>
inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

using PP = pair<ll, P>;

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

vector<vector<ll> > g, dist;
vector<vector<vector<ll>>> predicted_cost;
vector<vector<P>> pre;
const ll h = 30, w = 30;

void dijkstra(int sx, int sy) {
    dist = vector<vector<ll> >(h, vector<ll>(w, INF));
    pre = vector<vector<P> >(h, vector<P>(w, P(-1, -1)));
    dist[sx][sy] = 0;
    priority_queue<PP, vector<PP>, greater<PP> > pq;
    pq.push(PP(0, P(sx, sy)));

    while (!pq.empty()) {
        PP p = pq.top();
        pq.pop();
        ll c = p.first;
        ll vx = p.second.first, vy = p.second.second;

        rep(i, 4) {
            ll nx, ny;
            nx = vx + dx[i], ny = vy + dy[i];
            if (nx < 0 || ny < 0 || nx >= h || ny >= w) continue;
            if (dist[nx][ny] <= g[nx][ny] + c) continue;
            dist[nx][ny] = g[nx][ny] + c;
            pre[nx][ny] = P(vx, vy);
            pq.push(PP(dist[nx][ny], P(nx, ny)));
        }
    }
}

vector<P> restore(ll tx, ll ty) {
    vector<P> path;
    ll x, y;
    for (; tx != -1 || ty != -1; tx = pre[x][y].first, ty = pre[x][y].second) {
        path.push_back(P(tx, ty));
        x = tx, y = ty;
    }
    reverse(path.begin(), path.end());

    return path;
}

char compare_pos(P s, P t) {
    if (s.first < t.first)
        return 'D';
    if (s.first > t.first)
        return 'U';
    if (s.second < t.second)
        return 'R';
    if (s.second > t.second)
        return 'L';
}

ll vector_average(vector<ll> &cost){
    ll sum = accumulate(all(cost),0LL);
    return sum / cost.size();
}

//ミョ(-ω- ?)
int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    g.resize(30);
    predicted_cost.resize(30);
    rep(i, 30) {
        g[i].resize(30);
        predicted_cost[i].resize(30);
        rep(j, 30) g[i][j] = 6000;
    }

    rep(q,1000){
        P s,t;
        cin >> s.first >> s.second >> t.first >> t.second;
        dijkstra(s.first, s.second);
        auto rstr = restore(t.first, t.second);
        rep(i, rstr.size() - 1) { cout << compare_pos(rstr[i], rstr[i + 1]); }
        cout << endl;
        ll len;
        cin >> len;
        if(rstr.size())len /= rstr.size();
        rep(i, rstr.size() - 1) {
            predicted_cost[rstr[i].first][rstr[i].second].push_back(len);
            if(q > 100)g[rstr[i].first][rstr[i].second] = vector_average(predicted_cost[rstr[i].first][rstr[i].second]);
        }
    }
    return 0;
}

/*グラフ情報とは別に、パス長からの平均を順次格納していった2+1次元の配列をやりながら、その平均値を取って暫定の頂点間距離にするといい感じに収束していくのでは
-> 84163614602

パス長から乱数パラメーターのV_ip,H_ipを求められれば道のコストの尤度が大きくなって幸せになる

クエリの点数計算式 round(2312311 * \sigma_{k=1}^1000 {0.998^{1000-k} * a_k / b_k})から、序盤より後半のak / bkが重視される
序盤でより正確な道のコストを求められれば点数の改善ができる
パスに関して、M=1のときはパスの評価値に平均を使えるけど、M=2のときはyj(ramdom)によって[0,yj),[yj,2]

*/


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

ll solve() {
    ll A, B;
    cin >> A >> B;
    ll d = B-A;
    ll ans = 0;
    for ( ll d = B-A; d > 0; d-- ) {
        if ( B/d != (A%d==0 ? A/d : (A/d+1)) ) {
            return d;
        }
    }

    return 1;
}

int main() {
    auto ans = solve();
    cout << ans << "\n";
    return 0;
}

#include <bits/stdc++.h>

using namespace std;
// using namespace __gnu_pbds;

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

#define fori(i, n) for (int i = 0; i < n; i++)
#define ford(i, n) for (int i = n - 1; i >= 0; i--)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define repd(i, a, b) for (int i = a; i >= b; i--)
#define trav(x, a) for (auto &x : a)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define eb emplace_back
#define endl '\n'
#define sz(a) (int)(a).size()
#define fi first
#define se second

clock_t time_p = clock();
void time_taken()
{
    time_p = clock() - time_p;
    cerr << "Time Taken : " << (float)(time_p) / CLOCKS_PER_SEC << "\n";
}

const ll mod = 1e9 + 7;
const ll INF = 1e18;

template <class T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;

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

    int R, C;
    cin >> R >> C;

    vii adj[R * C];
    fori(i, R) fori(j, C - 1)
    {
        int from = i * C + j;
        int to = i * C + (j + 1);
        int w;
        cin >> w;
        adj[from].pb({to, w});
        adj[to].pb({from, w});
    }

    fori(i, R - 1) fori(j, C)
    {
        int from = i * C + j;
        int to = (i + 1) * C + j;
        int w;
        cin >> w;
        adj[from].pb({to, w});
    }

    min_heap<pair<ll, int>> pq;
    pq.push({0, 0});

    vl d(R * C, INF);
    d[0] = 0;
    while (!pq.empty())
    {
        auto [dis, at] = pq.top();
        pq.pop();
        auto [r, c] = make_pair(at / C, at % C);
        if (d[at] < dis)
            continue;
        for (auto [to, w] : adj[at])
        {
            if (d[to] > d[at] + w)
            {
                d[to] = d[at] + w;
                pq.push({d[to], to});
            }
        }

        for (int to = 0; to < r; to++)
        {
            int tto = to * C + c;
            int cost = r - to + 1;
            if (d[tto] > d[at] + cost)
            {
                d[tto] = d[at] + cost;
                pq.push({d[tto], tto});
            }
        }
    }

    cout << d.back() << endl;

    time_taken();
    return 0;
}


#include <bits/stdc++.h>

#define ll             long long
#define sf             scanf
#define pf             printf
#define nl             printf("\n")
#define pb             push_back
#define TEST           int Test;cin>>Test;for(int _t=1;_t<=Test;_t++)

using namespace std;

int main()
{
	int a,b,c,d;
	sf("%d %d %d %d",&a,&b,&c,&d);
	double theta = (atan((double)(c-a)/(double)(b+d)));
	double x = (b*tan(theta))+a;
	pf("%lf\n",x);
	return 0;
}

#include<iostream>
#include<iomanip>
#include<string>
#include<algorithm>
#include<vector>
#include<utility>
#include<tuple>
#include<map>
#include<queue>
#include<stack>
#include<deque>
#include<bitset>
#include<math.h>
using namespace std;
using ll = int64_t;
using Graph = vector<vector<int> >;
const ll M = 1000000007;

int main(){
    double sx,sy,gx,gy;
    cin >> sx >> sy >> gx >> gy;
    gy=-gy;
    double ans=sx-(sy*(gx-sx)/(gy-sy));
    cout << fixed << setprecision(8) << ans << endl;
}


#include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
// using namespace atcoder;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define rrep1(i, n) for (int i = n; i >= 1; i--)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define eb emplace_back
#define fi first
#define se second
#define sz(x) (int)(x).size()
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
typedef long long int ll;
typedef pair<ll, ll> P;
// typedef modint1000000007 mint;

void speedUpIO() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
}

template <class T> bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
template <class T> bool chmin(T &a, const T &b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}
/*--------------------------------------------------*/
const int INF = 1e9;
const ll LINF = 2e18;
const int MAX = 100010;

void solve() {
    ll k, n, m;
    cin >> k >> n >> m;
    V<ll> a(k);
    rep(i, k) cin >> a[i];
    V<ll> b(k);
    rep(i, k) b[i] = a[i] * m / n;
    ll d = m - accumulate(all(b), 0LL);
    V<P> bp(k);
    rep(i, k) bp[i] = P(m * a[i] % n, i);
    sort(rall(bp));
    rep(i, d) {
        int j = bp[i].se;
        b[j]++;
    }
    rep(i, k) {
        cout << b[i] << " ";
    }
    cout << "\n";
}

int main() {
    speedUpIO();
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
        // cout << solve() << "\n";
        // cout << (solve() ? "Yes" : "No") << "\n";
        // cout << fixed << setprecision(15) << solve() << "\n";
    }
    return 0;
}

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

#include<cstdio>
#include<cstring>

using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

const int CM = 1 << 17, CL = 12;
char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct;
const ll ma0 = 1157442765409226768;
const ll ma1 = 1085102592571150095;
const ll ma2 = 71777214294589695;
const ll ma3 = 281470681808895;
const ll ma4 = 4294967295;
inline int getint() {
	if (ci - owa > 0) {
		memcpy(cn, owa, CL);
		ci -= CM;
		fread(cn + CL, 1, CM, stdin);
	}
	ll tmp = *(ll*)ci;
	if ((tmp & ma0) ^ ma0) {
		int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0);
		tmp = tmp << dig & ma1;
		tmp = tmp * 10 + (tmp >> 8) & ma2;
		tmp = tmp * 100 + (tmp >> 16) & ma3;
		tmp = tmp * 10000 + (tmp >> 32) & ma4;
		ci += (72 - dig >> 3);
	}
	else {
		tmp = tmp & ma1;
		tmp = tmp * 10 + (tmp >> 8) & ma2;
		tmp = tmp * 100 + (tmp >> 16) & ma3;
		tmp = tmp * 10000 + (tmp >> 32) & ma4;
		ci += 8;
		if ((ct = *ci++) >= '0') {
			tmp = tmp * 10 + ct - '0';
			if (*ci++ == '0') {
				tmp = tmp * 10;
				ci++;
			}
		}
	}
	return tmp;
}
int tmp[200001];
void pakuri_sort(int N, int A[]) {
	const int b = 8;
	rep(k, 4) {
		int kazu[1 << b] = {}, kazu2[1 << b] = {};
		rep(i, N) kazu[A[i] >> k * b & ((1 << b) - 1)]++;
		rep(i, (1 << b) - 1) kazu[i + 1] += kazu[i];
		for (int i = N - 1; i >= 0; i--) tmp[--kazu[A[i] >> k * b & ((1 << b) - 1)]] = A[i];
		k++;
		rep(i, N) kazu2[tmp[i] >> k * b & ((1 << b) - 1)]++;
		rep(i, (1 << b) - 1) kazu2[i + 1] += kazu2[i];
		for (int i = N - 1; i >= 0; i--) A[--kazu2[tmp[i] >> k * b & ((1 << b) - 1)]] = tmp[i];
	}
}

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

	int N = getint(), M = getint();

	int A[200010];
	rep1(i, M) A[i] = getint();
	A[0] = 0;
	A[M + 1] = N + 1;
	pakuri_sort(M, A + 1);

	int mae = 0;
	int saishou = 2e9;
	int t = 0;
	rep(i, M + 1) {
		int kari = A[i + 1] - A[i] - 1;
		if (kari) {
			if (kari < saishou) saishou = kari;
			tmp[t++] = kari;
		}
	}

	if (saishou > 1.5e9) {
		printf("0");
		return 0;
	}

	ll kotae = 0;
	rep(i, t) kotae += (tmp[i] + saishou - 1) / saishou;

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

	Would you please return 0;
}


/*****author: maxifier******/
#include <algorithm>
#include <array>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <stack>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <vector>
#include <queue>
using namespace std;

#define int         long long int
#define emp         emplace_back
#define pb          push_back
#define ppb         pop_back
#define mp          make_pair
#define all(v)      v.begin(),v.end()
#define uniq(v)     (v).erase(unique(all(v)),(v).end())
#define lb          lower_bound
#define ub          upper_bound

const int inf = 1e18;
const int32_t m = 1e9 + 7;
const int32_t mm = 998244353;

void maxifier() {
	int n;
	cin >> n;

	vector<int> a(n), b(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	for (int i = 0; i < n; i++) {
		cin >> b[i];
	}
	sort(a.begin(), a.end());
	sort(b.begin(), b.end());

	int x = b[0] - a[n - 1];
	cout << ((x) >= 0 ? (x + 1) : 0);
}

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

	int tc, t = 0;
	tc = 1;
	//cin >> tc;
	while (tc--) {
		//++t;
		//cout << "Case" << " " << "#" << t << ":" << " ";
		maxifier();
	}
	return 0;
}

//Hare Krishna
//author: Dipjoy Basak
//dip_10

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

#define endl "\n"
#define ll long long
#define int ll
#define ld long double
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define vi vector<int>
#define pi pair<int, int>
#define vpi vector<pair<int, int>>
#define rep(i, l, r) for (int i = l; i <= r; i++)
#define rrep(i, r, l) for (int i = r; i >= l; i--)
#define readarray(a,n) rep(i,0,n-1)cin>>a[i];
#define all(v) v.begin(), v.end()
#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x<<" "; _print(x); cerr << endl;
#else
#define debug(x);
#endif
#define INF INT_MAX

//*----------------------------------------DEBUG functions--------------------------*

void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(ld t) {cerr << t;}
void _print(double t) {cerr << t;}

template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}

//-----------------------------------------------------------------------------------------------------------------------------

const int mod=1000000007;
const int maxn=100005;
int x,y,z,n,k;

void solve()
{
    string s;
    cin>>s;
    reverse(s.begin(),s.end());
    rep(i,0,s.size()-1){
        if(s[i]=='9'){
            cout<<'6';
        }else if(s[i]=='6'){
            cout<<'9';
        }else{
            cout<<s[i];
        }
    }
}
int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cout << setprecision(15) << fixed;
    int t;
    t = 1;
    // cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}