#include <bits/stdc++.h>

#define LL long long

#define ull unsigned long long

#define F(i, j, k) for(int i = j; i <= k; i++)

#define DF(i, j, k) for(int i = j; i >= k; i--)

using namespace std;

template <typename T> inline void read(T &n) {
	T w = 1;
	n = 0;
	char ch = getchar();
	while (!isdigit(ch) && ch != EOF) {
		if (ch == '-') w = -1;
		ch = getchar();
	}
	while (isdigit(ch) && ch != EOF) {
		n = (n << 3) + (n << 1) + (ch & 15);
		ch = getchar();
	}
	n *= w;
}

template <typename T> inline void write(T x) {
	T l = 0;
	ull y = 0;
	if (!x) {
		putchar(48);
		return;
	}
	if (x<0) {
		putchar('-');
		x = -x;
	}
	while (x) {
		y = y * 10 + x % 10;
		x /= 10;
		l++;
	}
	while (l) {
		putchar(y % 10 + 48);
		y /= 10;
		l--;
	}
}

template <typename T> inline void writes(T x) {
	write(x);
	putchar(' ');
}

template <typename T> inline void writeln(T x) {
	write(x);
	puts("");
}

template <typename T> inline void checkmax(T &a,T b) { a = a > b ? a : b; }

template <typename T> inline void checkmin(T &a,T b) { a = a < b ? a : b; }

const int MOD = 998244353;

inline int ksm(int x, int y) {
	int k = 1;
	while (y) {
		if (y & 1) k = (long long)k * x % MOD;
		x = (long long)x * x % MOD;
		y >>= 1;
	}
	return k;
}

inline void add(int &a, int b) { a = (a + b >= MOD) ? a + b - MOD : a + b; }

inline int dec(int a, int b) { return (a - b < 0) ? a - b + MOD : a - b; }

int main() {
	//freopen(".in", "r", stdin);
	//freopen(".out", "w", stdout);
	int n, m, k, ans = 0;
	read(n);
	read(m);
	read(k);
	if (n == 1 && m == 1) { writeln(k); return 0; }
	if (n == 1) {
		F(i, 1, k) add(ans, dec(ksm(k - i + 1, m), ksm(k - i, m)));
		writeln(ans);
		return 0;
	}
	if (m == 1) {
		F(i, 1, k) add(ans, dec(ksm(i, n), ksm(i - 1, n)));
		writeln(ans);
		return 0;
	}
	F(i, 1, k) add(ans, (long long)dec(ksm(i, n), ksm(i - 1, n)) * ksm(k - i + 1, m) % MOD);
	writeln(ans);
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define endl "\n"
#define yes "Yes\n"
#define no "No\n"
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL);

ll maxn = 1000006;

void solve(int test)
{
    vector<ll>sieve(maxn,0);
    ll i,j;

    for(i=2;i<maxn;i++)
    {
        if(sieve[i] == 0)
        {

            for(j=i;j<maxn;j+=i)sieve[j]++;
            for(j=i*i;j<maxn;j+=i*i)sieve[j] = -10000005;
        }
    }

    ll l,r;
    cin>>l>>r;

    if(l==1)
    {
        l = min(r,2LL);
    }

    if(l==1 && r==1)
    {
        cout<<0<<endl;
        return;
    }

    ll tot = (r-l+1)*(r-l);
    tot = tot/2;
    tot = 0;

    for(i=2;i<=r;i++)
    {
        if(sieve[i] <= 0)continue;

        ll val = r/i - (l-1)/i;
        if(sieve[i]%2==1)
        {
            tot += val*(val-1)/2;
        }
        else
        {
            tot -= val*(val-1)/2;
        }
    }
    ll sub = 0;
    for(i=l;i<=r;i++)
    {
        for(j=2*i;j<=r;j+=i)sub++;
    }

    tot = tot - sub;
    tot = 2*tot;
    cout<<tot<<endl;

}

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

  for(int x=1;x<=t;x++)
  {
      solve(x);
  }

  return 0;
}


#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using ld = long double;

template <typename T1, typename T2>
void mmin(T1& a, const T2& b) {
  if (b < a) a = b;
}

template <typename T1, typename T2>
void mmax(T1& a, const T2& b) {
  if (b > a) a = b;
}

const int MAXN = 5020, P = 998'244'353;
int n, m, k;
char c[MAXN][MAXN];
ll d[MAXN][MAXN];

constexpr ll qpow(ll base, ll exp) {
  ll ans = 1;
  while (exp) {
    if (exp % 2) ans = ans * base % P;
    base = base * base % P;
    exp /= 2;
  }
  return ans % P;
}

constexpr ll two_thirds = qpow(3, P - 2) * 2 % P;

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

  cin >> n >> m >> k;
  for (int i = 0; i < k; i++) {
    int h, w;
    char q;
    cin >> h >> w >> q;
    c[h][w] = q;
  }

  d[1][1] = qpow(3, n * m - k);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      if (i == 1 && j == 1) continue;
      if (i > 1) {
        if (!c[i - 1][j]) d[i][j] = (d[i][j] + d[i - 1][j] * two_thirds) % P;
        else if (c[i - 1][j] != 'R') d[i][j] = (d[i][j] + d[i - 1][j]) % P;
      }
      if (j > 1) {
        if (!c[i][j - 1]) d[i][j] = (d[i][j] + d[i][j - 1] * two_thirds) % P;
        else if (c[i][j - 1] != 'D') d[i][j] = (d[i][j] + d[i][j - 1]) % P;
      }
    }
  cout << d[n][m] << endl;
}

#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
typedef long double ld;
#define REP(i, n) for(ll i = 0LL; i < (ll)(n); i++)
#define REPR(i, n) for (ll i = (ll)(n) - 1; i >= 0; i--)
#define FOR(i, n, m) for(ll i = (ll)n; i < (ll)(m); i++)
#define ALL(x) (x).begin(),(x).end()
// #define MOD 1000000007
#define MOD 998244353

#define INF (1LL << 62)

#define PI (acos(-1))
#define PRINT(x) std::cout << x << endl

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; }
ll countDigit(ll n) { return floor(log10(n) + 1); }
typedef pair <ll,ll> P;
static const ll dx[8] = {0,1,0,-1, 1, -1, 1, -1}, dy[8] = {1,0,-1,0,1,1,-1,-1};

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    for (int i = 0; i < (int)v.size(); ++i) os << v[i] << " ";
    return os;
}

template <typename T1, typename T2>
ostream& operator<<(ostream& os, const map<T1, T2>& m)
{
    for (auto p : m) os << "<" << p.first << ", " << p.second << "> ";
    return os;
}

int main()
{
  ll N, K;
  string S;
  cin >> N >> K >> S;
  for (ll k = 0; k < K; k++) {
    string T = S;

    for(ll i = 0; i < T.size(); i+=1) {
      char te1 = S[(2*i)%N];
      char te2 = S[(2*i+1)%N];
      if (te1 != te2) {
        if (te1 == 'R') {
          if (te2 == 'S') {
            T[i] = te1;
          } else {
            T[i] = te2;
          }
        } else if (te1 == 'S') {
          if (te2 == 'R') {
            T[i] = te2;
          } else {
            T[i] = te1;
          }
        } else {
          if (te2 == 'R') {
            T[i] = te1;
          } else {
            T[i] = te2;
          }
        }
      } else {
        T[i] = te1;
      }
    }
    S = T;
  }
  cout << S[0] << endl;
  return 0;
}


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

typedef long long ll;

int main() {
    int N;
    scanf("%d", &N);
    vector<ll> A(N), S(N + 1, 0), M(N + 1, 0);
    rep(i, N) {
        scanf("%lld", &A[i]);
        S[i + 1] = S[i] + A[i];
        M[i + 1] = max(M[i], A[i]);
    }
    ll ans = 0;
    rep(i, N) {
        ans += S[i + 1];
        printf("%lld\n", ans + M[i + 1] * (i + 1));
    }
}

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

