#include <bits/stdc++.h>
using namespace std;
#define ios ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
#define debugg(x) cout<<#x<<'='<<x<<endl;
#define debug1(x,y,z) cout<<#x<<' '<<x<<' '<<#y<<' '<<y<<' '<<#z<<' '<<z<<endl;
#define debug cout<<endl<<"********"<<endl;
#define ll long long
#define ull unsigned long long
#define ding cerr<<endl<<"Time: "<<(clock() * 1000. / CLOCKS_PER_SEC)<<" ms"<<endl;
#define ld long double
#define itn int
#define pi (int)(3.1415926536)
#define pii pair<int,int>
#define rep(I, A, B) for (int I = (A); I <= (B); ++I)
#define dwn(I, A, B) for (int I = (A); I >= (B); --I)
#define repp(i,a,b) for(int i=(a);i<(b);i++)
#define mod (ll)(1e9+7)
#define mod1 (ll)(1e9)
#define int ll
void fre(){
    freopen("test.in","r",stdin);
    freopen("test.out","w",stdout);
}
void fc(){fclose(stdin);fclose(stdout);}
inline ll ccin(){
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-') f=-1;
        ch=getchar();
    }
    while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*f;
}
inline void cou(ll x){
    if(x<0) putchar('-'),x=-x;
    if(x>9) cou(x/10);
    putchar(x%10+'0');
}

const int maxn=2e5+10;
const ll inf=1e16;
const int maxm=5e5+10;

int n;
struct node{
    int v;int w;
};
vector<node> yuan[maxn];
int zhi[maxn],cnt0[70],cnt1[70];
void dfs(int now,int fa,int val){
    zhi[now]=val;
    for(int i=0;i<yuan[now].size();i++){
        int v=yuan[now][i].v;
        if(v==fa) continue;
        dfs(v,now,val^yuan[now][i].w);
    }
}

signed main(){
    ios;
    cin>>n;
    for(int i=1;i<n;i++){
        int a1,a2,a3;cin>>a1>>a2>>a3;
        yuan[a1].push_back((node){a2,a3});
        yuan[a2].push_back((node){a1,a3});
    }
    dfs(1,0,0);
    for(int i=0;i<=61;i++){
        int now=(1LL<<i);
        for(int j=1;j<=n;j++){
            if(now&zhi[j]) cnt1[i]++;
            else cnt0[i]++;
        }
    }
    ll ans=0;
    for(int i=0;i<=61;i++){
        ll now=(1LL<<i)%mod;
        ll cnt=cnt1[i]*cnt0[i]%mod;
        ans=(ans+now*cnt)%mod;
    }
    cout<<ans<<endl;
    ding;return 0;
}

#include <iostream>
#include <cstring>
#include <cstdio>
#define LL long long
#define mod 1000000007

using namespace std;

int T;
LL n,a,b;

inline LL cal(LL n,LL a)
{
    return (n-a+1)*(n-a+1)%mod;
}

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

LL inv2;

int main()
{
    inv2=pow_mod(2,mod-2);
    scanf("%d",&T);
    while (T--)
    {
        scanf("%lld%lld%lld",&n,&a,&b);
        if (a<b) swap(a,b);
        LL ans=cal(n,a)*cal(n,b)%mod;
        if (b==1)
        {
            LL tmp=cal(n,a)*cal(a,b)%mod;
            ans-=tmp;
            if (ans<0) ans+=mod;
        }
        else
        {
            ans-=(cal(n,a)*cal(a,b)%mod);
            if (ans<0) ans+=mod;
            LL h=min(a+b-1,n);
            LL p1=(a-b+1)*(n-a+1)%mod;
            LL p2=(n+1)*(h-a)%mod;
            LL p3=(a+1+h)%mod*(h-a)%mod*inv2%mod;
            p1=4LL*p1%mod*(p2-p3+mod)%mod;
            ans-=p1;
            if (ans<0) ans+=mod;
            p1=(p2-p3)%mod;
            p1=p1*p1%mod*4%mod;
            ans-=p1;
            if (ans<0) ans+=mod;
        }
        printf("%lld\n",ans);
    }
    return 0;
}


#line 2 "header.hpp"

//%snippet.set('header')%
//%snippet.fold()%
#ifndef HEADER_H
#define HEADER_H

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

// varibable settings
template <class T> constexpr T inf = numeric_limits<T>::max() / 2.1;

#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (ll i = (ll)(a); i < (ll)(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define _rrep(i, n) rrepi(i, 0, n)
#define rrepi(i, a, b) for (ll i = (ll)((b)-1); i >= (ll)(a); --i)
#define r_rep(...) _overload3(__VA_ARGS__, rrepi, _rrep, )(__VA_ARGS__)
#define each(i, a) for (auto &&i : a)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define mt(...) make_tuple(__VA_ARGS__)
#define ub upper_bound
#define lb lower_bound
#define lpos(A, x) (lower_bound(all(A), x) - A.begin())
#define upos(A, x) (upper_bound(all(A), x) - A.begin())
template <class T, class U> inline void chmax(T &a, const U &b) { if ((a) < (b)) (a) = (b); }
template <class T, class U> inline void chmin(T &a, const U &b) { if ((a) > (b)) (a) = (b); }
template <typename X, typename T> auto make_table(X x, T a) { return vector<T>(x, a); }
template <typename X, typename Y, typename Z, typename... Zs> auto make_table(X x, Y y, Z z, Zs... zs) { auto cont = make_table(y, z, zs...); return vector<decltype(cont)>(x, cont); }

template <class T> T cdiv(T a, T b){ assert(a >= 0 && b > 0); return (a+b-1)/b; }

#define is_in(x, a, b) ((a) <= (x) && (x) < (b))
#define uni(x) sort(all(x)); x.erase(unique(all(x)), x.end())
#define slice(l, r) substr(l, r - l)

#include <cxxabi.h>
string demangle(const char * name) {
    size_t len = strlen(name) + 256;
    char output_buffer[len];
    int status = 0;
    return string(abi::__cxa_demangle(name, output_buffer, &len, &status));
}
template<class T> string type(T x){ return demangle(typeid(x).name()); }

typedef long long ll;
typedef long double ld;

template <typename T>
using PQ = priority_queue<T, vector<T>, greater<T>>;
void check_input() { assert(cin.eof() == 0); int tmp; cin >> tmp; assert(cin.eof() == 1); }

#if defined(PCM) || defined(LOCAL)
#else
#define dump(...) ;
#define dump_1d(...) ;
#define dump_2d(...) ;
#define cerrendl ;
#endif

#endif /* HEADER_H */
//%snippet.end()%
#line 2 "solve.cpp"
template<class T=ll> using vec = vector<T>;
struct Fast { Fast() { std::cin.tie(0); ios::sync_with_stdio(false); } } fast;

using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using iii = tuple<int, int, int>;

int query(){
    cerrendl;
    ll n;cin>>n;

    ll num = 0;
    while (true) {
        if (n % 2 == 0) {
            n /= 2;
            num++;
        }
        else{
            break;
        }
    }
    if (num == 0){
        cout << "Odd" << endl;
    }
    else if (num == 1){
        cout << "Same" << endl;
    }
    else{
        cout << "Even" << endl;
    }

    return 0;
}

int main(){/*{{{*/
    ll Q;cin>>Q;
    rep(_, Q){
        query();
    }
    check_input();
    return 0;
}/*}}}*/

#include <bits/stdc++.h>
#define endl "\n"
#define rep(i, n) for(int i = 0; i < int(n); i++)
#define rep2(i, s, n) for(int i = (s); i < int(n); i++)
#define e_b emplace_back
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ipair;
typedef pair<ll, ll> lpair;
template <class T> ostream &operator << (ostream &o, const vector<T> &v) //vectorの中身を見る
{o << "{"; for(int i = 0; i < (int)v.size(); i++) o << (i > 0 ? ", ":"") << v[i]; o << "}"; return o;}
void map_p(map<int, int> &d){cout << "map: "; for(auto a : d){cout << "{" << a.first << ":" << a.second << "}, ";} cout << endl;} //mapの中身を見る
void set_p(set<int> &d){cout << "set: "; for(int a : d){cout << a << ", ";} cout << endl;} //setの中身を見る

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

    int t;
    cin >> t;
    rep(i, t){
        ll n;
        cin >> n;
        string ans;
        if(n % 2 == 1) ans = "Odd";
        else{
            if(n / 2 % 2 == 1) ans = "Same";
            else ans = "Even";
        }
        cout << ans << endl;
    }
    return 0;
}


#include<bits/stdc++.h>

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

using namespace __gnu_pbds;
using namespace std;

template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// find_by_order(k)  returns iterator to kth element starting from 0;
// order_of_key(k) returns count of elements strictly smaller than k;
typedef long long  ll;
typedef vector<ll> VL;
typedef vector<int> VI;
typedef pair<ll,ll> PLL;
typedef pair<int,int> PII;
#define pb push_back

#define F first
#define S second
#define SZ(a) int((a).size())
#define ALL(a) a.begin(),a.end()
#define fr(i,x,y) for(int i=x;i<y;i++)
#define frr(i,x,y) for(int i=x-1;i>=y;i--)
#define inf 1e18+1
const long double PI = acos(-1.0L);
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int mod=1000000007;
//const int mod=998244353;
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
ll power(ll a,ll b){ll ans=1;while(b!=0){if(b&1){ans*=a;}a=a*a;b>>=1;}return ans;}
ll powerm(ll a,ll b){ll ans=1;while(b!=0){if(b&1){ans*=a;ans%=mod;}a=a*a;a%=mod;b>>=1;}return ans%mod;}
VL dx={1,0,-1,0};
VL dy={0,1,0,-1};
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();

void solve()
{
    ll n,m;
    cin>>n>>m;
    mod=m;
    ll rem=powerm(10,n);
    //cout<<rem<<endl;
    mod=m*m;
    ll x=powerm(10,n);
    x/=m;
    x%=mod;
    cout<<x<<endl;
    return;
}
int main()
{
    IOS;
    ll t=1,pp;
    //cin>>t;
    pp=t;
    while(t--)
    {
        //cout<<"Case #"<<pp-t<<": ";
        solve();
    }
    return 0;
}
/* stuff you should look for
        * int overflow, array bounds
        * special cases (n=1?)
        * do smth instead of nothing and stay organized
        * WRITE STUFF DOWN
        * BE CAREFUL REGARDING THE DEFAULT VALUES IN segement trees etc
        * BE very careful in int vs long long vs unsigned long long
*/
/*
recursion - matrix exponential

*/
// BITMASK:
// 1)When some constrall is of the order of 15-20, think of bitmask DP.
// 2)When some constrall is around 40, try out meet in the middle
// 3) See Strings,palindromes,prefix,suffix etc -> KMP,Z algorithmQ

// atcoder/arc107/D/main.cpp
// author: @___Johniel
// github: https://github.com/johniel/

#include <bits/stdc++.h>

#define each(i, c) for (auto& i : c)
#define unless(cond) if (!(cond))

using namespace std;

template<typename P, typename Q> ostream& operator << (ostream& os, pair<P, Q> p) { os << "(" << p.first << "," << p.second << ")"; return os; }
template<typename P, typename Q> istream& operator >> (istream& is, pair<P, Q>& p) { is >> p.first >> p.second; return is; }
template<typename T> ostream& operator << (ostream& os, vector<T> v) { os << "("; for (auto& i: v) os << i << ","; os << ")"; return os; }
template<typename T> istream& operator >> (istream& is, vector<T>& v) { for (auto& i: v) is >> i; return is; }
template<typename T> ostream& operator << (ostream& os, set<T> s) { os << "#{"; for (auto& i: s) os << i << ","; os << "}"; return os; }
template<typename K, typename V> ostream& operator << (ostream& os, map<K, V> m) { os << "{"; for (auto& i: m) os << i << ","; os << "}"; return os; }

template<typename T> inline T setmax(T& a, T b) { return a = std::max(a, b); }
template<typename T> inline T setmin(T& a, T b) { return a = std::min(a, b); }

using lli = long long int;
using ull = unsigned long long;
using point = complex<double>;
using str = string;
template<typename T> using vec = vector<T>;

constexpr array<int, 8> di({0, 1, -1, 0, 1, -1, 1, -1});
constexpr array<int, 8> dj({1, 0, 0, -1, 1, -1, -1, 1});
constexpr lli mod = 998244353;

const int N = 3000 + 3;
const int K = 3000 + 3;
static int memo[N][K];
int rec(int n, int k)
{
  if (k == 0) return n == 0;
  if (n < k) return 0;
  if (n == k) return 1;
  // cout << n << ' ' << k << endl;
  int& ret = memo[n][k];
  if (ret != -1) return ret;
  return ret = (rec(n - 1, k - 1) + rec(n, k * 2)) % mod;
}

int main(int argc, char *argv[])
{
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.setf(ios_base::fixed);
  cout.precision(15);

  int n, k;
  while (cin >> n >> k) {
    if (n < k) {
      cout << 0 << endl;
      continue;
    }
    fill(&memo[0][0], &memo[N - 1][K - 1] + 1, -1);
    cout << rec(n, k) << endl;
  }

  return 0;
}


//
//AtCoder Beginner Contest 182
//D
//

#include <iostream>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <queue>
#include <map>
#include <set>
#include <tuple>
#include <functional>
#include <cassert>
#include <bitset>
#include <numeric>

using namespace std;

