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

using namespace std;
// vector push_back push front top empty pop make_pair long long insert begin end
typedef long long ll;
typedef vector<int> vi;
typedef vector<pair <int,int> > vpi;
typedef vector<long long> vll;
typedef pair<int,int> pi;
#define F first
#define ln '\n'
#define S second
#define PB push_back
#define MP make_pair
#define B begin()
#define RB rbegin()
#define E end()
#define RE rend()
#define Z size()
#define REP(i,a,b) for (int i = a; i < b; i++)
#define L length()
#define show(a) cerr << " *** " << a << endl;
#define show1(a) cerr << " /// " << a << endl;
#define valid(a,b,c) (a >= b && a < c ? 1 : 0)
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
const int mod = (int)1e9 + 7;
const ll INF64 = 3e18;
void smxl(ll &a, ll b){if (a < b)a=b;}
void smnl(ll &a, ll b){if (a > b)a=b;}
void adsl(ll &a, ll b){a += b;if (a >= mod)a -= mod;}
void misl(ll &a, ll b){a -= b;if (a >= mod)a -= mod; if (a < 0)a += mod;}
void smx(ll &a, ll b){if (a < b)a=b;}
void smn(ll &a, ll b){if (a > b)a=b;}
void ads(ll &a, ll b){a += b;if (a >= mod)a -= mod;}
void mis(ll &a, ll b){a -= b;if (a >= mod)a -= mod; if (a < 0)a += mod;}
ll gcd(ll a, ll b) {return (b==0? a:gcd(b,a%b));}
ll egcd(ll a, ll b, ll & x, ll & y) {if (a == 0){x = 0;y = 1;return b;}ll x1, y1;ll d = egcd(b % a, a, x1, y1);x = y1 - (b / a) * x1;y = x1;return d;}
ll mbinp(ll a, ll b){a %= mod;if (b == 0)return 1;ll ans = mbinp(a, b/2);ll tmp = (ans * ans) % mod;if (b % 2)return ((tmp * a) % mod);return ((tmp) % mod);}
ll binp(ll a, ll b){if (b == 0)return 1;ll ans = binp(a, b/2);ll tmp = (ans * ans);if (b % 2)return ((tmp * a));return ((tmp));}
long long C(int n, int m){ll ret=1;for(int i=1;i<=m;i++){ret*=(n-i+1);ret/=i;}return ret;}
long long overbinp(long long a, int b){long long res = 1;while (b){if (b & 1){if (res < INF64 / a) res *= a;else return INF64;}if (b > 1){if (a < INF64 / a) a *= a;else return INF64;}b >>= 1;}return res;}
/*const int FMAXN = 1000 * 1000 + 10;int fen[FMAXN];void add(int x, int val){for (int i = x + 1; i < MAXN; i += i & (-i)) fen[i] += val;}int get(int x){int ans = 0;for (int i = x; i > 0; i -= i & (-i)) ans += fen[i];return ans;}int sum(int x, int y){return get(y) - get(x);}*/
/*int distfloor(int n){int sum;int la;for (int i = 1; i <= n; i = la+1){la = n/(n/i);sum++;show(n/i);}return sum;}*/
//int pointsCoveredBySegment(pi a, pi b){return(gcd((int)abs(a.F-b.F),(int)abs(a.S-b.S))+1);}
//segment inters
/*struct seg{int x1, y1, x2, y2;seg(){};};
struct line{long long Al, Bl, Cl;line(){};line(seg a){Al = a.y1 - a.y2;Bl = a.x2 - a.x1;Cl = -Al * a.x1 - Bl * a.y1;};};
int get(seg a){int dx = a.x1 - a.x2;int dy = a.y1 - a.y2;return __gcd(abs(dx), abs(dy)) + 1;}
long long det(long long a, long long b, long long c, long long d){return a * d - b * c;}
bool in(int x, int l, int r){if (l > r) swap(l, r);return (l <= x && x <= r);}
bool inter(seg a, seg b, int& x, int& y){line l1(a), l2(b);long long dx = det(l1.Cl, l1.Bl, l2.Cl, l2.Bl);long long dy = det(l1.Al, l1.Cl, l2.Al, l2.Cl);long long d = det(l1.Al, l1.Bl, l2.Al, l2.Bl);if (d == 0)return false;if (dx % d != 0 || dy % d != 0)return false;x = -dx / d;y = -dy / d;if (!in(x, a.x1, a.x2) || !in(y, a.y1, a.y2))return false;if (!in(x, b.x1, b.x2) || !in(y, b.y1, b.y2))return false;return true;}*/
/*st int nax = 105;
int par[nax];
int siize[nax];
int get(int x)
{
  return (x == par[x] ? x : par[x] = get(par[x]));
}
void merge(int a, int b) {
  int x = get(a);
  int y = get(b);
  if (x == y)return;
  if (siize[x] < siize[y])swap(x,y);par[y] = x;siize[x] += siize[y];}*/
//const int nx = 1e5+10;ll cmd[nx];ll civ[nx];void initcmod(){	cmd[0] = 1;	cmd[1] = 1;	REP(i,2,nx)	{		cmd[i] = cmd[i-1]*i;		cmd[i] %= mod;	}	REP(i,0,nx)		civ[i] = mbinp(cmd[i],mod-2);}ll cmod(int n, int m){	initcmod();	ll ans = cmd[n];	ans *= civ[m];	ans %= mod;	ans *= civ[n-m];	ans %= mod;	return ans;}
//double triarea(double a, double b, double c){double p= (a+b+c)/2;return (sqrt(p*(p-a)*(p-b)*(p-c)));}
//ll diag(ll n){return ((n*(n-1))/2 - n);}
//number of divs of n=p^a*q^b*r^c  == (a+1)*(b+1)*(c+1)
//sum of divs of n=p^a*q^b*r^c  == (p^(a+1)-1)/(p-1) * (q^(b+1)-1)/(q-1) * (r^(c+1)-1)/(r-1)
//prod of divs of n=p^a*q^b*r^c  == n ^ (((a+1)*(b+1)*(c+1))/2)
void display(vi &a){for(int z : a)cout << z << " ";cout << endl;}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
  int n;
  cin >> n;
  queue<pi>kyou;
  kyou.push(MP(n,2));
  int ans = 1e9;
  while(kyou.Z)
  {
    pi x = kyou.front();
    kyou.pop();
    if (x.S==0)
    {
      ans = min(ans,x.F);
      continue;
    }
    vi divs;
    int tmp = x.F;
    for(int i = 2; i*i <= tmp;i++)
    {
      if(tmp%i == 0)
        divs.PB(i);
      while(tmp%i==0)
        tmp/=i;
    }
    if (tmp!= 1)
      divs.PB(tmp);
    kyou.push(MP(x.F,x.S-1));
    for (int z : divs)
    {
      int tmp = x.F-1;
      while(tmp%z != 0 && tmp > z)
      {
        kyou.push(MP(tmp,x.S-1));
        tmp--;
      }
    }
  }
  cout << ans;
}


#include<iostream>

using namespace std;

int main()
{
int n,a[50]={-1};
cin>>n;
long int prod=1;
int m,j,l=0;
while(n>=0)
{
m=n%10;
a[l]=m;
l++;
n/=10;
}
for( j=0;j<=l;j++)
{
	if(a[j]==9 || a[j]==6 || a[j]==5 || a[j]==4 || a[j]==2)
		prod=prod*1;
	else if(a[j]==8 || a[j]==0)
		prod=prod*2;
	else if(a[j]==7 || a[j]==1)
		prod=prod*5;
 	else
		prod=prod*3;
}
cout<<prod<<endl;
return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long llong;

const llong MOD = 1e9 + 7;

inline tuple<llong, llong, llong> count_remainders(int l, int r) {
	return make_tuple(r / 3 - l / 3, (r + 2) / 3 - (l + 2) / 3, (r + 1) / 3 - (l + 1) / 3);
}

void solve(int n, int l, int r) {
	vector<vector<llong>> dp(3, vector<llong>(n + 1, 0));
	dp[0][0] = 1;

	tuple<llong, llong, llong> abc = count_remainders(l, r);

	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < 3; ++j) {
			for (int k = 0; k < 3; ++k) {
				dp[(j + k) % 3][i + 1] += dp[j][i] * abc.get(k);
				dp[(j + k) % 3][i + 1] %= MOD;
			}
		}
	}

	cout << dp[0][n] << "\n";
}

int main() {
	int n, l, r; cin >> n >> l >> r;

	solve(n, l, r);

	return 0;
}