int main() {
  int N;
  cin >> N;
  int i = 1;
  int sum = 0;
  while(true) {
    sum = sum + i;
    if (sum >= N) {
      cout << i << endl;
      return 0;
    }
    i++;
  }
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<pair<ll,ll>> vp;
#define pb(a)	push_back(a)
#define mp(a,b)	make_pair(a,b)
#define mod 1000000007
#define all(a)	a.begin(),a.end()
#define rall(a)	a.rbegin(),a.rend()
#define rep(i,a,b,c)	for(ll i=a;i<b;i+=c)
#define repn(i,a,b,c)	for(ll i=a;i>=b;i-=c)
#define YES cout<<"YES\n"
#define NO cout<<"NO\n"
#define yes cout<<"Yes\n"
#define no "no\n"
#define endl '\n'
#define F first
#define S second
#define IOS ios::sync_with_stdio(0); cin.tie(0);
#define max(x,y)	((x<y)?y:x)
#define min(x,y)	((x<y)?x:y)
ll gcd(ll x, ll y){
	ll temp=min(x,y);
	y=x+y-temp;
	x=temp;
	while(y%x!=0){
	temp=x;
	x=y%x;
	y=temp;
	}
	return x;
}
bool prime[1000005];
vll spf(20000005,0);
void Seive(ll n){
	rep(i,0,n,1)	prime[i]=true;
	prime[0]=prime[1]=false;
    for (int p=2; p*p<n; p++)
    {
        if (prime[p]==false)	continue;
        for (int i=p*p; i<n; i+=p)
            prime[i] = false;
    }
}
void Seive_spf(ll n){
	spf[0]=0, spf[1]=1;
    for (ll p=2; p<n; p++){
    	if (spf[p]>0)	continue;
    	spf[p]=p;
    	for(ll i=p;i<n;i+=p)	if(spf[i]==0)	spf[i]=p;
    }
}
ll nCrmodp_dp(ll n, ll r, ll p){
	ll c[r+1]={0};
	c[0]=1;
	rep(i,1,n+1,1){
		repn(j,min(i,r),0,1)	c[j] = (c[j]+c[j-1])%p;
	}
	return c[r];
}
ll nCrmodp(ll n, ll r, ll p){
	if(r==0)	return 1;
	return (nCrmodp(n/p, r/p, p)*nCrmodp_dp(n%p, r%p, p))%p;
}
void print2d(vvll a){
	rep(i,0,a.size(),1){
		rep(j,0,a[i].size(),1)	cout<<a[i][j]<<' ';
		cout<<endl;
	}
}
void print1d(vll a){
	rep(i,0,a.size(),1)	cout<<a[i]<<' ';
	cout<<endl;
}

void solve(){
	ll n;
	cin>>n;
	vll a(n), b(n);
	rep(i,0,n,1)	cin>>a[i];
	rep(i,0,n,1)	cin>>b[i];
	ll mi=a[0], ma=b[0];
	rep(i,1,n,1){
		mi=max(mi,a[i]);
		ma=min(ma,b[i]);
	}
	cout<<max(ma-mi+1,0)<<endl;
}

int main(){
	IOS
	#ifdef OK
	freopen("input.txt", "r", stdin);
 	freopen("output.txt", "w", stdout);
 	#endif
	int t=1;
	//cin>>t;
	/*Seive(1000000)
	vll pr[1000000];
	rep(i,2,1000000,1)	if(prime[i]==false)	pr.pb(i);
	*/
	while(t--)	solve();
	return 0;
}

#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<queue>
#include<cmath>
//#include<bits/stdc++.h>
#define int long long
#define for0(i, n) for(int i = 0; i < (n); i++)
#define for1(i, n) for(int i = 1; i <= (n);i++)
#define mp make_pair
#define all(x) x.begin(),x.end()
#define puts(x) cout << (x) << "\n"
constexpr int mod = 998244353;
using namespace std;
int input() { int t; cin >> t; return t; }
int n, s, t, ndm, ndi, dt[234567], ans[234567];
vector<int>g[234567];
void f1(int i, int d, int p) {
	dt[i] = d;
	if (ndm < d) { ndm = d; ndi = i; }
	for (int j : g[i])if (j != p)f1(j, d + 1, i);
}
int f2t = 1;
void f2(int i, int p) {
	if (ans[i] == 0)ans[i] = f2t;
	for (int j : g[i])if (p != j && dt[j] > dt[i]) {
		f2t++; f2(j, i); f2t++;
	}
	for (int j : g[i])if (p != j && dt[j] < dt[i]) {
		f2t++; f2(j, i); f2t++;
	}
}
signed main() {
	//function<int(int, int)> gcd = [&gcd](int a, int b) {if (b == 0)return a; return gcd(b, a % b); };
	cin >> n;
	for0(i, n - 1) {
		int a, b; cin >> a >> b;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	ndm = 0; ndi = 1; f1(1, 0, 1); s = ndi;
	ndm = 0; ndi = 1; f1(s, 0, s); t = ndi;
	ndm = 0; ndi = 1; f1(t, 0, t);
	f2(s, s);
	for1(i, n) { cout << ans[i] << (i == n ? "\n" : " "); }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long   ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ll>  vl;
typedef vector<pair<int,int>> vpi;
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define pb    push_back
#define mp    make_pair
#define rev(i,a,b) for(int i=a;i<b;i++)
#define sz(a)   (int)a.size()
#define all(a)  a.begin(),a.end()
const int  MOD = 1000000000+7;
ll mul(ll x,ll y)
{return (x*y)%MOD;}
ll bin_pow(ll x,ll p)
{if(p==0)return 1;if(p&1)return mul(x,bin_pow(x,p-1));return bin_pow(mul(x,x),p/2);}
ll  gcd(ll a,ll b)
{if(b==0)return a;return gcd(b,a%b);}
ll lcm(ll a, ll b)
{return a / gcd(a, b) * b;}
bool prime(ll n)
{if(n<=1)return 0;for(ll i=2;i*i<=n;i++)if(n%i==0)return 0; return 1;}
bool adj[5005][5005];
int deg[5005];
int main()
{
    ll n;
    cin>>n;
    vl f;
    n=n*2;
    f.pb(1);
    f.pb(n);
    for(ll i=2;i*i<=n;i++)
    {
        if(n%i==0)
        {
            f.pb(i);
            if(i*i!=n)
                f.pb(n/i);
        }
    }
   // for(auto i:f)cout<<i<<" ";
    ll a=0;
    for(int i=0;i<sz(f);i++)
    {
        ll x=(n/f[i])+1-f[i];
        if(x%2==0)
            a++;
    }
    cout<<a<<endl;
}

#include <bits/stdc++.h>
#define clog(x) std::clog << (#x) << " is " << (x) << '\n';
using LL = long long;

int main() {
	//freopen("in", "r", stdin);
	std::cin.tie(nullptr)->sync_with_stdio(false);
	LL n;
	std::cin >> n;
	std::vector<LL> F{1, 1};
	while (F.back() <= n) F.emplace_back(F[F.size() - 2] + F[F.size() - 1]);
	F.pop_back();
	int s = F.size(), now = s - 1;
	std::vector<int> a(s), ans;
	while (now >= 0 && n) {
		if (F[now] <= n) { // 不会超过两次的
			n -= F[now];
			++a[s - 1 - now];
		}
		--now;
	}
	for (int i = 0; i < s; ++i) {
		if ((i & 1) == (s & 1)) {
			if (a[i]) ans.emplace_back(2);
			ans.emplace_back(4);
		} else {
			if (a[i]) ans.emplace_back(1);
			ans.emplace_back(3);
		}
	}
	std::cout << ans.size() << '\n';
	for (auto x : ans) std::cout << x << '\n';
	return 0;
}


/**
 * Author: Daniel
 * Created Time: 2021-03-27 20:02:48
**/

// time-limit: 2000
#include <bits/stdc++.h>

using namespace std;

#define F first
#define S second
#define ER erase
#define IS insert
#define PI acos(-1)
#define PB pop_back
#define MP make_pair
#define MT make_tuple
#define LB lower_bound
#define UB upper_bound
#define EB emplace_back
#define lowbit(x) (x & -x)
#define SZ(x) ((int)x.size())
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
#define SOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);cout<<fixed<<setprecision(10)

typedef long long LL;
typedef vector<LL> VL;
typedef vector<int> VI;
typedef pair<LL, LL> PLL;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef unsigned long long ULL;
typedef pair<double, double> PDD;
typedef vector<pair<LL, LL> > VPLL;
typedef vector<pair<int, int> > VPII;

template <typename A> using VE = vector<A>;
template <typename A> using USET = unordered_set<A>;
template <typename A> using HEAP = priority_queue<A>;
template <typename A, typename B> using PA = pair<A, B>;
template <typename A, typename B> using UMAP = unordered_map<A, B>;
template <typename A> using RHEAP = priority_queue<A, vector<A>, greater<A> >;

template <typename A> A MAX(const A &a) { return a; }
template <typename A> A MIN(const A &a) { return a; }
template <typename A> A MAX(const A *a, const A *b) { assert(b - a > 0); return *max_element(a, b); }
template <typename A> A MIN(const A *a, const A *b) { assert(b - a > 0); return *min_element(a, b); }
template <typename A, typename... B> A MAX(const A &a, const B&... b) { return max(a, MAX(b...)); }
template <typename A, typename... B> A MIN(const A &a, const B&... b) { return min(a, MIN(b...)); }
template <typename A, typename B = typename std::iterator_traits<A>::value_type> B MAX(A a, A b) { assert(b - a > 0); return *max_element(a, b); }
template <typename A, typename B = typename std::iterator_traits<A>::value_type> B MIN(A a, A b) { assert(b - a > 0); return *min_element(a, b); }

mt19937 rng((unsigned int)chrono::steady_clock::now().time_since_epoch().count());

///////////////////////////////////////////////////////////////////////////
//////////////////// DO NOT TOUCH BEFORE THIS LINE ////////////////////////
///////////////////////////////////////////////////////////////////////////

// check the limitation!!!
const int N = 100010, M = 1010;

// read the question carefully!!!
int main()
{
    SOS;

    int n, m, x, y;
    cin >> n >> m >> x >> y;
    x -- , y -- ;
    VS g(n);
    for (auto &s : g) cin >> s;
    int res = 1;
    for (int j = y + 1; j < m; j ++ )
        if (g[x][j] == '.') res ++ ;
        else break;
    for (int j = y - 1; j >= 0; j -- )
        if (g[x][j] == '.') res ++ ;
        else break;
    for (int i = x - 1; i >= 0; i -- )
        if (g[i][y] == '.') res ++ ;
        else break;
    for (int i = x + 1; i < n; i ++ )
        if (g[i][y] == '.') res ++ ;
        else break;
    cout << res << '\n';
    return 0;
}

// GOOD LUCK!!!

#include <iostream>
#include <algorithm>
#include <vector>
#include <tuple>
using namespace std;
using p = pair<int, int>;
vector<p> d = {p(-1,0), p(1,0), p(0,-1), p(0,1)};

int main() {
  int h, w, x, y;
  cin >> h >> w >> x >> y;
  x--;
  y--;

  vector<vector<char>> v(h, vector<char>(w));

  for (int i=0; i<h; i++) {
    for (int j=0; j<w; j++) {
      cin >> v[i][j];
    }
  }

  int ans = 0;

  for (auto coord: d) {
    bool b = true;
    int s, t;
    int t_x = x, t_y = y;
    tie(s, t) = coord;
    while (b) {
      t_x += s;
      t_y += t;
      if ((t_x>=h) || (t_x<0) || (t_y>=w) || (t_y<0) || (v[t_x][t_y]=='#')) {
        b = false;
      } else {
        ans += 1;
      }
    }
  }

  if (v[x][y]=='.') {
    cout << ans+1 << endl;
  } else {
    cout << ans << endl;
  }
}


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

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

#define FOR(i,a,b) for(int i=a; i <=(b); i++)
#define ROF(i,a,b) for(int i=a; i >=(b); i--)
#define loop(i,a) for(int i=0; i < (a); i++)
#define rloop(i,a) for(int i=(a-1); i >= 0; i--)
#define MAX(a,b) (a < b) ? (b) : (a)
#define BIGGEST(a,b,c) (MAX(a,b) < c) ? (c) : MAX(a,b)
#define END "\n"
#define pb push_back
#define ff first
#define ss second
#define sort(a) sort(a.begin(),a.end())

const int MAX_N = 1e5 + 5;
const int MAX_L = 20; // ~ Log N
const long long MOD = 1e9 + 7;
const long long INF = 1e9 + 7;

void solve(){

    int n;
    cin >> n;

    map<string,ll>mp;
    string s[n];
    string ans = "satisfiable";
    loop(i,n){
        cin >> s[i];
        if(s[i][0] != '!'){
          mp[s[i]] = 1;
        }
    }

    loop(i, n) {
        if (s[i][0] == '!') {
            if (mp[s[i].substr(1, s[i].length() - 1)]) {
                ans = s[i].substr(1, s[i].length() - 1);
            }
        }
    }
    cout << ans;

}

int main() {

    ios_base::sync_with_stdio(false);cin.tie(NULL);
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
        freopen("error.txt","w",stderr);
    #endif

    // int t; cin >> t;

    // while(t--)
	solve();

     cerr<<"\ntime taken : "<<(float)clock()/CLOCKS_PER_SEC<<" secs"<<endl;
   return 0;
}

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

using namespace std;

typedef long long ll;

const ll MAX_N = 1e18 + 2000;

int main () {
  ios::sync_with_stdio(false);

  ll n;
  cin >> n;

  ll on = n;

  vector<ll> fib = {0, 1};
  while (fib.back() < MAX_N) {
    ll a = fib.back();
    ll b = *next(fib.rbegin());
    fib.push_back(a + b);
  }

  int mx = -1;
  vector<int> decomp;
  for (int i = (int) fib.size() - 1; i > 0; i--) {
    if (n >= fib[i]) {
      decomp.push_back(i);
      n -= fib[i];
      mx = max(mx, i);
    }
  }

  if (mx % 2 == 1) {
    mx++;
  }

  vector<int> adds (mx, 0);
  for (int u : decomp) {
    adds[mx - u] = 1;
  }

  ll a = 0, b = 0;
  vector<int> sol;
  for (int i = 0; i < (int) adds.size(); i += 2) {
    if (a != 0) {
      b += a;
      sol.push_back(4);
    }

    if (adds[i]) {
      b++;
      sol.push_back(2);
    }

    if (b != 0) {
      a += b;
      sol.push_back(3);
    }

    if (adds[i + 1]) {
      a++;
      sol.push_back(1);
    }
  }

  cout << sol.size() << '\n';
  for (int o : sol) {
    cout << o << '\n';
  }
}


#include<bits/stdc++.h>
using namespace std;
#define     M                     1000000007
#define     ll                    long long
#define     FIO                   ios_base::sync_with_stdio(false);cin.tie(NULL)
#define     ifor(st,lim)          for(int i=st;i<lim;i++)
#define     jfor(stt,llim)        for(int j=stt;j<llim;j++)
#define     pb                    push_back

void solve() {
	// cout<<"X";
	int x,y; cin>>x>>y;

	if(x == y) {
		cout<<x;
	}else {
		// 0 1 2
		// 0 1 2
		cout<< 3 - (x + y);
	}
}
void file_i_o() {
	#ifndef ONLINE_JUDGE
		freopen("input.txt","r",stdin);
		freopen("output.txt","w",stdout);
	#endif
}
int main()
{
	file_i_o();
	FIO;
	int t=1;
	while(t--) {
		solve();
	}
}

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

#define ll long long int
#define fr(i,n) for(int i=0;i<n;i++)
#define pct(i,n) cout<<"Case "<<i<<": "<<n<<endl;
#define fast()    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define e   "\n"

int main()
{
    int x,y; cin>>x>>y;
    if(x==y){
        cout<<x<<e;
    }

    else if(x==0&&y==1){
        cout<<2<<e;
    }

    else if(x==1&&y==0){

        cout<<2<<e;
    }
    else if(x==1&&y==2){
        cout<<0<<e;
    }
    else if(x==2&&y==1){
        cout<<0<<e;

    }

    else if(x==2&&y==0){

        cout<<1<<e;
    }
    else if(x==0&&y==2){

        cout<<1<<e;
    }

    return 0;
}


#include<iostream>
using namespace std;
int A, B, W;
int main(){
  cin >> A >> B >> W;
  W*=1000;
  int ans_max= W/A;
  int ans_min= W/B + (W%B!=0);
  bool IsSatisfiable = false;
  for(int i=1; i<=1000000; i++){
    if(A*i<=W&&W<=B*i){
      IsSatisfiable = true;
      break;
    }
  }
  string S =to_string(ans_min) + " " + to_string(ans_max);
  cout << (IsSatisfiable? S: "UNSATISFIABLE") << endl;
  return 0;
}

//{{
/*
 * Created at: 06/21/21 17:49:29
 *
 * FB: https://facebook.com/tgbaodeeptry
 * From Viet Nam with Love :D
 *
 */

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

#define fast_io()                   \
  ios_base::sync_with_stdio(false); \
  cin.tie(NULL);
#define REP(i, a, b) for (int i = int(a); i < int(b); ++i)
#define RED(i, a, b) for (int i = int(a); i > int(b); --i)
#define FOR(i, a, b) for (int i = int(a); i <= int(b); ++i)
#define FOD(i, a, b) for (int i = int(a); i >= int(b); --i)
#define sz(x) ((int)x.size())
#define all(x) (x).begin(), (x).end()
#define f first
#define s second
#define eb emplace_back
#define pb push_back
#define endl '\n'

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

const int MOD = 1e9 + 7;
const int MAXN = 1e5;
const int INF = INT_MAX;

template <class T> void re(T &x) { cin >> x; };
template <class T> void re(vector<T> &v, int n) {
  FOR(i, 0, n - 1) cin >> v[i];
};
template <class T> void re(vector<T> &v, int f, int t) {
  FOR(i, f, t) cin >> v[i];
};
template <class T> void re(T v[], int n) { FOR(i, 0, n - 1) cin >> v[i]; };
template <class T> void re(T *v, int f, int t) { FOR(i, f, t) cin >> v[i]; };
template <class H, class... T> void re(H &v, T &... args) {
  re(v);
  re(args...);
};

void dbg_out() { cerr << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
  cerr << ' ' << H;
  dbg_out(T...);
}
template <typename T> void dbg_out(vector<T> a) {
  for (auto c : a) cerr << ' ' << c;
  cerr << endl;
};
template <typename T> void dbg_out(vector<T> a, int n) {
  FOR(i, 0, n - 1) cerr << ' ' << a[i];
  cerr << endl;
};
template <typename T> void dbg_out(vector<T> a, int u, int v) {
  FOR(i, u, v) cerr << ' ' << a[i];
  cerr << endl;
};
template <typename T> void dbg_out(T a[]) {
  for (auto c : a) cerr << ' ' << c;
  cerr << endl;
};
template <typename T> void dbg_out(T a[], int n) {
  FOR(i, 0, n - 1) cerr << ' ' << a[i];
  cerr << endl;
};
template <typename T> void dbg_out(T a[], int u, int v) {
  FOR(i, u, v) cerr << ' ' << a[i];
  cerr << endl;
};

#if defined(_CRUN) || defined(_RUN)
#define pd(...)                         \
  cerr << "(L:" << __LINE__ << "): "    \
       << "[" << #__VA_ARGS__ << "] =", \
      dbg_out(__VA_ARGS__)
#else
#define pd(...)
#endif

#if defined(_CRUN)
#define c_input() freopen("input", "r", stdin)
#define c_output() freopen("output", "r", stdin)
#else
#define c_input()
#define c_output()
#endif

void ps(){};
template <typename Head, typename... Tail> void ps(Head H, Tail... T) {
  cout << H;
  ps(T...);
};
template <typename T> void ps(vector<T> a, int n) {
  FOR(i, 0, n - 1) cout << a[i] << ' ';
};
template <typename T> void ps(T a[], int n) {
  FOR(i, 0, n - 1) cout << a[i] << ' ';
};

ll add_mod(ll a, ll b) { return ((a % MOD) + (b % MOD)) % MOD; }
ll sub_mod(ll a, ll b) { return ((a % MOD) - (b % MOD) + MOD) % MOD; }
ll mul_mod(ll a, ll b) { return ((a % MOD) * (b % MOD)) % MOD; }
ll pow_mod(ll a, ll b) {
  ll ans = 1;
  while (b) {
    if (b & 1) ans = mul_mod(ans, a);
    a = mul_mod(a, a);
    b >>= 1;
  };
  return ans;
};

ll lcm(ll a, ll b) { return a * b / __gcd(a, b); }
ll pow(ll a, ll b) {
  ll ans = 1;
  while (b) {
    if (b & 1) ans *= a;
    a *= a;
    b >>= 1;
  };
  return ans;
};

void solve();
void init();
bool multi_test = false;
// ---- main below ----- ///

int main() {
  fast_io();
  init();

  int T = 1;
  if (multi_test) re(T);

  FOR(i, 1, T) solve();
}
//}}

void init() {
  // multi_test = true;
}

int n, sum;
int a[100];
int memo[100][100001];

int find(int u, int cur) {
  if (u == n) {
    return (2*cur >= sum ? cur : INF);
  }

  if (~memo[u][cur]) {
    return memo[u][cur];
  }

  return memo[u][cur] = min(find(u + 1, cur), find(u + 1, cur + a[u]));
}

void solve() {
  re(n);
  re(a, n);

  // tim subset minimize
  memset(memo, -1, sizeof memo);

  sum = accumulate(a, a + n, 0);
  ps(find(0, 0));
}


#include <bits/stdc++.h>

using namespace std;

#define int long long

template <typename T>
void print_vec(const vector<T>& v, bool newline = true) {
    for (int i = 0; i < v.size(); ++i) {
        cout << v[i] << " ";
    }
    if (newline) {
        cout << "\n";
    }
}

mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());

const int maxn = (int)5105;
const int mod = (int)998244353;

#define sz(x) ((int)(x.size()))

#define pb push_back
#define rep(i,a,b) for (int i = (a); i <= (b); i++)
#define repr(i,a,b) for (int i = (a); i >= (b); i--)

int fact_setup=0; int * fact;

int ex (int a, int b){
    if(b==0)return 1; int e = ex(a,b/2); e=(1ll*e*e)%mod; if(b&1)e=(1ll*e*a)%mod; return e;
}
int inv (int a){
    return ex(a, mod - 2);
}
int choose (int a, int b){
    if(!fact_setup){
        fact_setup=1;
        fact = new int [maxn];
        fact[0]=1; rep (i,1,maxn-1) fact[i]=(i*fact[i-1])%mod;
    }
    if(a<b)return 0;
    int num = fact[a];
    int denom = (fact[b] * fact[a - b]) % mod;
    return (num * inv(denom)) % mod;
}

#define pii pair <int, int>
#define pvi pair <vector <int>, int>

#define pai array <int,2>
#define pdi array <double, 2>
#define pdt array <double, 3>

#define pti array <int,3>
#define pfi array <int,4>

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

main(){
    cin.tie(0); ios_base::sync_with_stdio(0);
    int n, k; cin >> n >> k;
    int ans = 0;
    rep (i, 1, n){
        rep (j, 1, k){
            ans += 100*i;
            ans += j;
        }
    }
    cout << ans;
    return 0;
}

#include <bits/stdc++.h>
// #include <atcoder/all>
// using namespace atcoder;
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define rrep(i,n) for(int i=int(n-1);i>=int(0);--i)
#define fore(i,a) for(auto &i:a)
#define all(x) x.begin(),x.end()
#define sz(x) ((int)(x).size())
#define bp(x) (__builtin_popcount((long long)(x)))
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define V vector
#define P pair<int,int>
#define TP tuple<int,int,int>
#define F first
#define S second
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; }
using ll = long long;
using ld = long double;
using namespace std;
const int INF = 1001001001;
const ll INFL = 1001001001001001001;
const int MAX = 2e6+1;

template<int MOD> struct Fp {
    long long val;
    constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
        if (val < 0) val += MOD;
    }
    constexpr int getmod() { return MOD; }
    constexpr Fp operator - () const noexcept {
        return val ? MOD - val : 0;
    }
    constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; }
    constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; }
    constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; }
    constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; }
    constexpr Fp& operator += (const Fp& r) noexcept {
        val += r.val;
        if (val >= MOD) val -= MOD;
        return *this;
    }
    constexpr Fp& operator -= (const Fp& r) noexcept {
        val -= r.val;
        if (val < 0) val += MOD;
        return *this;
    }
    constexpr Fp& operator *= (const Fp& r) noexcept {
        val = val * r.val % MOD;
        return *this;
    }
    constexpr Fp& operator /= (const Fp& r) noexcept {
        long long a = r.val, b = MOD, u = 1, v = 0;
        while (b) {
            long long t = a / b;
            a -= t * b; swap(a, b);
            u -= t * v; swap(u, v);
        }
        val = val * u % MOD;
        if (val < 0) val += MOD;
        return *this;
    }
    constexpr bool operator == (const Fp& r) const noexcept {
        return this->val == r.val;
    }
    constexpr bool operator != (const Fp& r) const noexcept {
        return this->val != r.val;
    }
    friend constexpr ostream& operator << (ostream &os, const Fp<MOD>& x) noexcept {
        return os << x.val;
    }
    friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
        if (n == 0) return 1;
        auto t = modpow(a, n / 2);
        t = t * t;
        if (n & 1) t = t * a;
        return t;
    }
};
const int MOD = 1000000007;
//const int MOD = 998244353;
using mint = Fp<MOD>;