#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=a;i<=(ll)(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=(ll)(b);i--)
#define ALL(x) (x).begin(),(x).end()
#define RALL(x) (x).rbegin(),(x).rend()
#define SIZE(x) (int)(x).size()
#define SIZEL(x) (ll)(x).size()
#define MSG(a) cout << #a << " " << a << endl;

using ll = long long;
using ld = long double;
using Graph = vector<vector<int>>;

const double PI = 3.14159265358979323846;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

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

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

    vector<int> a(n);
    REP(i, n) cin >> a[i];

    ll ans=0;
    ll sum=0;
    ll mvMax=0;
    ll now=0;

    REP(i, n){
        sum+=a[i];
        mvMax = max(mvMax,sum);
        ans = max(ans, now+mvMax);
        now = now+sum;
    }
    cout << ans << endl;
    return 0;
}

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N = 2e5+10;
struct Node
{
	int l,r;
	bool operator<(const Node& a )const
	{
		return l<a.l;
	}
}line[N];
int a[N],m;
long long tr[N];
int lowbit(int x)
{
	return x&(-x);
}
void add(int x,int d)
{
	for(;x<=m+m;x+=lowbit(x))
    {
		tr[x]+=d;
	}
}
long long query(int x)
{
	long long res=0;
	for(;x;x-=lowbit(x))
    {
		res+=tr[x];
	}
	return res;
}
int find(int x)
{
	return lower_bound(a+1,a+1+m,x)-a;
}
int main()
{
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
    {
            int t,l,r;
            cin>>t>>l>>r;
            l<<=1;
            r<<=1;
            if(t==2)
            {
                r--;
            }
		else if(t==3)
        {
            l++;
        }else if(t==4)
		{
			l++;r--;
		}
		line[i]={l,r};
		a[++m]=l;
		a[++m]=r;
	}
	sort(a+1,a+1+m);
	m=unique(a+1,a+1+m)-(a+1);//去除相邻重复数字
	sort(line+1,line+1+n);
	int pre=0;
	long long res=0;
	for(int i=1;i<=n;i++)
	{
		int l=line[i].l;
		int r=line[i].r;
		l=find(l);
		r=find(r);
		int t=query(m)-query(l-1);
		res+=max(t,0);
		//pre=max(r,pre);
		add(r,1);
	}
	cout<<res;
	return 0;
}


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

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

typedef long long ll;

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

#define fi first
#define se second

int main(){
	ios::sync_with_stdio(0); cin.tie(0);
	int n; cin >> n;
	string s; cin >> s;
	int q; cin >> q;
	bool is_flip = 0;
	fr(qq,q){
		int t, a, b; cin >> t >> a >> b;
		a--,b--;

		if(t==1){
			if(is_flip){
				if(a<n) a+=n;
				else a-=n;

				if(b<n) b+=n;
				else b-=n;
			}
			swap(s[a],s[b]);
		} else{
			is_flip = !is_flip;
		}
	}

	int i = 0;
	if(is_flip) i = n;
	fr(cnt,2*n){
		cout << s[i++];
		if(i==sz(s)) i = 0;
	}
	cout << "\n";
}

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

int main() {
  int N, Q;
  string S, S1, S2;
  cin>>N>>S>>Q;
  vector <int> T (Q+1);
  vector <int> A (Q);
  vector <int> B (Q);

  for (int i=0; i<Q; i++)
    cin>>T.at(i)>>A.at(i)>>B.at(i);

  T.at(Q)=1;

  for (int i=0; i<Q; i++){

    if (T.at(i)==1)
      swap(S.at(A.at(i)-1), S.at(B.at(i)-1));
    else if (T.at(i)==2 && T.at(i+1)==2)
      i+=1;
    else{
      S=S.substr(N, N)+S.substr(0, N);
    }
  }

  cout<<S;
}


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

#define int long long
#define puts(x) cout<<x<<endl
#define inf 100000000000
#define minf -LLONG_MAX
#define all(v) (v).begin(),(v).end()
#define odd(x) x%2==1
#define YES cout<<"YES\n"
#define NO cout<<"NO\n"
#define Yes cout<<"Yes\n"
#define No cout<<"No\n"
#define present(c,x) (c.find(x) != c.end())
#define ANS cout<<ans<<endl;
#define sz(x) x.size()
#define desc greater<int>()
#define nl cout<<endl
#define modP(x) (x+P)%P
#define rep(i,x,y)  for(int i=x;i<y;i++)
#define vll vector<long long>
#define ll long long int

const int MAX_N = 1e5 + 1;
const int P = 1e9 + 7;
const int INF = 1e9;
const int LINF = 1e18;

void pra(int a[], int n)
{
    for (int i = 0;i<n;i++)
        cout << a[i] << " ";
    cout << '\n';
}

void prv(vector<int> &a, int n)
{
    int i = 0;
    for(int x : a)
    {
        i++;
        cout << x << " ";
        if(i == n) break;
    }
    cout << '\n';
}

void prvp(vector<pair<int,int>> vp, int n)
{
    int i = 0;
    for (auto x : vp)
    {
        i++;
        cout << x.first << " " << x.second << '\n';
        if(i == n) break;
    }

    cout << '\n';
}
int mod(int n) {return (n % P + P) % P; }
int modM(int n, int m) { return ((n % P * m % P) + P) % P; }
int modA(int n, int m) { return ((n % P + m % P) + P) % P; }
int modS(int n, int m) { return ((n % P - m % P) + P) % P; }

//*******************************************************************************************************************************************************************************************************
void solve() {
    int h,w,n ,m ;
    cin>>h>>w>>n>>m;
    int a[h+1][w+1];
    memset(a,-1, sizeof(a));
    for (int i=0;i<n;i++){
        int r,c ;
        cin>>r>>c;
        a[r][c]=1;
    }
    for(int i=0;i<m;i++){
        int r,c;
        cin>>r>>c;
        a[r][c]=0;
    }
    int b[h+1][w+1];
    memset(b,0,sizeof(b));
    for(int i=1;i<=h;i++){
        int l=0;
        for(int j=1;j<=w;j++){
            if(a[i][j]==1)l=1;
            else if(a[i][j]==0)l=0;
            b[i][j]|=l;
        }
    }
    for(int i=1;i<=h;i++){
        int l=0;
        for(int j=w;j>=1;j--){
            if(a[i][j]==1)l=1;
            else if(a[i][j]==0)l=0;
            b[i][j]|=l;
        }
    }
    for(int j=1;j<=w;j++){
        int l=0;
        for(int i=1;i<=h;i++){
            if(a[i][j]==1)l=1;
            else if(a[i][j]==0)l=0;
            b[i][j]|=l;
        }
    }
    for(int j=1;j<=w;j++){
        int l=0;
        for(int i=h;i>=1;i--){
            if(a[i][j]==1)l=1;
            else if(a[i][j]==0)l=0;
            b[i][j]|=l;
        }
    }
    int ans =0;
    for(int i=1;i<=h;i++)
        for (int j=1;j<=w;j++)
            if(b[i][j])ans++;

    cout  <<  ans  <<endl;

}
//************************************************************************************************************************************************************************************************************************

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
   // int tc; cin >> tc;
int tc =1;

    for (int t = 1; t <= tc; t++) {
        //cout << "Case #" << t  << ": ";
        solve();
    }
}

#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define dump(...)
#endif
#define endl '\n'
//#define int long long
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define INF 2147483647
#define LLINF 9223372036854775807LL
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define BIT(n) (1LL << (n))
#define TOUPPER(s) transform(s.begin(), s.end(), s.begin(), ::toupper)
#define TOLOWER(s) transform(s.begin(), s.end(), s.begin(), ::tolower)
#define FILLZERO(s, n)                      \
    do {                                    \
        ostringstream os;                   \
        os << setw(n) << setfill('0') << s; \
        s = os.str();                       \
    } while (0);
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())  // sortしてからつかうこと

using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using pi = pair<int, int>;
template <typename T>
using Pq = priority_queue<T>;
template <typename T>
using pQ = priority_queue<T, vector<T>, greater<T>>;

template <typename T>
vector<T> make_vec(size_t a, T n) {
    return vector<T>(a, n);
}
template <typename T, typename... Ts>
auto make_vec(size_t a, Ts... ts) {
    return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <class T>
inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T>
bool isProductOverflow(T a, T b) {
    return (a > numeric_limits<T>::max() / b);
}
template <typename T>
istream &operator>>(istream &is, vector<T> &vec) {
    for (T &x : vec) is >> x;
    return is;
}

__attribute__((constructor)) void ioInit() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    cout << std::fixed << std::setprecision(10);
}

constexpr ll MOD = 1e9 + 7;
// constexpr ll MOD = 998244353;
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
ll A, B, C, D, E, F, G, H, N, M, L, K, P, Q, R, W, X, Y, Z;
string S, T;
ll ans = 0;

signed main() {
    cin >> N >> X;
    ans = LLINF;
    vector<ll> a(N);
    cin >> a;
    REPS(n, N) {
        auto dp = make_vec<ll>(N + 1, N + 1, N + 1, 0LL);
        REP(i, N) {
            REP(j, N) {
                REP(k, N) {
                    chmax(dp[i + 1][j][k], dp[i][j][k]);
                    if (dp[i][j][k] > 0 or (j == 0 and k == 0))
                        chmax(dp[i + 1][j + 1][(k + a[i]) % n], dp[i][j][k] + a[i]);
                }
            }
        }
        REP(k, N) {
            if ((X - dp[N][n][k]) % n == 0 and dp[N][n][k] > 0) {
                chmin(ans, (X - dp[N][n][k]) / n);
            }
        }
        dump(n, dp);
    }
    cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(),(x).end()
template<typename T1,typename T2> bool chmin(T1 &a,T2 b){if(a<=b)return 0; a=b; return 1;}
template<typename T1,typename T2> bool chmax(T1 &a,T2 b){if(a>=b)return 0; a=b; return 1;}
int dx[4]={0,1,0,-1}, dy[4]={1,0,-1,0};
long double eps = 1e-9;
long double pi = acos(-1);
#define totori signed
#define nyaa main

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

	int a,b,w;
	cin>>a>>b>>w;
	w *= 1000;
	if(w < a){
		cout << "UNSATISFIABLE" << endl;
		return 0;
	}
	int mi = w / b;
	int rem = w % b + mi*(b-a);
	if(rem >= a || w%b==0){
		mi += (w%b!=0);
	}
	else {
		cout << "UNSATISFIABLE" << endl;
		return 0;
	}
	int ma = w / a;
	rem = w % a;
	if(w % a == 0)cout << mi << " "<<ma << endl;
	else if((b-a)*ma >= rem)cout <<mi<<" "<< ma << endl;
	else if((b-a)*(ma+1)>=rem)cout << mi << " " << ma+1 << endl;
	else cout << "UNSATISFIABLE" << endl;

}

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

#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------

int A,B,W;

void solve() {
	int i,j,k,l,r,x,y; string s;

	cin>>A>>B>>W;
	W*=1000;
	int mi=100000000,ma=-1;
	for(i=1;i<=1000000;i++) {
		if(A*i<=W&&W<=B*i) mi=min(mi,i), ma=max(ma,i);

	}
	if(mi<=ma) cout<<mi<<" "<<ma<<endl;
	else cout<<"UNSATISFIABLE"<<endl;

}

int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}


#include <bits/stdc++.h>
#include <unordered_set>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repm(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define repm2(i, s, n) for (int i = (int)(n - 1); i >= (s); i--)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using ll = long long; // more than 10^9
using P = pair<int, int>;
const int INF = 0x3fffffff;
template <typename T>
bool chmax(T &a, const T &b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}
template <typename T>
bool chmin(T &a, const T &b)
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}

int main()
{
    int n, m;
    int a, b;
    vector<int> ans;
    unordered_set<ll> a_s, b_s, cap, cup, ans_s;

    cin >> n >> m;

    rep(i, n)
    {
        cin >> a;
        a_s.insert(a);
        cup.insert(a);
    }

    rep(i, m)
    {
        cin >> b;
        b_s.insert(b);
        cup.insert(b);

        if (a_s.count(b))
        {
            cap.insert(b);
        }
    }

    for (auto as : a_s)
        if (cup.count(as) && !cap.count(as))
            ans_s.insert(as);

    for (auto bs : b_s)
        if (cup.count(bs) && !cap.count(bs))
            ans_s.insert(bs);

    for (auto an : ans_s)
        ans.push_back(an);

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

    rep(i, ans.size())
            cout
        << ans.at(i) << endl;

    return 0;
}

#include <algorithm>
#include <cstdint>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include <cfloat>

using namespace std;

using lint=int64_t;
using uint=uint32_t;
using ulint=uint64_t;

template<class T>
using vector2d=vector<vector<T>>;

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

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

template <class T>
void OutVec(const vector<T>& vec){
  for(int i = 0; i < vec.size() - 1; ++i){
    cout<<vec[i]<<" ";
  }
  cout<<vec.back()<<endl;
}

template <class T>
void OutVec2d(const vector2d<T>& vec){
  for(auto v:vec){
    OutVec(v);
  }
}

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

  int n,m;
  cin>>n>>m;
  vector<bool> a(1001, false), b(1001, false);
  vector<int> results;
  for(int i=0;i<n;i++){
    int v;
    cin>>v;
    a[v]=true;
  }
  for(int i=0;i<m;i++){
    int v;
    cin>>v;
    b[v]=true;
  }

  for(int i=1;i<=1000;i++){
    if(a[i]!=b[i]) results.push_back(i);
  }
  if(results.empty()) return 0;
  OutVec(results);

  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define INIT  ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define mp make_pair