#include <iostream>
using namespace std;
char table[9][9];
bool lineGoodW[9] = {1,1,1,1,1,1,1,1,1};
bool lineGoodB[9] = {1,1,1,1,1,1,1,1,1};
int main() {
    for(int i = 0 ; i < 8 ; i++){
            cin>>table[i];
    }
    char *p;
    char *t;
    int P,T;
    int firstW = 0,firstB = 0;
    for(int i = 0 ; i < 8 ; i++){
        for(int j = 0 ; j < 8 ; j++){
            if(table[i][j] == 'B')
                lineGoodW[j] = false;
            else if(table[i][j] == 'W' && lineGoodW[j] == true){
                firstW = i;
                i = -1;
            }
        }
    }
    for(int i = 7 ; i >= 0 ; i--){
        for(int j = 0 ; j < 8 ; j++){
            if(table[i][j] == 'W')
                lineGoodB[j] = false;
            else if(table[i][j] == 'B' && lineGoodB[j] == true){
                firstB = 7-i;
                i = -1;

            }
        }
    }
    cout << firstB << firstW;
    if(firstB < firstW)
        cout << "B";
    else
        cout << "A";

    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
    int r1,r2,c1,c2,d1,d2;
    while(cin>>r1>>r2>>c1>>c2>>d1>>d2)
    {
        int b=(r1-c1+d2)/2;
        int a=r1-b,c=d2-b,d=c2-b;
        int flag=1;
        if(a+b!=r1||c+d!=r2||a+c!=c1||b+d!=c2||a+d!=d1||b+c!=d2)flag=0;
        if(flag)
        {cout<<r1-b<<' '<<b<<endl;
        cout<<d2-b<<' '<<c2-b<<endl;
        }
        else cout<<-1<<endl;
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <stack>
#include <utility>

using namespace std;
int n,m;

int main() {
	scanf("%d%d", &n,&m);
	vector<vector<bool>> vis(n, vector<bool>(m,false));
	string hh; cin >> hh;
	string vv; cin >> vv;
	stack<pair<int,int>> st;
	st.push(make_pair(0, 0));
	int r = 1;
	vis[0][0] = true;
	while (!st.empty()) {
		auto &c = st.top(); st.pop();
		int x = c.first;
		int y = c.second;
		if (hh[x] == '>'&& (y+1) < m && !vis[x][y+1]) {
			st.push(make_pair(x, y+1));
			r++;
		}else if (hh[x] == '<'&&y - 1>=0 && !vis[x][y-1]) {
			st.push(make_pair(x , y-1));
			r++;
		}
		if (vv[y] == 'v' && (x + 1) < n && !vis[x+1][y]) {
			st.push(make_pair(x+1, y)); r++;
		}
		else if (vv[y] == '^'&&x - 1 >= 0 && !vis[x-1][y]) {
			st.push(make_pair(x-1, y)); r++;
		}
	}
	if (r == m * n) {
		cout << "YES";
	}
	else {
		cout << "NO";
	}

}


#include <limits.h>
#include <stdlib.h>
#include <iostream>
#include <map>
using namespace std;

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

    long long a,b,c;
    cin>>a>>b>>c;
    if(max(a,b)>c)
    {
        cout<<0;
        return 0;
    }
    if(a<=0&&b<=0)
    {
        cout<<-1;
        return 0;
    }

    long long ans=0;
    if(a<0)
    {
        ans+=(-1*a)/b;
        a+=((-1*a)/b)*b;
    }
    while(a<c&&b<c)
    {
        ans++;
        a+=b;
        if(a>b)
            swap(a,b);

    }

    cout<<ans;

    return 0;

}


#define _CRT_SECURE_NO_WARNINGS
//#pragma comment(linker, "/STACK:100000000")
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <cstring>
#include <set>
#include <stdio.h>
#include <vector>
#include <map>
#include <time.h>
#include <memory.h>

#define ll long long
#define ld long double
#define mp make_pair
#define pii pair<ll, ll>
#define pll pair<ll, ll>
#define pi 3.141592653589793238462643383279
ll gcd(ll a, ll b) {
	if (b == 0)
		return a;
	else
		return gcd(b, a % b);
}

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

ll phi(ll n){
	ll result = n;
	for (ll i = 2; i*i <= n; i++) {
		if (n % i == 0) {
			while (n % i == 0) n /= i;
			result -= result / i;
		}
	}
	if (n > 1) result -= result / n;
	return result;
}
#define inf 1000000007

#define eps 0.0001
#define ls (v << 1)
#define rs ((v << 1)+1)
//#define y1 privet

void nxi(ll& n){
	bool min = 0; char c; n = 0;
	while ((c = getc(stdin)) && c <= 32);
	if (c == '-') min = 1; else n = c - 48;
	while ((c = getc(stdin)) && c>32) n = (n << 3) + (n << 1) + c - 48;
	if (min) n = -n;
}

void prl(ll n){
	if (n == 0) { puts("0"); return; }
	if (n<0) { putchar('-'); n = -n; }
	static ll s[10];
	ll top = 0;
	while (n>0) s[top++] = n % 10, n /= 10;
	while (top--) putchar(s[top] + 48);
	puts("");
}

#define x1 first
#define y1 second
#define x0 privet
#define y0 pak
#define pll pair<ll, ll>
#define X first
#define Y second
#define ppii pair< pii ,  pii >
using namespace std;

const ll nmax = 2100;
ll n, t, cb[nmax][nmax], e, maxbit=62;

int main()
{
    ios::sync_with_stdio(0);
#ifdef ONLINE_JUDGE
	freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#endif

	cin >> n >> t;
	if (t & (t - 1)) return cout << 0 << endl, 0;
	int e = 0;
	for (; t != 1; t >>= 1, ++e);

	for (int i = 0; i < maxbit; ++i)
		for (int j = 0; j <= i; ++j)
			cb[i][j] = j ? cb[i - 1][j - 1] + cb[i - 1][j] : 1;

	ll ans = 0;
	int x = 0;
	n += 2;
	 ++e;
	for (int b = maxbit - 1; b >= 0; --b) {
		if (n & (1ll << b)) {
			ans += cb[b][e - x];
			++x;
			if (x > e) break;
		}
	}
	if (e == 1) ans -= 1;

	cout << ans << endl;

	return 0;
}


#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
using namespace std;

typedef long long int64;

const int INF = 0x3f3f3f3f;
const int MAXN = 1e7 + 10;

bool isnt_prime[MAXN];
int prime[MAXN], phi[MAXN];
int64 mfac_cnt[MAXN], mfac_sum[MAXN];

#define sum(l, r) (((r) < (l)) ? 0 : (mfac_sum[(r)] - mfac_sum[(l) - 1]))

int main() {
    int n;
    cin >> n;
    isnt_prime[1] = true;
    phi[1] = 1;
    int prime_cnt = 0;
    for (int i = 2; i <= n; i++) {
        if (!isnt_prime[i]) {
            prime[prime_cnt++] = i;
            mfac_cnt[i]++;
            phi[i] = i - 1;
        }
        for (int j = 0; j < prime_cnt; j++) {
            if (i * prime[j] > n) break;
            isnt_prime[i * prime[j]] = true;
            mfac_cnt[prime[j]]++;
            if (i % prime[j] == 0) {
                phi[i * prime[j]] = phi[i] * prime[j];
                break;
            } else {
                phi[i * prime[j]] = phi[i] * (prime[j] - 1);
            }
        }
    }
    for (int i = 1; i <= n; i++) {
        mfac_sum[i] = mfac_sum[i - 1] + mfac_cnt[i];
    }
    int path2 = 0;
    for (int i = 1; i <= n; i++) {
        path2 += phi[i] - 1;
        // clog << phi[i] << endl;
    }
    int path1 = (n - 1) * (n - 2) / 2 - path2;
    // 先假设都是长度为2的路径, 待会统计的时候再调整
    int64 ans = path2 * 2 + path1;
    for (int i = 0; i < prime_cnt; i++) {
        // 长度为3的路径, fa < n / 2
        ans += mfac_cnt[prime[i]] *
               sum(max(prime[i] + 1, n / prime[i] + 1), max(prime[i], n / 2));
        // 不存在的路径, fa > n / 2
        ans -= 2 * mfac_cnt[prime[i]] * sum(max(prime[i], n / 2) + 1, n);
    }
    cout << ans << endl;
    return 0;
}


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

// -- All pre-processors and typedef constants -- //
#define scd(t) scanf("%d",&t)
#define scld(t) scanf("%ld",&t)
#define sclld(t) scanf("%lld",&t)
#define scc(t) scanf("%c",&t)
#define scs(t) scanf("%s",t)
#define scf(t) scanf("%f",&t)
#define sclf(t) scanf("%lf",&t)
#define mem(a, b) memset(a, (b), sizeof(a))
#define repd(i,k) for(ll i=k;i>=0;i--)
#define rep(i,k) for(ll i=0;i<k;i++)
#define repn(i,k1,k2) for(ll i=k1;i<k2;i++)
#define sz(x) (ll)(x).size()
#define ff first
#define ss second

#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define foreach(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define in(A, B, C) assert( B <= A && A <= C)
#define mp make_pair
#define pb push_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define ee6 (ll)1000001
#define ee5 (ll)100001

#define tt ll t;cin>>t; while(t--)
typedef unsigned long long int ll;

typedef pair<ll, ll> pr;
typedef vector<ll> vi;
typedef vector<string> vs;
typedef vector<pr> vpr;
typedef vector<vpr>vvpr;
typedef vector<vi> vvi;
typedef map<ll,ll> MPII;
typedef set<ll> SETI;
typedef multiset<ll> MSETI;
#define MAXN 1e8

// stores smallest prime factor for every number
ll spf[(ll)MAXN];

// Calculating SPF (Smallest Prime Factor) for every
// number till MAXN.
// Time Complexity : O(nloglogn)
void sieve()
{
    spf[1] = 1;
    for (ll i=2; i<MAXN; i++)
        spf[i] = i;

    for (ll i=4; i<MAXN; i+=2)
        spf[i] = 2;

    for (ll i=3; i*i<MAXN; i++)
    {
        if (spf[i] == i)
        {
            for (ll j=i*i; j<MAXN; j+=i)

                if (spf[j]==j)
                    spf[j] = i;
        }
    }
}

// A O(log n) function returning primefactorization
// by dividing by smallest prime factor at every step
ll getFactorization(ll x)
{
    ll temp =x;
    vector<ll> ret;
    ll mini = INT_MAX;
    while (x != 1)
    {
        ret.push_back(spf[x]);
        x = x / spf[x];
        mini=min(mini,spf[x]+temp/spf[x]);
    }
    return mini;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    ll n;
    cin>>n;
    if(n==0){
        cout<<0<<endl;
        exit(0);
    }
    if(n==1){
        cout<<2<<endl;
        exit(0);
    }
    if(n==2){
        cout<<3<<endl;
        exit(0);
    }
    sieve();
    cout<<min(getFactorization(n),min(getFactorization(n+1),getFactorization(n+2)))<<endl;
}


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

#define sc( x ) scanf( "%d" , &x )
#define REP( i , n ) for( int i = 0 ; i < n ; ++i )
#define clr( t , val ) memset( t , val , sizeof( t ) )

#define pb push_back
#define all( v ) v.begin() , v.end()
#define SZ( v ) ((int)(v).size())

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

#define N 1000000
#define M 1000000007LL

#define look1( v , n ) REP( i , n ) cout<<v[ i ]<<char( i + 1 == n ? 10:32 );
typedef long long ll;
typedef unsigned long long ull;

typedef vector< ull > vull;
ll pow( ll a , ll b , ll c ){
    ll ans = 1;
    while( b ){
        if( b & 1 ) ans = ( ans * a )%c;
        a = ( a * a )%c;
        b >>= 1;
    }
    return ans;
}
ll mod_inv( ll a , ll p ){
    return pow( a , p - 2 , p );
}
ll F[ N + 4 ] , P[ N + 4 ];
bool ok( ll s , int a , int b ){
    while( s ){
        int d = s%10;
        s /= 10;
        if( d != a && d != b ) return 0;
    }
    return 1;
}
int main(){
    int a , b , n;
    cin>>a>>b>>n;

    F[ 0 ] = P[ 0 ] = 1;
    for( int i = 1; i <= N ; i++ ) F[ i ] = ( F[ i - 1 ]*i ) %M;
    for( int i = 1; i <= N ; i++ ) P[ i ] = mod_inv( F[ i ] , M );

//  look1( P , 20 );
    ll ans = 0;

    REP( i , n + 1 ){
        ll sum = i*a + ( n - i )* b;
        ll j = n - i, val = 0;
        if( ok( sum , a , b ) ){
            val = ( ((F[ n ]*P[ i ])%M)*P[ j ] ) % M;
        }
        ans += val;
    }
    cout<<ans<<endl;

}


#include<cstdio>

const int MAXN = 100;

int a,b,k;
int l;
bool is_prime[MAXN];
int prime_lst[MAXN];
int prime_size=0;
int prime_cnt[MAXN];

void cal_prime()
{
	for (int i = 0; i < MAXN; i++)
		is_prime[i]=true;
	for (int i = 4; i < MAXN; i+=2)
		is_prime[i]=false;
	is_prime[0] = is_prime[1] = false;
	prime_lst[prime_size++] = 2;
	for (int i = 3; i<MAXN; i+=2){
		if(is_prime[i])	{
			prime_lst[prime_size++] = i;
			//printf("prime:%d\n", i);
		}
		for (int j = 0; j < prime_size; j++){
			unsigned long long s = i * prime_lst[j];
			if (s >= MAXN) break;
			is_prime[s] = false;
			if (i % prime_lst[j] == 0)	break;
		}
	}
}

int main()
{
	scanf("%d%d%d", &a, &b, &k);
	cal_prime();
//	printf("1");
	prime_cnt[0] = 0;
	for (int i = 1; i < MAXN; i++){
		prime_cnt[i] = prime_cnt[i-1] + (is_prime[i]?1:0);
	//	printf("%d %d %d\n", i, prime_cnt[i], is_prime[i]);
	}
//	printf("%d %d\n", prime_cnt[a], prime_cnt[b]);
	if (prime_cnt[b] - prime_cnt[a-1] < k){
		printf("-1\r\n");
	//	while(1);
		return 0;
	}

	int cnt = 0;
	l = 1;
	for (int x = a,xl = a; x <= b&& xl <= b; x++){

		while(cnt < k){
			if (xl > b){
				xl++;
				break;
			}
			if (is_prime[xl])
				cnt++;
			xl++;

		}
		if (xl - x > l)l = xl-x;
		if (is_prime[x])
			cnt--;
	}
	printf("%d\r\n", l);
//	while(1);

}


#include <bits/stdc++.h>

using namespace std;

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

#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define TEST freopen("in.txt","r",stdin);
#define ab(a) ((a < 0) ? (-(a)) : (a))

int main(){
	fastIO;
	int n;
	cin >> n;
	vector<int>a;
	int k;
	for(int i = 0;i<n;i++){
		cin >> k;
		a.push_back(k);
	}
	while(!a.empty() and a.back() == 0)
		a.pop_back();
	int l = 0;
	for(int i = 0;i<a.size()-1;i++){
		if(a[i] == 0 and a[i+1] == 0){
			i++;
		}
		l++;
	}
	cout << l;
	return 0;
}


///*/////////////////// /// *
/*//    author-joydip007x  ///  *
/ *///  <^> <^> <^> <^> <^> <^> /// *
///*<^> Never tired :)<^>:V	*///
//*/*** Never Give UP ***///
///     Date<^>XX/08/2018	*///

#include<bits/stdc++.h>

using namespace std;

#define  LOOP(i,L,U)        for(long long int i=(long long int)L;i<U;i++)
#define  LOOPeq(i,L,U)     for(long long int i=(long long int)L;i<=U;i++)
#define  preflag 	            long long flag=0,i=0,j=0,k=0,l=0
#define  preflag2             long double flag=0,i=0,j=0,k=0,l=0,n,R,r
#define  mem(arr,x)          memset((arr), (x), sizeof((arr)));

#define  pb		push_back
#define  ppb 	pop_back()
#define  pii 		pair<long long int , long long int >
#define  msi 	map< string,long long int>
#define  vi 		vector<long long int >
#define  iit 		iterator it,it1,it2
#define  iip 		iterator ip,ip1,ip2

#define  sz 		                100000
#define  check(x) 	        cout<<":::"<<x<<":::"<<endl
#define  doublecheck(x,y) cout<<":::"<<x<<":::"<<y<<endl
#define  i(c) 		            cin>>c
#define  o(c) 		            scanf("%lld",&c);
#define  mz(c,r)   	            long long r=c;while(c--)

#define  fs              first
#define  sc              second
#define  pi 		       acos(-1.0)
#define  eps 		   1e-9

#define notrue 		false
#define ifnot 		    else
#define eee 		        cout<<endl

int main()
{
  while(1){
  preflag2;
  cin>>n>>R>>r;
  long double theta=n*asin((r)/(R-r));
  ///check(theta);
  if(theta+eps<=pi ) cout<<"YES"<<endl;
  else cout<<"NO"<<endl;

  }
}


#include <cstring>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <deque>
#include <fstream>
#include <unordered_map>
#include <unordered_set>
#include <iostream>
#include <queue>
#include <stack>
#include <time.h>
#include <limits>
#include <cmath>

using namespace std;

const long long N = 1000100;

vector<pair<int, int>> points;
bool isUsed[N];

long long ccw(pair<int, int> a, pair<int, int> b, pair<int, int> c) {
    return (b.first - a.first) * (c.second - a.second) - (b.second - a.second) * (c.first - a.first);
}

void solve() {
    long long n;
    cin >> n;
    for (long long i = 0; i < n; i++) {
        long long x, y;
        cin >> x >> y;
        points.push_back({x, y});
    }
    string s;
    cin >> s;
    pair<int, int> curP = {INT_MAX, INT_MAX};
    long long pivot = 0;
    for (long long i = 0; i < points.size(); i++) {
        if (points[i].second < curP.second) {
            curP = points[i];
            pivot = i;
        }
        if (points[i].second == curP.second) {
            if (points[i].first < curP.first) {
                curP = points[i];
                pivot = i;
            }
        }
    }
    isUsed[pivot] = true;
    for (long long i = 0; i < s.size(); i++) {
        cout << pivot + 1 << " ";
        long long middle = -1;
        if (s[i] == 'L') {
            for (long long j = 0; j < n; j++) {
                if (isUsed[j]) continue;
                if (middle == -1 || ccw(points[pivot], points[middle], points[j]) < 0) {
                    middle = j;
                }
            }
        } else {
            for (long long j = 0; j < n; j++) {
                if (isUsed[j]) continue;
                if (middle == -1 || ccw(points[pivot], points[middle], points[j]) > 0) {
                    middle = j;
                }
            }
        }
        pivot = middle;
        isUsed[pivot] = true;
    }
    cout << pivot + 1 << " ";
    for (long long i = 0; i < n; i++) {
        if (!isUsed[i]) {
            cout << i + 1 << endl;
        }
    }

}

long long main() {
    solve();

    return 0;

}


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

#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

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

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

auto _fast_io = []() { std::ios::sync_with_stdio(false); cin.tie(nullptr); return cout.tie(nullptr); }();

template <typename T>
istream& operator>>(istream& in, vector<T>& v) {
    for (size_t i = 0; i < v.size(); ++i) {
        in >> v[i];
    }
    return in;
}

template <typename T>
ostream& operator<<(ostream& out, vector<T>& v) {
    out << '{';
    if (!v.empty()) {
        out << v.front();
        for (size_t i = 0; i < v.size(); ++i) {
            out << ", " <<  v[i];
        }
    }
    out << '}';
    return out;
}

template <typename T, size_t Size>
istream& operator>>(istream& in, array<T, Size>& v) {
    for (size_t i = 0; i < Size; ++i) {
        in >> v[i];
    }
    return in;
}

bool operator<(const pi& l, const pi& r) {
    return l.first < r.first || (l.first == r.first && l.second < r.second);
}

pi operator-(const pi& left, const pi& right) {
    return make_pair(left.first - right.first, left.second - right.second);
}

int main() {
    int n;
    cin >> n;
    vector<pair<pi, int>>  ps(n);
    for (int i = 0; i < n; ++i) cin >> ps[i].first.first >> ps[i].first.second;
    for (int i = 0; i < n; ++i) ps[i].second = i + 1;
    sort(ps.begin(), ps.end());
    string s;
    cin >> s;
    pi last_d = { 1, 1 };
    auto angle = [&last_d] (const pi& p) {
        const double cosine = (int64_t(p.first) * last_d.first + int64_t(p.second) * last_d.second) / hypot(p.first, p.second);
        const double sine = (int64_t(p.first) * last_d.second - int64_t(p.second) * last_d.first) / hypot(p.first, p.second);
        return std::atan2(sine, cosine);
    };
    for (int i = 0; i < s.size(); ++i) {
        auto comparator = [p=ps[i].first, &angle](pair<pi, int>& left, pair<pi, int>& right) {
            return  angle(left.first - p) < angle(right.first - p);
        };
        //cout << ps[i].second << ": ";
        //for (int j = i + 1; j < ps.size(); ++j) cout << ps[j].second << '(' << angle(ps[j].first - ps[i].first) << ") ";
        //cout << endl;
        if (s[i] == 'L') {
            iter_swap(ps.begin() + i + 1, max_element(ps.begin() + i + 1, ps.end(), comparator));
        } else {
            iter_swap(ps.begin() + i + 1, min_element(ps.begin() + i + 1, ps.end(), comparator));
        }
        last_d.first = ps[i+1].first.first - ps[i].first.first;
        last_d.second = ps[i+1].first.second - ps[i].first.second;
    }
    for (int i = 0; i < ps.size(); ++i) cout << ps[i].second << ' ';
    cout << endl;
}


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

#define F first
#define S second
//#define int long long
#define ll long long
//#define int  unsigned long long
#define pb push_back
//#define double long double

using namespace std;
using namespace __gnu_pbds;
typedef  tree< int , null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
const int mod = 1e9 + 7;
const int N = 1200010;
const int K = 15;
int l[N], r[N], f[N], len[N], p[N], used[30][N];
vector < int > u, v, t, pr;
map < vector < int >, map < int, int > > d;
map < int, int > d0;
queue < pair < int, int > > q;
void calc (int p, int v, int x, int pr = 100){
    if (x < 0) return;
    if (used[p][v] > x) return;
    used[p][v] = x;
    q.push({p, v});
    if (d[u].find(v) == d[u].end()) d[u][v] = K - x; else d[u][v] = min(d[u][v], K - x);
    if (p >= u.size()) return;
    for (int i = max(1, u[p] - x); i < u[p] + x; i++){
        int y = v;
        calc(p + 1, v / u[p] * i, x - abs(i - u[p]), i);
    }
}
void precalc (int p, int v, int x, int pr = 100){
    if (x < 0) return;
    if (d0.find(v) == d0.end()) d0[v] = K - x; else d0[v] = min(d0[v], K - x);
    if (p >= t.size()) return;
    for (int i = max(1, t[p] - x); i < t[p] + x && i <= pr; i++){
        int y = v;
        precalc(p + 1, v / t[p] * i, x - abs(i - t[p]), i);
    }
}
vector < int > tovec(int x){
    vector < int > res, z;
    while (x != 1){
        z.pb(p[x]);
        x /= p[x];
    }
    int pr = -1;
    int c = 0;
    sort(z.begin(), z.end());
    for (int i = 0; i < z.size(); i++) if (pr == z[i]) c++; else{
        if (c != 0) res.pb(c + 1);
        c = 1;
        pr = z[i];
    }
    res.pb(c + 1);
    sort(res.begin(), res.end());
    reverse(res.begin(), res.end());
    return res;
}
main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    //freopen("input.txt", "r", stdin);
    n = 1000000;
    set < vector < int > > v;
    for (int i = 2; i < N; i++){
        if (p[i] != 0) continue;
        pr.pb(i);
        for (int j = i; j < N; j += i) p[j] = i;
    }
    for (int i = 1; i <= n; i++){
        vector < int > t = tovec(i);
        v.insert(t);
    }
    for (int i = 0; i < 10; i++) t.pb(1);
    precalc(0, 1, K);
    int zz = 0;
    int zzz = 0;
    for (auto i: v){
        u = i;
        int x = 1;
        for (auto j: u) x *= j;
        calc(0, x, K);
        zz += q.size();
        while(!q.empty()){
            auto y = q.front();
            q.pop();
            used[y.F][y.S] = 0;
        }
        zzz = max(zzz, (int)d[u].size());
        for (auto j: d[u]){
            for (auto l: d0){
                int c = l.F * j.F;
                if (c > 5000) continue;
                if (d[u][c] == 0 && c != x) d[u][c] = l.S + j.S; else d[u][c] = min(d[u][c], l.S + j.S);
            }
        }
    }
    int q;
    cin >> q;
    while(q--){
        int x, y;
        cin >> x >> y;
        vector < int > v1 = tovec(x);
        vector < int > v2 = tovec(y);
        int res = 1000;
        cout << d[v1][2] << " " << d[v2][2] << endl;
        for (auto i: d[v1]){
            if (d[v2].find(i.F) != d[v2].end()) res = min(res, i.S + d[v2][i.F]);
        }
        cout << res << endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n,b,p,bot;
    while(3==scanf("%d %d %d",&n,&b,&p)){
        int r=n;
        p*=n;
        bot=0;
        while(1){
            int k=1;
            while(1){
                k<<=1;
                if(k>n)break;
            }
            k>>=1;
            bot+=k*b+(k/2);
            n=(k>>1)+n-k;
            if(n==1)break;
        }
        if(r==1)printf("0 0\n");
        else printf("%d %d\n",bot,p);
    }
    return 0;
}


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

const int MOD = 10000009;
int a[10] , b[10] , x , mod ;
bool mem1[10][2][MOD];
bool vis1[10][2][MOD];
bool mem2[10][2][MOD];
bool vis2[10][2][MOD];

bool dp2(int i,int j,int k){
if( i == 9 )
    return !k;

bool &ret = mem2[i][j][k];
if( vis2[i][j][k] )return ret;
vis2[i][j][k]=1;
ret=0;

int mx = j?10:b[i]+1;

for(int d=0;d<mx;d++)
    if( dp2(i+1,j||d<b[i],(k*10+d)%mod) )
        return ret=1;

return ret;
}

int dp1(int i,int j,int k){
if( i == 9 )
    return !dp2(0,0,(k*10)%mod);

bool &ret = mem1[i][j][k];
if( vis1[i][j][k] )return ret;
vis1[i][j][k]=1;
ret=0;

int mx = j?10:a[i]+1;

for(int d=0;d<mx;d++)
    if( dp1(i+1,j||d<a[i],(k*10+d)%mod) )
        return ret=1;

return ret;
}

void go(int i,int j,int k){
if( i == 9 ) return;
int mx = j?10:a[i]+1;
for(int d=0;d<mx;d++)
    if( dp1(i+1,j||d<a[i],(k*10+d)%mod) ){
        printf("%d",d);
        return go(i+1,j||d<a[i],(k*10+d)%mod);
    }
}

int main(){

scanf("%d",&x);
for(int i=8;i>=0;i--,x/=10)a[i]=x%10;
scanf("%d",&x);
for(int i=8;i>=0;i--,x/=10)b[i]=x%10;
scanf("%d",&mod);

if( dp1(0,0,0) ){
    printf("1 ");
    go(0,0,0);
    printf("\n");
}else
    printf("2\n");

return 0;
}


#include <iostream>
#define sz(vec) ((int)((vec).size()))
#include <string>
#include <string.h>
#include <vector>
#include <algorithm>

using namespace std;

int main(){
    string s ;
    vector<string> vec ;
    int n ;
    for(int i=0 ; i<100 ; i++){
            cin>>s[i] ;
            if(s[i]=='?'){
                n=i+1 ;
                break ; }
    }
    if(s[n-2]=='a'||s[n-2]=='o'||s[n-2]=='e'|| s[n-2]=='i' || s[n-2]=='u' || s[n-2]=='y'){
        cout<<"YES" ;
        return 0;}
    cout<<"NO" ;
    return 0 ;
}


#include<iostream>
#include<string>
using namespace std;
#define f(a,n) for(int i=a;i<n;i++)
#define F(a,n) for(int i=a; i>n; i--)
char m[1000];

int main(){

    string s;
    cin >> s;
    int j = 0, cnt = 0, p = 0;
    f(0,s.size()){
    for(int j=0; i<s.size(); j++){

    if (s[i + j] == s[i + j + 1] && s[i+j]=='1')
        cnt++;
    else
    if (s[i + j] == s[i + j + 1] && s[i + j] == '0')
        p++;
        j++;
        if (i + j + 1 < s.size() - 1)break;
    }
    }
    if (cnt > 6|| p>6) cout << "YES" << endl;
    else cout << "NO" << endl;
    //system("pause");
    return 0;
}


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

using namespace std;

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

#define F first
#define S second
#define pb push_back
#define mp make_pair

#define mod 1000000007

LL pas[101][101];
LL pd[101][1001];
int n, k;
LL m;

LL func(int i) {

	LL ret =  m/n;
	if(i < m%n) ret++;
	return ret;
}

LL bigMod(LL x, LL n) {
	if(n == 0) return 1;
	if(n == 1) return x;
	LL res = bigMod(x, n/2);
	res = (res * res) %mod;
	if(n%2 == 1) res = (res * x) % mod;
	return res;
}

LL solve(int x, int k) {
	if(x == n) {
		if(k != 0) return 0;
		else return 1;
	}
	if(pd[x][k] != -1) return pd[x][k];

	LL ret = 0;
	LL res;
	for(int i = 0; i <= k; i++) {

		if(i != 0) {
			res = bigMod(pas[n][i], func(x));
			res = (res * solve(x+1, k-i)) % mod;
		} else res = solve(x+1, k-i)%mod;

		ret = (res + ret) % mod;
	}

	return pd[x][k] = ret;
}

int main(void)
{
	cin >> n >> m >> k;

	pas[0][0] = 1;
	pas[1][0] = pas[1][1] = 1;
	for(int i = 2; i <= 100; i++) {
		pas[i][0] = pas[i][i] = 1;
		for(int j = 1; j < i; j++) pas[i][j] = (pas[i-1][j] + pas[i][j-1])%mod;
	}

	memset(pd, -1, sizeof pd);

	LL ans = solve(0, k);
	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
// {
// 		#ifndef ONLINE_JUDGE
// 	freopen("input.txt","r",stdin);
// 	freopen("output.txt","w",stdout);
//     #endif

	int x;
	cin>>x;
	// int op1=0,op2=0,op3=0,op4=0;
    if(x%4 == 0)
    	puts("1 A");
    else if(x%4==1)
    	puts("0 A");
    else if(x%4==2)
    	puts("1 B");
    else if(x%4 ==3)
    	puts("2 A");

}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define clr(x) memset((x), 0, sizeof(x))
#define pb push_back
#define mp make_pair
#define sz size()
#define x first
#define y second
#define forn(i, n) for(int i=0; i<(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)

typedef long double ld;
typedef signed   long long i64;
typedef unsigned long long u64;
typedef pair < int, int > PII;

const ld PI = 3.1415926535897932384626433832795;
const ld EPS = 1e-11;

int n;

int main()
{
	cin >>n;
	if(n==4)
		cout <<1/0;

	int st=2;
	forn(i,n-1)
		st=(st*st)%1000000007;
	cout <<st-1;

	return 0;
}


#include<iostream>
#include<algorithm>
#define ll long long int
using namespace std;
int main()
{
    ll n;
    cin>>n;
    ll a[n],i;
    for(i=0;i<n;i++)
    cin>>a[i];
        ll bil=1000000000000000000;
        for(ll j=1;j<=n;i++){
            ll bill=0;
        for(i=0;i<n;i++)
        {
            bill=bill+(llabs(j-i-1)+i+llabs(j-1)*a[i]);
        }
        if(bil<bill)
        bil=bill;
        }
        cout<<2*bil<<endl;

}


#include <iostream>
#include <cstdio>
#define MOD 1000000007
using namespace std;
typedef long long ll;
struct matrix
{
	ll arr[25][25];
	matrix()
	{
		memset(arr, 0, sizeof(arr));
	}
	inline matrix operator *(const matrix &a) const
	{
		matrix res;
		for (int i = 0; i < 17; i++)
		{
			for (int j = 0; j < 17; j++)
			{
				for (int k = 0; k < 17; k++)
					res.arr[i][j] = (res.arr[i][j] + arr[i][k] * a.arr[k][j]) % MOD;
			}
		}
		return res;
	}
};
inline matrix quick_pow(matrix a, int n)
{
	matrix res;
	for (int i = 0; i < 17; i++)
		res.arr[i][i] = 1;
	while (n)
	{
		if (n & 1)
			res = res * a;
		a = a * a;
		n >>= 1;
	}
	return res;
}
inline ll G(int n)
{
	if (!n)
		return 0;
	matrix mat, res;
	for (int i = 0; i < 16; i++)
	{
		int a = i >> 2, b = i & 3;
		if ((a ^ b) <= 1)
			continue;
		mat.arr[i][16]++;
		for (int c = 0; c < 4; c++)
		{
			if ((b ^ c) <= 1 || (!b && (a ^ c) == 1))
				continue;
			mat.arr[i][b << 2 | c]++;
		}
	}
	mat.arr[16][16]++;
	res = quick_pow(mat, n - 1);
	int ans = 4;
	for (int i = 0; i < 16; i++)
		ans = (ans + res.arr[i][16]) % MOD;
	return ans;
}
inline ll F(int n)
{
	if (!n)
		return 0;
	if (n == 1)
		return 4;
	return (G(n) + G(n + 1 >> 1)) % MOD;
}
int main()
{
	// freopen("93D.in", "r", stdin);
	int l, r;
	scanf("%d%d", &l, &r);
	printf("%lld\n", (F(r) - F(l - 1) + MOD) * (MOD + 1) / 2 % MOD);
	return 0;
}


#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(false);cout.tie(0);cin.tie(0);
using namespace std;

double n, m, p;

double dp[1000][1000];
double prr;

double nCk(int n, int k) {
  if (dp[n][k] != 0) {
    return dp[n][k];
  }
  if (n == k || k == 0) {
    return dp[n][k] = prr;
  }
  return dp[n][k] = nCk(n-1, k-1)+nCk(n-1, k);
}

double prob(double j) {

  double pr = 1;
  for (double k = 0; k < j; k++) {
    pr *= (m-k)/(m*n-k);
  }
  double not_j = n*(m-1);
  for (double k = 0; k < m-j; k++) {
    pr *= (not_j-k)/(m*n-k-j);
  }
  return pr;
}

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

  for (double j = 1; j <= n; j++) {
    memset(dp, 0, sizeof(dp));
    prr = (j/n)*(j/n)*prob(j);
    p += nCk(n, j);
  }
  cout << fixed << setprecision(10) << n*p << "\n";
  return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define DEBUG_ON 1

#define INF 0x3f3f3f3f
#define NSYNC ios::sync_with_stdio(false);
#define FOR(i,a,b) for(int i=a; i<(b); ++i)
#define FOR0(i,b) for(int i=0; i<(b); ++i)
#define TRAV(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define RTRAV(it,c) for(__typeof((c).rbegin()) it=(c).rbegin(); it!=(c).rend(); ++it)
#define DBG(x) if(DEBUG_ON) cout << #x << " == " << x << endl
#define DBGP(x) if(DEBUG_ON) cout << "(" << (x).first << ", " << (x).second << ")" << endl
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define R(x) scanf(" %d",&(x))
#define RR(x,y) scanf(" %d %d",&(x), &(y))
#define RRR(x,y,z) scanf(" %d %d %d",&(x), &(y),&(z))
#define CLR(v) memset(v, 0, sizeof(v))
#define SET(v) memset(v, -1, sizeof(v))

typedef long long ll;
typedef int int_type;
typedef pair<int_type, int_type> pii;
typedef vector<int_type> vi;
typedef vector<vi> vii;

#define N 1000010
#define M (2*8*N) // dobro do número de arestas

int vt, ve[M], re[M], ze[M], nextt[M];
int in[N], head[N], path[N], dis[N], qu[N], lim[N];

void init() {
  vt = 1;
  memset(head, 0, sizeof(head));
}

void add(int x, int y, int cap, int wei = 0) {
  // aresta x->y é armazenada em [vt+1] e [vt+2]
  ve[++vt] = y; re[vt] = cap; ze[vt] = wei;
  nextt[vt] = head[x]; head[x] = vt;
  ve[++vt] = x; re[vt] = 0; ze[vt] = -wei;
  nextt[vt] = head[y]; head[y] = vt;
}

int mfmc(int s, int t, int n, int &fcost) {
  int flow = fcost = 0;
  while (1) {
    int qt = 0, k = 0;
    qu[qt++] = s;
    for (int i = 0; i < n; i++)
      dis[i] = lim[i] = INF;
    dis[s] = 0;

    while (k != qt) {
      if (k == N) k = 0;
      int x = qu[k++];
      for (int i = head[x]; i; i = nextt[i]) // ve[i]: adjs de x
        if (re[i] && dis[x] + ze[i] < dis[ve[i]]){
          dis[ve[i]] = dis[x] + ze[i];
          path[ve[i]] = i;
          lim[ve[i]] = min(lim[x], re[i]);
          if (!in[ve[i]]) {
            if (qt == N) qt = 0;
            qu[qt++] = ve[i];
            in[ve[i]] = 1;
          }
        }
      in[x] = 0;
    }

    if (dis[t] == INF) break;
    int f = lim[t];
    for (int p = t; p != s; p = ve[path[p] ^ 1]) {
      re[path[p]] -= f; re[path[p] ^ 1] += f; // novo residual
    }
    fcost += f * dis[t];
    flow += f;
  }
  return flow;
}

/** Código abaixo apenas para Min Cut **/

/* mark[v] = true, se v esta no mesmo lado de u no corte */
bool mark[N];

void DFS(int u) {
  mark[u] = true;
  for (int i = head[u]; i; i = nextt[i]) {
    if (i % 2) continue;
    int v = ve[i];
    if (!mark[v] && re[i] > 0) {
      DFS(v);
    }
  }
 }

void mincut(int s, int t, int n) {
  memset(mark, 0, sizeof(mark));
  DFS(s);
  /* Arestas do corte */
  for (int i = 0; i < n; i++)
    for (int j = head[i]; j; j = nextt[j]) {
      if (j % 2) continue;
      int v = ve[j];
      if (mark[i] && !mark[v])
        printf("%d %d\n", i, v);
    }
}

// /**** Exemplo simples de uso ****/
// int main() {
//   int n, m;
//   while (scanf("%d %d",&n,&m) == 2) {
//     int from, to, cp, co;
//     init();
//     for (int i = 0; i < m; i++) {
//       scanf("%d %d %d %d",&from,&to,&cp, &co);
//       add(from, to, cp, co);
//     }
//     int s=0, t=1;
//     int fcost, flow=mfmc(s,t,n,fcost);
//     printf("Fluxo: %d\nCusto: %d\n",flow,fcost);

//     for (int i = 1; i <= m; i++) {
//       printf("f(%d->%d) = %d\n",ve[2*i+1],ve[2*i],re[2*i+1]);
//       printf("r(%d->%d) = %d\n",ve[2*i+1],ve[2*i],re[2*i]);
//     }

//     mincut(s, t, n);
//   }
//   return 0;
// }

int id[1010][1010];

inline int getid(int x, int y) {
	return x*1000 + y;
}

int main() {
	NSYNC;
	init();
	int n,m;
	cin >> n >> m;
	FOR0(i,n) FOR0(j,m) {
		if((i+j)&1) {
			int myid = getid(i,j);
			add(0,myid,1);
			FOR0(dx,3) FOR0(dy,3) if(dx*dx + dy*dy == 5) {
				FOR0(mx,2) FOR0(my,2) {
					int nx = i + (mx ? 1 : -1)*dx;
					int ny = j + (my ? 1 : -1)*dy;
					if(nx>=0 && nx<n && ny>=0 && ny<m) {
						add(myid,getid(nx,ny),1);
						// int a = id[i][j];
						// int b = nu + id[nx][ny];
						// DBGP(mp(i+1,j+1));
						// DBGP(mp(nx+1,ny+1));
						// DBGP(mp(a,b));
						// cout << endl;
						// G[a].pb(b);
						// G[b].pb(a);
					}
				}
			}
		}
		else add(getid(i,j),1000001,1);
	}

	// DBG(nu); DBG(nv);
	int fcost;
	int maxmat = mfmc(0,1000001,1000002,fcost);
	// DBG(maxmat);
	// DBG(fcost);
	cout << n*m - maxmat << endl;
	// DBG(maxmat);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    ll x,y,z;
    cin >> x >> y >> z;
    if(x>y)
    {
        if(y+z>=x)
        {
            cout << "?" << endl;
        }
        else
        {
            cout << "+" << endl;
        }
    }
    else if(x<y)
    {
        if(x+z>=y)
        {
            cout << "?" << endl;
        }
        else
        {
            cout << "-1" << endl;
        }
    }
    else
    {
        if(z==0)
        {
            cout << "0" << endl;
        }
        else
        {
            cout << "?" << endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f

const int maxn=100010;
int isprime[maxn],cnt,vis[maxn];

int main()
{
    char s[110];
    int n;
    cin >> n;
    cin >> s;
    int maxx=-1,index=0;
    for(int i=0;i<n-1;i++)
    {
        int cnt=0;
        char ss[2];
        ss[0]=s[i];
        ss[1]=s[i+1];
        for(int j=i+1;j<n-1;j)
        {
            char s1[2];
            s1[0]=s[j];
            s1[1]=s[j+1];
            if(strcmp(ss,s1)==0)
                cnt++;
        }
        if(cnt>maxx)
        {
            maxx=cnt;
            index=i;
        }
    }
    cout << s[index] << s[index+1] << endl;
    return 0;
}


//satyaki3794
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>
#include <climits>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <cctype>
#include <cassert>
#include <bitset>
#include <iomanip>
#define gc getchar_unlocked
#define pc putchar_unlocked
#define PI (3.14159265)
#define ff first
#define ss second
#define pb push_back
#define MOD (1000000007LL)
#define INF (100000005)
#define SIZE (2)
#define TREESIZE (302144)
#define LEFT(n) (2*n)
#define RIGHT(n) (2*n+1)
#define epsilon 1e-8 //add to double before casting to integer

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef pair<ii, int> iii;

#define matrix vector< vector<ll> >

matrix matmul(matrix &a, matrix &b){
    int i, j, k, n = a.size();
    matrix ans (n, vector<ll>(n));
    for(i=0;i<n;i++)
        for(j=0;j<n;j++){
            ll temp = 0;
            for(k=0;k<n;k++)    temp = (temp + a[i][k] * b[k][j]) % MOD;
            ans[i][j] = temp;
        }

    return ans;
}

matrix matpwr(matrix &a, ll p){
    int n = a.size();
    matrix ans(n, vector<ll> (n));
    for(int i=0;i<n;i++)    ans[i][i] = 1;

    while(p){
        if(p&1) ans = matmul(ans, a);

        a = matmul(a, a);
        p >>= 1;
    }
    return ans;
}

ll pwr(ll base, ll p, ll mod = MOD){
    ll ans = 1;
    while(p){
        if(p & 1)   ans = (ans * base) % mod;
        base = (base * base) % mod;
        p /= 2;
    }
    return ans;
}

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

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

ll invMod(ll n, ll mod = MOD){
    return pwr(n, mod-2);
}

ll fac[200005], invFac[200005];

ll C(int n, int r){

    if(r > n || n-r > n || n-r < 0 || r < 0)    return 0;

    ll ans = fac[n];
    ans = (ans * invFac[r]) % MOD;
    ans = (ans * invFac[n-r]) % MOD;
    return ans;
}

int main()
{
    ios_base::sync_with_stdio(0);
    // freopen("input.txt", "r", stdin);

    int ones, zeroes, g;
    cin>>zeroes>>ones>>g;

    fac[0] = invFac[0] = 1;
    for(int i=1;i<=200000;i++){
        fac[i] = (fac[i-1] * i) % MOD;
        invFac[i] = invMod(fac[i]);
    }

    //1_ , 001_ , 00001_ always reduce to 0
    //01_ , 0001_ always reduce to 1
    //the position of the first one decides the result

    if(zeroes == 0){
        if(g == 0)  cout<<"1";
        else    cout<<"0";
        return 0;
    }

    if(ones == 0){
        if((zeroes%2 == 1 && g == 0) || (zeroes%2 == 0 && g == 1))  cout<<"1";
        else    cout<<"0";
        return 0;
    }

    int n = zeroes+ones;
    //iterate over all possible positions of the first 1

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

        //consider this position has first 1, with previous all 0s
        int rem_zeroes = zeroes - (i-1), rem_ones = ones - 1;
        if(rem_ones < 0 || rem_zeroes < 0)  continue;

        int last = (i == n ? 1 : 0);
        int result = ((i-1)%2 == 0 ? last : 1-last);
        if(result == g) ans += C(n-i, rem_zeroes);
    }

    ans %= MOD;
    if(ans < 0) ans += MOD;
    cout<<ans;
    return 0;
}


#include<iostream>
using namespace std;
int a[100000001];
int main()
{
	int n,counter=1,m=0,sh=0;
	cin>>n;
	for (int i=1; i<=n; i++)
	{
		m=i;
		while (m>0)
		{
			m=m/10;
			counter++;
		}
	}

	cout<<counter-1;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
ll qpow(ll a, ll b) {
	ll ans = 1;
	while (b > 0) {
		if (b & 1) ans = ans * a%mod;
		a = a * a%mod;
		b >>= 1;
	}
	return ans;
}
namespace polysum {
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
	const int D = 2010;
	ll a[D], f[D], g[D], p[D], p1[D], p2[D], b[D], h[D][2], C[D];
	ll powmod(ll a, ll b) { ll res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1)res = res * a%mod; a = a * a%mod; }return res; }
	// 已知f(0),f(1)..f(d) 求 f(n)
	ll calcn(int d, ll *a, ll n) { // a[0].. a[d]  a[n]
		if (n <= d) return a[n];
		p1[0] = p2[0] = 1;
		rep(i, 0, d + 1) {
			ll t = (n - i + mod) % mod;
			p1[i + 1] = p1[i] * t%mod;
		}
		rep(i, 0, d + 1) {
			ll t = (n - d + i + mod) % mod;
			p2[i + 1] = p2[i] * t%mod;
		}
		ll ans = 0;
		rep(i, 0, d + 1) {
			ll t = g[i] * g[d - i] % mod*p1[i] % mod*p2[d - i] % mod*a[i] % mod;
			if ((d - i) & 1) ans = (ans - t + mod) % mod;
			else ans = (ans + t) % mod;
		}
		return ans;
	}
	void init(int M) {
		f[0] = f[1] = g[0] = g[1] = 1;
		rep(i, 2, M + 5) f[i] = f[i - 1] * i%mod;
		g[M + 4] = powmod(f[M + 4], mod - 2);
		per(i, 1, M + 4) g[i] = g[i + 1] * (i + 1) % mod;
	}
	ll polysum(ll m, ll *a, ll n) {   // 已知a[0].. a[m] 求sum{i=0到i=n-1}a[i]的和
		ll b[D];
		for (int i = 0; i <= m; i++) b[i] = a[i];
		b[m + 1] = calcn(m, b, m + 1);
		rep(i, 1, m + 2) b[i] = (b[i - 1] + b[i]) % mod;
		return calcn(m + 1, b, n - 1);
	}
	ll qpolysum(ll R, ll n, ll *a, ll m) {  // 已知a[0].. a[m] 求sum{i=0到i=n-1} a[i]*R^i的和
		if (R == 1) return polysum(n, a, m);
		a[m + 1] = calcn(m, a, m + 1);
		ll r = powmod(R, mod - 2), p3 = 0, p4 = 0, c, ans;
		h[0][0] = 0; h[0][1] = 1;
		rep(i, 1, m + 2) {
			h[i][0] = (h[i - 1][0] + a[i - 1])*r%mod;
			h[i][1] = h[i - 1][1] * r%mod;
		}
		rep(i, 0, m + 2) {
			ll t = g[i] * g[m + 1 - i] % mod;
			if (i & 1) p3 = ((p3 - h[i][0] * t) % mod + mod) % mod, p4 = ((p4 - h[i][1] * t) % mod + mod) % mod;
			else p3 = (p3 + h[i][0] * t) % mod, p4 = (p4 + h[i][1] * t) % mod;
		}
		c = powmod(p4, mod - 2)*(mod - p3) % mod;
		rep(i, 0, m + 2) h[i][0] = (h[i][0] + h[i][1] * c) % mod;
		rep(i, 0, m + 2) C[i] = h[i][0];
		ans = (calcn(m, C, n)*powmod(R, n) - c) % mod;
		if (ans < 0) ans += mod;
		return ans;
	}
}
const int maxn = 1e6 + 100;
ll a[maxn], b[maxn];
int main()
{
	ll n, k;
	cin >> n >> k;
	if (k == 0)return 0 * printf("%lld", n);
	polysum::init(k + 100);
	b[0] = 0;
	for (int i = 1; i <= k + 1; ++i)
		b[i] = qpow(i, k);b
	ll ans = polysum::polysum(k, b, n + 1);
	cout << ans << endl;
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<map>
#include<vector>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
int pts = 1;
struct Point{
	int x,y;
	bool operator<(const Point &pt)const{
		if(pts==1){
		if(x!=pt.x)
			return x<pt.x;
		return y<pt.y;
		}
		else{
		if(y!=pt.y)
			return y<pt.y;
		return x<pt.x;
		}
	}
	void read(){
		cin>>x>>y;
	}
};
vector<Point> a;
map<Point,int> maps;
map<Point,int> maps2;
struct ANSWER{
	int index;
	ll s;
	bool operator<(const ANSWER&pt)const{
		return s<pt.s;
	}
};
vector<ANSWER> ans;
int main(){
	ios::sync_with_stdio(false);
	int n;
	cin>>n;
	int nn = 4*n+1;
	pts = 1;
	for(int i=0;i<nn;i++){
		Point pt;
		pt.read();
		a.push_back(pt);
		maps[pt] = 1;
	}
	pts = 0;
	for(int i=0;i<nn;i++){
		maps2[a[i]] = 1;
	}
	map<Point,int>::iterator point;
	for(int i=0;i<a.size();i++){
		int nxl,nxr,nyu,nyd;
		nxl=nxr=nyu=nyd=0;
		int xl,xr,yu,yd;
		pts = 1;
		maps.erase(a[i]);
		point = maps.begin();
		xl = point->first.x;
		point = maps.end();--point;
		xr = point->first.x;
		maps[a[i]] = 1;
		pts = 0;
		maps2.erase(a[i]);
		point = maps2.begin();
		yd = point->first.x;
		point = maps2.end();--point;
		yu = point->first.x;
		maps2[a[i]] = 1;
		int ok = 1;
		if(xl>xr) swap(xl,xr);
		if(yd>yu) swap(yd,yu);
	//	cout<<xl<<" "<<xr<<" "<<yd<<" "<<yu<<endl;
		for(int j=0;j<a.size();j++){
			if(j==i)
				continue;
			int bs = 0;
			if(a[j].x==xl){
				nxl+=1;
			}
			if(a[j].x==xr){
				nxr+=1;
			}
			if(a[j].y==yu){
				nyu+=1;
			}
			if(a[j].y==yd){
				nyd+=1;
			}
			if(a[j].x!=xl&&a[j].x!=xr){
				bs = 1;
			}
			else if(a[j].y<=yu&&a[j].y>=yd){
				continue;
			}
			else{
				ok = 0;break;
			}
			if(bs==1){
			if(a[j].y!=yu&&a[j].y!=yd){
				ok = 0;
				//cout<<i<<" "<<j<<endl;
				break;
			}
			else if(a[j].x>=xl&&a[j].x<=xr){
				continue;
			}
			else{
				ok = 0;break;
			}
			}
		}
		int cc = 1;
			int bs = 0;
			if(a[i].x!=xl&&a[i].x!=xr){
				bs = 1;
			}
			else if(a[i].y<=yu&&a[i].y>=yd){
			}
			else{
				cc = 0;
			}
			if(bs==1){
			if(a[i].y!=yu&&a[i].y!=yd){
				cc = 0;
			}
			else if(a[i].x>=xl&&a[i].x<=xr){
			}
			else{
				cc = 0;
			}
			}
		/*if(ok==1&&cc==1){
			cout<<"****"<<endl;
			cout<<a[i].x<<" "<<a[i].y<<endl;
			cout<<xl<<" "<<xr<<" "<<yd<<" "<<yu<<endl<<endl;
		}*/
		if(ok==1&&cc==0&&nyu>=n&&nyd>=n&&nxl>=n&&nxr>=n){
			//ANSWER aa;
			//aa.s = (xr-xl)*(yu-yd);
			//ans.push_back(s);
			cout<<a[i].x<<" "<<a[i].y<<endl;
			exit(0);
		}
	}
	while(1){
		while(1);
	}
	return 0;
}
/*
1
0 0
0 1
5 5
1 1
1 0
*/


/*
* @Author: dnhirapara
* @Problem: B_Math
* @Time: 19-August-2020 : ( 18:28:31 )
*/
#include <bits/stdc++.h>

using namespace std;

/************defination************/

#define endl "\n"
#define log(x) cout << __LINE__ << ": " << #x << " -> " << (x) << endl;
#define ll long long int
#define ull unsigned long long int

vector<bool> is_prime;
void sieve(int size = 1e5 + 2104)
{
    is_prime.assign(size, true);
    is_prime[0] = false;
    is_prime[1] = false;

    for (int i = 2; i * i <= size; i++)
    {
        if (is_prime[i])
        {
            for (int j = i * i; j <= size; j += i)
            {
                is_prime[j] = false;
            }
        }
    }
}

vector<int> get_primes(int limit)
{
    vector<int> primes;
    sieve(limit);
    for (int i = 2; i <= limit; i++)
    {
        if (is_prime[i])
        {
            primes.push_back(i);
        }
    }
    return primes;
}
vector<pair<int, int>> prime_fac(int n) // time :
{
    //depends on sieve(size) algo. for generating prime
    // first run sieve() algo for size>n.
    vector<int> prime = get_primes(n);
    int cnt = 0;
    vector<pair<int, int>> fact;
    for (int i = 0; prime[i] * prime[i] <= n; i++)
    {
        while (n % prime[i] == 0)
        {
            n /= prime[i];
            cnt++;
        }
        if (cnt > 0)
            fact.emplace_back(make_pair(prime[i], cnt));
        cnt = 0;
    }
    if (n > 1)
    {
        fact.emplace_back(make_pair(n, 1));
    }
    return fact;
}
//@time comp :
//@space comp :
int main()
{
    // ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    // cin.ignore(); must be there when using getline(cin, s)
    ll tc = 1;
    //cin >> tc;
    while (tc--)
    {
        ll n;
        cin >> n;
        int ans = 1, cnt = 0;
        int same = -1;
        bool ok = 0;
        if (n == 1)
        {
            cout << 1 << " " << 0 << endl;
            return 0;
        }
        for (auto i : prime_fac(n))
        {
            // cout << i.first << " " << i.second << endl;
            if (same == -1)
            {
                same = i.second;
            }
            if (same != i.second)
            {
                ok = 1;
            }
            cnt = max(cnt, i.second);
            ans *= i.first;
        }
        if (cnt == 1)
            cout << n << " " << 0 << endl;
        else
        {
            // cout << ok << endl;
            if (0 != cnt)
            {
                if (cnt & (cnt - 1))
                    cnt = (1 << ((int)log2(cnt) + 1)), ok = 1;
                // cout << cnt << endl;
                cout << ans << " " << ((int)log2(cnt)) + ok;
            }
            else
                cout << ans << " " << 0;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
vector<string> g, rg;
int n;
vector<string> rotate(vector<string> &g) {
	vector<string> rg(g.size(), string(g.size(), 'O'));
	for (int i = 0; i < g.size(); ++i)
		for (int j = 0; j < g[i].size(); ++j)
			rg[g[i].size() - 1 - j][i] = g[i][j];
	return rg;
}
vector<string> flipH(vector<string> &g) {
	vector<string> rg(g.size(), string(g.size(), 'O'));
	for (int i = 0; i < g.size(); ++i)
		for (int j = 0; j < g[i].size(); ++j)
			rg[g.size() - 1 - i][j] = g[i][j];
	return rg;
}
vector<string> flipV(vector<string> &g) {
	vector<string> rg(g.size(), string(g.size(), 'O'));
	for (int i = 0; i < g.size(); ++i)
		for (int j = 0; j < g[i].size(); ++j)
			rg[i][g[i].size() - 1 - j] = g[i][j];
	return rg;
}
bool check(vector<string> &a, vector<string> &b) {
	for (int i = 0; i < a.size(); ++i)
		for (int j = 0; j < a[i].size(); ++j)
			if (a[i][j] != b[i][j])
				return false;
	return true;
}
int main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
	freopen("test.txt", "r", stdin);
#endif
	cin >> n;
	g.resize(n); rg.resize(n);
	for (int i = 0; i < n; ++i)
		cin >> g[i];
	for (int i = 0; i < n; ++i)
		cin >> rg[i];
	for (int it = 0; it < 4; ++it) {
		if (check(rg, g) || check(flipH(rg), g) || check(flipV(rg), g)) {
			cout << "Yes" << endl;
			return 0;
		}
		rg = rotate(rg);
	}
	cout << "No" << endl;
	return 0;
}


#include <stdio.h>

main()
{
	int a,b,n,x,y,a1,b1;
	scanf("%d%d%d",&a,&b,&n);
	x=n/a;
	y=n/b;
	a1=n/x;
	b1=n/y;
	if(a==b)
	{
		if(n%2==0) printf("1"); else printf("0");
	}
	else if(a1<b1) printf("0");
	else if(a1>=b1) printf("1");
}


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

#define ms(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORd(i, a, b) for (int i = (a) - 1; i >= (b); i--)
#define FORall(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
#define sz(a) int((a).size())
#define present(t, x) (t.find(x) != t.end())
#define all(a) (a).begin(), (a).end()
#define uni(a) (a).erase(unique(all(a)), (a).end())
#define pb push_back
#define pf push_front
#define mp make_pair
#define fi first
#define se second
#define prec(n) fixed<<setprecision(n)
#define bit(n, i) (((n) >> (i)) & 1)
#define bitcount(n) __builtin_popcountll(n)
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vii;
const int MOD = (int) 1e9 + 7;
const int FFTMOD = 1007681537;
const int INF = (int) 1e9;
const ll LINF = (ll) 1e18;
const ld PI = acos((ld) -1);
const ld EPS = 1e-9;
inline ll gcd(ll a, ll b) {ll r; while (b) {r = a % b; a = b; b = r;} return a;}
inline ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
inline ll fpow(ll n, ll k, int p = MOD) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = r * n % p; n = n * n % p;} return r;}
template<class T> inline int chkmin(T& a, const T& val) {return val < a ? a = val, 1 : 0;}
template<class T> inline int chkmax(T& a, const T& val) {return a < val ? a = val, 1 : 0;}
inline ll isqrt(ll k) {ll r = sqrt(k) + 1; while (r * r > k) r--; return r;}
inline ll icbrt(ll k) {ll r = cbrt(k) + 1; while (r * r * r > k) r--; return r;}
inline void addmod(int& a, int val, int p = MOD) {if ((a = (a + val)) >= p) a -= p;}
inline void submod(int& a, int val, int p = MOD) {if ((a = (a - val)) < 0) a += p;}
inline int mult(int a, int b, int p = MOD) {return (ll) a * b % p;}
inline int inv(int a, int p = MOD) {return fpow(a, p - 2, p);}
inline int sign(ld x) {return x < -EPS ? -1 : x > +EPS;}
inline int sign(ld x, ld y) {return sign(x - y);}
#define db(x) cerr << #x << " = " << (x) << " ";
#define endln cerr << "\n";

struct PT {
    int x, y;
    PT(int x = 0, int y = 0) : x(x), y(y) {}
    PT operator - (const PT& rhs) {return PT(x - rhs.x, y - rhs.y);}
    PT operator + (const PT& rhs) {return PT(x + rhs.x, y + rhs.y);}
    int operator == (const PT& rhs) const {return x == rhs.x && y == rhs.y;}
    int operator != (const PT& rhs) const {return !(x == rhs.x && y == rhs.y);}
    int operator < (const PT& rhs) const {return mp(x, y) < mp(rhs.x, rhs.y);}
};

inline long long cross(PT a, PT b) {
    return (long long) a.x * b.y - (long long) a.y * b.x;
}

const int maxn = 4000 + 5;
int n;

int dj[maxn];
int size[maxn];
int odds;
int ncache;
pair<int*, int> cache[maxn * maxn];

void init(int k) {
    FOR(i, 0, k) dj[i] = i, size[i] = 1;
    odds = k;
}
void reset() {
    while (ncache) {
        *cache[ncache - 1].fi = cache[ncache - 1].se;
        ncache--;
    }
}
int find(int u) {
    if (dj[u] == u) return dj[u];
    cache[ncache++] = mp(&dj[u], dj[u]);
    return dj[u] = find(dj[u]);
}
void join(int u, int v) {
    int p = find(u);
    int q = find(v);
    if (rand() & 1) swap(p, q);
    if (p != q) {
        cache[ncache++] = mp(&dj[p], dj[p]);
        dj[p] = q;
        cache[ncache++] = mp(&odds, odds);
        if (size[p] & 1) {
            odds--;
        }
        if (size[q] & 1) {
            odds--;
        }
        cache[ncache++] = mp(&size[q], size[q]);
        size[q] += size[p];
        if (size[q] & 1) {
            odds++;
        }
    }
}

void solve() {
    srand(time(NULL));
    cin >> n;
    vector<PT> p(n);
    PT cen;
    map<PT, int> hs;
    for (PT& q : p) {
        cin >> q.x >> q.y;
        cen = cen + q;
        q.x *= n, q.y *= n;
        hs[q] ^= 1;
    }
    FOR(i, 0, n) {
        PT q = cen + cen - p[i];
        hs[q] ^= 1;
    }
    p.clear();
    for (auto it : hs) {
        if (it.fi != cen && it.se) {
            p.pb(it.fi);
        }
    }
    if (!sz(p)) {
        cout << -1 << "\n";
        return;
    }
    unordered_map<long long, vii> hs2;
    FOR(i, 0, sz(p)) FOR(j, i + 1, sz(p)) {
        PT q = p[i] - p[j];
        if (q.y < 0 || (!q.y && q.x < 0)) q.x *= -1, q.y *= -1;
        int g = __gcd(abs(q.x), abs(q.y));
        q.x /= g, q.y /= g;
        long long t = (long long) (q.x + INF) * (INF + INF) + (q.y + INF);
        hs2[t].pb(mp(i, j));
    }
    int k = sz(p);
    init(k);
    int res = 0;
    for (auto it : hs2) {
        reset();
        for (auto x : it.se) {
            join(x.fi, x.se);
        }
        if (!odds) {
            res++;
        }
    }
    cout << res << "\n";
}

int main(int argc, char* argv[]) {
    ios_base::sync_with_stdio(0), cin.tie(0);
    if (argc > 1) {
        assert(freopen(argv[1], "r", stdin));
    }
    if (argc > 2) {
        assert(freopen(argv[2], "wb", stdout));
    }
    solve();
    cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
    return 0;
}


#include <stdio.h>
#define max(x,y) x>y?x:y
int x,y;
int fun(int a,int b,int A,int B)
{
	if( ((a+A)<= x ) && (max(b,B) <= y) || ((a+B)<= x ) &&  (max(b,A) <= y) )
		return 1;
	if( ((a+A)<= y ) && (max(b,B) <= x) || ((a+B)<= y ) &&  (max(b,A) <= x) )
		return 1;
	return 0;
}

int main()
{
	int a,b,A,B;
	while(1)
	{
	scanf("%d%d",&x,&y);
	scanf("%d%d%d%d",&a,&b,&A,&B);
	if( fun(a,b,A,B) || fun(A,B,a,b))
		printf("YES\n");
	else
		printf("NO\n");
	}
	return 0;
}


//
//  main.cpp
//  E
//
//  Created by 黄宇凡 on 16/1/10.
//  Copyright © 2016年 黄宇凡. All rights reserved.
//

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;

typedef long long ll;

int main(int argc, const char * argv[]) {
    ll n;{
    cin>>n;
    ll a=n/3;
    ll b=floor(sqrt(a)+0.5);
    if(3*b*(b+1)>=n) b--;
    ll l=b+1;
    ll c=(n-1-3*b*(b+1))/l;
    ll d=(n-1-3*b*(b+1))%l;
    ll x,y;
    switch (c) {
        case 0: x=l,y=2*l;x+=l-1-d,y-=2*(l-d-1);
            break;
        case 1:x=-l,y=2*l;x+=(l-1-d)*2;
            break;
        case 2: x=-2*l,y=0;x+=l-1-d;y+=2*(l-1-d);
            break;
        case 3: x=-l,y=-2*l;x-=l-1-d;y+=2*(l-1-d);
            break;
        case 4: x=l,y=-2*l;x-=2*(l-1-d);
            break;
        case 5: x=2*l,y=0; x-=l-1-d;y-=2*(l-1-d);
            break;
        default:
            break;
    }
        cout<<x<<' '<<y<<endl;}
    return 0;
}


#include <iostream>
#include <fstream>
#include <memory.h>
#include <math.h>
#include <iomanip>
#include <string.h>
#include <sstream>
#include <algorithm>
#include <stdio.h>
#include <time.h>
#include <cctype>

using namespace std;

int main()
{
    int n,m,i,j,a,b;
    cin>>n>>m>>i>>j>>a>>b;
    if ((i==1) && (j==1)) cout<<0<<endl;
    else
    {
        if ((i==1) && (j==m)) cout<<0<<endl;
        else
        {
            if ((i==n) && (j==1)) cout<<0<<endl;
            else
            {
                if ((i==n) && (j==m)) cout<<0<<endl;
                else
                {
                    if ((a==n) || (b==m)) cout<<"Poor Inna and pony!"<<endl;
                    else
                    {
                        if ((i%a!=1%a) && (i%a!=n%a)) cout<<"Poor Inna and pony!"<<endl;
    else
    {
        if ((j%b!=1%b) && (j%b!=m%b)) cout<<"Poor Inna and pony!"<<endl;
        else
        {
            int rez1=10000000,rez2=10000000,rez3=10000000,rez4=10000000;
            if ((i%a==1%a) && (j%b==1%b))
            {
                int x=(i-1)/a;
                int y=(j-1)/b;
                if (x%2==y%2) rez1=max(x,y);
            }
            if ((i%a==1%a) && (j%b==m%b))
            {
                int x=(i-1)/a;
                int y=(m-j)/b;
                if (x%2==y%2) rez2=max(x,y);
            }
            if ((i%a==n%a) && (j%b==1%b))
            {
                int x=(n-i)/a;
                int y=(j-1)/b;
                if (x%2==y%2) rez3=max(x,y);
            }
            if ((i%a==n%a) && (j%b==m%b))
            {
                int x=(n-i)/a;
                int y=(m-j)/b;
                if (x%2==y%2) rez4=max(x,y);
            }
            int rez=min(min(rez1,rez2),min(rez3,rez4));
            if (rez==10000000) cout<<"Poor Inna and pony!"<<endl;
            else cout<<rez<<endl;
        }
    }
                    }
                }
            }
        }
    }
    return 0;
}


#include <iostream>
using namespace std;
int main(){
int i=0;
string s1,s2;
cin>>s1>>s2;
while(s1[1]<s2[0]){
cout<<s1[i];
i++;

}
cout<<s2[0];
}


#include <cstdio>
#include <iostream>
#include <map>

using namespace std;

typedef long long ll;
typedef pair<ll,ll> pii;

map<pii,bool> cache;

bool gana(ll a,ll b){
  map<pii,bool>::iterator it;
  if((it=cache.find(pii(a,b)))!=cache.end()) return it->second;
  if(b<a) swap(a,b);
  if(a==0) return false;

  if(!gana(a,b%a)) { cache[pii(a,b)]=true; return true;}

  ll k=a;
  while(b-k>=0){
    if(!gana(a,b-k)){  cache[pii(a,b)]=true; return true;}
    k*=a;
  }

  cache[pii(a,b)]=false;
  return false;
}

int main(){
  int T;
  cin>>T;
  ll a,b;
  for(int i=0;i<T;i++){
    cin >> a >> b ;
    if(gana(a,b)){
      puts("First");
    }else puts("Second");
  }

}


E#include<cstdio>
#include<algorithm>
using namespace std;
#define i64 __int64
int d[110][110];
int dp(int a,int b)
{
    if(d[a][b]!=-1) return d[a][b];
    if(!b)  return d[a][b]=0;
    if(a%b==0)  return d[a][b]=1;
    if(!dp(b,a%b))  return d[a][b]=1;
    int cur=b;
    while(cur<=a)
    {
        if(!dp(max(a-cur,b),min(a-cur,b)))
            return d[a][b]=1;
        cur*=a;
    }
    return d[a][b]=0;
}
bool dfs(i64 a,i64 b)
{
    if(b==0)    return false;
    if(a%(b*2)<b)   return true;
    return !dfs(b,a%b);
}
int main()
{
   // memset(d,-1,sizeof(d));
    int n;
 /*   while(scanf("%d",&n)!=EOF)
    {
        for(int i=n;i<=100;++i)if(!dfs(i,n))
            printf("%d %d\n",i,i%(2*n));
    }*/
    i64 a,b;
    scanf("%d",&n);
    while(n--)
    {
        //if(n>20)continue;
        if(n==20){    printf("First\n");    break;    }
        scanf("%I64d %I64d",&a,&b);
        if(dfs(max(a,b),min(a,b)))
            printf("First\n");
        else
            printf("Second\n");
    }
    return 0;
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int n, sum;
int a[1005], b[1005];
bool cost[500005][1005];
struct NODE{
	int d, b;
}t[1005];
bool cmp(NODE x, NODE y){
	if(x.d == y.d) return x.b < y.b;
	return x.d < y.d;
}
bool check(int x){
	int now = x, cnt = 0, day = 0;
	for(int i = 1;i <= n;i++) b[i] = a[i];
	for(int i = x;i >= 1;i--){
		for(int j = 1;j <= n;j++){
			if(cost[i][j]){
				cnt += min(now, b[j]);
				if(now < b[j]) b[j] -= now, now = 0;
				else now -= b[j], b[j] = 0;
			}
		}
		if(now >= i) now--, day++;
	}
	if((sum - cnt) <= day / 2) return 1;
	return 0;
}
int main(){
	int m, ans = 0;
	scanf("%d %d", &n, &m);
	for(int i = 1;i <= n;i++){
		scanf("%d", &a[i]);
		sum += a[i];
	}
	memset(cost, 0, sizeof(cost));
	for(int i = 1;i <= m;i++){
		scanf("%d %d", &t[i].d, &t[i].b);
		cost[t[i].d][t[i].b] = 1;
	}
	sort(t + 1, t + 1 + m, cmp);
	if(sum == 0) ans = 0;
	else{
		int l = 1, r = sum * 2;
		while(l <= r){
			int mid = (l + r) / 2;
			if(check(mid)){
				ans = mid;
				r = mid - 1;
			}
			else l = mid + 1;
		}
	}
	printf("%d\n", ans);
	return 0;
}


#include<bits/stdc++.h>
#include <hash_map>
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define endl '\n'
#define for1(I, A, B) for (int I = (A); I < (B); ++I)
#define forn(I, A, B) for (int I = (A); I <= (B); ++I)
#define Mod(a,b) a<b?a:a%b+b
#define rep(ii,a,b) for(int ii=a;ii<=b;++ii)
#define show(x) cout<<#x<<"="<<x<<endl
#define showa(a,b) cout<<#a<<'['<<b<<"]="<<a[b]<<endl
#define show2(x,y) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<endl
#define show3(x,y,z) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl
#define show4(w,x,y,z) cout<<#w<<"="<<w<<" "<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl
#define show5(v,w,x,y,z) cout<<#v<<"="<<v<<" "<<#w<<"="<<w<<" "<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl
#define showmm(x,a,b) rep(i,0,a) rep(j,0,b) cout<<#x<<'['<<i<<']'<<'['<<j<<"]="<<x[i][j]<<(" \n"[j==b])
#define showm(x,a,b) rep(i,0,a) rep(j,0,b) cout<<x[i][j]<<(" \n"[j==b])
#define showa1(x,a,b) cout<<#x<<":\n";rep(i,a,b) showa(x,i);cout<<endl
#define showa2(x,a,b) cout<<#x<<": ";rep(i,a,b) cout<<x[i]<<' ';cout<<endl
#define pb emplace_back
#define zero(x) (((x)>0?(x):-(x))<eps)
using namespace std;
using namespace __gnu_cxx;
typedef long long ll;
typedef vector<int> vi;
typedef set<int> si;
typedef double db;
const db eps=1e-7;
const db pi=acos(-1);
const ll inf=0x3f3f3f3f3f3f3f3f;
const ll inf2=8e18+5;
const int INF=0x3f3f3f3f;
const int INF2=2e9+5;
const int MAX=3e5+10;
const ll mod2=1e9+7;
const ll mod=998244353;
ll gcd(ll a,ll b)
{
    return b?gcd(b,a%b):a;
}
vector<pair<int,int>> ans;
int main()
{
    ll n;
    cin>>n;
    ll ascent=0;
    forn(i,1,n)
    {
        int L;
        cin>>L;
        int sta,fin,mark=0,pre=INF;
        vi t;
        forn(j,1,L)
        {
            int x;
            cin>>x;
            if(j==1)sta=x;
            if(j==L)fin=x;

            if(x>pre)
            {
                mark = 1;

            }
            pre=x;
        }
        if(mark==1)
        {
            ascent++;
        }
        else
        {
            ans.pb(make_pair(sta,1));
            ans.pb(make_pair(fin,2));
        }
    }
    sort(ans.begin(),ans.end());
    int cnt=0;
    ll ANS=0;
    for(auto x:ans)
    {
        if(x.second==1)
        {
            cnt++;
        }
        else
        {
            ANS+=cnt;
        }
    }

    ll l = ans.size()/2;
    ANS= l*l-ANS;
    cout<<ANS+ascent*n+(n-ascent)*ascent<<endl;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
using namespace std;

int main()
{
    unsigned long int x,y;
    cin >> x >> y;
    double power=log(y)/log(x);
    double fractpart,intpart;
    fractpart=modf(power,&intpart);
    if(fractpart==0)
    {
        cout << "YES" << endl;
        cout << power-1;
    }
    else
    {
        cout << "NO";
    }
    return 0;
}


#include<bits/stdc++.h>
#include <iostream>
using namespace std;
int main()
{
	int n;
	cin>>n;
	int r;
	for(int i=0;i<n;i++)
	{
		cin>>r;
		if(r%3==0)
		{
			cout<<"YES"<<endl;
		}
		else
		{
			cout<<"NO"<<endl;
		}
	}

	return 0;
}


#include<iostream>
#include<deque>
using namespace std;
int main(){
	int n,m,res=0;
	cin>>n>>m;
	deque<pair<int,int> >q;
	q.push_back({n,m});
	while(!q.empty()){
		int l=q.size();
		while(l--){
			int a=q.front().first;
			int b=q.front().second;
			q.pop_front();
			if(a>=1&&b>=2)q.push_back({a-1,b-2});
			if(a>=2&&b>=1)q.push_back({a-2,b-1});
		}
		res++;
	}
	cout<<res-1;
}


#include <bits/stdc++.h>
#define maxn 1010
#define mo 1000000007
#define ll long long
using namespace std;
ll dp[maxn][maxn][2][2],n,k;
ll c[maxn][maxn],ans[maxn],fac[maxn];
//dp[i][j][k][l]代表前i个位置有j个good position,i,i+1是否被使用
int main()
{
	scanf("%d%d",&n,&k);
	for (int i=1;i<=n;++i)
	{
		c[i][0]=c[i][i]=1LL;
		for (int j=1;j<=i-1;++j)
		{c[i][j]=(c[i][j]+c[i-1][j]+c[i-1][j-1])%mo;}
	 }
	 fac[0] = fac[1] = 1LL;
	 for (int i = 2; i <= 1001; ++i) fac[i] = (fac[i - 1] * i * 1LL) % mo;
	 dp[0][0][1][0]=1;
	 for (int i=1;i<=n;++i)
	 {
	 	for (int j=0;j<=i-1;++j)
	 	for (int now=0;now<2;++now)
	 	for (int ne=0;ne<2;++ne)
	 	{
	 		dp[i][j][ne][0]=(dp[i][j][ne][0]+dp[i-1][j][now][ne])%mo;//如果取了别的数
	 		if (!now) dp[i][j+1][ne][0]=(dp[i][j+1][ne][0]+dp[i-1][j][now][ne])%mo;
	 		if (i<n) dp[i][j+1][ne][1]=(dp[i][j+1][ne][1]+dp[i-1][j][now][ne])%mo;
	 		//除了i=n,其他都会存在一种情况是一个去了的i-1对状态有影响。
			 //只考虑下一位会不会有使用的问题，那么dp方程就显得比较简单了。但是，这一步是存在重复的。
		 }
	 }
	 for (int i=0;i<=n;++i)
	 {
	 	for (int now=0;now<2;++now)
	 	for (int ne=0;ne<2;++ne)
	 	ans[i]=(ans[i]+dp[n][i][now][ne])%mo;
	 	ans[i] = ans[i] * fac[n - i] % mo;
	 //	for(int j=2;j<=n-i;++j) ans[i]=ans[i]*j%mo;

//	 for (int i=1;i<=n;++i) printf("%lld ",ans[i]);
	 int flag=-1;

	 for (int i=k+1;i<=n;++i)
	 {
	 	ans[k]=(ans[k]+flag*c[i][k]*ans[i]+mo)%mo;
	 	//对于每一个ans[i],我们要容斥处理。
	 	flag*=-1;
	 }
	 printf("%lld\n",ans[k]);
}


# 【CF285E】Positions in Permutations（动态规划，容斥）
## 题面
[CF](https://codeforces.com/problemset/problem/285/E)
[洛谷](https://www.luogu.org/problemnew/show/CF285E)
## 题解
首先发现恰好很不好算，所以转成至少，这样子只需要确定完一部分数之后剩下随意补。
然后套一个二项式反演进行容斥就可以得到答案了。
考虑怎么算至少$m$个的贡献，
设$f[i][j][S]$表示当前填到了位置$i$，一个有$j$个贡献，$i$和$i+1$的使用情况是$S$的方案数，每次枚举一下这个位置是填$i+1$还是$i-1$还是其他就可以进行转移了。
```cpp
#include<iostream>
#include<cstdio>
using namespace std;
#define MOD 1000000007
#define MAX 1010
void add(int &x,int y){x+=y;if(x>=MOD)x-=MOD;}
inline int read()
{
	int x=0;bool t=false;char ch=getchar();
	while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
	if(ch=='-')t=true,ch=getchar();
	while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
	return t?-x:x;
}
int n,m,jc[MAX],a[MAX],ans;
int f[MAX][MAX][4],C[MAX][MAX];
int main()
{
	n=read();m=read();
	jc[0]=1;for(int i=1;i<=n;++i)jc[i]=1ll*jc[i-1]*i%MOD;
	for(int i=0;i<=n;++i)C[i][0]=1;
	for(int i=1;i<=n;++i)
		for(int j=1;j<=i;++j)
			C[i][j]=(C[i-1][j]+C[i-1][j-1])%MOD;
	f[0][0][1]=1;
	for(int i=1;i<=n;++i)
		for(int j=0;j<i;++j)
			for(int S=0;S<4;++S)
			{
				if(!f[i-1][j][S])continue;
				add(f[i][j][S>>1],f[i-1][j][S]);
				if(!(S&1))add(f[i][j+1][S>>1],f[i-1][j][S]);
				if(i!=n)add(f[i][j+1][(S>>1)|2],f[i-1][j][S]);
			}
	for(int i=0;i<=n;++i)
		for(int S=0;S<4;++S)
			add(a[i],1ll*f[n][i][S]*jc[n-i]%MOD);
	for(int i=m,d=1;i<=n;++i,d=MOD-d)add(ans,1ll*d*C[i][m]%MOD*a[i]%MOD);
	printf("%d\n",ans);
	return 0;
}
```


#include<bits/stdc++.h>
using namespace std;
#define N 100000007
typedef long long ll;
ll prime[100000];
bool isprime[N];
int pc=0;
void sieve(ll n)
{
    for(int i=2;i<=n;++i){
        isprime[i]=1;
    }
    for(int i=2;i<=n;++i){
        if(!isprime[i]){
            continue;
        }
        prime[++pc]=i;
        for(int j=i+i;j<=n;j+=i){
            isprime[j]=0;
        }
    }
}

int main()
{

    ll p,y;
    scanf("%I64d %I64d",&p,&y);
    sieve ( p);

    for(ll i=y;i>p;--i){
        ll f=0;
        for(ll j=1;j<=pc;++j){
            if(i%prime[j]==0){
                break;
                f=1;
        }
        if(f==0){
            printf("%I64d\n",i);
            return 0;
        }
    }
    }
    printf("-1\n");

}


#include <iostream>
using namespace std;
typedef long long LL;
const LL MOD = 1e9+7;
const int N = 302;
int n;
int c[N][N];
char s[N];

LL dp[202][202][202];
LL dfs(int pre, int x, int y) {
    //printf("%d %d %d\n", pre, x, y);
    if (dp[pre][x][y] != -1) return dp[pre][x][y];
    if (x == 0 && y == 0) return 1;
    dp[pre][x][y] = 0;
    if (pre > 0) {
        if (y) dp[pre][x][y] += dfs(pre-1, x, y-1);
    }
    if(x) dp[pre][x][y] += dfs(pre+1,x-1,y);
    dp[pre][x][y]%=MOD;
    return dp[pre][x][y];
}

int main() {
    for(int i=0;i<N;i++)
        c[i][0]=1;
    for(int i=1;i<N;i++)
        for(int j=1;j<=i;j++)
            c[i][j]=(c[i-1][j]+c[i-1][j-1])%MOD;
    memset(dp,-1,sizeof(dp));
    scanf("%d %s",&n, s+1);

    int len = strlen(s+1);
    int cnt = 0, mxR = 0;

    for(int i=1;i<=len;i++) {
        if(s[i]=='(') cnt ++;
        else cnt --;
        if (cnt < 0) mxR = max(mxR, -cnt);
    }
    LL ans = 0;
    for(int x=0;x<N;x++){
        for(int y=0;y<N;y++){
            if (x-y<mxR) continue;
            int rem = 2*n - len - x - y;
            if (rem<0) continue;
            if ((rem+x-y+cnt)%2 == 1) continue;
            int R = (rem+x-y+cnt)/2;
            int L = rem - R;
            if(L<0||R<0) continue;
            /*
            R + L = rem
            R - L = x - y + cnt
            R = (rem+x-y+cnt)
            */
            ans = ans + dfs(0,x,y) * dfs(x-y+cnt,L,R) % MOD;
            ans = ans % MOD;
        }
    }
    cout << ans << endl;
}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
using namespace std;
typedef long long ll;
typedef vector<vector<ll>> mat;
int x, k, p, q, B, curx, n;
ll c[10],w;
map<int, ll> mp;
vector<ll> dp,dpn;
vector<int> bitset;
mat a, o, p;
void pow2(mat &a) {
	for (int i : bitset)
			for (int j : bitset)
			    p[i][j]=1e18;
	for (int k : bitset) {
		for (int i : bitset)
			for (int j : bitset)
				p[i][j] = min(p[i][j], a[i][k] + a[k][j]);
	}
	swap(a,p);
}
void operator *=(vector<ll> &dp, mat &a) {
	for(int i:bitset)
	    dpn[i]=1e18;
	for (int i : bitset) {
		for (int j : bitset) {
			dpn[j] = min(dpn[j], dp[i] + a[i][j]);
		}
	}
	swap(dp, dpn);
}
void jump(ll to) {
	a = o;
	ll jump = to - curx;
	for (;jump;pow2(a), jump >>= 1) {
		if (jump & 1) dp *= a;
	}
	curx = to;
}
int main() {
	ios::sync_with_stdio(0), cin.tie(0);
	cin >> x >> k >> n >> q;
	B = 1 << k;
	dp.resize(B, 1e18);
	dpn.resize(B, 1e18);
	o.resize(B, vector<ll>(B, 1e18));
	p.resize(B, vector<ll>(B, 1e18));
	for (int b = 1;b < B;b++)
		if (__builtin_popcount(b) == x) bitset.push_back(b);
	dp[bitset[0]] = 0;
	for (int i = 1;i <= k;i++) cin >> c[i];
	for (int cur : bitset) {
		int next = cur >> 1;
		if (cur & 1) {
			for (int i = 1, b = 1;i <= k;i++, b <<= 1) if (~next & b) {
				o[cur][next | b] = c[i];
			}
		}
		else o[cur][next] = 0;
	}
	for (int i = 1;i <= q;i++) {
		cin >> p >> w;
		if (w) mp[p] = w;
	}
	mp[n + k - x + 1] = 0;
	curx = k;
	for (auto it = mp.begin();curx < n + k - x;) {
		if (it->first <= curx - k) {
			it++; continue;
		}
		if (it->first <= curx + 1) {
			for(int i:bitset)
			    dpn[i]=1e18;
			curx++;
			for (int cur : bitset) {
				int next = cur >> 1;
				if (cur & 1) {
					for (int i = 1, b = 1;i <= k;i++, b <<= 1) if (~next & b) {
						dpn[next | b] = min(dpn[next | b], dp[cur] + c[i] + (mp.count(curx - k + i) ? mp[curx - k + i] : 0));
					}
				}
				else dpn[next] = min(dpn[next], dp[cur]);
			}
			swap(dp, dpn);
		}
		else jump(it->first - 1);
	}
	printf("%lld", dp[bitset[0]]);
}


#include <iostream>
#include <queue>
#include <bitset>
#include <cstring>
using namespace std;

const int INF = 0x3f3f3f3f;

struct Mat {
    vector<vector<int> > data;
    Mat(vector<vector<int> > &d) {
        swap(d, data);
    }
    Mat(){}
    void operator *= (const Mat &o) {
        // cout<<data[0][3]<<'\n';
        vector<vector<int> > res(data.size(), vector<int>(o.data[0].size(), INF));
        for(int i = 0; i < data.size(); i++) {
            for(int j = 0; j < o.data[0].size(); j++) {
                for(int k = 0; k < o.data.size(); k++) {
                    res[i][j] = min(res[i][j], data[i][k] + o.data[k][j]);
                    // if(data.size() == 1)
                    //     cout<<bitset<8>(i)<<','<<bitset<8>(k)<<' '<<bitset<8>(k)<<','<<bitset<8>(j)<<' '<<data[i][k]<<' '<<o.data[k][j]<<' '<<res[i][j]<<'\n';
                }
            }
        }
        swap(data, res);
    }
};

ostream &operator << (ostream &_cout, Mat& mat) {
    cout<<"{\n";
    for(auto &m: mat.data) {
        for(int &v: m) {
            cout<<v<<' ';
        }
        cout<<'\n';
    }
    cout<<"}";
    return _cout;
}

int X, K, N, Q, jmp[9];vector<vector<int> > oneStep;
struct Stone {
    int loc, val;
    bool operator < (const Stone& o) const {
        return loc < o.loc;
    }
} magic[25];

vector<int> &go(vector<int> &beginState, int stepCnt, vector<int> &res) {
    // cout<<"go: "<<stepCnt<<'\n';
    auto step = oneStep;
    Mat mat(step);
    Mat origin;origin.data.resize(1);swap(origin.data[0], beginState);
    // cout<<bitset<8>(beginState)<<' '<<origin.data[0][beginState]<<'\n';
    for(int i = 0; (stepCnt >> i); i++) {
        if((stepCnt>>i) & 1){
            origin *= mat;
            // cout<<origin<<'\n';
        }
        mat *= mat;
        // cout<<mat<<'\n';
    }
    // cout<<origin<<'\n';
    swap(res, origin.data[0]);
    return res;
}

vector<int> &dp(vector<int> &lastState, int stepCnt, int stoneAfter[201], int stoneEffect[26]) {
    // cout<<"dp: "<<stepCnt<<'\n';
    // cout<<"dp: ";for(int i = 0; i < lastState.size(); i++) cout<<lastState[i]<<' ';
    // cout<<'\n';
    vector<int> res(lastState.size());
    int dp[stepCnt+1][lastState.size()];
    memset(dp, 0x3f, sizeof dp);
    for(int i = 0; i < lastState.size(); i++) {
        dp[0][i] = lastState[i];
    }
    for(int step = 0; step < stepCnt; step++) {
        for(int cur = 1; cur < (1<<K); cur++){
            if(__builtin_popcount(cur) != X || dp[step][cur] == INF) continue;
            if(cur & 1){
                for(int nb = 0; nb < K; nb++) {
                    if(!((cur>>1) & (1<<nb))) {
                        // cout<<"HI\n";
                        int nxt = (cur>>1) | (1<<nb);
                        // cout<<nxt<<'\n';
                        dp[step+1][nxt] = min(dp[step+1][nxt], dp[step][cur] + jmp[nb+1] + stoneEffect[stoneAfter[nb + step+1]]);
                    }
                }
            }
            else{
                int nxt = cur>>1;
                dp[step+1][nxt] = min(dp[step+1][nxt], dp[step][cur]);
            }
        }
        // cout<<"dp: ";for(int i = 0; i < lastState.size(); i++) cout<<dp[step+1][i]<<'('<<bitset<8>(i)<<')'<<' ';
        // cout<<'\n';
    }
    for(int i = 0; i < lastState.size(); i++) {
        lastState[i] = dp[stepCnt][i];
    }
    return lastState;
}

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

    cin>>X>>K>>N>>Q;
    for(int i = 1; i <= K; i++) {
        cin>>jmp[i];
    }

    for(int i = 0; i < Q; i++) {
        cin>>magic[i].loc>>magic[i].val;
    }
    sort(magic, magic + Q);
    oneStep.resize(1<<K, vector<int> (1<<K, INF));
    for(int cur = 1; cur < (1<<K); cur++) {
        if(__builtin_popcount(cur) == X && (cur & 1) == 1) {
            for(int nb = 0; nb < K; nb++) {
                if(!((cur>>1) & (1<<nb))) {
                    // cout<<"HI\n";
                    int nxt = (cur>>1) | (1<<nb);
                    // cout<<nxt<<'\n';
                    oneStep[cur][nxt] = min(oneStep[cur][nxt], jmp[nb+1]);
                }
            }
        }
        else if (__builtin_popcount(cur) == X && (cur & 1) != 1) {
            oneStep[cur][cur>>1] = 0;
        }
    }
    // Mat fp(oneStep);
    // cout<<fp<<'\n';
    vector<int> res;
    vector<int> lastState(1<<K, INF);lastState[(1<<X)-1] = 0;
    int stoneEffect[26] = {};
    for(int i = 0; i < Q; i++) {
        stoneEffect[i+1] = magic[i].val;
    }
    int curLoc = 0;
    for(int i = 0; i < Q;) {
        if(magic[i].loc-K-curLoc > 0){
            // cout<<magic[i].loc<<' '<<K<<' '<<curLoc<<'\n';
            res = go(lastState, magic[i].loc-K-curLoc, res);
            curLoc = magic[i].loc-K;
            // cout<<curLoc<<'\n';
            swap(lastState, res);
        }
        int stoneAfter[201] = {};

        stoneAfter[K] = ++i;
        int st_loc = magic[i-1].loc;
        // cout<<st_loc<<' '<< K <<' '<< magic[i].loc<<'\n';
        while(i < Q && st_loc + K >= magic[i].loc){
            st_loc = magic[i].loc;
            stoneAfter[st_loc - curLoc] = ++i;
            // cout<<"st_loc: "<<st_loc<<'\n';
        }
        lastState = dp(lastState, st_loc - curLoc, stoneAfter, stoneEffect);
        curLoc = st_loc;
        // for(int &i: lastState) {
        //     cout<<i<<' ';
        // }
        // cout<<'\n';
    }
    res = go(lastState, N - curLoc - K, res);
    // for(int &i: res) {
    //     cout<<i<<' ';
    // }
    // cout<<'\n';
    int ans = INF;
    for(int i = 0; i <= (K-X); i++){
        ans = min(ans, res[((1<<X)-1)<<(K-X-i)]);
        swap(lastState, res);
        res = go(lastState, 1, res);
    }
    cout<<ans<<'\n';
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main ()
{
    string s;
    cin>>s;
    int cnt = 0,check = 0,j = 0;
    char i;
    for( i = 'a';;i++) {
        for(j = 0;j < s.length();j++) {
            if(s[j] == i) break;
        }
        if(j == s.length() - 1) break;
    }
    if(i == s[s.length() - 1]) cout<<"YES";
    else cout<<"NO";
    return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;
string s;
int l,ans=1,gs=-1;
int a[100];
inline bool pd(char x)
{
    if (x=='A'||x=='E'||x=='I'||x=='O'||x=='U') return true;
    return false;
}
int main()
{
    cin>>s;
    l=s.size();
    for (int i=1;i<=l;++i)
     if (pd(s[i])) {
         gs++;
         a[gs]=ans;
         ans=0;
     }
     else {
        ans++;
     }
    sort(a+1,a+1+gs);
    cout<<a[gs]+1;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main(){
    ll n,m,k;
    cin>>n>>m>>k;
    ll ar[n];
    for(ll i=1;i<=n;i++)
        cin>>ar[i];
    ll i=m-1,j=m+1;
    ll ans1=0,ans2=0;
    while(1){
        if(ar[i]<=k&&i>0&&ar[i]!=0){
            ans1=(m-i)*10;
            break;
        }
        else if(ar[j]<=k&&j<n+1&&ar[j]!=0){
            ans2=(j-m)*10;
            break;
        }
        else{
            i--;
            j++;
        }
    }
    if(ans1)
        cout<<ans1<<endl;
    else
        cout<<ans2<<endl;
    return 0;
}


#include<bits/stdc++.h>

using namespace std;
#define ll long long
const int MX=5000;
int n,m,a,b,mx;
string s;

int32_t main(){
    cin>>n;
    while(n){
        cin>>s;
        for(int i=0; i<s.size(); i++){
            if(int(s[i])<=96) mx++;
        }
        m=max(m,mx);
        n-=s.size();
        if(n!=1) n--;
    }
    cout<<m;
    return 0;
}


// https://codeforces.com/contest/822/problem/D

#include <bits/stdc++.h>
using namespace std;
// #define cerr if(false) cerr
#define watch(x) cerr << (#x) << " is " << (x) << endl;
#define endl '\n'
#define ld long double
// #define int long long
#define pii pair<int, int>
#define fi first
#define se second
#define sz(a) (int)a.size()
#define all(x) (x).begin(), (x).end()

void solve() {

}

// CHANGE WHEN SUBMITTING
const int maxn = (int)5e6;
// const int maxn = (int)1e3;
int t, l, r;

// divisors is cbrt(5e6) or around 170
set<int> divisors[maxn+50];

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

    cin >> t >> l >> r;
    int it = 0;
    for(int i = 1; i <= maxn; i++) {
        for(int j = i; j <= maxn; j += i) {
            // watch(j)
            divisors[j].emplace(i);
            it++;
        }
    }

watch(it)

}

/*

*/

// Did you read the bounds?
// Did you make typos?
// Are there edge cases (N=1?)
// Are array sizes proper (scaled by proper constant, for example 2* for koosaga tree)
// Integer overflow?
// DS reset properly between test cases?
// Is using long longs causing TLE?
// Are you using floating points?


#include <iostream>

using namespace std;

int main(int argc, char** argv)
{
    int n = 0;
    cin >> n;
    int minx = 0, miny = 0, maxx = 0, maxy = 0;
    cin >> minx >> miny >> maxx >> maxy;
    int s = (maxx - minx) * (maxy - miny);
    --n;
    for(; n; --n)
    {
        int x1 = 0, y1 = 0;
        int x2 = 0, y2 = 0;
        cin >> x1 >> y1 >> x2 >> y2;
        if(x1 < minx)
            minx = x1;
        if(x2 > maxx)
            maxx = x2;
        if(y1 < miny)
            miny = y1;
        if(y2 > maxy)
            maxy = y2;
        s += (x2 - x1) * (y2 - y1);
    }
    if((maxx - minx) * (maxy - miny) == s)
        cout << "YES\n";
    else
        cout << "NO\n";
    return 0;
}


// codeforces c676 pa.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
#include<iostream>

using namespace std;

int main()
{
A:
	int n,x1=-1,x2,max;
	cin >> n;
	int *a = new int[n];
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
		if (a[i] == 1 || a[i] == n)
		{
			if (x1 == -1)
				x1 = i;
			else
				x2 = i;
		}
	}
	x2 = n - x2-1;
	max = (x1 > x2) ? n-x2 : n - x1;
	cout << max-1;
	goto A;
//	cin.ignore();
//	cin.ignore();
	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>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int mod = 1000000000 + 7;
const int INF = 1000000000;
const int maxn = 200 + 10;
int T,n,m;
char s[maxn], name;
struct node {
    int B, G, R;
    node(int b=0, int g=0, int r=0):B(b),G(g),R(r) {}
}S;
const char table[] = { 'B', 'G', 'R' };
vector<int> ans;
void BFS() {
    queue<node> q;
    q.push(S);
    ans.clear();
//    memset(vis, false, sizeof(vis));
//    vis[S.B][S.G][S.R] = true;
    while(!q.empty()) {
        node u = q.front(); q.pop();
      //  printf("%d %d %d\n",u.B,u.G,u.R);
        if(u.B == 1 && u.G == 0 && u.R == 0) ans.push_back((int)('B'-'A'));
        else if(u.G == 1 && u.B == 0 && u.R == 0) ans.push_back((int)('G'-'A'));
        else if(u.R == 1 && u.B == 0 && u.G == 0) ans.push_back((int)('R'-'A'));
        for(int i=0;i<3;i++) {
            for(int j=0;j<3;j++) {
                if(i == j) {
                    if(i == 0 && u.B >= 2) q.push(node(u.B-1,u.G,u.R));
                    else if(i == 1 && u.G >= 2) q.push(node(u.B,u.G-1,u.R));
                    else if(i == 2 && u.R >= 2) q.push(node(u.B, u.G, u.R-1));
                }
                else {
                    if(i == 0 && j == 1 && u.B >= 1 && u.G >= 1) q.push(node(u.B-1,u.G-1,u.R+1));
                    else if(i==0&&j==2&&u.B>=1&&u.R>=1) q.push(node(u.B-1,u.G+1,u.R-1));
                    else if(i==1&&j==2&&u.G>=1&&u.R>=1) q.push(node(u.B+1,u.G-1,u.R-1));
                }
            }
        }
    }
}

int main() {
    scanf("%d%s",&n,s);
    S = node(0, 0, 0);
    for(int i=0;i<n;i++) {
        if(s[i] == 'B') S.B++;
        else if(s[i] == 'R') S.R++;
        else S.G++;
    }
    BFS();
    sort(ans.begin(), ans.end());
    int len = ans.size();
    for(int i=0;i<len;i++) {
        printf("%c",(char)(ans[i]+'A'));
    }
    printf("\n");
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long int lli;
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
typedef pair <lli,lli> pii;
#define all(vec) vec.begin(),vec.end()
#define ff first
#define ss second
#define pi 3.141592653589
#define inf 4e18
#define MOD 1000000007
#define pb push_back
#define endl "\n"
#define for1(low,high,k) for(int i=low;i<high;i+=k)
#define for2(low,high,k) for(int j=low;j<high;j+=k)
#define rev(high,low,k) for(int i=high;i>=low;i-=k)

lli solve(int i,int j,int in,string s,string &t,int k,vector <vector <vector <lli> > > &dp){
	if (j>k) return -inf;
	if (i==s.size()) return 0;
	if (dp[i][j][in]!=-1) return dp[i][j][in];
	int e0=0,e1=0,e2=0;
	if (t[0]==t[1]) e2=1;
	if (s[i]==t[0]) e0=1;
	if (s[i]==t[1]) e1=1;
	//ignore ith char
	lli ans=-inf;
	string temp=s;
	ans=max(ans,solve(i+1,j,in+e0,temp,t,k,dp)+(e1?in:0));
	//convert to t[0]
	temp[i]=t[0];
	ans=max(ans,solve(i+1,j+1,in+1,temp,t,k,dp)+(e2?in:0));
	//convert to t[1]
	temp[i]=t[1];
	ans=max(ans,solve(i+1,j+1,in+e2,temp,t,k,dp)+in);
	dp[i][j][in]=ans;
	return dp[i][j][in];
}

int main(){
	fast;
	lli n,k;
	cin>>n>>k;
	string s,t;
	cin>>s>>t;
	vector <vector <vector <lli> > > dp((n+1)*(k+1)*(n+1),vector <vector <lli> > ((k+1)*(n+1),vector <lli> (n+1,-1)));
	dp[0][0][0]=solve(0,0,0,s,t,k,dp);
	cout<<dp[0][0][0]<<endl;
}


#include<iostream>
using namespace std;
int a,b,lin;
int main()
{
cin>>a>>b;
lin=a/b;
cout<<lin;
return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n, k, c[500010], ans, tot;
queue <int> q;

int main() {
	cin >> n >> k;
	q.push(0);
	while(k--) q.push(0);

	for(int i = 1; i <= n; ++i) cin >> c[i];
	sort(c+1, c+n+1);
	for(int i = n; i > 0; --i) {
		int x = q.top();
		q.pop();
		tot += x;
		q.push(x + c[i]);
	}
	cout << tot << endl;
	return 0;
}


#include<bits/stdc++.h>

using namespace std;
#define ll long long int
int main()
{
 set<double>::iterator it;
 set<double>S;
    double d;
    int l,r,x,y,k,i;
    cin>>l>>r>>x>>y>>k;
    for(i=l;i<=r;++i)
    {

        d=(double)i/k;

        S.insert(d);
    }

    bool flag=false;
   for(i=x;i<=y;++i)
   {
       if(S.find(i)!=S.end())
       {

           flag=true;
           break;
       }
   }
   if(flag) cout<<"YES"<<endl;
   else cout<<"NO"<<endl;

    return 0;
}


#include <iostream>
#include <math.h>
using namespace std;

int main(void)
{
    int n, a, b, c;
    int l[10000];
    cin>>n>>a>>b>>c;
    for (int i = 0; i<4000; i++){
        for (int j = 0; j<4000; j++){
            for(int k = 0; k<4000; k++){
                l[a*k + b*j + c*i] = i+j+k;
            }
        }
    }
    cout<<l[n];
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll mod = 1000000007;

#define IOS  ios::sync_with_stdio(0); cin.tie(0);
#define endl "\n"
ll dp[55][55][3];
ll c[55][55];
signed main() {

        IOS;
        dp[0][0][1] = 1;
        dp[1][0][1] = 0;
        dp[1][0][0] = 1;
        dp[0][0][0] = 1; int n , k; cin >>n >> k;
        c[0][0] = 1;
        for (int i = 1; i <= n; c[i][0] = 1, i++)
            for (int j = 1; j <= n; j++)
                c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % mod;

        for(int i = 2; i <= n ; i++) {
         	for(int j = 0; j <= k; j++)  {
         		for(int Left = 1; Left <= i - 1; Left++)  {

    	     	    ll coef = c[i - 2][Left - 1] * Left * max(1 , (i - Left - 1) )  %  mod;

    	    	    for(int kLeft = 0 ; kLeft <= j - 1; kLeft++) {

    	    	    	ll addFi = dp[Left][kLeft][0] * ((i - Left - 1  ) > 0 ? dp[i - Left - 1][j - 1 - kLeft][1] + dp[i - Left -1][j - 1 - kLeft][0] : j - kLeft - 1 == 0 ? 1 : 0);
			     		ll addSe = ( (i - Left - 1 )> 0 ? dp[Left][kLeft][1] *  dp[i - Left -1][j - 1 - kLeft][0] : 0);
			    		dp[i][j][1] = ( dp[i][j] + coef * ((addFi + addSe ) % mod )  )% mod;
			    	}
				for(int kLeft = 0 ; kLeft <=j ; kLeft++) {

					dp[i][j][0] = dp[i][j][0] +  coef * dp[Left][kLeft][1] % mod * dp[i - Left -1][j - kLeft][1] % mod;
					dp[i][j][0] %= mod;

				}
			}
		}
	}
	cout << (dp[n][k][0] + dp[n][k][1]) % mod;
 }


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

#define ll long long int
#define MOD 1000000007LL
ll n, k;

ll ans[55][55][55];

ll max(ll a, ll b) {
    if (a > b) return a;
    return b;
}

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

int main() {
    cin >> n >> k;
    memset(ans, sizeof(ans), 0);
    ans[1][0][0] = 1;
    for (ll i = 2; i <= n; i++) {
        for (ll x1 = 0; x1 <= (i - 1) / 2; x1++) {
            for (ll j = 0; j < 3; j++) {
                ll y1 = x1 + dy[j];
                if (y1 < 0) continue;
                ll x = y1 + 1;
                ll y = max(x1, y1);
                ans[i][x][y] += (ans[i-1][x1][y1]) * (i - 1);
                ans[i][x][y] %= MOD;
            }
        }

        for (ll z = 1; z <= (i - 1) / 2; z++) {
            for (ll x1 = 0; x1 <= z / 2; x1++) {
                for (ll x2 = 0; x2 <= (i - 1 - z) / 2; x2++) {
                    for (ll j1 = 0; j1 < 3; j1++) {
                        ll y1 = x1 + dy[j1];
                        if (y1 < 0) continue;
                        for (ll j2 = 0; j2 < 3; j2++) {
                            ll y2 = x2 + dy[j2];
                            if (y2 < 0) continue;
                            ll x = max(1 + x1 + y2, 1 + x2 + y1);
                            ll y = max(x1, y1) + max(x2, y2);
                            ll delta = ans[z][x1][y1] * ans[i-1-z][x2][y2];
                            delta %= MOD;
                            ll q = (i-1) * (i-2);
                            if (z == (i - 1 - z)) q /= 2;
                            delta *= q;
                            delta %= MOD;
                            ans[i][x][y] += delta;
                            ans[i][x][y] %= MOD;
                        }
                    }
                }
            }
        }
    }

    ll res = ans[n][k-1][k] + ans[n][k][k] + ans[n][k][k-1];
    cout << res << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
long long n, m, x, y, v;
bool can(long long size){
	long long a, b, c, d;     //inclusion  - exclusion
	a = size / x;
	b = size / x / y;
	c = size / y;
	d = size - a - c + b; //intersection
	long long x1 = size - a; //Multiple x - total
	long long y1= size - c; //Multiple y - total
	if(x1 + y1 - d >= n + m && x1 >= n && y1 >= m) return true;
	return false;
}
int main(){
	freopen("in", "r", stdin);
	freopen("out", "w", stdout);
	scanf("%I64d %I64d %I64d %I64d", &n, &m, &x, &y);
	long long low = m + n;
	long long high = 1e18;
	while(high >= low){
		long long mid = (low + high) >> 1;
		if(can(mid)){ v = mid; high = mid - 1;}
		else low = mid + 1;
	}
	printf("%I64d\n", v);
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long LL;

template <typename T> inline void chkmax(T &x , T y) { x = max(x , y); }
template <typename T> inline void chkmin(T &x , T y) { x = min(x , y); }
template <typename T> inline void read(T &x) {
	T f = 1; x = 0;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
	x *= f;
}

const int K = 5000 + 10;
const int mod = 998244353;

int n , m , k;
int fac[K] , ifac[K] , S[K][K];

inline void inc(int &x , int y) {
	x = x + y < mod ? x + y : x + y - mod;
}
inline int quickpow(int a , int n) {
	int res = 1;
	for (; n; n >>= 1 , a = (LL) a * a % mod)
		if (n & 1) res = (LL) res * a % mod;
	return res;
}
int main() {

	read(n) , read(m) , read(k);
	fac[0] = 1;
	for (int i = 1; i <= k; ++i) fac[i] = (LL) fac[i - 1] * i % mod;
	ifac[k] = quickpow(fac[k] , mod - 2);
	for (int i = k - 1; i >= 0; --i) ifac[i] = (LL) ifac[i + 1] * (i + 1) % mod;
	S[0][0] = 1;
	for (int i = 1; i <= k; ++i)
	for (int j = 1; j <= i; ++j)
		S[i][j] = (S[i - 1][j - 1] + 1LL * S[i - 1][j] * j % mod) % mod;
	int coef = 1 , ans = 0;
	for (int i = 0; i <= k; ++i) {
		if (i) coef = (LL) coef * (n - i + 1) % mod;
		inc(ans , (LL) S[k][i] * coef % mod * quickpow(m - 1 , n - i) % mod * quickpow(quickpow(m - 1 , mod - 2) + 1 , n - i) % mod);
	}
	ans = 1LL * ans * quickpow(quickpow(m , n) , mod - 2) % mod;
	printf("%d\n" , ans);
	return 0;
}


#include <cstdio>
int n;
int main() {
	scanf("%d", n);
	int ans = (n - 1) / 2;
	printf("%d", ans);
	return 0;
}