int main() {
    int n;cin>>n;
    V<ll> a(n);
    rep(i,n)cin>>a[i];
    a.push_back(0);
    sort(all(a));
    mint ans=1;
    rep(i,1,n+1){
        ans*=a[i]-a[i-1]+1;
    }
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
#define int long long
#define N 200000
#define P 1000000009
using namespace std;
int T,n,aa,hsh[4][2*N+1],pw2[2*N+1];
//n1[2][2*N+1],n2[2][2*N+1],n3[2][2*N+1],l0,l1,st[2][4];
string s1,s2,s3,ans,ss[4];
//int calmi(string ns){
//	for(int i=0;i<ns.size();i++){
//		if(ns[i]=='0'){
//			int nans=0,ni=i;
//			while(ni<ns.size()&&ns[ni]=='0')ni++;
//			nans=ni-i;
//			return nans;
//		}
//	}
//}
bool fnd(int x,string ns){
	int np=0;
	for(int i=0;i<ns.size();i++){
		while(ss[x][np]!=ns[i]&&np<ss[x].size())np++;
		if(np>=ss[x].size())return 0;
		np++;
	}return 1;
}
signed main(){
	pw2[0]=1;for(int i=1;i<=2*N;i++)pw2[i]=pw2[i-1]*2%P;
	cin>>T;
	while(T--){
		cin>>n;
		cin>>s1>>s2>>s3;
		ss[1]=s1+s1;ss[2]=s2+s2;ss[3]=s3+s3;
		//for(int i=1;i<=3;i++)calhsh(i);
		bool ffl=0;
		for(int t=0;t<=1;t++){
		for(int i=1;i<=3;i++){
			for(int j=0;j<=1;j++){
				string ns=ss[i].substr(0,2*n);
				if(t==0)ns+=j+'0';
				else{
					string ng;ng.clear();ng+=j+'0';
					ns.insert(0,ng);
				}
				//int nh=(hsh[i][2*n]*2+j)%P;
				bool nfl=1;
				for(int k=1;k<=3;k++){
					if(k!=i&&!fnd(k,ns)){
						nfl=0;break;
					}
				}
				if(nfl){
					cout<<ns<<endl;
					ffl=1;break;
				}
			}if(ffl)break;
		}if(ffl)break;
		}
		if(ffl)continue;
		string ns;ns.clear();
		for(int i=0;i<=1;i++){
			for(int j=0;j<=1;j++){
				ns.clear();
				for(int k=1;k<=n;k++)ns+=i+'0';
				ns+=j+'0';
				for(int k=1;k<=n;k++)ns+=1-i+'0';
				bool nfl=1;
				for(int k=1;k<=3;k++){
					if(k!=i&&!fnd(k,ns)){
						nfl=0;break;
					}
				}
				if(nfl){
					cout<<ns<<endl;
					ffl=1;break;
				}
				ns.clear();ns+=j+'0';
				for(int k=1;k<=n;k++)ns+=i+'0';

				for(int k=1;k<=n;k++)ns+=1-i+'0';
				nfl=1;
				for(int k=1;k<=3;k++){
					if(k!=i&&!fnd(k,ns)){
						nfl=0;break;
					}
				}
				if(nfl){
					cout<<ns<<endl;
					ffl=1;break;
				}
				ns.clear();
				for(int k=1;k<=n;k++)ns+=i+'0';

				for(int k=1;k<=n;k++)ns+=1-i+'0';ns+=j+'0';
				nfl=1;
				for(int k=1;k<=3;k++){
					if(k!=i&&!fnd(k,ns)){
						nfl=0;break;
					}
				}
				if(nfl){
					cout<<ns<<endl;
					ffl=1;break;
				}
			}
			if(ffl)break;
		}
	}
	return 0;
}
/*
5
3
000111
001110
011100
2
0011
1001
0110
3
001011
101010
000111
3
001011
010101
110010
4
11010010
01010110
00011101

s1+=s1;s2+=s2;s3+=s3;
//		l0=l1=4*n;
//		for(int i=4*n-1;i>=0;i--){
//			n1[0][i]=l0;
//			n1[1][i]=l1;
//			if(s1[i]=='0')l0=i;
//			else l1=i;
//		}
//		st[0][1]=l0;st[1][1]=l1;
//		l0=l1=4*n;
//		for(int i=4*n-1;i>=0;i--){
//			n2[0][i]=l0;
//			n2[1][i]=l1;
//			if(s2[i]=='0')l0=i;
//			else l1=i;
//		}
//		st[0][2]=l0;st[1][2]=l1;
//		l0=l1=4*n;
//		for(int i=4*n-1;i>=0;i--){
//			n3[0][i]=l0;
//			n3[1][i]=l1;
//			if(s3[i]=='0')l0=i;
//			else l1=i;
//		}
//		st[0][3]=l0;st[1][3]=l1;
//		int np=st[0][1],nq=st[0][2],nr=st[0][3],nans=0;
//		ans.clear();
//		while(np<4*n&&nq<4*n&&nr<4*n){
//			nans++;ans+=s1[np];
//			if(max(n1[0][np],max(n2[0][nq],n3[0][nr]))<max(n1[1][np],max(n2[1][nq],n3[1][nr])))np=n1[0][np],nq=n2[0][nq],nr=n3[0][nr];
//			else np=n1[1][np],nq=n2[1][nq],nr=n3[1][nr];
//		}
//		if(nans>=2*n+1){
//			for(int i=0;i<=2*n;i++)printf("%c",ans[i]);printf("\n");
//			continue;
//		}
//		np=st[1][1],nq=st[1][2],nr=st[1][3],nans=0;
//		ans.clear();
//		while(np<4*n&&nq<4*n&&nr<4*n){
//			nans++;ans+=s1[np];
//			if(max(n1[0][np],max(n2[0][nq],n3[0][nr]))<max(n1[1][np],max(n2[1][nq],n3[1][nr])))np=n1[0][np],nq=n2[0][nq],nr=n3[0][nr];
//			else np=n1[1][np],nq=n2[1][nq],nr=n3[1][nr];
//		}
//
//		for(int i=0;i<=2*n;i++)printf("%c",ans[i]);printf("\n");
*/

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

int main() {
	int n,t;
  string s;
  cin >> t;
  for(int i=0; i<t; i++) {
    cin >> n >> s >> s >> s;
    for(int i=0; i<n; i++) cout << 0;
    for(int i=0; i<n; i++) cout << 1;
    cout << 0 << endl;
  }
}


/*
 * @Author: RBQRBQ
 * @Date: 2020-11-16 10:14:40
 * @LastEditors: RBQRBQ
 * @LastEditTime: 2020-11-16 10:36:16
 */
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef pair<int,int> pii;
namespace IO{
    template<typename T>inline void read(T &x){
        x=0;ll f=1;char ch=getchar();
        while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();}
        while(isdigit(ch)){x=x*10+ch-48;ch=getchar();}
        x=x*f;
    }
}
using namespace IO;