#define pb push_back
#define ft first
#define sc second
#define ll long long
#define pii pair<int, int>
#define count_bits __builtin_popcount
#define int ll

int t, n, m, k, a[300010], q, l, r;
int y, x;

map<int,int> h;

int rec(int n){
    if(n==1){return x-1;}
    if(n==0){return x;}
    if(h[n]!=0){
        return h[n];
    }
    //cout<<n<<" "<<flush;
    h[n]=min(abs(x-n), min(rec((n-(n%2) )/2  ),rec((n+(n%2) )/2 ) )+1+(n%2) );
    return h[n];
}

int32_t main(){
INIT
cin>>x>>y;
cout<<rec(y);

return 0;
}

#include<cstdio>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;

#define fi first
#define se second
#define rep(i, l, r) for(int i=(l), i##_end_=(r); i<=i##_end_; ++i)
#define fep(i, l, r) for(int i=(l), i##_end_=(r); i>=i##_end_; --i)
typedef long long ll;
template<class T>inline T fab(T x){ return x<0? -x: x; }
template<class T>inline T readin(T x){
    x=0; int f=0; char c;
    while((c=getchar())<'0' || '9'<c) if(c=='-') f=1;
    for(x=(c^48); '0'<=(c=getchar()) && c<='9'; x=(x<<1)+(x<<3)+(c^48));
    return f? -x: x;
}

const int maxn=2000;
const int inf=maxn*2+5;
const int dir[][2]={{0, 1}, {1, 0}};

int n, m;
char a[maxn+5][maxn+5];
int f[maxn+5][maxn+5][2];

inline int inside(int x, int y){
    return 0<x && x<=n && 0<y && y<=m;
}

int getf(int i, int j, int k){
    if(i==n && j==m) return 0;
    if(f[i][j][k]<inf) return f[i][j][k];
    int ti, tj, res;
    if(k==0){
        res=-inf;
        rep(t, 0, 1){
            ti=i+dir[t][0], tj=j+dir[t][1];
            if(!inside(ti, tj)) continue;
            res=max(res, getf(ti, tj, k^1)+(a[ti][tj]=='-'? -1: 1));
        }
    }
    else{
        res=inf;
        rep(t, 0, 1){
            ti=i+dir[t][0], tj=j+dir[t][1];
            if(!inside(ti, tj)) continue;
            res=min(res, getf(ti, tj, k^1)+(a[ti][tj]=='-'? 1: -1));
        }
    }
    // f[i][j][k]=res;
    // printf("f[%d, %d, %d] == %d\n", i, j, k, f[i][j][k]);
    return f[i][j][k]=res;
}

signed main(){
    n=readin(1), m=readin(1);
    rep(i, 1, n) scanf("%s", a[i]+1);
    rep(i, 1, n) rep(j, 1, m)
        f[i][j][0]=f[i][j][1]=inf;
    int res=getf(1, 1, 0);
    if(res<0) printf("Aoki\n");
    else if(res>0) printf("Takahashi\n");
    else printf("Draw\n");
    return 0;
}


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

#define ll long long
#define forn_1(i,a,b) for(int i=a;i<b;i++)
#define forn(i,a,b) for(int i=a;i<=b;i++)

void solve()
{

}

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

	string str;
	cin>>str;
	int n=str.size();
   reverse(str.begin(),str.end());
	forn_1(i,0,n) {if(str[i]=='9') cout<<'6';
	else if(str[i]=='6') cout<<'9';
	else cout<<str[i];
}

	return 0;
}

#include <bits/stdc++.h>
#define ull unsigned long long
#define ll long long
using namespace std;
#define rep(i, x) for (int i = 0; i < x; i++)
// const int INF = 998244353;
const int MOD = 998244353;

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


#include <bits/stdc++.h>
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
ll I=1167167167167167167;
ll Q=1e9+7;
#define rep(i,a) for (ll i=0;i<a;i++)
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end());reverse(v.begin(),v.end());}
template<class T> void print_tate(vector<T> &v) {rep(i,v.size()) cout<<v[i]<<"\n";}
void yneos(bool a){if(a) cout<<"Yes"<<"\n"; else cout<<"No"<<"\n";}

int N=50,M=0;
int start_x,start_y;
vector<vector<int>> tile(N,vector<int>(N));
vector<vector<int>> wrote_point(N,vector<int>(N));
vector<int> point;
int start_tile;

vector<int> vari_x={0,0,-1,1},vari_y={-1,1,0,0};

struct date
{
	string route;
	int x;
	int y;
	int now_point;
	set<int> arrived;
	int rate;
	 bool operator<(const date& another) const {
        //メンバ変数であるnum1で比較した結果を
        //この構造体の比較とする
        return now_point < another.now_point;
    }
	 bool operator>(const date& another) const {
        //メンバ変数であるnum1で比較した結果を
        //この構造体の比較とする
        return now_point > another.now_point;
    }
};

int value(int a){
	return a;
}

bool check(int x,int y){
	return (0<=x&&x<N&&0<=y&&y<N);
}

date add(date a,int x,int y){//add_checkを通過していること
	if(x-a.x==1) a.route+="D";
	else if(x-a.x==-1) a.route+="U";
	else if(y-a.y==1) a.route+="R";
	else a.route+="L";
	a.arrived.insert(tile[x][y]);
	a.now_point+=point[tile[x][y]];
	a.x=x;
	a.y=y;
	return a;
}

bool add_check(date &a,int X,int Y){
	if(!check(X,Y)) return false;
	if(a.arrived.count(tile[X][Y])) return false;
	if(abs(a.x-X)+abs(a.y-Y)!=1) return false;
	return true;
}

void solve(){
	int range=30;
	date ans={"",start_x,start_y,point[start_tile],{start_tile},point[start_tile]};
	vector<date> looking={ans};
	while(looking.size()!=0&&clock()<1.9*CLOCKS_PER_SEC){
		vector<date> next_looking;
		ans=looking[0];
		rep(i,min(range,(int)looking.size())){
			rep(j,4){
				int s=looking[i].x+vari_x[j],t=looking[i].y+vari_y[j];
				if(add_check(looking[i],s,t)){
					next_looking.push_back(add(looking[i],s,t));
				}
			}
		}
		if(next_looking.size()==0) break;
		Sore(next_looking);
		looking=next_looking;
	}
	cout<<looking[0].route<<endl;
}

//main関数だよーーー
int main() {
	cin>>start_x>>start_y;
	rep(i,N) rep(j,N) cin>>tile[i][j],chmax(M,tile[i][j]);
	rep(i,N) rep(j,N) cin>>wrote_point[i][j];
	M++;
	point.resize(M);
	rep(i,N) rep(j,N) point[tile[i][j]]+=wrote_point[i][j];
	start_tile=tile[start_x][start_y];
	solve();
}

// #pragma GCC optimize("unroll-loops", "omit-frame-pointer", "inline")
// #pragma GCC option("arch=native", "tune=native", "no-zero-upper")
// #pragma GCC
// target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("tree-vectorize","openmp","predictive-commoning")
// #pragma GCC option("D_GLIBCXX_PARALLEL","openmp")

// #pragma GCC optimize("O3")
// #pragma GCC target("avx2")

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<ll> vll;
// #define int long long
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000              // 2e9
#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)
#define fi first
#define sec second
#define all(x) (x).begin(), (x).end()
#define sq(x) ((x) * (x))
#define dmp(x) cerr << #x << ": " << x << endl;

template <class T>
void chmin(T &a, const T &b) {
  if (a > b) a = b;
}
template <class T>
void chmax(T &a, const T &b) {
  if (a < b) a = b;
}

template <class T>
using MaxHeap = priority_queue<T>;
template <class T>
using MinHeap = priority_queue<T, vector<T>, greater<T>>;
template <class T>
vector<T> vect(int len, T elem) {
  return vector<T>(len, elem);
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << p.fi << ',' << p.sec;
  return os;
}
template <class T, class U>
istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.fi >> p.sec;
  return is;
}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) {
    os << vec[i];
    if (i + 1 < vec.size()) os << ' ';
  }
  return os;
}
template <class T>
istream &operator>>(istream &is, vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) is >> vec[i];
  return is;
}
void fastio() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout << fixed << setprecision(20);
}

#define endl "\n"

template <class T>
struct RangeSum {
  vector<T> vec;
  RangeSum() {}
  RangeSum(vector<T> elems) : vec(elems) {
    for (int i = 1; i < vec.size(); i++) { vec[i] += vec[i - 1]; }
  }
  T sum(int l, int r) {
    if (l > r) return T(0);
    if (l == 0)
      return vec[r];
    else
      return vec[r] - vec[l - 1];
  }
};

void solve() {
  int N, M;
  cin >> N >> M;
  vector<ll> w(N);
  auto cost = vect(N, vect(N, -LLINF));
  cin >> w;
  ll mxw = *max_element(all(w));
  vector<pair<ll, ll>> bs(M);
  ll minv = LLINF;
  for (int i = 0; i < M; i++) {
    cin >> bs[i].first >> bs[i].second;
    chmin(minv, bs[i].second);
  }
  sort(all(bs));
  for (int i = M - 2; i >= 0; i--) chmin(bs[i].second, bs[i + 1].second);
  set<pair<int, int>> s;
  for (int i = 0; i < M; i++) {
    s.insert(pair<int, int>(bs[i].second, bs[i].first));
  }
  s.insert(pair<int, int>(0, 0));

  if (minv < mxw) {
    cout << -1 << endl;
    return;
  }

  ll ans = LLINF;
  vector<int> perm;
  for (int i = 0; i < N; i++) perm.push_back(i);
  do {
    vector<ll> W(N);
    for (int i = 0; i < N; i++) W[i] = w[perm[i]];
    RangeSum<ll> rs(W);
    vector<ll> dp(N, 0);
    for (int i = 1; i < N; i++) {
      chmax(dp[i], dp[i - 1]);
      for (int j = 0; j < i; j++) {
        ll wsum = rs.sum(j, i);
        auto it = s.lower_bound(pair<int, int>(wsum, -1));
        it--;
        chmax(dp[i], dp[j] + it->second);
      }
    }
    chmin(ans, dp[N - 1]);
  } while (next_permutation(all(perm)));
  cout << ans << endl;
  return;
}

signed main() {
  fastio();
  solve();
  // int t; cin >> t; while(t--)solve();

  // int t; cin >> t;
  // for(int i=1;i<=t;i++){
  //   cout << "Case #" << i << ": ";
  //   solve();
  // }
  return 0;
}


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

int main(){
	string s,t="";
	cin >> s;
	int n=s.size();
	int i=0;
	while(i < n && s.at(i)!='.'){
		t+=s.at(i);
		i++;
	}
	cout << t << endl;
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define fst ios_base::sync_with_stdio(false);cin.tie(0), cout.tie(0);
#define pb                push_back
#define all(x)            (x).begin(),(x).end()
#define debug(a...)       cout<<#a<<": ";for(auto it:a)cerr<<it<<" ";cout<<endl;
typedef long long ll;
vector<int>a;
void solve(){
	string s;
	cin>>s;
	for(int i=0;i<s.length();i++){
		if (s[i]=='.')break;
		else cout<<s[i];
	}
}

int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
int t = 1;
// cin >> t;
while(t--){
solve();
}
return 0;
}


#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAXN=1e5+4;
int fir[MAXN],nex[MAXN],to[MAXN],f[MAXN],sz[MAXN],tot;
struct node{
	int x,id;
	bool operator<(const node &i) const{
		return x>i.x;
	}
}st[MAXN];

void add(int u,int v) {
	nex[++tot]=fir[u];
   fir[u]=tot;
   to[tot]=v;
}

void dfs(int u) {
	int i,s1=0,s2=0,top=0;
	sz[u]=f[u]=1;
	for(i=fir[u];i;i=nex[i]) {
      dfs(to[i]);
   }
	for(i=fir[u];i;i=nex[i]) {
		sz[u]+=sz[to[i]];
		if (sz[to[i]]-f[to[i]]>=f[to[i]]&&(sz[to[i]]&1)==0) {
         f[u]+=f[to[i]];
		} else {
			if (sz[to[i]]&1) {
            st[++top]=(node){sz[to[i]]-f[to[i]]*2,to[i]};
         } else {
            s1+=f[to[i]];
            s2+=sz[to[i]]-f[to[i]];
         }
		}
	}
	sort(st+1,st+1+top);
	for(i=1;i<=top;++i) {
		if (i&1) {
         f[u]+=f[st[i].id];
      } else {
         f[u]+=sz[st[i].id]-f[st[i].id];
      }
	}
	if (top&1) {
      f[u]+=s2;
   } else {
      f[u]+=s1;
   }
}

int main() {
	int n, i,u;
   cin>>n;
	for(i=2;i<=n;++i) {
      cin>>u;
		add(u,i);
	}
	dfs(1);
	cout<<f[1]<<"\n";
   return 0;
}

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