const  int maxn (200);
string s[maxn];
double point[maxn];
int mp[maxn][maxn];
int dp[maxn][maxn];
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>s[i];
        s[i]=" "+s[i];
    }
    for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++)
    if(s[i][j]=='1') mp[i][j]=1;
    for(int i=1;i<=n;i++) mp[i][i]=1;
    for(int k=1;k<=n;k++)
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                mp[i][j]=mp[i][j]|(mp[i][k]&mp[k][j]);
    double ans=.0;
    for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++)
    point[i]+=mp[j][i];
    for(int i=1;i<=n;i++){
    ans+=(double)(1.0)/(double)(point[i]);}
    cout<<fixed<<setprecision(10);
    cout<<ans;
    return 0;
}

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

#define ff first
#define se second
#define pb push_back
#define all(x) (x).begin(),(x).end()

const int mod = 1e9+7;
const int inf = 1e9;
const int mx  = 1e6+5;

int getsolve(int x){

    int ans = 0;
    for(int i=-200; i<=x; i++){
        int sum = 0;
        for(int j=i; j<=x; j++){
            sum += j;
            if(sum >= x) break;
        }ans += (sum == x);
    }return ans;
}

int add(ll x){
    if(x&1) return 1;
    return 0;
}

void solve(int cs){
//    cout << "Case " << cs << ": ";
    ll x; cin >> x; ll ans = 0;
    for(int i=1; i*1LL*i<=x; i++){
        if(x%i) continue;
        ans += add(i);
        if(i != (x/i))
            ans += add(x/i);
    }ans <<= 1;
    cout << ans << "\n";
}

int main(){

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

    int tc = 1, cs = 1;
//    cin >> tc;
    while(tc--) solve(cs++);

    return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;

int main()
{
    ll n,i,j,k;
    ll a=0,b=0,c=0;
    ll ci=0;
    ll cs=0;
    scanf("%lld",&n);
    while(n)
    {
        k=n%10;
        ci+=k;
        cs++;
        if(k%3==0)
            a++;
        if(k%3==1)
            b++;
        if(k%3==2)
            c++;
        n/=10;
    }
    //printf("%lld\n",cs);
    if(ci%3==0)
        printf("0\n");
    else if(ci%3==1)
    {
        if(b>0)
        {
            cs--;
            if(cs)
                printf("1\n");
            else
                printf("-1\n");
        }
        else if(c>1)
        {
            cs-=2;
            if(cs)
                printf("2\n");
            else
                printf("-1\n");
        }
        else
            printf("-1\n");
    }
    else
    {
        if(c>0)
        {
            cs--;
            if(cs)
                printf("1\n");
            else
                printf("-1\n");
        }
        else if(b>1)
        {
            cs-=2;
            if(cs)
                printf("2\n");
            else
                printf("-1\n");
        }
        else
            printf("-1\n");
    }
    return 0;
}

#include <bits/stdc++.h>

#define endl '\n'

using namespace std;
typedef long long ll;

int main() {
    ios_base::sync_with_stdio(0), cin.tie(0);
#ifdef CLION
    freopen("in.txt", "rt", stdin);
    freopen("out.txt", "w", stdout);
#endif
    int cnt = 0;
    int num;
    cin>>num;
    while (true){
        if(num%100==0&&cnt!=0){
            break;
        }
        cnt++;
        num++;
    }
    cout<<cnt<<endl;
}


#include <array>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <complex>
#include <cmath>
#include <numeric>
#include <bitset>
#include <functional>
#include <random>
#include <ctime>
using namespace std;

/* Defines begin */
#define fastio ios_base::sync_with_stdio(false), cin.tie(NULL) , cout.tie(NULL), cout<<fixed<<setprecision(20);
#define w1 solve();
#define for0(i, n) for (int i = 0; i < (int)(n); ++i) // 0 based indexing
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i) // 1 based indexing
#define forc(i, l, r) for (int i = (int)(l); i <= (int)(r); ++i) // closed interver from l to r r inclusive
#define forr0(i, n) for (int i = (int)(n) - 1; i >= 0; --i) // reverse 0 based.
#define forr1(i, n) for (int i = (int)(n); i >= 1; --i) // reverse 1 based
#define tr(it, x) for(auto it = x.begin(); it != x.end(); it++) //traversal for vector, set, pair, map
#define all(x) (x).begin(), (x).end() //Forward traversal
#define rall(x) (x).rbegin, (x).rend() //reverse traversal
#define cinvi0(a,n) vi a(n); for0(i,n) { cin>>a[i]; }
#define cinvs0(s,n) vs s(n); for0(i,n) { cin>>s[i]; }
#define cinvd0(a,n) vd a(n); for0(i,n) { cin>>a[i]; }
//short hand for usual tokens
#define pb push_back
#define mp make_pair
#define double long double
#define fi first
#define se second
#define sz(x) ((int)(x).size())
#define endl '\n'

//debugger
#define LOCAL
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
/* Defines end */

/* Constants begin */
const long long inf = 2e18 + 7;
const long long mod = 1e9 + 7;
const double eps = 1e-7;
const double PI = 2*acos(0.0);
const double E = 2.71828;
/* Constants end */

/* Typedef begin */
typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll,ll> pi;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<double> vd;
typedef vector<bool> vb;
typedef vector<vi> vvi;
typedef vector<string> vs;
/* Typedef end */

/* Template begin */
//Short hand for token
template <class T> using V = vector<T>; // all type of vector regardless of class
template <class T> using VV = V<V<T>>; // all type of vector inside of vector regardless of class

//Debugger stuff
    template <typename Arg1>
void __f(const char* name, Arg1&& arg1)
{
    cerr << name << ": " << arg1 << endl;
}

    template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args)
{
    const char* comma = strchr(names + 1, ',');
    cerr.write(names, comma - names) << ": " << arg1 << " |";
    __f(comma + 1, args...);
}
/* Template end */

//Set reminder
// x << b is equal to x * (2)^b
// x >> b is equal to x / (2)^b
// bitset<8>(x) is equal to binary number of specific x

void solve()
{
    int n; cin >> n;
    int cnt = 0;
    int temp = 0;
    while(temp < n)
    {
        if(temp <= n)
            temp += 100;
        cnt++;
    }
    cout << cnt << "\n";

}

auto main() -> int
{
    fastio
        w1
        return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ppb pop_back
#define make make_pair
#define FAST ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
typedef long long int ll;
typedef double dd;
#define test int t;cin>>t;while(t--)
#define f(i,a,b) for(ll i=a;i<b;i++)
#define fr(j,a,b) for(ll j=a;j>=b;j--)
#define ln cout<<"\n";
#define se second
#define fi first
#define vll vector<ll>
using namespace std;
const ll mod=1e9+7;
const ll MAX=LLONG_MAX;
const ll MIN=LLONG_MIN;
int main()
{FAST;

ll n,q;
cin>>n>>q;
ll a[n];
f(i,0,n)
cin>>a[i];
ll pref[n];
for(ll i=0;i<n;i++)
{
    if(i==0)
    {
        pref[i]=a[i]-1;
    }
    else
    {
        pref[i]=a[i]-1-(i);
    }

}
while(q--)
{
    ll x;
    cin>>x;
    if(x>pref[n-1])
    {
        cout<<a[n-1]+(x-pref[n-1])<<"\n";
    }
    else
    {
        ll k=lower_bound(pref,pref+n,x)-pref;

        ll ans=(a[k]-1)-(pref[k]-x);
        cout<<ans<<"\n";
    }
}

return 0;
}

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

using namespace std;

typedef long long ll;

typedef vector<vector<int>> vvi;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vector<string>> vvs;
typedef vector<ll> vll;

typedef vector<vector<ll>> vvll;

#define mp(x, y) make_pair(x, y)

typedef queue<int> qi;
typedef queue<string> qs;

#define rep(i, n) for(int i=0; i<(n); ++i)
#define repp(i, a, b) for(int i = (a); i <= (b); i++)
#define repm(i, n) for(int i=n; i >= 0; i--)

#define all(v) v.begin(), v.end()
//sort( all(v) )　などと使える

//nが素数かをO(√n)で判断
bool is_prime(int n) {
    bool res = true;

    if (n == 1) return false;
    else if (n == 2) return true;

    else {
        for (int i = 2; i * i <= n; i++) {
            if (n % i == 0) {
                res = false;
                break;
            }
        }
    }

    return res;
}

int perm(int n, int r) {
    if (r == 1) return n;
    return n * perm(n - 1, r - 1);
}

int fact(int n) {
    if (n == 1) return 1;
    return fact(n - 1) * n;
}

int comb(int n, int r) {
    int a = min(n, r), b = max(n, r);
    return perm(a, b) / fact(a);
}

//aの逆元　（a^(-1)）を求める
ll inv_mod(ll a, ll mod) {
    ll b = mod, u = 1, v = 0;
    while (b) {
        ll t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= mod;
    if (u < 0) u += mod;
    return u;
}

//n!
ll fact_mod(ll n, ll mod) {
    ll f = 1; for (int i = 2; i <= n; i++) f = f * (i % mod) % mod;
    return f;
}

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

ll my_pow(ll x, ll n) {
    ll res = 1;
    while (n > 0) {
        if (n & 1) res = (res * x);
        x = (x * x);
        n >>= 1;
    }
    return res;
}

// nCr
ll comb_mod(ll n, ll r, ll mod) {
    if (r > n - r) r = n - r;
    if (r == 0) return 1;
    ll a = 1;
    for (int i = 0; i < r; i++) a = a * ((n - i) % mod) % mod;
    ll b = pow_mod(fact_mod(r, mod), mod - 2, mod);
    return (a % mod) * (b % mod) % mod;
}

//bool v included in S
bool contain(int v, int s) {
    return s & (1 << v);
}

//unionFind

/*
int n;

struct unionFind{
  vector<int> par,R;

  unionFind(int n){
    par.resize(n);
    r.resize(n);
    init(n);
  }

  void init(int n){
    for(int i = 0; i < n; i++) par[i] = i;
    for(int i = 0; i < n; i++) R[i] = 0;
  }

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

  void unite(int x,int y){
    rx = find(x);
    ry = find(y);

    if(rx == rx) return;
    if(R[rx] < R[ry]){
      par[rx] = y;
    }else{
      par[ry] = rx;
      if(R[rx] == R[ry]) R[rx]++;
    }
  }

  bool same(int x,int y){
    return find(x) == find(y);
  }
};
*/

#define P pair<int, int>

//int: 2,147,483,647;
//ll 9,223,372,036,854,775,80
//小数点以下15コまで出力
//cout << fixed << setprecision(15);

const int INF = 1e9;
const ll LINF = 1e18;

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

    for (ll b = 1; b <= 26; b++) {
        if (my_pow(5, b) >= N) break;

        for (ll a = 1; a <= 38; a++) {
            if (my_pow(3, a) + my_pow(5, b) > N) break;
            if (my_pow(3, a) + my_pow(5, b) == N) {
                cout << a << " " << b << endl;
                return 0;
            }
        }
    }

    cout << -1 << endl;

    return 0;
}


#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
//#include<boost/multiprecision/cpp_int.hpp>
//#include<boost/multiprecision/cpp_dec_float.hpp>
//namespace mp=boost::multiprecision;
//#define mulint mp::cpp_int
//#define mulfloat mp::cpp_dec_float_100
//#include<atcoder/all>
//using namespace atcoder;
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}} __init;
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define INF (1<<30)
#define LINF (lint)(1LL<<56)
#define endl "\n"
#define rep(i,n) for(lint (i)=0;(i)<(n);(i)++)
#define reprev(i,n) for(lint (i)=(n-1);(i)>=0;(i)--)
#define flc(x) __builtin_popcountll(x)
#define pint pair<int,int>
#define pdouble pair<double,double>
#define plint pair<lint,lint>
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define vec vector<lint>
#define nep(x) next_permutation(all(x))
typedef long long lint;
typedef __int128_t llint;
int dx[8]={1,1,0,-1,-1,-1,0,1};
int dy[8]={0,1,1,1,0,-1,-1,-1};
const int MAX_N=3e5+5;
//vector<int> bucket[MAX_N/1000];
//constexpr int MOD=1000000007;
constexpr int MOD=998244353;

lint inverse(int *A,int N){
  lint ans=0;
  lint a[N+1000]={};
  rep(i,N){
    for(int j=A[i];j;j-=(j&(-j))) ans-=a[j];ans+=i;
    for(int j=A[i];j<=N;j+=(j&(-j))) a[j]++;
  }
  return ans;
}

int main(void){
    lint N;
    cin >> N;
    int A[N];
    rep(i,N) cin >> A[i],A[i]++;
    lint ans=inverse(A,N);
    cout << ans << endl;
    rep(i,N-1){
        ans+=N+1-A[i]*2;
        cout << ans << endl;
    }
}

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); i++)
using namespace std; using ll = long long;
using ull = unsigned long long; using P = pair<int, int>;
const int INF = 1e9; const int MOD = 1000000007;
const int dx[] = {-1,0,1,0}; const int dy[] = {0,-1,0,1};
// const int dx[] = {-1,-1,-1, 0, 0, 1, 1, 1}; const int dy[] = {-1, 0, 1,-1, 1,-1, 0, 1};
#define PI 3.14159265358979323846264338327950L
// static const double e = 2.718281828459045;
//cout << fixed << setprecision(10) << ans << endl;
vector<ll> A, B;
struct UnionFind {
    vector<int> par, rnk, mem;
    vector<ll> sum_a, sum_b;
    UnionFind(int n) {
        par.resize(n); fill(par.begin(), par.end(), -1);
        rnk.resize(n); fill(rnk.begin(), rnk.end(),  0);
        mem.resize(n); fill(mem.begin(), mem.end(),  1);
        sum_a.resize(n);
        rep(i, n) sum_a[i] = A[i];
        sum_b.resize(n);
        rep(i, n) sum_b[i] = B[i];
    }
    int root(int x) {
        if (par[x] < 0) return x;
        else return par[x] = root(par[x]);
    }
    bool issame(int x, int y) {
        return root(x) == root(y);
    }
    void merge(int x, int y) {
        x = root(x); y = root(y);
        if (x == y) return;
        if (rnk[x] < rnk[y]) {
            par[x] = y; mem[y] += mem[x];
            sum_a[y] += sum_a[x];
            sum_b[y] += sum_b[x];
        } else {
            par[y] = x; mem[x] += mem[y];
            sum_a[x] += sum_a[y];
            sum_b[x] += sum_b[y];
            if (rnk[x] == rnk[y]) rnk[x]++;
        }
    }
    int size(int x) {
        return mem[root(x)];
    }
    ll size_a(int x) {
        return sum_a[root(x)];
    }
    ll size_b(int x) {
        return sum_b[root(x)];
    }
};

int main() {
    int N, M; cin >> N >> M;
    A.resize(N); B.resize(N);
    rep(i, N) cin >> A[i];
    rep(i, N) cin >> B[i];

    UnionFind uf(N);
    rep(i, M) {
        int c, d; cin >> c >> d; c--; d--;
        uf.merge(c, d);
    }
    int flg = 1;
    rep(i, N) {
        if (uf.size_a(i) != uf.size_b(i)) flg = 0;
    }
    if (flg == 1) cout << "Yes" << endl;
    else cout << "No" << endl;
}


#include <algorithm>
#include <iostream>
#include <stack>
#include <queue>
#include <math.h>
#include <cmath>
#include <string>
#include <string.h>
#include <iterator>
#include <map>
#include <set>
#include <iomanip>
#include <vector>
#include <cstdint>
#include <time.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
#define rep(i, N) for (int i = 0; i < N; i++)
#define rep2(i, l, r) for (ll i = (ll)l; i < (ll)(r); i++)
#define INF 1000000000
#define MAX 200001
#define PI 3.141592653589793

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
const ll MOD = 1000000007;

/*clock_t start = clock();
clock_t end = clock();
const double time = static_cast<double>(end - start) / CLOCKS_PER_SEC * 1000.0;
printf("%lf", time);*/

template <typename T > inline string toString(const T &a) {ostringstream oss; oss << a; return oss.str();};

int main(){
    int N,M,T;
    cin >> N >> M >> T;
    int MAX_N = N;
    int now = 0;
    rep(i,M){
        int A,B;
        cin >> A >> B;
        N -= A-now;
        if(N <= 0){
            cout << "No" << endl;
            return 0;
        }
        N += B-A;
        N = min(MAX_N,N);
        now = B;
        if(i == M-1 && N <= T-B){
            cout << "No" << endl;
            return 0;
        }
    }
    cout << "Yes" << endl;
}

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main() {
	int n,m,t;
	cin>>n>>m>>t;
	int prev_t = 0;
	int curr = n;
	for(int i=0;i<m;i++){
		int a, b;
		cin>>a>>b;
		curr = max(curr-(a-prev_t)*1,0);
		if(curr==0){
			cout<<"No"<<endl;
			return 0;
		}
		curr = min(curr+(b-a)*1, n );
		prev_t = b;
	}
	curr = curr - (t-prev_t)*1;
	if(curr>0) cout<<"Yes";
	else cout<<"No";

    return 0;

}


#include <iostream>
#include <vector>
#include <set>
using namespace std;
using pairint = pair<int, int>;