// Editorial AC

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

    ll N; cin >> N;
    VVLL G(N);
    FOR(i,1,N-1+1) {
        ll p; cin >> p;
        p--;
        G[p].push_back(i);
    }

    // dp[i] := 頂点 i を根とする部分木において，
    //          双方最善を尽くした場合の先手の相対獲得枚数 (先手-後手)
    VLL dp(N,-INF);
    VLL ds(N,1);  // 頂点 i を根とする部分木の頂点数
    auto rec = [&](auto self, ll u, ll p) -> ll {
        if (dp[u] != -INF) return dp[u];
        ll res = 1;  // 先手の相対獲得枚数
        VLL A;  // 偶数回 & 後手が損しない (<= 0)
        VLL B;  // 偶数回 & 後手が損する   ( > 0)
        VLL C;  // 奇数回
        for (ll v : G[u]) if (v != p) {
            self(self, v, u);
            ds[u] += ds[v];
            if (ds[v] % 2 == 0) {
                (dp[v] <= 0 ? A : B).push_back(dp[v]);
            } else {
                C.push_back(dp[v]);
            }
        }
        res += SUMLL(A);
        SORT(C);
        REP(i,SZ(C)) {
            res += C[i] * (i % 2 == 0 ? 1 : -1);
        }
        res += SUMLL(B) * (SZ(C) % 2 == 0 ? 1 : -1);
        return dp[u] = res;
    };
    ll ans = (N + rec(rec, 0, -1)) / 2;
    COUT(ans);

    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define pii pair<int,int>
#define pll pair<long long, long long>
using namespace std;

const int mod = 1e9+7;
#define INF INT_MAX

int sum(int x, int y)
{
	return (x+y)%mod;
}

int mult(int x, int y)
{
	return (1ll * x * y)% mod;
}

int fastpow(int val, int pow)
{
	int res = 1;
	while(pow > 0)
	{
		if(pow % 2 != 0)
			res = mult(res, val);
		val = mult(val, val);
		pow/=2;
	}
	return res;
}

vector<vector<int>> matmult(vector<vector<int>> &mat1, vector<vector<int>> &mat2)
{
	vector<vector<int>> res(mat1.size(),vector<int>(mat1[0].size(),0));
	int m = mat1.size(), n = mat1[0].size();
	for(int i=0;i<m;i++)
	{
		for(int j = 0; j < n; j++)
		{
			for(int k=0;k<n;k++)
			{
				res[i][j] = sum(res[i][j], mult(mat1[i][k], mat2[k][j]));
			}
		}
	}

	return res;
}

vector<vector<int>> matexp(vector<vector<int>> mat, int pow)
{
	vector<vector<int>>res(mat.size(), vector<int>(mat[0].size(),0));

	for(int i=0;i<mat.size();i++)
		res[i][i] = 1;

	while(pow > 0)
	{
		if(pow%2!=0)
			res  = matmult(res, mat);
		pow/=2;
		if(pow == 0)
			break;
		mat = matmult(mat, mat);

	}

	return res;
}

vector<int> deg(105);

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	int n,m,k;
	cin>>n>>m>>k;
	vector<int> a(n);

	int inv_2 = fastpow(2,mod-2);
	int inv_m = fastpow(m,mod-2);
	vector<vector<int>> mat(n, vector<int>(n,0));
	for(int i=0;i<n;i++)
		cin>>a[i];

	for(int i=0;i<m;i++)
	{
		int x,y;
		cin>>x>>y;
		x--;
		y--;
		deg[x]++;
		deg[y]++;
		mat[x][y] = mult(inv_2, inv_m);
		mat[y][x] = mult(inv_2, inv_m);

	}

	for(int i=0;i<n;i++)
	{
		mat[i][i] = mult(sum(mult(2,m), -deg[i]),mult(inv_2,inv_m));
	}
	mat = matexp(mat,k);
	vector<int> res(n);
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
			res[i] = sum(res[i], mult(a[j],mat[j][i]));
	}
	for(int i=0;i<n;i++)
		cout<<res[i]<<"\n";

}

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

//0-indexed
template<typename T>
struct BIT {
    using F = function<T(T,T)>;

    private:
    const int n;
    vector<T> dat;
    const F operation;

    public:
    BIT (const int n_, const F operation_) : n(n_+1), dat(n_+1, 0), operation(operation_) {}
    BIT (const vector<T> vec, const F operation_) : n((int) vec.size()), dat(n+1, 0), operation(operation_) {
        for(int i = 0; i < n; i++) add(i,vec[i]);
    }

    T operator [] (const int i) { return sum(i, i+1); }

    //O(logn)
    inline void add (const int i, const T x) {
        for(int k = i+1; k < n+1; k += k&-k) dat[k] = operation(dat[k], x);
    }

    //O(logn)
    T sum (const int i) {
        T ret = 0;
        for(int k = i; k > 0; k -= k&-k) ret = operation(ret, dat[k]);
        return ret;
    }
    T sum (const int a, const int b) {
        return operation(sum(b), sum(a));
    }
};

void ABC185_F_RangeXorQuery(){
    int n, q;
    cin >> n >> q;

    vector<int> a(n);
    for(int i = 0; i < n; i++) cin >> a[i];

    auto Xor = [&](int left, int right) { return left ^ right; };

    BIT<int> tree(a, Xor);

    vector<int> ans;
    for(int i = 0; i < q; i++){
        int t, x, y;
        cin >> t >> x >> y;
        x--;
        if(t == 1) tree.add(x, y);
        else cout << tree.sum(x, y) << endl;
    }
}

void ABC106_D_AtCoderExpress2(){
    int n, m, q;
    cin >> n >> m >> q;

    struct point {
        int left, right, type;
        bool operator < (const point &other) {
            return make_pair(make_pair(left, right),type) > make_pair(make_pair(other.left, other.right), other.type);
        }
    };

    vector<point> obj;
    for(int i = 0; i < m; i++){
        int l,r;
        cin >> l >> r;
        obj.push_back({l, -r, 0});
    }
    for(int i = 0; i < q; i++){
        int l,r;
        cin >> l >> r;
        obj.push_back({l, -r, -i-1});
    }

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

    auto plus = [&](int left, int right) { return left + right; };

    BIT<int> tree(n, plus);

    int k = obj.size();
    vector<int> ans(q);
    for(int i = 0; i < k; i++){
        if(-obj[i].type) ans[-obj[i].type-1] = tree.sum(-obj[i].right+1);
        else tree.add(-obj[i].right,1);
    }

    for(int i = 0; i < q; i++) cout << ans[i] << endl;

}

int main(){ABC185_F_RangeXorQuery();}


#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using iPair = pair<int,int>;
using lPair = pair<ll, ll>;
using ivector = vector<int>;
using lvector = vector<ll>;
using istack = stack<int>;
using iqueue = queue<int>;
using ivv = vector<vector<int>>;
using lvv = vector<vector<ll>>;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
vector<iPair> dir = {{1,0}, {-1,0}, {0,1}, {0,-1}};
#define dump(x) cout << #x << " = " << (x) << endl
#define ALL(x) begin(x),end(x)
#define rep(i,s,e) for(ll i=(s); i<(ll)(e); ++i)
#define repRev(i,s,e) for(ll i=(e-1); i>=(ll)(s); --i)
#define range(i,s,n) for(ll i=(s), i_stop=(s)+(n); i<i_stop; ++i)
#define rangeRev(i,s,n) for(ll i=(s), i_stop=(s)-(n); i>i_stop; --i)
#define foreach(x,container) for(auto &&x:container)
bool chmax(ll& a, ll b) {if(a<b) {a=b;return true;} return false;}
bool chmin(ll& a, ll b) {if(a>b) {a=b;return true;} return false;}
template<typename T> void printArr(vector<T> &arr){
    for(auto &&x:arr) {cout << x << " ";} cout << endl;
}

/*
https://atcoder.jp/contests/abc184/tasks/abc184_e

## 關鍵詞：
圖論，最短路，BFS，Dijkstra，優化邊數，01BFS

## 題意：
給一副地圖，高H寬W。0 <= H,W <= 2e3。
若某個格子標記這小寫字母，代表該格子遷移到其他標記同樣字母的格子的費用是0。
求S到G的最短距離。

## 思路：
|V| <= 4e6, |E| <= |V|^2/2 = 8e12, TLE。
顯然直接建邊的話不可通過。
想用上次拿到題的思路，分兩層建邊，但是寫道一半就感覺非常麻煩。
做應該還是可以做的。

https://atcoder.jp/contests/abc184/editorial/352
題解的思路使用BFS。O(HW)復雜度。

## 雜多：

*/

void solve(){
    int h, w; cin >> h >> w;
    vector<string> grid(h+1);
    range(i, 0, h) cin >> grid[i];

    // 讀取S，G坐標
    // 讀取各個傳送點坐標
    int sy, sx, gy, gx;
    map<char, vector<iPair>> mapping;
    range(i, 0, h) range(j, 0, w) {
        if(grid[i][j] == 'S') {sy = i; sx = j; continue;}
        if(grid[i][j] == 'G') {gy = i; gx = j; continue;}
        char teleport = grid[i][j];
        if(teleport >= 'a' && teleport <= 'z') {
            mapping[teleport].emplace_back(i, j);
        }
    }

    // BFS遍歷全圖
    vector<vector<bool>> visited(h+1, vector<bool>(w+1, false));
    vector<lvector> dist(h+1, lvector(w+1, LINF));
    vector<bool> flag(30, false);
    queue<iPair> q;
    dist[sy][sx] = 0; q.emplace(sy, sx); visited[sy][sx] = true;
    ll ans = -1;
    while(q.size()) {
        auto [r, c] = q.front(); q.pop();
        ll d = dist[r][c];
        if(r==gy && c==gx) {
            ans = d;
            break;
        }

        // 向四周遍歷
        foreach(dir_item, dir) {
            auto [dy, dx] = dir_item;
            int rr = r+dy;
            int cc = c+dx;
            if(rr<0 || rr>=h) continue;
            if(cc<0 || cc>=w) continue;
            if(grid[rr][cc] == '#') continue;
            if(visited[rr][cc]) continue;
            visited[rr][cc] = true;

            if(chmin(dist[rr][cc], d+1)) q.emplace(rr, cc);
        }

        // 如果這個傳送點沒被用過，那麼將所有相連的傳送點也加入隊列
        char teleport = grid[r][c];
        if(teleport >= 'a' && teleport <= 'z' && !flag[teleport-'a']) {
            flag[teleport-'a'] = true;
            foreach(tele_coor, mapping[teleport]) {
                auto [rrr, ccc] = tele_coor;
                if(visited[rrr][ccc]) continue;
                visited[rrr][ccc] = true;
                if(chmin(dist[rrr][ccc], d+1)) q.emplace(rrr, ccc);
            }
        }
    }

    cout << ans << endl;
}

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

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <iostream>
#include <vector>
#include <map>
#include <memory>
#include <list>
#include <deque>
#include <queue>
#include <iomanip>
#include <set>
#include <stack>
#include <numeric>
#include <unordered_set>
#include <unordered_map>
#include <string.h>
#include <functional>
#include <sstream>

using namespace std;

#define REP(i,n) for (decltype(n) i = 0; i < n; i++)
#define THE_MOD 1'000'000'007

using ll = long long;
using ivec = vector<int>;
using lvec = vector<long>;
using ipair = pair<int, int>;
using llvec = vector<ll>;
using llpair = pair<ll, ll>;

#define umap unordered_map
#define uset unordered_set

template<typename T> ostream& operator <<(ostream& os, const vector<T> &v) { auto n = v.size(); REP(i,n) os << (i ? " " : "") << v[i]; return os; }
template<typename T> istream& operator >>(istream& is, vector<T> &v) { for(auto &e : v) is >> e; return is; }
template<typename T, typename U> ostream& operator <<(ostream& os, const pair<T, U> &p) { os << p.first << " " << p.second; return os; }
template<typename T, typename U> istream& operator >>(istream& is, pair<T, U> &p) { is >> p.first >> p.second; return is; }

void in() {} template <typename T, typename... Args> void in(T& t, Args& ...args) { cin >> t; in(args...); }
void out() { cout << endl; } template <typename T, typename... Args> void out(const T& t, const Args& ...args) { cout << t; if (sizeof...(args)) cout << " "; out(args...); }

constexpr ll LLINF = numeric_limits<ll>::max();

template<class T> void sort(T& v) {	sort(v.begin(), v.end()); }
template<class T> void rsort(T& v) { sort(v.begin(), v.end(), greater<typename T::value_type>()); }

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

template <typename T = ll, typename U = ll>
vector<T> Dijkstra(const vector<vector<pair<U,T>>>& edges, U start, U numNodes)
{
	using Node = pair<U,T>;
	auto compare = [](Node a, Node b){ return a.second > b.second;};
	priority_queue<Node, vector<Node>, decltype(compare)> q{compare};

	vector<T> costs(numNodes, numeric_limits<T>::max());

	q.emplace(start, 0);
	while (!q.empty()) {
		auto [from, cost] = q.top();
		q.pop();
		if (cost > costs[from]) continue;

		for (const auto& edge : edges[from]) {
			auto to = edge.first;
			auto newCost = cost + edge.second;
			if (newCost < costs[to]) {
				costs[to] = newCost;
				q.emplace(to, newCost);
			}
		}
	}
	return costs;
}

struct Range {
	ll x;
	ll y;
	Range(ll x, ll y) : x(x), y(y)
	{
		if (y > x) swap(x,y);
	}

	struct Iterator {
		using difference_type   = ptrdiff_t;
		using value_type        = ll;
		using pointer           = ll*;
		using reference         = ll&;
		using iterator_category = std::random_access_iterator_tag;
		Iterator() = default;
		Iterator(ll n) : n(n) {}
		ll n;
		bool operator!=(const Iterator&rhs) const { return n != rhs.n; }
		bool operator==(const Iterator&rhs) const { return n == rhs.n; }
		Iterator& operator++() { n++; return *this; }
		Iterator& operator--() { n--; return *this; }
		Iterator& operator+=(ll x) { n+=x; return *this; }
		ll operator*() const { return n; }
		ptrdiff_t operator-(const Iterator&rhs) const { return n - rhs.n; }
	};