int main(void){
    int N, M, Abuf, Bbuf;
    vector<int> dp;
    vector<vector<int>> cgroups;
    vector<pairint> upperbits;
    vector<int> birank;
    cin >> N >> M;
    int numid = 1<<N;
    upperbits.assign(numid, pairint(0, 0));
    birank.assign(numid, 0);
    cgroups.assign(N, {});
    for(int i=0; i<N; i++)upperbits[1<<i].first=i;
    for(int i=1; i<numid; i++){
        if(upperbits[i].first==0)upperbits[i].first=upperbits[i-1].first;
        upperbits[i].second=upperbits[i-(1<<upperbits[i].first)].first;
        birank[i]=birank[i-(1<<upperbits[i].first)]+1;
    }
    dp.assign(numid, 0);
    for(int i=1; i<numid; i<<=1){
        dp[i]=1;
        for(int j=i<<1; j<numid; j<<=1)dp[i+j]=2;
    }
    for(int i=0; i<M; i++){
        cin >> Abuf >> Bbuf;
        Abuf--;
        Bbuf--;
        int id = (1<<Abuf)+(1<<Bbuf);
        dp[id]=1;
        cgroups[Bbuf].push_back(id);
    }
    for(int i=1; i<numid; i++){
        if(dp[(1<<upperbits[i].first)+(1<<upperbits[i].second)]!=1)continue;
        if(dp[i-(1<<upperbits[i].first)]!=1)continue;
        if(dp[i-(1<<upperbits[i].second)]!=1)continue;
        dp[i]=1;
        cgroups[upperbits[i].first].push_back(i);
    }
    for(int i=3; i<numid; i++){
        if(dp[i]==1)continue;
        int j = 1<<upperbits[i].first;
        if(dp[i-j]==1){
            dp[i]=2;
            continue;
        }
        bool dividable = false;
        for(int id: cgroups[upperbits[i].first]){
            int jd = id-j;
            if(jd != (i&jd))continue;
            int kd = i&(~id);
            if(dp[jd+kd] == dp[kd]+1){
                dp[i] = dp[kd]+1;
                dividable = true;
                break;
            }
        }
        if(!dividable)dp[i] = dp[i-j]+1;
    }
    cout << dp[numid-1] << endl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define loop(i,n) for(ll i=0;i<n;i++)
#define loop1(i,n) for(ll i=1;i<=n;i++)
#define fast_az_fck ios_base::sync_with_stdio(false);cin.tie(NULL);
#define mod 1000000007
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
//template<class T, class H>using umap=unordered_map<T,H,custom_hash>;
void init_code()
{
    fast_az_fck
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif // ONLINE_JUDGE
}

int main()
{
    init_code();
    ll t=1;
    //cin>>t;
    while(t--)
    {
        ll a,b;cin>>a>>b;
        if(a>=b)
        {
            ll sum1=(a*(a+1))/2;
            ll sum2=-sum1;
            ll sum3=-((b*(b-1))/2);
            for(ll i=1;i<=a;i++)
            {
                cout<<i<<" ";
            }
            for(ll i=-1;i>-b;i--)
            {
                cout<<i<<" ";
            }
            cout<<(sum2-sum3);
        }
        else
        {
            ll sum1=-((b*(b+1))/2);
            ll sum2=-sum1;
            ll sum3=((a*(a-1))/2);
            for(ll i=-1;i>=-b;i--)
            {
                cout<<i<<" ";
            }
            for(ll i=1;i<a;i++)
            {
                cout<<i<<" ";
            }
            cout<<(sum2-sum3);
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
const ll MOD = 1000000007;
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> w(n);
  vector<int> sums(1 << n), mn(1 << n);
  REP(i, n) cin >> w[i];
  int mx = *max_element(w.begin(), w.end());
  for (int s = 0; s < (1 << n); s++) {
    int x = 0;
    REP(j, n) if (s >> j & 1) x += w[j];
    sums[s] = x;
  }
  REP(i, m) {
    int l, v;
    cin >> l >> v;
    if (v < mx) {
      cout << -1 << endl;
      return 0;
    }
    for (int s = 0; s < (1 << n); ++s)
      if (sums[s] > v) mn[s] = max(mn[s], l);
  }
  int ans = 1e9;
  vector<int> perm(n);
  iota(perm.begin(), perm.end(), 0);
  do {
    vector<int> pos(n);
    REP(i, n) {
      int s = 1 << perm[i];
      for (int j = i - 1; j >= 0; j--) {
        s |= 1 << perm[j];
        pos[i] = max(pos[i], pos[j] + mn[s]);
      }
    }
    ans = min(ans, pos[n - 1]);
  } while (next_permutation(perm.begin(), perm.end()));
  cout << ans << endl;
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
int main(){
  int N, M;
  cin >> N >> M;
  vector<int> w(N);
  for (int i = 0; i < N; i++){
    cin >> w[i];
  }
  vector<int> l(M), v(M);
  for (int i = 0; i < M; i++){
    cin >> l[i] >> v[i];
  }
  vector<pair<int, int>> P(M + 1);
  for (int i = 0; i < M; i++){
    P[i] = make_pair(v[i], l[i]);
  }
  P[M] = make_pair(0, 0);
  sort(P.begin(), P.end());
  for (int i = 0; i < M; i++){
    P[i + 1].second = max(P[i + 1].second, P[i].second);
  }
  int wmax = *max_element(w.begin(), w.end());
  int vmin = *min_element(v.begin(), v.end());
  if (wmax > vmin){
    cout << -1 << endl;
  } else {
    long long ans = INF;
    vector<int> p(N);
    for (int i = 0; i < N; i++){
      p[i] = i;
    }
    while (1){
      vector<long long> w2(N);
      for (int i = 0; i < N; i++){
        w2[i] = w[p[i]];
      }
      vector<long long> S(N + 1);
      S[0] = 0;
      for (int i = 0; i < N; i++){
        S[i + 1] = S[i] + w2[i];
      }
      vector<long long> x(N);
      x[0] = 0;
      for (int i = 1; i < N; i++){
        long long minx = x[i - 1];
        for (int j = 0; j < i; j++){
          int wsum = S[i + 1] - S[j];
          auto itr = lower_bound(P.begin(), P.end(), make_pair(wsum, -INF)) - 1;
          long long d = (*itr).second;
          minx = max(minx, x[j] + d);
        }
        x[i] = minx;
      }
      ans = min(ans, x[N - 1]);
      if (!next_permutation(p.begin(), p.end())){
        break;
      }
    }
    cout << ans << endl;
  }
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define int long long
#define F(i, n) for (int i = 0; i < n; i++)
#define all(x)(x).begin(), (x).end()
#define show(A) for (auto i: A) cout << i << " "; cout << '\n';
#define show_(A, n) F(i, n) { show(A[i]) }

using namespace std;
using ld = long double;
using vi = vector < int > ;
using mi = map < int, int > ;
using pi = pair < int, int > ;

const int N = 100005;
const int MOD = 1e9 + 7;
const int inf = 1e18 + 1;
/***************************code begins here*****************************/
vector<vi> adj;
void dijkstra(int s, vector<int> & d, vector<int> & p) {
    int n = adj.size();
    d.assign(n, 1e9);
    p.assign(n, -1);

    d[s] = 0;
    set<pair<int, int>> q;
    q.insert({0, s});
    while (!q.empty()) {
        int v = q.begin()->second;
        q.erase(q.begin());

        for (auto edge : adj[v]) {
            int to = edge;
            int len = 1;

            if (d[v] + len < d[to]) {
                q.erase({d[to], to});
                d[to] = d[v] + len;
                p[to] = v;
                q.insert({d[to], to});
            }
        }
    }
}
void solve() {
    int n,m,k, ans = inf;
    cin >> n >> m;
    adj.resize(n+1);
    F(i, m){
    	int x,y;
    	cin >> x >> y;
    	adj[x].pb(y);
    	adj[y].pb(x);
    }
    cin >> k;
    vector<int> c(k+1);
    vector<vi> dis(k+1);
    vector<vi> vis(k+1);
    F(i, k){
    	cin >> c[i+1];
    	dijkstra(c[i+1],dis[i+1],vis[i+1]);
    }
    int dp[(1ll<<k)][k+1];
    for(int i=0;i<(1ll<<k);i++){
    	for(int j=0;j<=k;j++){
    		dp[i][j]=1e9;
    	}
    }
    for(int i=0;i<k;i++){
    	dp[(1ll<<i)][i+1]=1;
    }
    for(int i=0;i<(1ll<<k);i++){
    	for(int j=0;j<k;j++){
    		if((i&(1ll<<j))==0) continue;
    		int t = (i^(1ll<<j));
    		for(int l=0;l<k;l++){
    			if((t&(1ll<<l))==0) continue;
    			dp[i][j+1] = min(dp[i][j+1],dp[t][l+1] + dis[l+1][c[j+1]]);
    		}
    	}
    }
    for(int i=1;i<=k;i++){
    	ans = min(ans,dp[(1ll<<k)-1][i]);
    }
    ans = (ans>=1e9) ? -1 : ans;

    cout << ans << '\n';
}
int32_t main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int t = 1;
    // cin >> t;
    while (t--) solve();

    return 0;
}

#include<bits/stdc++.h>
#include<bits/extc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
using namespace __gnu_pbds;
#ifdef DEBUG
#define debug(x) {{cerr<<#x<<": "; cerr<<x<<endl;}}
#define debugv(x) {{cerr<<#x<<": "; for(auto i:x) cerr<<i<<" "; cerr<<endl;}}
#else
#define debug(x)
#define debugv(x)
#define cerr while(0) cerr
#endif
#define int long long
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 INF=1e15;
vi adj[100005], v;
bool vis[100005];
ll dp[150000][20], dist[20][100005];
void bfs(int src, int idx){
	memset(vis, false, sizeof vis);
	for(int i=0;i<100005;i++) dist[idx][i]=INF;
	dist[idx][src]=0;
	queue<int> q;
	q.push(src);
	vis[src]=true;
	while(!q.empty()){
		int x=q.front(); q.pop();
		for(int y:adj[x]){
			if(vis[y]) continue;
			q.push(y);
			vis[y]=true;
			assert(dist[idx][y]==INF);
			dist[idx][y]=dist[idx][x]+1;
		}
	}
}
signed main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	memset(dp, -1, sizeof dp);
	int n, m;
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		int x, y;
		cin>>x>>y;
		adj[x].push_back(y);
		adj[y].push_back(x);
	}
	int k;
	cin>>k;
	for(int i=0;i<k;i++) dp[1<<i][i]=1;
	for(int i=1;i<=k;i++){
		int x;
		cin>>x;
		v.push_back(x);
		bfs(x, i-1);
	}
	vi cur;
	for(int i=1;i<(1<<k);i++){
		cur.clear();
		if(dp[i][(int)(log2(i))]!=-1) continue;
		for(int j=0;j<k;j++){
			if(i&(1<<j)) cur.push_back(j);
		}
		cerr<<i<<" ";
		for(int j:cur){
			dp[i][j]=INF;
			for(int k:cur){
				if(j==k) continue;
				dp[i][j]=min(dp[i][j], dp[i^(1<<j)][k]+dist[j][v[k]]);
			}
			cerr<<dp[i][j]<<" ";
		}
		cerr<<endl;
	}
	ll ans=INF;
	for(int i=0;i<k;i++){
		ans=min(ans, dp[(1<<k)-1][i]);
	}
	if(ans>=INF){
		cout<<-1<<endl;
		return 0;
	}
	cout<<ans<<endl;
}


#include <bits/stdc++.h>
#define f(i,j,k) for(long long i=j;i<k;i++)
#define f2(i,j,k) for(long long i=j;i>=k;i--)
#define ll long long
using namespace std;
int main(){
    bool j=true;
    ll n,m,c,d;
    cin>>n>>m;
    ll a[n],b[n],ansa=0,ansb=0;
    vector <ll>now(n,0);
    f(i,0,n){
        cin>>a[i];
    }
    f(i,0,n){
        cin>>b[i];
        if(a[i]!=b[i]){
            j=false;
        }
    }
    f(i,0,m){
        cin>>c>>d;
        if(now[c-1]==0){
            ansa+=a[c-1];
            ansb+=b[c-1];
            now[c-1]++;
        }
        if(now[d-1]==0){
            ansa+=a[d-1];
            ansb+=b[d-1];
            now[d-1]++;
        }
    }
    if(ansa==ansb&&ansa!=0){
        cout<<"Yes"<<endl;
    }
    else if(j){
        cout<<"Yes"<<endl;
    }
    else{
        cout<<"No"<<endl;
    }
    return 0;
}

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

int main() {
  int n;
  string s;
  cin >> n >> s;
  vector<int> a(n + 1);
  for (auto& e : a) cin >> e;

  for (int k = *max_element(a.begin(), a.end()); k >= 1; k--) {
    bool valid = true;
    for (int i = 0; valid && i < n; i++) {
      int l1 = a[i] / k, l2, l3;
      int r1 = a[i + 1] / k, r2, r3;
      l2 = l3 = l1;
      r2 = r3 = r1;
      if (a[i] % k > a[i + 1] % k) l2++;
      if (a[i] % k < a[i + 1] % k) r2++;
      if (a[i] % k != 0) l3++;
      if (a[i + 1] % k != 0) r3++;

      if (s[i] == '<') {
        valid = valid && l1 < r1 && l2 < r2 && l3 < r3;
      } else {
        valid = valid && l1 > r1 && l2 > r2 && l3 > r3;
      }
    }
    if (!valid) continue;

    cout << k << endl;
    for (int j = 0; j < k; j++) {
      for (int i = 0; i < n + 1; i++) cout << a[i] / k + (j < a[i] % k ? 1 : 0) << ' ';
      cout << endl;
    }
    return 0;
  }

  return 1;
}


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

using Int = long long;
#define PUTS(x) std::cout << (x) << endl;
#define rep(i, n) for (int i = 0, i##_len = (int)(n); i < i##_len; i++)
#define rep3(i, m, n) for (int i = (int)(m), i##_len = (int)(n); i < i##_len; i++)

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

const Int INF = 1000000000;

struct Node {
    vector<Int> to;
    Int dist = INF;
};

int main() {
    Int n, m;
    cin >> n >> m;

    auto G = vector<Node>(n);

    rep(i, m) {
        Int a, b;
        cin >> a >> b;
        a--;
        b--;
        G[a].to.push_back(b);
        G[b].to.push_back(a);
    }

    Int k;
    cin >> k;
    auto C = vector<Int>();
    rep(i, k) {
        Int c;
        cin >> c;
        c--;
        C.push_back(c);
    }

    auto M = 1;
    rep(i, k) {
        M *= 2;
    }

    // d[i][j]: C[i] -> C[j] の最短距離 を記録
    auto d = vector<vector<Int>>(k, vector<Int>(k, 0));
    rep(i, k - 1) {
        auto g = G;
        g[C[i]].dist = 0;
        auto q = queue<Int>();
        q.push(C[i]);
        while (!q.empty()) {
            auto j = q.front();
            auto &node = g[j];
            q.pop();
            for (auto p : node.to) {
                auto oldValue = g[p].dist;
                auto newValue = g[j].dist + 1;
                if (newValue < oldValue) {
                    g[p].dist = newValue;
                    q.push(p);
                }
            }
        }
        rep3(j, i + 1, k) {
            d[i][j] = g[C[j]].dist;
            d[j][i] = d[i][j];
        }
    }

    // dp[i][bits] : 既に訪れた頂点の集合が bits かつ今C[i]にいるという条件を満たす最小工程
    // bits は右からjビット目がC[j]を既に訪れていることを示す（C[i]自身も「既に訪れた」扱いにする）
    auto dp = vector<vector<Int>>(k, vector<Int>(M, INF));

    // 配るdp
    function<void(Int, Int)> dfs = [&](Int current, Int bits) {
        rep(i, k) {
            if (((1 << i) & bits) == 0) { // まだ訪れていないC[i]について
                auto nextBits = (1 << i) | bits; // C[i]を次に訪れた場合の bit flag
                auto oldValue = dp[i][nextBits];
                auto newValue = dp[current][bits] + d[current][i];
                if (newValue < oldValue) { // 最短記録が更新されたらそこから再度DFS
                    dp[i][nextBits] = newValue;
                    dfs(i, nextBits);
                }
            }
        }
    };

    // 初期値設定（C[i] のみの列はコスト1扱い）
    rep(i, k) {
        dp[i][1 << i] = 1;
    }
    // スタート
    rep(i, k) {
        dfs(i, 1 << i);
    }

    Int ans = INF;
    rep(i, k) {
        // M-1 = [11111...]_2  は全てのC[j]を訪れたフラグ。dp[i][M-1]は全てのC[j]を訪れて最後にC[i]に至る場合の最短列の長さを示す。
        chmin(ans, dp[i][M - 1]);
    }
    PUTS(ans == INF ? -1 : ans)
}

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
const int maxk = 17;
const int maxs = (1 << maxk) + 10;
const int INF = 0x3f3f3f3f;
int n, m, k;
vector<int> E[maxn];
int c[maxk], dis[maxn], a[maxk][maxk], f[maxk][maxs];
void bfs(int x)
{
	memset(dis, 0x3f, sizeof dis);
	queue<int> Q;
	Q.push(x); dis[x] = 0;
	while(!Q.empty())
	{
		x = Q.front(); Q.pop();
		for(auto v : E[x])
			if(dis[v] == INF)
			{
				dis[v] = dis[x] + 1;
				Q.push(v);
			}
	}
}
int main()
{
	scanf("%d%d", &n, &m);
	for(int i = 1; i <= m; ++i)
	{
		int u, v;
		scanf("%d%d", &u, &v);
		E[u].push_back(v);
		E[v].push_back(u);
	}
	scanf("%d", &k);
	for(int i = 0; i < k; ++i)
		scanf("%d", &c[i]);
	for(int i = 0; i < k; ++i)
	{
		bfs(c[i]);
		for(int j = 0; j < k; ++j)
			a[i][j] = dis[c[j]];
	}
	const int S = (1 << k) - 1;
	memset(f, 0x3f, sizeof f);
	for(int i = 0; i < k; ++i) f[i][1 << i] = 0;
	for(int s = 1; s <= S; ++s)
		for(int i = 0; i < k; ++i)
			if(s & (1 << i))
			{
				for(int j = 0; j < k; ++j)
					if(i != j && (s & (1 << j)))
						f[i][s] = min(f[i][s], f[j][s ^ (1 << i)] + a[j][i]);
			}
	int ans = INF;
	for(int i = 0; i < k; ++i)
		ans = min(ans, f[i][S] + 1);
	printf("%d\n", ans == INF ? -1 : ans);
	return 0;
}


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

typedef long double ld;
typedef long long ll;

using namespace std;

const ld eps = 1e-12;

int main() {
  ll n;
  cin >> n;
  ll L = 1, R = 2e9;
  while (L != R) {
    ll M = (L + R + 1) / 2;
    ll val = M * (M + 1) / 2;
    if (val <= n + 1)
      L = M;
    else
      R = M - 1;
  }
  cout << n - L + 1;
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
//#include <ext/pb_ds/assoc_container.hpp>
//using namespace __gnu_pbds;
//typedef tree <int,null_type,less <int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
// #include <ext/rope>
// using namespace __gnu_cxx;
typedef long long int ll;
typedef long double ld;
#define FAST                 \
    ios::sync_with_stdio(0); \
    cin.tie(0);              \
    cout.tie(0);
#define rep(g, i, n) for (ll g = i; g < n; g++)
#define rev(g, n, i) for (ll g = n - 1; g >= i; g--)
#define all(v) v.begin(), v.end()
#define pb push_back
#define mxe(v) *max_element(v.begin(), v.end())
#define mne(v) *min_element(v.begin(), v.end())
#define ve vector
#define lb lower_bound
#define ub upper_bound
#define bb break
#define cc continue
#define rem 1000000007
#define PI 3.141592653589793238462643383279502
ll power(ll x, ll y, ll p)
{
    ll res = 1; // Initialize result
    x = x % p;  // Update x if it is more than or
    while (y > 0)
    {
        if (y & 1)
            res = (res * x) % p;
        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }
    return res;
}
ll modInverse(ll n, ll p)
{
    return power(n, p - 2, p);
}
struct point
{
    ll x, y;
};
int main()
{
    FAST
    // freopen("input1.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    /*ll tests;
    cin>>tests;
    rep (gg,0,tests)
    {}*/
    ll n;
    cin>>n;
    ld val=17+8*n;
    val=sqrt(val);
    val-=3;
    val/=2;
    ll z=val;
    ll low=max(z-100,(ll)1);
    ll high=z+100;
    ll ans=1e18;
    rep (i,low,high)
    {
        ll s=(i*(i+1))/2;
        ll cnt=i;
        if (s>n+1)
        {
            break;
        }
        if (n+1-s>i)
        {
            ans=min(ans,n-(i+1)+1);
        }
        else
        {
            ans=min(ans,n-i+1);
        }
    }
    cout<<ans<<"\n";
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;
using pll = pair<ll,ll>;

#define INF (1LL << 60)
#define MOD 1000000007
#define PI 3.14159265358979323846

#define REP(i,m,n) for(ll (i)=(m),(i_len)=(n);(i)<(i_len);++(i))
#define FORR(i,v) for(auto (i):v)
#define ALL(x) (x).begin(), (x).end()
#define PR(x) cout << (x) << endl
#define PS(x) cout << (x) << " "
#define SZ(x) ((ll)(x).size())
#define MAX(a,b) (((a)>(b))?(a):(b))
#define MIN(a,b) (((a)<(b))?(a):(b))
#define REV(x) reverse(ALL((x)))
#define ASC(x) sort(ALL((x)))
#define DESC(x) ASC((x)); REV((x))
#define pb push_back
#define eb emplace_back

int main()
{
    ll a, b, c, d;
    cin >> a >> b >> c >> d;
    PR(a*d-b*c);

    return 0;
}

/*

*/

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <cstdio>
#include <math.h>
#include <algorithm>
#include <vector>
#include <string>
#include <stdlib.h>
#include <queue>
#include <stack>
#include <utility>
#include <fstream>
//#include <random>
#include <map>
#include <unordered_map>
#include <cstdlib>
#include <functional>
#include <time.h>
//#include <chrono>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <iostream>
#include <limits>
#include <numeric>
#include <sstream> // std::stringstream
#include <bitset>  // std::bitset<N>
//#include <type_traits>
//#include <atcoder/scc>
#define ll long long
#define rep(i,n) for(ll i=0;i<n;i++)
#define rrep(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=a;i<b;i++)
#define RFOR(i,a,b) for(ll i=b-1;i>=a;i--)
#define INF 1000000001
#define MOD 1000000007
#define SORT(s) sort(s.begin(), s.end());
#define REVERSE(s) reverse(s.begin(), s.end());
#define SUM(s) accumulate(s.begin(), s.end(), 0LL);
#define P pair<ll,ll>
#define mp make_pair
#define valid(x,y,h,w) (0<=x&&x<h&&0<=y&&y<w)
//#define vint vector<int>
//#define vvint vector<vector<int>>
#define vll vector<ll>
#define vvll vector<vector<ll>>
#define ALL(a)  (a).begin(),(a).end()
//#define int ll
ll dx[4] = { 0,1,0,-1 };
ll dy[4] = { 1,0,-1,0 };
ll ndx[8] = { 0,1,0,-1, 1,1,-1,-1 };
ll ndy[8] = { 1,0,-1,0, 1,-1,-1,1 };
double pi = 3.141592653589793;
using namespace std;

int main() {
	int n;
	cin >> n;
	vll a(n);
	rep(i, n) {
		cin >> a[i];
	}
	SORT(a);

	vll im(n);
	rep(i, n) {
		if (i == 0) {
			im[0] = a[0];
		}
		else {
			im[i] = im[i - 1] + a[i];
		}
	}

	//x=0
	ll ans = im[n - 1] * 2;
	rep(i, n) {
		ll x2 = a[i];
		ans = min(ans, n * x2 + im[n - 1] * 2
			- 2 * (im[i] + x2 * (n - i - 1)));
	}
	cout << std::fixed << std::setprecision(15) << (double)ans / (n*2) << endl;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

#define fi first
#define se second
#define pb push_back
#define mp make_pair

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0);
	ll n; cin>>n;
	vector<ll> t(n),p(n),s(n);
	ll ans=1e9;
	ll none=1;
	for(int i=0; i<n; i++){
		cin>>t[i]>>p[i]>>s[i];
		if(s[i]-t[i]>0){
			none=0;
			ans = min(ans,p[i]);
		}
	}
	if(none==0) cout<<ans;
	else cout<<-1;

}

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

int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
#ifdef LOCAL
    freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    clock_t start,end;
    start = clock();
#endif
    void work();
    work();
#ifdef LOCAL
    end = clock();
    cout<<"运行时间"<<(double)(end-start)<<"ms"<<endl;
#endif
    return 0;
}
int n,m;
vector<pair<int, int>> mmap[2005];
// bool operator < (const pair<int, int>& i,const pair<int, int>& j){
//     return i.second<j.second;
// }
void work(){
    cin>>n>>m;
    int a,b,c;
    while(m--){
        cin>>a>>b>>c;
        mmap[a].push_back(make_pair(b,c));
    }
    for(int i=1;i<=n;i++){
        vector<int> d(n+1,(int)1e9);
        d[i]=0;
        priority_queue<pair<int, int>, vector<pair<int, int>>,greater<pair<int, int>> > pq;
        pq.push(make_pair(d[i],i));
        int ans=1e9;
        int flag=0;
        while(!pq.empty()){
            auto topp=pq.top();pq.pop();
            // cout<<topp.second<<endl;
            for(auto j:mmap[topp.second]){
                if(j.first==i) {
                    ans=min(d[topp.second]+j.second,ans);
                }
                if(d[j.first]>d[topp.second]+j.second){
                    d[j.first]=d[topp.second]+j.second;
                    pq.push(make_pair(d[j.first],j.first));
                }
            }
            if(ans<d[topp.second]) break;
        }
        if(ans!=int(1e9))
            cout<<ans<<endl;
        else cout<<-1<<endl;
    }
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <utility>
#include <vector>

using namespace std;

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

#define min_e(v) *min_element(ALL(v))
#define max_e(v) *max_element(ALL(v))

#define order(neq_sl) [](const auto s, const auto l){return neq_sl;}
#define make_lr(v,type,l,r) vector<type> l(n+1),r(n+1);FOR(i,n){l[i+1]=l[i]+v[i];r[i+1]=r[i]+v[n-1-i];}

#define acm accumulate
#define SUM(v) accumulate(ALL(v), 0l)
#define SORT(v) sort(ALL(v))
#define P 1000000007
long powP (long a, long b) {
  if (b == 1) return a;
  else if (b == 0) return 1;
  else if (b < 0) return powP(a, P - 1 + b);
  else {
    long b2 = powP(a, b/2);
    b2*=b2;
    b2%=P;
    return (b2 * powP(a, b%2)) % P;
  }
}

template <typename T>
using vec = vector<T>;

template <typename T>
using vvec = vector<vector<T>>;

/*
template <typename T>
T gcd(T a, T b) {
    if (a > b) swap(a, b);
    if (a == 0) return b;
    return gcd(b%a, a);
}
template <typename InputItr, typename T>
T gcd(InputItr first, InputItr last, T init) {
    for (; first != last; ++first) init = gcd(init, *first);
    return init;
}
template <typename T>
T lcm(T a, T b) {
    return (a * b / gcd(a, b)) % P;
}
template <typename InputItr, typename T>
T lcm(InputItr first, InputItr last, T init) {
    for (; first != last; ++first) init = lcm(init, *first);
    return init;
}

#define GCD(v) gcd(ALL(v), 0l)
#define LCM(v) lcm(ALL(v), 1l)
*/

//#define x first
//#define y second

int main() {

  vec<int> p(3);
  cin>>p[0]>>p[1]>>p[2];
  SORT(p);

  if (p[1]==0) cout<<100-p[2];
  else if (p[0]==0) {
    vvec<double> v(101, vec<double>(101));
    v[100][100]=0;
    for (int i = 99; i >= p[1]; i--) {
      for (int j = 99; j >= p[2]; j--) {
        v[i][j] = (v[i+1][j]*i+v[i][j+1]*j)/(i+j)+1;
      }
    }
    cout<<setprecision(12)<<v[p[1]][p[2]];
  } else {
    vec<vvec<double>> v(101, vvec<double>(101, vec<double>(101)));
    v[100][100][100]=0;
    for (int i = 99; i >= p[0]; i--) {
      for (int j = 99; j >= p[1]; j--) {
        for (int k = 99; k >= p[2]; k--) {
          v[i][j][k] = (v[i+1][j][k]*i+v[i][j+1][k]*j+v[i][j][k+1]*k)/(i+j+k)+1;
        }
      }
    }
    cout<<setprecision(12)<<v[p[0]][p[1]][p[2]];
  }

  return 0;

}


#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) REP(i, 0, n)
#define rrep(i, n) for (int i = (int)(n-1); i >= 0; i--)
#define sz(x) int(x.size())
#define bitsz(x) int(__builtin_popcount(x))
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb(x) push_back(x)
#define INF 1e9
#define LINF 1e18
template<class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; }
template < typename T > inline string toString( const T &a ) { ostringstream oss; oss << a; return oss.str(); };
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
const int di[4] = {1,0,-1,0};
const int dj[4] = {0,1,0,-1};
const int mod = 1000000007;
//const int mod = 998244353;
//cout << fixed << setprecision(15);

int main() {
    ll r1,c1,r2,c2; cin >> r1 >> c1 >> r2 >> c2;
    if (r1==r2 && c1==c2) {
        cout << 0 << endl;
        return 0;
    }
    if (r1+c1 == r2+c2 || r1-c1 == r2-c2 || abs(r1-r2)+abs(c1-c2) <= 3) {
        cout << 1 << endl;
        return 0;
    }
    if ((r1+c1)%2 == (r2+c2)%2 || abs(r1-r2)+abs(c1-c2) <= 6 || abs((r1+c1)-(r2+c2)) <= 3 || abs((r1-c1)-(r2-c2)) <= 3) {
        cout << 2 << endl;
    } else {
        cout << 3 << endl;
    }
}


#include<bits/stdc++.h>
#define RG register
#define R RG int
#define ll long long
#define D double
#define I inline
using namespace std;
int main()
{
	ll b,c;
	scanf("%lld%lld",&b,&c);
	if(b==0)printf("%lld\n",(c>>1)+(c-1>>1)+1);
	else if(b>0)
	{
		printf("%lld\n",(max(c-2>>1,0ll))+(min((b<<1)-1,(c>>1)+(c-1>>1)))+(c-1>>1)+2);
	}
	else printf("%lld\n",(c>>1)+(min((-b<<1)-1,(c-1>>1)+(c-2>>1)))+(c-1>>1)+2);
	return 0;
}

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

int N;
vector<int> A,B;

int solve(){
    int res = 2e5;
    REP(i,N) res = min(res,A[i]+B[i]);
    REP(i,N){
        REP(j,N){
            if(i == j) continue;
            res = min(res,max(A[i],B[j]));
        }
    }
    return res;
}

int main(){
    cin >> N;
    A.resize(N); B.resize(N);
    REP(i,N) cin >> A[i] >> B[i];
    cout << solve();
    return 0;
}


#include <iostream>
#include <iomanip>
#include <vector>
#include <array>
#include <stack>
#include <queue>
#include <tuple>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>
#include <cmath>
#include <map>
#include <numeric>
#include <cassert>
#include <string>
#include <list>

using ull = unsigned long long;
using ll = long long;

#define REP(i, n) for(ll i=0;i<(ll)n;i++)
#define REPab(i,a,b)for(ll i=(ll)a;i<(ll)b;i++)
constexpr bool bitp(ull bits, ull pos) { return bits & (1ULL << pos); }
constexpr ll mod(ll val, ll m) { return (val >= 0) ? (val % m) : (val % m + m); }
template <class T> void chmin(T& a, T b) { a = std::min(a, b); }
template <class T> void chmax(T& a, T b) { a = std::max(a, b); }
ll extgcd(const ll a, const ll b, ll& x, ll& y) {ll d = a;if (b != 0) { d = extgcd(b, a % b, y, x);y -= (a / b) * x;} else { x = 1; y = 0; } return d;}

int main() {
    ll N;
    std::cin >> N;
    std::unordered_set<ll> set;
    REP(i, N) {
        ll a;
        std::cin >> a;
        set.insert(a);
    }
    for (ll i = 1; i <= N;i++) {
        if (set.count(i) == 0) {
            std::cout << "No\n";
            return 0;
        }
    }
    std::cout << "Yes\n";
}

#include <bits/stdc++.h>
// #include <atcoder/all>
// using namespace atcoder;
using namespace std;
#define rep2(i, m, n) for (int i = (m); i < (n); ++i)
#define rep(i, n) rep2(i, 0, n)
#define drep2(i, m, n) for (int i = (m)-1; i >= (n); --i)
#define drep(i, n) drep2(i, n, 0)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#ifdef LOCAL
void debug_out() { cerr << endl; }
template <class Head, class... Tail> void debug_out(Head H, Tail... T) { cerr << ' ' << H; debug_out(T...); }
#define debug(...) cerr << 'L' << __LINE__ << " [" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define dump(x) cerr << 'L' << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
template<class T> using V = vector<T>;
template<class T> using P = pair<T, T>;
using ll = long long;
using ld = long double;
using Vi = V<int>;  using VVi = V<Vi>;
using Vl = V<ll>;   using VVl = V<Vl>;
using Vd = V<ld>;   using VVd = V<Vd>;
using Vb = V<bool>; using VVb = V<Vb>;
using Pi = P<int>;
using Pl = P<ll>;
using Pd = P<ld>;
template<class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
template<class T> vector<T> make_vec(size_t n, T a) { return vector<T>(n, a); }
template<class... Ts> auto make_vec(size_t n, Ts... ts) { return vector<decltype(make_vec(ts...))>(n, make_vec(ts...)); }
template<class T> inline int sz(const T &x) { return size(x); }
template<class T> inline bool chmin(T &a, const T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T &a, const T b) { if (a < b) { a = b; return true; } return false; }
template<class T1, class T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; }
template<class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << '(' << p.first << ", " << p.second << ')'; return os; }
template<class T, size_t n> istream &operator>>(istream &is, array<T, n> &v) { for (auto &e : v) is >> e; return is; }
template<class T, size_t n> ostream &operator<<(ostream &os, const  array<T, n> &v) { for (auto &e : v) os << e << ' '; return os; }
template<class T> istream &operator>>(istream &is, vector<T> &v) { for (auto &e : v) is >> e; return is; }
template<class T> ostream &operator<<(ostream &os, const vector<T> &v) { for (auto &e : v) os << e << ' '; return os; }
template<class T> inline void deduplicate(vector<T> &a) { sort(all(a)); a.erase(unique(all(a)), a.end()); }
template<class T> inline int count_between(const vector<T> &a, T l, T r) { return lower_bound(all(a), r) - lower_bound(all(a), l); } // [l, r)
inline ll cDiv(const ll x, const ll y) { return (x+y-1) / y; } // ceil(x/y)
inline int fLog2(const ll x) { assert(x > 0); return 63-__builtin_clzll(x); } // floor(log2(x))
inline int cLog2(const ll x) { assert(x > 0); return (x == 1) ? 0 : 64-__builtin_clzll(x-1); } // ceil(log2(x))
inline int popcount(const ll x) { return __builtin_popcountll(x); }
inline void fail() { cout << -1 << '\n'; exit(0); }
struct fast_ios { fast_ios(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
// const int INF  = (1<<30) - 1;
// const ll INFll = (1ll<<60) - 1;
// const ld EPS   = 1e-10;
// const ld PI    = acos(-1.0);

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

  if (n == 1) {
    if (m != 0) fail();
    cout << "1 2\n";
    return 0;
  }

  if (m < 0 || m > n-2) fail();

  int k = n - 1 - m;

  rep(i, k)   cout << (i+1) << ' ' << (2*n-i) << '\n';
  rep(i, n-k) cout << (k + 2*i + 1) << ' ' << (k + 2*i + 2) <<'\n';
}


#include "bits/stdc++.h"

using namespace std;

void solve(){
    int a,b,c,d;
    cin>>a>>b>>c>>d;
    cout<<a*d-b*c<<'\n';
}

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

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

typedef long long ll;
typedef vector<ll> vint;
typedef vector< vector<ll> > vvint;

#define rep(i,n) for(ll i=0;i<n;i++)
#define repf(i,f,n) for(ll i=f;i<n;i++)
#define repr(i,n) for(ll i=n-1;i>=0;i--)
#define mp make_pair
#define mt make_tuple
#define ALL(obj) (obj).begin(), (obj).end()
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; }

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

int main() {
    cout<<fixed<<setprecision(10);
    vint v(4); rep(i,4) cin>>v[i];
    cout << v[0]*v[3] -  v[1]*v[2] << endl;

    return 0;

}


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

//Standard input
bool checkMod7(int n, int mod){
    for(; n > 0; n = n / mod){
        if(n % mod == 7) return true;
    }
    return false;
}

int main(){
    int N, counter;
    bool dec_flag, oct_flag;
    cin >> N;
    counter = N;
    for(int i = N; i > 0; i--){
        if(checkMod7(i, 10) || checkMod7(i, 8)) counter--;
    }
    cout << counter << endl;
    return 0; //Must return 0 if you want to use return statement
}

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

        #define ff              first
        #define ss              second
        #define int             long long
        #define pb              push_back
        #define mp              make_pair
        #define pii             pair<int,int>
        #define vi              vector<int>
        #define mii             map<int,int>
        #define pqb             priority_queue<int>
        #define pqs             priority_queue<int,vi,greater<int> >
        #define setbits(x)      __builtin_popcountll(x)
        #define zrobits(x)      __builtin_ctzll(x)
        #define ps(x,y)         fixed<<setprecision(y)<<x
        #define mk(arr,n,type)  type *arr=new type[n];
        #define w(x)            int x; cin>>x; while(x--)
        #define fo(i, n)        for (int i = 0; i < n; i++)
        #define Fo(i, k, n)     for (int i = k; i < n; i++)
        #define all(v)          v.begin(), v.end()
        #define allr(v)          v.rbegin(), v.rend()
        #define endl            '\n'
        //mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());

        bool decimal(int n){
        bool flag=0;
        int l = 0;

        while(n){
            l = n%10;
            n = n/10;
            if(l==7){
                flag=1; break;
            }
        }

        if(flag) return 1;
        return 0;
        }

        bool octal(int n){
        int flag=0;
        //cout << pow << endl;

        while(n){
          if(n%8 == 7){
            flag = 1; break;
          }
          n = n/8;
        }

        if(flag) return 1;
        else return 0;
        }

        int32_t main()
        {

        int c, x=INT_MAX , t, i, y, sum=0, n, m;
        t = 1;
       ///cin >> t;
        while(t--){
             cin >> n ;
         Fo(i, 1, n+1){
         if(!decimal(i) && !octal(i))
                sum++;
         }

         cout << sum;

        }
        }


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

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)   FOR(i,0,n)
#define ALL(a)     (a).begin(),(a).end()
#define RALL(a)     (a).rbegin(),(a).rend()
#define PRINT(a)   cout << (a) << endl

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define Fi first
#define Se second