	Iterator begin() { return Iterator(x); }
	Iterator end() { return Iterator(y); }
};

ll run()
{
	ll n;
	in(n);
	ll r = -1;
	REP(i, n) {
		ll a, p, x;
		in(a,p,x);
		if (a < x) {
			if (r == -1) {
				r = p;
			} else {
				r = min(r, p);
			}
		}

	}
	out(r);
	return 0;
}

int main()
{
	cout << fixed << setprecision(15);
	auto r = run();
	//YN(r);
	//out(r);
	//cout << r << endl;
	return 0;
}


/**
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡀⠀⠀⠀⠀⢀⡀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⡖⠁⠀⠀⠀⠀⠀⠀⠈⢲⣄
⠀⠀⠀⠀⠀⠀⠀⠀⣼⡏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⣧
⠀⠀⠀⠀⠀⠀⠀⣸⣿⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣿⣇
⠀⠀⠀⠀⠀⠀⠀⣿⣿⡇⠀⢀⣀⣤⣤⣤⣤⣀⡀⠀⢸⣿⣿
⠀⠀⠀⠀⠀⠀⠀⢻⣿⣿⣔⢿⡿⠟⠛⠛⠻⢿⡿⣢⣿⣿⡟
⠀⠀⠀⠀⣀⣤⣶⣾⣿⣿⣿⣷⣤⣀⡀⢀⣀⣤⣾⣿⣿⣿⣷⣶⣤⡀
⠀⠀⢠⣾⣿⡿⠿⠿⠿⣿⣿⣿⣿⡿⠏⠻⢿⣿⣿⣿⣿⠿⠿⠿⢿⣿⣷⡀
⠀⢠⡿⠋⠁⠀⠀⢸⣿⡇⠉⠻⣿⠇⠀⠀⠸⣿⡿⠋⢰⣿⡇⠀⠀⠈⠙⢿⡄
⠀⡿⠁⠀⠀⠀⠀⠘⣿⣷⡀⠀⠰⣿⣶⣶⣿⡎⠀⢀⣾⣿⠇⠀⠀⠀⠀⠈⢿
⠀⡇⠀⠀⠀⠀⠀⠀⠹⣿⣷⣄⠀⣿⣿⣿⣿⠀⣠⣾⣿⠏⠀⠀⠀⠀⠀⠀⢸
⠀⠁⠀⠀⠀⠀⠀⠀⠀⠈⠻⢿⢇⣿⣿⣿⣿⡸⣿⠟⠁⠀⠀⠀⠀⠀⠀⠀⠈
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣼⣿⣿⣿⣿⣧⡀
⠀⠀⠀⠐⢤⣀⣀⢀⣀⣠⣴⣿⣿⠿⠋⠙⠿⣿⣿⣦⣄⣀⠀⠀⣀⡠⠂
⠀⠀⠀⠀⠀⠈⠉⠛⠛⠛⠛⠉⠀⠀⠀⠀⠀⠈⠉⠛⠛⠛⠛⠋⠁
 *    author:  Daredevil666
 *    institution: IIT Patna
**/
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define nl cout<<"\n";
#define ll long long int
#define F(i,a,b) for(i=a;i<b;i++)
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
const double PI = 3.14159265358979323846264338327950288419716939937510582097494459230781641;
const ll M=998244353;
const ll MAXN=100200;
ll i,j,mask;
ll mod(ll x)
{
    return (x+M)%M;
}
ll add(ll a,ll b)
{
    return mod(mod(a)+mod(b));
}
ll mul(ll a,ll b)
{
    return mod(mod(a)*mod(b));
}
ll power(ll p,ll x)
{
    ll res=1;
    while(x>0)
    {
        if(x%2==1)
        {
            res=mul(res,p);
        }
        x/=2;
        p=mul(p,p);
    }
    return res;
}
void solve()
{
   ll h,w,k;
   cin>>h>>w>>k;
   vector<vector<char> > grid(h,vector<char> (w,'?'));
   F(i,0,k)
   {
       ll a,b;
       char c;
       cin>>a>>b>>c;
       grid[a-1][b-1]=c;
   }
   vector<vector<ll> > dp(h,vector<ll> (w,0));
   dp[0][0]=power(3,h*w-k);
      ll t=mul(2,power(3,M-2));
   F(i,0,h)
   {
       F(j,0,w)
       {
            if (j+ 1 < w && grid[i][j] != 'D')
           {
            dp[i][j + 1]= add(dp[i][j+1],mul(dp[i][j] ,(grid[i][j] == '?' ? t : (ll)1)));
           }

            // Go down
            if (i+ 1 < h && grid[i][j] != 'R')
                {dp[i+ 1][j]=add(dp[i+1][j], mul(dp[i][j] , (grid[i][j] == '?' ? t :(ll) 1)));
            }

       }
   }
   cout<<dp[h-1][w-1];
}
int main()
{
IOS
/*#ifndef ONLINE_JUDGE
   freopen("input.txt","r",stdin);
   freopen("output.txt","w",stdout);
#*/
/*ll t,test;
cin>>test;
F(t,1,test+1)
{
    //cout<<"Case #"<<t<<": ";
    solve();
    nl
}*/
solve();
return 0;
}

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

#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define MOD 1000000007
#define MOD1 998244353
#define vin(v,k) fo(i,k)cin>>a,v.pb(a);
#define INF 1e18
#define nline "\n"
#define pb push_back
#define ppb pop_back
#define makep make_pair
#define ff first
#define ss second
#define PI 3.141592653589793238462
#define set_bits __builtin_popcountll
#define all(x) (x).begin(), (x).end()
#define siz 2000010

typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;

#define debug(x) cout << #x <<" "; _print(x); cout << endl;

void _print(ll t) {cout << t;}
void _print(int t) {cout << t;}
void _print(string t) {cout << t;}
void _print(char t) {cout << t;}
void _print(lld t) {cout << t;}
void _print(double t) {cout << t;}
void _print(ull t) {cout << t;}

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

template<class T>void _print(vector<vector<T>> v){cout<<"\n";for(auto i:v){for(auto j:i){cout<<j<<" ";}cout<<"\n";}}

void solve(){
    ll n,m,a,b,c,x,y,z,k;
    ll ans=0,val=0,cnt=0;
    cin>>a>>b>>c;
    cout<<max(a+b,max(a+c,b+c));
}

int main()
{
    fast;
    ll t;
    /*cin>>t;
    while(t--)*/
    solve();
}


#include <bits/stdc++.h>
using namespace std;
const int MAXN = 400005;
const long long int MOD = 998244353;
long long int fact[MAXN], inv_fact[MAXN], ways[MAXN];
int pfact[MAXN];
long long int power(long long int a, int b)
{
	if (b == 0)
	{
		return 1;
	}
	long long int ans = power(a, b/2);
	ans = (ans * ans) % MOD;
	if (b % 2)
	{
		ans = (ans * a) % MOD;
	}
	return ans;
}
void precomp()
{
	fact[0] = 1;
	for (int i = 1; i < MAXN; ++i)
	{
		fact[i] = (fact[i - 1] * i) % MOD;
	}
	inv_fact[MAXN - 1] = power(fact[MAXN - 1], MOD - 2);
	for (int i = MAXN - 2; i >= 0; --i)
	{
		inv_fact[i] = (inv_fact[i + 1] * (i + 1)) % MOD;
	}
}
long long int comb(int n, int r)
{
	if (r > n)
	{
		return 0;
	}
	long long int ans = (fact[n] * inv_fact[r]) % MOD;
	ans = (ans * inv_fact[n - r]) % MOD;
	return ans;
}
int main(int argc, char const *argv[])
{
	precomp();
	int n, m;
	cin>>n>>m;

	ways[1] = 1;
	for (int i = 2; i < MAXN; ++i)
	{
		if (pfact[i] == 0)
		{
			for (int j = i; j < MAXN; j += i)
			{
				pfact[j] = i;
			}
		}
		int cval = i, pcount = 0;
		while (cval % pfact[i] == 0)
		{
			pcount++;
			cval /= pfact[i];
		}
		ways[i] = (ways[cval] * comb(n - 2 + pcount, pcount)) % MOD;
	}
	for (int i = 1; i < MAXN; ++i)
	{
		ways[i] += ways[i - 1];
		if (ways[i] >= MOD)
		{
			ways[i] -= MOD;
		}
	}

	long long int ans = 0;
	for (int i = 1; i <= m; ++i)
	{
		ans = (ans + ways[(m / i)]) % MOD;
	}
	cout<<ans<<"\n";
	return 0;
}

/*  Jai Shree Ram 🚩🚩🚩 */
#include "bits/stdc++.h"
#define ll long long int
#define oo 1000000000000000000
#define forr(i,n) for(int i=0;i<n;i++)
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define all(x) x.begin(),x.end()
#define unique(v) sort(all(v)); v.resize(distance(v.begin(),unique(all(v))))
#define eb emplace_back
#define FF first
#define SS second
#define mem(a,v) memset(a,v,sizeof(a))
#define pb push_back
#define popcount(x) __builtin_popcount(x)

using namespace std;

template<typename T>
ostream &operator<<(ostream &output,const vector<T> &v){
    if(v.empty()) return output;
    for(int i=0;i<v.size()-1;i++) output << v[i] <<" ";
    output << v.back();
    return output;
}
template<typename T>
istream &operator>>(istream &input,vector<T> &v){
    for(auto &i: v) cin >> i;
    return input;
}
const int N = 3005 , mod = 1e9 + 7;
int best[N][N];
int n;
ll dp[N][N];

ll get(int i,int k){
    if( i > n) return 0;
    else if(best[i][k] == n+1) return 1;
    else if(best[i][k] == n+2) return 0;
    else if(dp[i][k]!=-1) return dp[i][k];
    else{
        ll ans = 0;
        ans += get(best[i][k] , k + 1);
        ans %= mod;
        ans += get(best[i][k] , k);
        ans %= mod;
        return dp[i][k] = ans;
    }
}

void __sol(){
    cin >> n;
    vector<ll> v(n); cin >> v;
    forr(i,n+3) forr(j,n+3) best[i][j] = n+2;
    mem(dp,-1);
    for(int k=1;k<=n;k++){
        map<ll,int> m;
        ll sum = 0;
        m[sum] = n + 1;
        for(int j=n;j>=1;j--){
            sum += v[j-1]%k;
            sum %= k;
            if(m.count(sum)) best[j][k] = m[sum];
            m[sum] = j;
        }
    }
    cout << get(1,1)<<"\n";
    return;
}