#define debug(x) cerr << x << " " << "(L:" << __LINE__ << ")" << '\n';

using ll = long long int;
using P = pair<int,int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<int, int>;

template <typename T> using PQ = priority_queue<T>;
template <typename T> using minPQ = priority_queue<T, vector<T>, greater<T>>;

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

const int INF = 1001001001;
const ll LINF = 1001001001001001001ll;
const int MOD = 1e9 + 7;

int main(){
  int n;
  cin >> n;
  vi a(n),b(n);
  REP(i,n)cin >> a[i] >> b[i];

  ll ans=LINF;
  REP(i,n)REP(j,n){
    if(i==j){
      chmin(ans, (ll)(a[i]+b[j]));
    }else{
      chmin(ans, (ll)max(a[i],b[j]));
    }
  }
  cout << ans << endl;
}

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

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

#define rep(i,cc,n) for(int i=cc;i<n;++i)
#define lrep(i,cc,n) for(long long i=cc;i<n;++i)
#define sqrep(i,cc,n) for(long long i=cc;i*i<=n;++i)
#define rrep(i,cc,n) for(int i=cc;i>=n;--i)
#define pii pair<int, int>
#define pll pair<long long, long long>

using ll = long long;

const vector<int> dx = {1, 0, -1,  0};
const vector<int> dy = {0, 1,  0, -1};

const vector<int> dx2 = {1, 1,  1, 0,  0, 0, -1, -1, -1};
const vector<int> dy2 = {1, 0, -1, 1, -1, 0,  1,  0, -1};

const double PI = 3.1415926535;
const ll inf = 1001001001;
const ll e9  = 1000000000;
const ll mod = 1000000007;
const ll mod2 = 998244353;

const ll MAX = 1000000;
const int MOD = 1000000007;
const ll big = (1ll<<60);

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

vector<vector<ll>>calc(vector<vector<ll>>a, vector<vector<ll>>b){
    vector<vector<ll>> c(3,vector<ll>(3));
    rep(i2, 0, 3){
        rep(j2, 0, 3){
            rep(k2, 0, 3){
                c[i2][j2] += a[i2][k2]*b[k2][j2];
            }
        }
    }
    return c;
}

int main(){
    int n;
    cin >> n;
    vector<ll>x(n),y(n);
    rep(i, 0, n)cin >> x[i] >> y[i];
    int m;
    cin >> m;
    vector<pll>op(m);
    vector<vector<vector<ll>>>matrix(m+1,vector<vector<ll>>(3,vector<ll>(3)))
    ,accum(m+1,vector<vector<ll>>(3,vector<ll>(3)));
    rep(i, 0, m){
        cin >> op[i].first;
        vector<vector<ll>>temp(3,vector<ll>(3));
        if(op[i].first==3||op[i].first==4)cin >> op[i].second;
        if(op[i].first==1){
            temp[0][1] = 1;
            temp[1][0] = -1;
        }else if(op[i].first==2){
            temp[0][1] = -1;
            temp[1][0] = 1;
        }else if(op[i].first==3){
            temp[0][0] = -1;
            temp[0][2] = 2*op[i].second;
            temp[1][1] = 1;
        }else{
            temp[0][0] = 1;
            temp[1][2] = 2*op[i].second;
            temp[1][1] = -1;
        }
        temp[2][2] = 1;
        matrix[i+1] = temp;
    }
    rep(i, 1, m+1){
        if(i==1)accum[i] = matrix[i];
        else{
            accum[i] = calc(matrix[i],accum[i-1]);
        }
    }
    int q;
    cin >> q;
    vector<pll>query(q);
    rep(i, 0, q)cin >> query[i].first >> query[i].second;
    rep(i, 0, q){
        if(query[i].first==0){
            cout << x[query[i].second-1] << " " << y[query[i].second-1] << endl;
        }else{
            ll ansx, ansy;
            ansx = accum[query[i].first][0][0]*x[query[i].second-1]+
            accum[query[i].first][0][1]*y[query[i].second-1]+
            accum[query[i].first][0][2]*1;

            ansy = accum[query[i].first][1][0]*x[query[i].second-1]+
            accum[query[i].first][1][1]*y[query[i].second-1]+
            accum[query[i].first][1][2]*1;

            cout << ansx << " " << ansy << endl;
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
//#include<atcoder/all>
//using namespace atcoder;
using ll = long long;
#define For(i,n,k) for(int i=(n);i<(k);i++)
#define ALL(a) (a).begin(),(a).end()
using P = pair<int, int>;
void Main(){
    int n, m;
    cin >> n >> m;
    vector<vector<P>> path(n);
    For(i,0,m){
        int a, b, c;
        cin >> a >> b >> c;
        path[a-1].push_back({b-1, c});
    }
    auto dijkstra = [&](int start, auto &graph){
        vector<int> dist(n+1, 1e9);
        priority_queue<P, vector<P>, greater<P>> pq;
        pq.push({0, start});
        while(!pq.empty()){
            auto [cur, now] = pq.top();
            pq.pop();
            if(cur > dist[now]) continue;
            for(auto [to, cost]:graph[now]){
                if(dist[to] > cur + cost){
                    dist[to] = cur + cost;
                    pq.push({dist[to], to});
                }
            }
        }
        return dist[start] < 1e9 ? dist[start] : -1;
    };
    For(i,0,n) cout << dijkstra(i, path) << endl;
}
int main(){
    Main();
    /*
    東方風神録は神が出てくるので当然神ゲー
    */
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
unsigned long long r,x,y;
int main(){
    cin >> r >> x >> y;
    unsigned long long target = x*x+y*y;
    unsigned long long r2 = r*r;
    unsigned long long ans = 0;
    if(target%r2==0){
        ans = target/r2;
        if(sqrt(ans)!=(unsigned long long)sqrt(ans)){
            cout << (unsigned long long)sqrt(ans)+1 << endl;
        }else{
            cout << (unsigned long long)sqrt(ans) << endl;
        }
    }else if(r2>target){
        cout << 2 << endl;
    }else{
        long double tmp = (long double)target/(long double)r2;
        if(sqrt(tmp)!=(long)sqrt(tmp)){
            cout << (long)sqrt(tmp)+1 << endl;
        }else{
            cout << (long)sqrt(tmp) << endl;
        }
    }
    return 0;
}


// Author: wlzhouzhuan
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pb push_back
#define fir first
#define sec second
#define rep(i, l, r) for (int i = l; i <= r; i++)
#define per(i, l, r) for (int i = l; i >= r; i--)
#define mset(s, t) memset(s, t, sizeof(s))
#define mcpy(s, t) memcpy(s, t, sizeof(t))
#define poly vector<int>
#define SZ(x) (int(x.size()))
template<typename T1, typename T2> void ckmin(T1 &a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> void ckmax(T1 &a, T2 b) { if (a < b) a = b; }
int read() {
  int x = 0, f = 0; char ch = getchar();
  while (!isdigit(ch)) f |= ch == '-', ch = getchar();
  while (isdigit(ch)) x = 10 * x + ch - '0', ch = getchar();
  return f ? -x : x;
}
template<typename T> void print(T x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10) print(x / 10);
  putchar(x % 10 + '0');
}
template<typename T> void print(T x, char let) {
  print(x), putchar(let);
}

const int N = 5005;
vector<int> adj[N];
bool vis[N];
int n, m;

int main() {
  n = read(), m = read();
  rep(i, 1, m) {
    int u = read(), v = read();
    adj[u].pb(v);
  }
  int num = 0;
  rep(i, 1, n) {
    rep(j, 1, n) vis[j] = 0;
    queue<int> q;
    q.push(i), vis[i] = 1;
    while (!q.empty()) {
      ++num;
      int u = q.front(); q.pop();
      for (auto v: adj[u]) {
        if (!vis[v]) {
          vis[v] = 1, q.push(v);
        }
      }
    }
  }
  print(num, '\n');
  return 0;
}

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

vector<bool> seen;
ll dfs(const Graph &G, ll v, ll cnt) {
    seen[v] = true;
    ll res = cnt;

    for (auto next_v : G[v]) {
        if (seen[next_v]) continue;
        res = dfs(G, next_v, res)+1;
    }
    return res;
}

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

    Graph G(N);
    for (ll i = 0; i < M; i++)
    {
        ll a, b;
        cin >> a >> b;
        a--;b--;
        G[a].push_back(b);
    }

    ll ans = 0;
    for(ll i = 0; i < N; i++){
        seen.assign(2010, false);
        ans += dfs(G,i,0);
    }

    cout << ans + N << endl;

    return 0;
}


/*{{{*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<limits.h>
#include<assert.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#define MP make_pair
#define PB emplace_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<PII> VPII;
typedef pair<LL,LL> PLL;
typedef vector<PLL> VPLL;
template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(int64_t &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); }
template<class T> void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const int64_t &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T,class U> void _W(const pair<T,U> &x) {_W(x.F); putchar(' '); _W(x.S);}
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }
#ifdef HOME
 #define DEBUG(...) {printf("[DEBUG] ");W(__VA_ARGS__);}
#else
 #define DEBUG(...)
#endif
int MOD = 1e9+7;
void ADD(LL& x,LL v){x=(x+v)%MOD;if(x<0)x+=MOD;}
/*}}}*/
const int SIZE = 1<<20;
const int COMBINATION_SIZE = 1<<21;
LL pp[5001][5001];
int N,M;
void pre() {
    pp[0][0]=1;
    FOR(i,1,5000){
        pp[i][0]=1;
        FOR(j,1,5000){
            pp[i][j]=(LL)pp[i][j-1]*i%MOD;
        }
    }
}
void solve() {
    LL an=0;
    FOR(i,1,N){
        FOR(j,1,M){
            if(j<M||(j==M&&i==1)){
                ADD(an,pp[M-j][i-1]*(j-1)%MOD*(N-i)%MOD*pp[M][N-i-1]);
                ADD(an,pp[M-j][i-1]*pp[M][N-i]);
            }
        }
    }
    W(an);
}
void input() {
    R(N,M);
}
int main(){
    MOD=998244353;
    pre();
    input();
    solve();
    return 0;
}

#include <bits/stdc++.h>
#define rep0(i,r) for (int i=0,i##end=r;i<i##end;++i)
#define rep(i,l,r) for (int i=l;i<=r;++i)
#define per(i,r,l) for (int i=r;i>=l;--i)
#define ll long long
#define pb push_back
using namespace std;
const int N=5e3+3,P=998244353;
int dp[N][2],pw[N];
void upd(int x,int &y,int c){ y=(y+(ll)x*c)%P; }
int main(){
	int n,m,ans=0; cin>>n>>m;
	pw[0]=1; rep(i,1,n) pw[i]=(ll)pw[i-1]*m%P;
	rep(v,1,m){
		dp[0][0]=1;//count
		rep(i,1,n) dp[i][0]=dp[i][1]=0;
		rep(i,1,n){
			upd(dp[i-1][0],dp[i][0],m-1);
			upd(dp[i-1][0],dp[i][1],1);
			ans=(ans+(ll)dp[i-1][0]*pw[n-i])%P;
			upd(dp[i-1][1],dp[i][0],v-1);
			upd(dp[i-1][1],dp[i][1],m-v+1);
		}
	}
	printf("%d\n",ans);
 	return 0;
}