int main(){
    fastio;
    int tc=1;  // cin >> tc;
    while(tc--) __sol();
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <fstream>
#include <sstream>
#include <iomanip>
#define ll long long

using namespace std;

struct info
{
    ll bit;
    int place;
};

long long MOD = 1000000007;

ll dp[1LL<<17][17];
int N;
ll dist[17][17];

void bfs(){
    queue<info> que;

    que.push({0,0});

    while(!(que.empty())){
        ll bit=que.front().bit;
        int place=que.front().place;
        que.pop();

        for(int i=0; i<N; i++){
            ll tmp=1<<i;
            if(!(bit&tmp)){
                ll nbit=bit|tmp;
                if(dp[nbit][i]==MOD*MOD){
                    que.push({nbit,i});
                }
                dp[nbit][i]=min(dp[nbit][i],dp[bit][place]+dist[place][i]);
            }
        }
    }
}

ll XYZ[17][3];

int main(){
    cin >> N;
    for(int i=0; i<N; i++){
        for(int j=0; j<3; j++){
            cin >> XYZ[i][j];
        }
    }

    for(int i=0; i<N; i++){
        for(int j=0; j<N; j++){
            dist[i][j]=abs(XYZ[j][0]-XYZ[i][0])+abs(XYZ[j][1]-XYZ[i][1])+max(0LL,XYZ[j][2]-XYZ[i][2]);
        }
    }

    for(int i=(1<<N)-1; i>=0; i--){
        for(int j=0; j<N; j++){
            dp[i][j]=MOD*MOD;
        }
    }
    dp[0][0]=0;

    bfs();
    /*
    for(int i=0; i<(1<<N); i++){
        for(int j=0; j<N; j++){
            cout << dp[i][j] << ' ';
        }
        cout << endl;
    }*/
    cout << dp[(1<<N)-1][0] << endl;
}

#include <bits/stdc++.h>

using namespace std;

#define F first
#define S second
#define ll long long
#define pb push_back
#define Mid ((L+R)/2)
#define pi pair<int,int>
#define pii pair<pi,int>
#define deb(x) cout<<#x<<"="<<x<<endl
#define go ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
int n;
ll dp[17][1<<18],a[20],b[20],c[20];
ll Solve(int i,int msk)
{
    if(msk+1==(1<<n) && !i)
        return 0;
    ll &ret=dp[i][msk];
    if(ret+1) return ret;
    ret=1e18;
    for(int j=0;j<n;j++)
        ret=min(ret,Solve(j,msk|(1<<j))+abs(a[i]-a[j])+abs(b[i]-b[j])+max(0LL,c[j]-c[i]));
    return ret;
}
int main()
{
    go;
    cin>>n;
    for(int i=0; i<n; i++)
        cin>>a[i]>>b[i]>>c[i];
    memset(dp,-1,sizeof dp);
    cout<<Solve(0,0);
    return 0;
}


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

int main(){
	int n;
	cin >> n;
	vector<long long> a(n, 0);
	vector<long long> b(n+1, 0);
	vector<long long> c(n+1, 0);
	for(int i = 0; i < n; ++i){
		cin >> a[i];
	}
	long long ans = 0;
	for(int i = 0; i < n; ++i){
		b[i+1] = b[i] + a[i];
		// cout << b[i+1] << " ";
	}
	// cout << endl;
	for(int i = 0; i < n; ++i){
		c[i+1] = b[i+1] + c[i];
		// cout << c[i+1] << " ";
	}
	for(int i = 0; i < n; ++i){
		b[i+1] = max(b[i+1], b[i]);
	}
	// cout << endl;
	for(int i = 0; i < n; ++i){
		ans = max(c[i]+b[i+1], ans);
	}
	cout << ans << endl;
	return 0;
}

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

#define REP(i,m,n) for(int i=(m); i<(int)(n); i++)
#define RREP(i,m,n) for(int i=(int)((n)-1); i>=m; i--)
#define rep(i,n) REP(i,0,n)
#define rrep(i,n) RREP(i,0,n)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define fi first
#define se second
#define debug(...) {cerr<<"[L"<<__LINE__<<"] "; _debug(__VA_ARGS__);}

template<typename T>
string join(const vector<T>&v, string del=", "){ stringstream s;
    for(auto x : v) s << del << x; return s.str().substr(del.size());
}
template<typename T>
ostream& operator<<(ostream& o, const vector<T>&v){
    if(v.size()) o << "[" << join(v) << "]"; return o;
}
template<typename T>
ostream& operator<<(ostream& o, const vector<vector<T> >&vv){
    int l = vv.size();
    if(l){ o<<endl; rep(i,l) o << (i==0 ? "[ " : ",\n  " ) << vv[i] << (i==l-1 ? " ]" : ""); }
    return o;
}
template<typename T1, typename T2>
ostream& operator<<(ostream& o, const pair<T1, T2>& p){
    return o << "(" << p.first << ", " << p.second << ")";
}
inline void _debug(){cerr<<endl;}
template<class First, class... Rest>
void _debug(const First& first, const Rest&... rest){cerr<<first<<" ";_debug(rest...);}

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

const double PI = (1*acos(0.0));
const double EPS = 1e-9;
const int INF = 0x3f3f3f3f;
const ll INFL = 0x3f3f3f3f3f3f3f3fLL;
const ll mod = 1e9 + 7;

inline void finput(string filename) {
    freopen(filename.c_str(), "r", stdin);
}

int main(){
    ios_base::sync_with_stdio(0);
    // finput("./input");
    int n; cin >> n;
    vl a(n);
    rep(i,n) cin >> a[i];

    ll ans = 0;
    ll sa_max = 0;
    ll sa = 0;
    ll pos = 0;
    rep(i,n){
        sa += a[i];
        sa_max = max(sa_max, sa);
        ans = max(ans, pos+sa_max);
        pos += sa;
    }
    cout << ans << endl;
    return 0;
}


/**
 *    author:  Ujjwal Tripathi
 *    created: 29.05.2021 16:07:47
**/

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

#define ar array<ll, 2>
#define ll long long
#define in insert
#define pb push_back
#define pii pair<ll,ll>
#define vt vector
#define P_Q(x) priority_queue<x>
#define p_q(x) priority_queue<x, vector<x>, greater<x>>
#define For(i, n) for(ll i = 0; i < n; i++)
#define Rev(i, n) for(ll i = n-1; i >= 0; i--)
#define FOR(i, n) for(ll i = 1; i <= n; i++)
#define REV(i, n) for(ll i = n; i >= 1; i--)
#define Rep(i,a,b) for(ll i = a; i <= b; i++)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int) (x).size()

// Maximum priority queue
template <typename T>
using mxpq = priority_queue<T>;

// Minimum priority queue
template <typename T>
using mnpq = priority_queue<T, vt<T>, greater<T>>;

// x | (x + 1) sets lowest unset bit of x
// x & (x - 1) unsets lowest set bit of x

const ll mod = 1e9L + 7;
const ll INF = 1e18L + 5;
const ll N = 2e5 + 1;

ll timer=0;
// remember to read no of test_cases
void solve()
{
  ll n;
  cin>>n;
   //vt<ll> a(n+1,0);
   vt<ll> g[N+1];
   vt<ll> depth[N];
   vt<ll> intime(N);
   vt<ll> outtime(N);
   for(int i=2;i<=n;i++)
   {
      ll x;
      cin>>x;
      g[x].pb(i);
   }
   function<void(int i,int dep)> dfs=[&](int i,int dep=0)
   {
      intime[i]=timer++;
      depth[dep].pb(intime[i]);
      for(auto x:g[i])
      {
         dfs(x,dep+1);
      }
      outtime[i]=timer;
   };
   dfs(1,0);
   ll q;
   cin>>q;
   for(int i=0;i<q;i++)
   {
     ll x,y;
     cin>>x>>y;
     cout<<lower_bound(all(depth[y]),outtime[x])-lower_bound(all(depth[y]),intime[x])<<endl;
   }
   return;
}

int main()
{
  ios::sync_with_stdio(0);  cin.tie(0);
  ll t = 1;
  // cin >> t;
  FOR(tt, t) solve();
  return 0;
}

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

int main(){
    int n; cin >> n;
    double x0, y0, xn, yn;
    cin >> x0 >> y0;
    cin >> xn >> yn;

    double cx=(x0+xn)/2, cy=(y0+yn)/2;

    double move_x=x0-cx, move_y=y0-cy;

    // 角度を求める
    double sita=2*M_PI/n;

    // 回転する
    double rotation_x = move_x*cos(sita) - move_y*sin(sita);
    double rotation_y = move_x*sin(sita) + move_y*cos(sita);

    // 平行移動
    rotation_x += cx, rotation_y += cy;

    printf("%.10f %.10f\n", rotation_x, rotation_y);
}


#include <iostream>

using namespace std;

int main()
{
  char x ,y,z;
  cin>>x>>y>>z;

  if(x>=65&&x<=90)
  {
      if(y>=65&&y<=90)
      {
          if(z>=65&&z<=90)
          {
              if(x==y && y==z)
    cout<<"Won\n";
  else
      cout<<"Lost\n";

          }

      }
  }

    return 0;
}

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

int main() {
    char c1, c2, c3;
    cin >> c1 >> c2 >> c3;
    string ans = "Lost";
    if (c1 == c2) {
        if (c2 == c3) {
            ans = "Won";
        }
    }
    cout << ans << endl;
    return 0;
}


// Its just opening , middle game and end game
#include<bits/stdc++.h>
using namespace std ;
#define F first
#define S second

int MOD = 998244353 ;
int mod = 1e9 + 7 ;
int inf = 1e9 ;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

vector<int> a(2005) ;
vector<int> all_divisors ;
signed main()
{
    // OPENING

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

    int n ;
    cin >> n ;
    int mn = inf ;
    for( int i = 0 ; i < n ; i++ )
        cin >> a[i] , mn = min( mn , a[i] ) ; ;
 //   sort( a , a + n ) ;
    // MIDDLE GAME

    for( int i = 0 ; i < n ; i++ ){
        for( int j = 2 ; j*j <= a[i] ; j++ ){
            if( a[i] % j == 0 ){
                all_divisors.push_back(j) ;
                all_divisors.push_back(a[i]/j) ;
            }
        }
    }
    sort( all_divisors.begin() , all_divisors.end() ) ;
    auto it = unique( all_divisors.begin() , all_divisors.end() ) ;
    all_divisors.resize( distance( all_divisors.begin() , it ) ) ;
    all_divisors.emplace_back(1) ;

    // END GAME

    int ans = 0 , one = 1 ;
    for( auto divisor : all_divisors ){
        int g = 0 ;
        if( divisor >= mn )
            continue ;
        for( int i = 0 ; i < n ; i++ ){
            if( a[i]%divisor )
                continue ;
            g = __gcd( g , a[i] ) ;
            if( g == divisor ){
                ans++ ;
                break ;
            }
        }
    }
    cout << ans + 1 ;

    cerr << "Time elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " sec \n";
}

#include <bits/stdc++.h>
//#include <atcoder/lazysegtree>
//#include <atcoder/segtree>
#define overload4(_1, _2, _3, _4, name, ...) name
#define rep1(n) for(ll i = 0; i < (n); ++i)
#define rep2(i, n) for(ll i = 0; i < (n); ++i)
#define rep3(i, a, b) for(ll i = (a); i < (b); ++i)
#define rep4(i, a, b, c) for(ll i = (a); i < (b); i += (c))
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)

#define rrep(i,a,n) for (int i=n-1;i>=a;i--)
#define ALL(x) x.begin(),x.end()

#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pause "read -p 'Press Enter to continue...' var"

using namespace std;
//using namespace atcoder;

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

/*
テスト通りますように
　　●
　　/⌒ヽ
　 ｜ 　|/⌒ヽ(ヽ
　 (｀　∥ー⌒) |
　|￣||￣￣￣￣￣|
　|―||―――――|
　|　Ｕ　　　　　|
　|￣￣￣￣￣￣￣|
　|＿＿＿＿＿＿＿|
　　|―――――|
　　|―――――|
ｗｗＷｗｗＷｗＷＷｗ
*/

typedef long long ll;
typedef vector<ll> vll;
typedef pair<ll,ll> pll;
typedef vector<pll> vpll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}

const ll INF = numeric_limits<ll>::max()/4;
const ll MAX = 100005;
const int MOD = 1000000007;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

int main(){
  ll n;
  cin >> n;
  double L[2005],R[2005];
  rep(n){
    ll t;
    double l,r;
    cin >> t >> l >> r;
    if(t == 1){
      L[i] = l;
      R[i] = r;
    }
    if(t == 2){
      L[i] = l;
      R[i] = r-0.5;
    }
    if(t == 3){
      L[i] = l+0.5;
      R[i] = r;
    }
    if(t == 4){
      L[i] = l+0.5;
      R[i] = r-0.5;
    }
  }
  ll ans = 0;
  rep(n){
    rep(j,i+1,n){
      if(L[i] <= R[j] && R[i] >= L[j]) ans++;
    }
  }
  cout << ans << endl;
  return 0;
}


#include<iostream>
using namespace std;

void solve(){
	int A, B;
	cin >> A >> B;
	int X = int((A+B) / 2);
	int Y = A - X;
	cout << X << " " << Y << endl;
}

int main(){
	solve();
	cin.get();
}

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

#define endl "\n"
#define LL long long
#define ULL unsigned long long
const LL        INF = 1LL<<59;
const double    PI = 2.0 * acos(0.0);

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
typedef vector<int> vi;
typedef vector<LL> vl;
typedef vector<pii> vii;
typedef vector<pll> vll;

//// Including Policy Based DS
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//
/////cout<<*X.find_by_order(1)<<endl;
/////cout<<X.order_of_key(-5)<<endl;
//
//typedef tree<
//int,
//null_type,
//less< int >,
//rb_tree_tag,
//tree_order_statistics_node_update>
//ordered_set;

#define READ        freopen("in.txt","r",stdin)
#define WRITE       freopen("out.txt","w",stdout)
#define BOOST		ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define sf 			scanf
#define pf 			printf
#define F  			first
#define S  			second
#define pb 			push_back
#define NL			pf("\n")
#define ALL(v)		v.begin(),v.end()
#define si(x)		sf("%d",&x);
#define sii(x,y)	sf("%d%d",&x,&y);
#define siii(x,y,z)	sf("%d%d%d",&x,&y,&z);
#define sl(x)		sf("%lld",&x);
#define sll(x,y)	sf("%lld%lld",&x,&y);
#define slll(x,y,z)	sf("%lld%lld%lld",&x,&y,&z);
#define sqr(x)      ( (x) * (x) )
#define mem(x,y) 	memset(x,y,sizeof x )
#define ch 			printf("Came Here!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n")
#define deb(x)      cerr<<#x<<" :: "<<x<<"\n"
#define SV(v)		sort(v.begin(),v.end())
#define SVR(v,cmp)	sort(v.begin(),v.end(),cmp)
#define FOR(i,n)	for(int i=0;i<n;i++)
#define cnd			tree[idx]
#define lc			tree[idx*2]
#define rc			tree[idx*2+1]
#define lnd 		(2*idx),(b),( (b+e) /2 )
#define rnd 		((2*idx)+1),(((b+e)/2)+1),(e)

// graph direction array [4]
int X[4]={0,0,-1,1};
int Y[4]={1,-1,0,0};
// graph direction array [8]
//int X[8]={0,0,1,-1,-1,1,1,-1};
//int Y[8]={1,-1,0,0,-1,-1,1,1};
// Bishop direction array [8]
//int BX[8]={0,0,1,-1,-1,1,1,-1};
//int BY[8]={1,-1,0,0,-1,-1,1,1};
// Knight Direction Array
//int KX[8] = {1,1,2,2,-1,-1,-2,-2};
//int KY[8] = {2,-2,1,-1,2,-2,1,-1};

// Bit Operations
#define popcountL   __builtin_popcountll
#define popcount    __builtin_popcount
inline bool checkBit(int N,int pos){return (bool)(N & (1<<pos));} // check bit
inline int setBit(int N,int pos){ return N=N | (1<<pos);}// set bit
inline int unsetBit(int N,int pos){	return N=( N & ( ~( 1<<pos ) ) );}// unset bit
inline int toggleBit(int N,int pos){ return N = ( N ^(1<<pos) ); }// toggle bit

inline LL modMul(LL a, LL b,LL mod){
    LL ans = 0;
    a = a % mod;
    while (b > 0){
        if ( b % 2 )ans = (ans%mod+ a%mod) % mod;
        a = (a%mod * 2%mod) % mod;
        b /= 2;
    }
    return ans % mod;
}
inline LL powerMod(LL a,LL b,LL mod){
	if(b==0)return 1LL%mod;
	LL x=powerMod( a,b/2,mod );
	x = ( x%mod * x%mod ) % mod;
	if( b%2==1 )x = ( x%mod * a%mod ) % mod;
	return x%mod;
}
inline LL lcm( LL a,LL b ){
	LL g = __gcd(a,b);
	return ((a/g) * b);
}

//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
const int mx = 1e5+5;

int main(){
//    BOOST;
    #ifdef MujahidPC
        double start = clock();
//        READ;
//        WRITE;
    #endif
    int t,n,m,q;

    int a,b;
    cin>>a>>b;
    int x = (a+b)/2;
    int y = a-x;
    cout<<x<<" "<<y<<endl;

    #ifdef MujahidPC
        double tim = (clock() - start)/CLOCKS_PER_SEC;
        cerr<<"Running Time : "<<tim<<" \n";
    #endif
    return 0;
}


//競技プログラミング用のテンプレート
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <string>
#include <math.h>
#include <stack>
#include <limits>

#define rep(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define repr(i, n) for(int i = n - 1; i >= 0; i--)

using namespace std;
//エイリアス
using ll = long long int;
using p = pair<int, int>;
using pl = pair<ll, ll>;
using v = vector<int>;
using vd = vector<double>;
using vs = vector<string>;
using vl = vector<ll>;

//定数
const int intmax = numeric_limits<int>::max();
const ll llmax = numeric_limits<ll>::max();
const ll mod = 1e9 + 7;
const double pi = M_PI;

int main()
{
  string n;
  cin >> n;
  int k = n.size();
  int mincnt = k + 1;
  rep(i, pow(2, k)) {
    string result;
    for(int tmp = 1, index = 0; index < k; tmp = tmp << 1, index++) {
      if (i & tmp) {
        result.push_back(n[index]);
      }
    }
    if (result.size() == 0) continue;
    ll tmp = stoll(result);
    if (tmp % 3 == 0) {
      int cnt = k - result.size();
      mincnt = min(mincnt, cnt);
    }
  }
  mincnt = mincnt == k + 1 ? -1 : mincnt;
  cout << mincnt << endl;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL n, rem[20], len, num, ans;
bool fla[20], fla1;
vector<int> v;
inline LL read() {
	LL x = 0;
	bool f = 0;
	char c = getchar();
	while (c < '0' || c > '9') {
		if (c == '-')
			f = 1;
		c = getchar();
	}
	while (c <= '9' && c >= '0') {
		x = (x << 1) + (x << 3) + c - '0';
		c = getchar();
	}
	if (f)
		return ~(x - 1);
	return x;
}
inline void work(int x, int m) {
	if (fla1)
		return;
	if (v.size() > m || v.size() + (len - x + 1) < m)
		return;
	if (x == len + 1) {
		int sum = 0;
		for (register int i = 1; i <= len; i++)
			if (!fla[i])
				sum += rem[i];
		if (sum % 3 == 0 && sum != 0) {
			printf("%d\n", m);
			fla1 = 1;
			return;
		}
	}
	v.push_back(x);
	fla[x] = 1;
	work(x + 1, m);
	v.pop_back();
	fla[x] = 0;
	work(x + 1, m);
}
int main() {
//	freopen ("test.in", "r", stdin);
//	freopen ("test.out", "r", stdin);
	n = read();
	if (n % 3 == 0) {
		puts("0");
		return 0;
	}
	LL n1 = n;
	while (n1) {
		rem[++len] = n1 % 10;
		n1 /= 10;
	}
	for (register int i = 1; i <= len; i++) {
		work(1, i);
		if (fla1)
			return 0;
	}
	puts("-1");
	return 0;
}


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

int main() {

    double r, x, y, l2;
    std::cin >> r >> x >> y;

    l2 = (x * x) + (y * y);

    double l;
    l = pow(l2, 0.5);

    double a = l / r;
    int inta = (int)a;

    if (a < 1) {
        std::cout << 2 << '\n';
    }
    else if (a == (double)inta) {
        std::cout << inta << '\n';
    } else {
        std::cout << inta + 1 << '\n';
}

}

#include<bits/stdc++.h>

using namespace std;

#define  GODSPEED       ios:: sync_with_stdio(0);cin.tie(0);cout.tie(0);cout<<fixed;cout<<setprecision(15);
#define  f              first
#define  s              second
#define  newl           cout<<"\n";
#define  pb             push_back
#define  mset(a,x)      memset(a,x,sizeof(a))
#define  debv(a)        for(auto it: a)cout<<it<<" ";newl;
#define  deb1(a)        cout<<a<<"\n";
#define  deb2(a,b)      cout<<a<<" "<<b<<"\n";
#define  deb3(a,b,c)    cout<<a<<" "<<b<<" "<<c<<"\n";
#define  deb4(a,b,c,d)  cout<<a<<" "<<b<<" "<<c<<" "<<d<<"\n";
#define  uniq(a)        a.resize(unique(a.begin(), a.end()) - a.begin());
#define  all(a)         a.begin(),a.end()

typedef  long long             ll;
typedef  long double           ld;
typedef  pair<ll, ll>           pll;
typedef  vector<ll>            vll;
typedef  vector<pll>           vpll;

const ll   N     =  2e5 + 5;
const ll   mod   =  998244353;
const ll   INF   =  0x7f7f7f7f7f7f7f7f;
const int  INFi  =  0x7f7f7f7f;
const ll   LEVEL =  log2(N) + 1;

ll a, b;

void solve() {
	cin >> a >> b;
	ld ans = (a - b) * 100.0 / a;
	deb1(ans)
}

int main() {
	GODSPEED;
	int test = 1;
	//cin >> test;
	for (int i = 1; i <= test; i++) {
		solve();
	}
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
//# define int int64_t
#define pb push_back
//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); //used to take input a line after taking input a variable
//#define first fi
//#define second se
#define gok ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const ll mod = 1e9 + 7,mxn=5e5 + 5;

signed main(){
    gok

   int n;
   cin>>n;
   string s;
   cin>>s;

   stack<char> st;
   int an=0;
   for(int i=0;i<=n;i++)
   {
       if(st.size()<3&&i<n)
       {
           st.push(s[i]);
       }
       else if(st.size()>=3)
       {
           char c = st.top();
           st.pop();
           char b = st.top();
           st.pop();
           char a = st.top();
           st.pop();
           //cout<<a<<" "<<b<<" "<<c<<endl;
           if(a=='f'&&b=='o'&&c=='x')
            an+=3;
           else
           {
               st.push(a);
               st.push(b);
               st.push(c);
           }
           if(i<n)
            st.push(s[i]);
       }
   }
   cout<<n-an;

      return 0;
    }

/**    Fucc 😕
	-  doanphuc : /
	* author: doanphuc. *
**/
#include <bits/stdc++.h>
#define TASK ""
#define inpf freopen(TASK".inp", "r", stdin)
#define outf freopen(TASK".out", "w", stdout)
template<class T> T gcd(T a, T b) { T r; while (b != 0) { r = a % b; a = b; b = r; } return a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define FOR(i,x,y) for(int i = x; i <= y ; i ++)
#define FORT(i,x,y) for(int i = x; i < y ; i ++)
#define FORD(i,x,y) for(int i = x; i >= y ; i --)
#define clean(x) memset(x, 0, sizeof(x))
#define F first
#define S second
#define maxn 100005
#define MOD 1000000007
#define pii pair<int,int>
#define ll long long
#define db double
#define mp make_pair
#define pb push_back
#define yes cout << "YES" << "\n"
#define no  cout << "NO" << "\n"
#define ct(x) cout << x << endl
#define bit(p,x) ((x>>p) & 1)
#define all(x) (x).begin(), (x).end()
#define resized(x) (x).resize(unique(all(x)) - (x).begin())
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
ll m, n, k, t, dem = 0, ans = 0 ;
string s ;
int main()
{
    cin >> n >> s ;
    for(int i = 0; i < n; i ++)
    {
        if(s[i] == 'f' && s[i + 1] == 'o' && s[i + 2] == 'x' && i + 1 < n && i + 2 < n)
        {
            s.erase(i, 3) ;
            i = max(-1, i - 3) ;
//            i = i - 2 ;
        }
    }
//    cout << s << endl;
    cout << s.length() ;

}


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

using namespace std;
using ll = long long;

struct Input{
    int n;
    struct Data
    {
        int x, y, r;
    };
    Input(){}

    vector<Data> data;

    void in(){
        cin >> n;
        data.resize(n);
        for (int i = 0; i < n; i++) cin >> data[i].x >> data[i].y >> data[i].r;
    }
};

//ミョ(-ω- ?)
int main() {
    Input input;
    input.in();

    rep(i,input.n){
        cout << input.data[i].x << " " << input.data[i].y << " " << input.data[i].x + 1<< " "
             << input.data[i].y + 1 << "\n";
    }
    return 0;
}

//雪花飄飄北風嘯嘯
//天地一片蒼茫

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ll long long
#define ii pair<ll,ll>
#define iii pair<ii,ll>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << " is " << x << endl

#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define lb lower_bound
#define up upper_bound

#define rep(x,start,end) for(auto x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()

#define indexed_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
//change less to less_equal for non distinct pbds, but erase will bug

mt19937 rng(chrono::system_clock::now().time_since_epoch().count());

int n;

struct dim{
	int u,d,l,r;
};

dim rect[205];
ii ori[205];
int r[205];

bool grid[10005][10005];

double sq(double i){return i*i;}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin.exceptions(ios::badbit | ios::failbit);

	//freopen("in/0003.txt","r",stdin);

	cin>>n;

	int a,b,c;
	rep(x,0,n){
		cin>>a>>b>>c;

		ori[x]=ii(a,b);
		r[x]=c;
		grid[a][b]=true;
		rect[x]={a,a,b,b};
	}

	ll stime=std::chrono::system_clock::now().time_since_epoch().count();

	while (std::chrono::system_clock::now().time_since_epoch().count()-stime<4.5e9){
		int idx=rng()%n;
		int dir=rng()%4;

		bool can=true;
		if (dir==0){
			if (rect[idx].u==0){can=false;continue;}
			rep(x,rect[idx].l,rect[idx].r+1) if (grid[rect[idx].u-1][x]) can=false;
		}
		if (dir==1){
			if (rect[idx].d==9999){can=false;continue;}
			rep(x,rect[idx].l,rect[idx].r+1) if (grid[rect[idx].d+1][x]) can=false;
		}
		if (dir==2){
			if (rect[idx].l==0){can=false;continue;}
			rep(x,rect[idx].u,rect[idx].d+1) if (grid[x][rect[idx].l-1]) can=false;
		}
		if (dir==3){
			if (rect[idx].r==9999){can=false;continue;}
			rep(x,rect[idx].u,rect[idx].d+1) if (grid[x][rect[idx].r+1]) can=false;
		}

		if (!can) continue;

		int ai=(rect[idx].d-rect[idx].u+1)*(rect[idx].r-rect[idx].l+1);
		int af=ai+(dir<2?rect[idx].r-rect[idx].l+1:rect[idx].d-rect[idx].u+1);

		double ci=1-sq(1-(double)min(ai,r[idx])/max(ai,r[idx]));
		double cf=1-sq(1-(double)min(af,r[idx])/max(af,r[idx]));

		//cout<<ci<<" "<<cf<<endl;

		if (cf>ci){
			if (dir==0){
				rect[idx].u--;
				rep(x,rect[idx].l,rect[idx].r+1) grid[rect[idx].u][x]=true;
			}
			if (dir==1){
				rect[idx].d++;
				rep(x,rect[idx].l,rect[idx].r+1) grid[rect[idx].d][x]=true;
			}
			if (dir==2){
				rect[idx].l--;
				rep(x,rect[idx].u,rect[idx].d+1) grid[x][rect[idx].l]=true;
			}
			if (dir==3){
				rect[idx].r++;
				rep(x,rect[idx].u,rect[idx].d+1) grid[x][rect[idx].r]=true;
			}
		}
	}

	rep(x,0,n){
		cout<<rect[x].u<<" "<<rect[x].l<<" "<<rect[x].d<<" "<<rect[x].r<<endl;
	}
}


#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <string.h>
#include <vector>
#include <queue>
#include <cmath>
#include <complex>
#include <functional>
#include <numeric>
#include <iomanip>
#include <cassert>
#include <random>
#include <chrono>

/* #include <atcoder/all> */
/* using namespace atcoder; */
using namespace std;

void debug_out(){ cout << "\n"; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cout << H << " ";
  debug_out(T...);
}
#ifdef _DEBUG
#define debug(...) debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

#define SPBR(w, n) std::cout<<(w + 1 == n ? '\n' : ' ');
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
#define ALL(i) (i).begin(), (i).end()
#define FOR(i, a, n) for(int i=(a);i<(n);++i)
#define RFOR(i, a, n) for(int i=(n)-1;i>=(a);--i)
#define REP(i, n) for(int i=0;i<int(n);++i)
#define RREP(i, n) for(int i=int(n)-1;i>=0;--i)
#define IN(a, x, b) (a<=x && x<b)
#define OUT(a, x, b) (x<a || b<=x)
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

#define int ll
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const ll MOD = 1000000007;
/* const ll MOD = 998244353; */
const ll INF = 1ll<<60;
const double PI = acos(-1);

struct INIT { INIT(){
  cin.tie(0); ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
}}INIT;

struct Edge {
  int to, t, k;
  Edge(int to, int t, int k): to(to), t(t), k(k) {}
};

signed main() {
  int N, M, s, g;
  cin >> N >> M >> s >> g;
  s--; g--;

  vector<vector<Edge>> graph(N);
  REP(i, M){
    int u, v, t, k;
    cin >> u >> v >> t >> k;
    u--; v--;
    graph[u].emplace_back(Edge(v, t, k));
    graph[v].emplace_back(Edge(u, t, k));
  }

  vector<int> dist(N, INF);
  dist[s] = 0;

  using P = pair<int, int>;
  priority_queue<P, vector<P>, greater<P>> q;
  q.emplace(0, s);

  while(!q.empty()){
    auto [d, v] = q.top(); q.pop();
    if(dist[v] < d) continue;

    for(auto u : graph[v]){
      int nd = (d+u.k-1)/u.k*u.k + u.t;
      if(chmin(dist[u.to], nd)){
        q.emplace(nd, u.to);
      }
    }
  }

  if(dist[g] == INF) dist[g] = -1;
  cout << dist[g] << "\n";

  return 0;
}

#include <bits/stdc++.h>
#define INF 10000000000000
using namespace std;
typedef long long ll;
const int maxn=3e5+5;
ll n,m,s,t;
struct op{
    ll v;
    ll w,k;
    ll next;
}e[maxn*2];
ll head[maxn*2],cnt;
void add(int u,int v,ll w,ll k){
    e[++cnt]={v,w,k,head[u]};
    head[u]=cnt;
}
ll dis[maxn];
ll vis[maxn];
struct node{
    ll w;
    ll now;
    bool operator <(const node &x)const{
        return w>x.w;
    }
};
priority_queue<node>q;
void dijkstra(){
    memset(dis,127, sizeof(dis));
    dis[s]=0;
    q.push((node){0,s});
    while(!q.empty()){
        node x=q.top();
        q.pop();
        ll u=x.now;
        if(vis[u])continue;
        vis[u]=1;
        for (int i = head[u]; i ; i=e[i].next) {
            ll v=e[i].v;
            if((dis[v]>dis[u]+e[i].w+e[i].k-dis[u]%e[i].k&&dis[u]%e[i].k!=0)||(dis[v]>dis[u]+e[i].w&&dis[u]%e[i].k==0))
            {
                if(dis[u]%e[i].k!=0)
                    dis[v]=dis[u]+e[i].w+e[i].k-dis[u]%e[i].k;
                else dis[v]=dis[u]+e[i].w;
                q.push({dis[v],v});
            }
        }
    }
}
int main()
{
    scanf("%lld %lld %lld %lld",&n,&m,&s,&t);
    ll a,b;
    ll c,d;
    for (int i = 1; i <= m; ++i) {
        scanf("%lld %lld %lld %lld",&a,&b,&c,&d);
        add(a,b,c,d);
        add(b,a,c,d);
    }
    dijkstra();
    printf("%lld\n",!vis[t]?-1:dis[t]);
    return 0;
}


/*
{
######################
#       Author       #
#        Gary        #
#        2020        #
######################
*/
//#pragma GCC target ("avx2")
//#pragma GCC optimization ("O3")
//#pragma GCC optimization ("unroll-loops")
#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define rb(a,b,c) for(int a=b;a<=c;++a)
#define rl(a,b,c) for(int a=b;a>=c;--a)
#define LL long long
#define IT iterator
#define PB push_back
#define II(a,b) make_pair(a,b)
#define FIR first
#define SEC second
#define FREO freopen("check.out","w",stdout)
#define rep(a,b) for(int a=0;a<b;++a)
#define SRAND mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define random(a) rng()%a
#define ALL(a) a.begin(),a.end()
#define POB pop_back
#define ff fflush(stdout)
#define fastio ios::sync_with_stdio(false)
#define check_min(a,b) a=min(a,b)
#define check_max(a,b) a=max(a,b)
using namespace std;
const int INF=0x3f3f3f3f;
typedef pair<int,int> mp;
/*}
*/
priority_queue<int,vector<int>,greater<int> > heap;
int h[200000+20],w[200000+20];
int dp1[200000+20],dp2[200000+20];
int main(){
	fastio;
	int n,m;
	cin>>n>>m;
	rb(i,1,n) cin>>h[i];
	rb(i,1,m) cin>>w[i],heap.push(w[i]);
	sort(h+1,h+1+n);
	rb(i,2,n){
		dp1[i]=dp1[i-2]+h[i]-h[i-1];
	}
	rl(i,n-1,1){
		dp2[i]=dp2[i+2]+h[i+1]-h[i];
	}
	LL best=1e18;
	rb(i,0,n){
		while(!heap.empty()&&(i==n||heap.top()<=h[i+1])){
			LL rest=0;
			if(i&1){
				rest+=dp1[i-1];
				rest+=heap.top()-h[i];
				rest+=dp2[i+1];
			}
			else{
				rest+=dp1[i];
				rest+=h[i+1]-heap.top();
				rest+=dp2[i+2];
			}
			check_min(best,rest);
			heap.pop();
		}
	}
	cout<<best<<endl;
	return 0;
}
/** 程序框架：
  *
  *
  *
  *
  **/

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++)
#define all(v) v.begin(), v.end()
#define sz(v) v.size()
#define INF 100000000000000 //10^14
template <typename T>
bool chmax(T &a, const T& b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
bool chmin(T &a, const T& b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
const int mod = 1000000007;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x=0):x((x%mod+mod)%mod){}
  mint operator-() const { return mint(-x);}
  mint& operator+=(const mint a) {
  if ((x += a.x) >= mod) x -= mod;
  return *this;
  }
  mint& operator-=(const mint a) {
  if ((x += mod-a.x) >= mod) x -= mod;
  return *this;
  }
  mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
  mint operator+(const mint a) const { return mint(*this) += a;}
  mint operator-(const mint a) const { return mint(*this) -= a;}
  mint operator*(const mint a) const { return mint(*this) *= a;}
  mint pow(ll t) const {
  if (!t) return 1;
  mint a = pow(t>>1);
  a *= a;
  if (t&1) a *= *this;
  return a;
  }
  // for prime mod
  mint inv() const { return pow(mod-2);}
  mint& operator/=(const mint a) { return *this *= a.inv();}
  mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, const mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}

mint mpow(mint x, ll y){
  if(y == 0){
    return 1;
  }
  if(y % 2 == 0){
    mint a = mpow(x, y / 2);
    return a * a;
  }
  else{
    mint a = mpow(x, y / 2);
    return a * a * x;
  }
}

int main()
{
    ll H, W;
    cin >> H >> W;
    vector<string> S(H);
    rep(i, H){
      cin >> S.at(i);
    }
    mint ans = 0;
    vector<vector<ll>> clut_r(H, vector<ll>(1, -1));
    rep(i, H){
      rep(j, W){
        if(S.at(i).at(j) == '#'){
          clut_r.at(i).push_back(j);
        }
      }
      clut_r.at(i).push_back(W);
    }
    vector<vector<ll>> clut_c(W, vector<ll>(1, -1));
    rep(i, W){
      rep(j, H){
        if(S.at(j).at(i) == '#'){
          clut_c.at(i).push_back(j);
        }
      }
      clut_c.at(i).push_back(H);
    }
    vector<vector<ll>> space(H, vector<ll>(W, 0)); //各マスを照らす照明を置けるマスの数
    rep(i, H){
      ll p = 0;
      rep(j, W){
        ll left = clut_r.at(i).at(p);
        ll right = clut_r.at(i).at(p+1);
        if(S.at(i).at(j) == '#'){
          p++;
          continue;
        }
        space.at(i).at(j) += right-left-1;
      }
    }
    rep(i, W){
      ll p = 0;
      rep(j, H){
        ll left = clut_c.at(i).at(p);
        ll right = clut_c.at(i).at(p+1);
        if(S.at(j).at(i) == '#'){
          p++;
          continue;
        }
        space.at(j).at(i) += right-left-2;
      }
    }
    ll K = 0;
    rep(i, H){
      rep(j, W){
        if(S.at(i).at(j) == '.'){
          K++;
        }
      }
    }
    rep(i, H){
      rep(j, W){
        if(S.at(i).at(j) == '.'){
          ans += mpow(2, K) - mpow(2, K-space.at(i).at(j)); //全体 - (i,j)を照らさない置き方
        }
      }
    }
    cout << ans << endl;
}


#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("fast-math")
#pragma GCC optimize("trapv")
#pragma GCC target("sse4")

#include <bits/stdc++.h>

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

using namespace std;
using namespace __gnu_pbds;

#define deb(x) cout << #x << " is " << x << "\n"
#define int long long
#define MOD 1000000007LL
#define mod(x) (x % MOD + MOD) % MOD
#define PI acos(-1)

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

template <typename T>
using max_heap = priority_queue<T>;

template <class T>
using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;

template <typename... T>
void read(T &... args) {
	((cin >> args), ...);
}

template <typename... T>
void write(T &&... args) {
	((cout << args), ...);
}

template <typename T>
void readContainer(T &t) {
	for (auto &e : t) read(e);
}

template <typename T>
void writeContainer(T &t) {
	for (const auto &e : t) write(e, " ");

	write("\n");
}

void solve(int tc) {
	int H, W, M;
	read(H, W, M);
	vector<int> X(M), Y(M);
	vector<int> hLimit(H + 1, W), wLimit(W + 1, H);

	for (int i = 0; i < M; i++) {
		read(X[i], Y[i]);
		hLimit[X[i]] = min(hLimit[X[i]], Y[i] - 1);
		wLimit[Y[i]] = min(wLimit[Y[i]], X[i] - 1);
	}

	int ans = 0;

	for (int i = 1; i <= hLimit[1]; i++) {
		ans += wLimit[i];
	}

	vector<pair<int, int>> arr;

	for (int i = 2; i <= wLimit[1]; i++) {
		arr.push_back({hLimit[i], i});
	}

	sort(arr.begin(), arr.end());
	ordered_set<int> st;
	int r = 0;

	for (const auto &ele : arr) {
		while (r < ele.first && r < hLimit[1]) {
			st.insert(wLimit[++r]);
		}

		ans += (hLimit[ele.second] - st.size() + st.order_of_key(ele.second - 1));
	}

	write(ans);
}

signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
// #ifndef ONLINE_JUDGE
// 	freopen("input.txt", "r", stdin);
// 	freopen("output.txt", "w", stdout);
// #endif
	int tc = 1;
	// read(tc);

	for (int curr = 1; curr <= tc; curr++) solve(curr);

	return 0;
}

#include <bits/stdc++.h>

using namespace std;

const int N = 3e5 + 123;
const int mod = 998244353;

#define int long long

int t[N * 4];

void upd(int v, int tl, int tr, int pos, int val) {
	if(tl == tr) {
		t[v] += val;
		t[v] = max(t[v], 0LL);
		return;
	}
	int tm = (tl + tr) / 2;

	if(pos <= tm)
		upd(v + v, tl, tm, pos, val);
	else upd(v + v + 1, tm + 1, tr, pos ,val);
	t[v] = t[v + v]+ t[v + v + 1];
}

int get(int v, int tl, int tr, int l, int r) {
	if(l > r) return 0;
	if (l == tl && r == tr)
		return t[v];
	int tm = (tl + tr) / 2;
	return (get (v*2, tl, tm, l, min(r,tm))
		+ get (v*2+1, tm+1, tr, max(l,tm+1), r));
}

vector <int> v[N];
int n, m;
 main () {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	cin >> n >> m;
	int q;
	cin >> q;
	for(int i = 1; i <= q; i++) {
		int x, y;
		cin >> x >> y;
		v[y].push_back(x);
	}
	for(int i = 1; i <= n; i++) {
		upd(1, 1, n, i, 1);
	}
	long long ans = n * 1LL*m;

	v[1].push_back(n + 1);
	sort(v[1].begin(), v[1].end());
	for(int j = v[1][0]; j <= n; j++){
		upd(1, 1, n, j, -1);
		ans--;
	}
	for(int i = 2; i <= m; i++) {
		v[i].push_back(n + 1);
		v[i].push_back(0);
		sort(v[i].begin(), v[i].end());
		ans -= v[i].size() - 2;
		int l = 1, r = v[i][1] - 1;

		if(get(1, 1, n, l, r) == r - l + 1 or get(1, 1, n, 1, 1) == 1){
		}else {
			ans -= -get(1, 1, n, l, r) + r - l + 1;
		}
		for(int j = 1; j < v[i].size() - 1; j++) {
			int st = v[i][j] + 1;
			int ed = v[i][j + 1] - 1;
			ans -= max(ed - st + 1 - get(1, 1, n, st, ed), 0LL);
		}
		for(int j = 1; j < v[i].size() - 1; j++) {
			if(v[i][j] >= 1 && v[i][j] <= n){
			upd(1, 1, n, v[i][j], -1);
			}
		}

	}
	cout << ans;
}
//N?