#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long LL;

int main() {
  vector<int> A(3); cin >> A[0] >> A[1] >> A[2];
  int K; cin >> K;
  sort(A.begin(),A.end());
  for (int i = 0; i < K; ++i) A[2] *= 2;
  int sum = A[0] + A[1] + A[2];
  cout << sum << endl;
}

#include<iostream>
#include<string>
using namespace std;
int main(void){
    string str;
    int q;
    cin >> str;
    cin >> q;
    for (int i=0;i<q;i++){
        string c;
        cin >> c;
        if (c=="print"){
            int a,b;
            cin >> a >> b;
            for (int i=a;i<=b;i++)
                cout << str[i];
            cout << endl;
        }
        if (c=="reverse"){
            int a,b;
            cin >> a >> b;
            char t;
            for (int i=0;i<=(b-a)/2;i++){
                t=str[a+i];
                str[a+i]=str[b-i];
                str[b-i]=t;
            }
        }
        if (c=="replace"){
            int a,b;
            string r;
            cin >> a >> b >> r;
            str.replace(a,b-a+1,r);
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define rep(i, n) FOR(i, 0, n)
#define whole(x) (x).begin(),(x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
using P = pair<int, int>;
#define debug(var) cerr << "[" << #var << "] " << var << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
const ll mod = 1000000007;
const int dx[] = {-1,0,1,0};
const int dy[] = {0,-1,0,1};

int main(){
    int n;
    cin >> n;
    string ans = "AGC";
    if (n<1200) ans = "ABC";
    else if (n<2800) ans = "ARC";

    cout << ans << endl;
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for(int i = 0; i < n; i++)
typedef long long ll;
template<typename T> bool chmax(T &a, T b) {if(a <= b){a = b; return true;}return false;}
template<typename T> bool chmin(T &a, T b) {if(a >= b){a = b; return true;}return false;}

#define ABC 001
#define ARC 010
#define AGC 100

int main(void){
    int r;
    cin >> r;
    int frag = AGC;
    if(r < 1200){
        frag |= ABC;
        frag |= ARC;
    }else if( r < 2800){
        frag |= ARC;
    }
    if(frag & ABC)cout << "ABC" << endl;
    else if(frag & ARC)cout << "ARC" << endl;
    else cout << "AGC" << endl;
}


#include<iostream>
#include<cstring>
const int mod=1000000007;
using namespace std;
long long npow(long long x)
{
	long long ans=1,n=mod-2;
	while(n)
	{
		if(n&1) ans=ans*x%mod;
		x=x*x%mod;
		n>>=1;
	}
	return ans;
}
long long co(long long x,long long y)
{
	long long ans=1;
	for(int i=0;i<x;i++)
		ans=(ans*(y-i))%mod;
	for(int i=0;i<x-1;i++)
		ans=(ans*npow(x-i))%mod;
	return ans;
}
int main()
{
	long long h,w,a,b,c1=1,c2,ans=0;
	cin>>h>>w>>a>>b;
	c1=1;
	c2=co(h-1,h+w-b-2);
	ans=(ans+c1*c2%mod)%mod;
	for(int i=2;i<=h-a;i++)
	{
		c1=(c1*(i+b-2)%mod*npow(i-1))%mod;
		c2=(c2*(h-i+1)%mod*npow(h-i+w-b))%mod;
		ans=(ans+c1*c2%mod)%mod;
	}
	cout<<ans<<endl;
	return 0;
 }

#include<iostream>
#include<unordered_map>
#include<bitset>
#include<math.h>
#include<vector>
#include<set>
#include<algorithm>
#include<ctype.h>
#include<unordered_set>
#include<string>
#include<iomanip>
#include<queue>
#include<limits>
#include<map>
#include<stack>
#include<iterator>
#include<cstring>

#include<deque>
#define pi 3.141592653589793238
#include<chrono>
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#define MOD 1000000007
#define INF 999999999999999999
#define pb push_back
#define ff first
#define ss second

#define mt make_tuple
#define ll long long
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>

using namespace __gnu_pbds;

typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
const int N = 9e5 + 1;
ll fac[N];
ll power(ll x, ll y, ll p)
{
    ll res = 1;
    x = x % p;

    while (y > 0)
    {

        if (y & 1)
            res = (res * x) % p;

        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}

ll modi(ll a, ll m)
{
    return power(a, m - 2, m);

}
ll nCr(ll n, ll r, ll p)
{

    if (r == 0)
        return 1;
    fac[0] = 1;

    return (fac[n] * modi(fac[r], p) % p *
        modi(fac[n - r], p) % p) % p;
}
int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    fast;
    ll T = 1, i, j;
    fac[0] = 1;
    for (i = 1; i <= 9e5; i++){
        fac[i] = (fac[i - 1] * i) % MOD;
    }

    //cin >> T;
    while (T--) {
        ll h, w, a, b;
        cin >> h >> w >> a >> b;
        ll ans = 0;
        ll ways = 1;
        for(i = 1; i <= h - a; i++){
            ll v1, v2;
            ll m, n;
            m = i, n = b;
            v1 = nCr(m + n - 2, n - 1, MOD);
            ways = v1;
            m = h - i + 1, n = w - b;
            v2 = nCr(m + n - 2 , n - 1, MOD);
            ans += v1 * v2;
            ans %= MOD;
        }

        cout << ans;
    }
    return 0;
}


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

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    string str;
    while(getline(cin, str)) {
        for(int i=0; i<25; i++) {
            if(str.find("the")!=string::npos) break;
            if(str.find("this")!=string::npos) break;
            if(str.find("that")!=string::npos) break;
            for(int i=0, l=str.length(); i<l; i++) {
                if(str[i]=='z') str[i]='a';
                else if(islower(str[i])) ++str[i];
            }
        }
        cout<<str<<'\n';
    }
    return 0;
}

#include <iostream>
#include <string>
using namespace std;
int main(){
	string s;
	while(getline(cin, s)){
		for(int i = 0; i < 26; i++){
			for(int i = 0; s[i] != '\0'; i++){
				if(s[i] >= 97 && s[i] <= 122) s[i] = (s[i] - 97 + 1) % 26 + 97;
			}
			if(s.find("the", 0) != string::npos || s.find("this", 0) != string::npos || s.find("that", 0) != string::npos){
				cout << s << endl;
				break;
			}
		}
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
#define int long long
signed main(){
        int a;
        while( scanf("%lld", &a) != EOF ){
                if(a==0) break;
                vector<vector<int>> m(a+1,vector<int>(a+1,0));
                for(int i=0;i<a;i++){
                        int s = 0;
                        for(int j=0;j<a;j++){
                                int b;
                                scanf("%lld", &b);
                                m[i][j] = b;
                                s += b;
                        }
                        m[i][a] = s;
                }
                for(int j=0;j<a+1;j++){
                        int s = 0;
                        for(int i=0;i<a;i++){
                                s += m[i][j];
                        }
                        m[a][j] = s;
                }
                for(int i=0;i<a+1;i++){
                        for(int j=0;j<a+1;j++){
                                printf("%5lld", m[i][j]);
                        }
                        printf("\n");
                }
        }
        return 0;
}

// C++ 14
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define loop(__x, __start, __end) for(int __x = __start; __x < __end; __x++)
//  cout.precision(15); cout << fixed;

int main() {
  vector<int> A(3); loop(i,0,3) cin >> A[i];
  int k; cin >> k;
  while (k--) {
    sort(A.rbegin(), A.rend());
    A[0] *= 2;
  }
  cout << A[0]+A[1]+A[2] << endl;
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair< ll, ll > Pi;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep2(i,n) for(int i=1;i<=(n);i++)
#define rep3(i,i0,n) for(int i=i0;i<(n);i++)
#define pb push_back
#define mod 1000000007
const ll INF = 1LL << 60;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) {return a/gcd(a,b)*b;}
#define all(x) x.begin(), x.end()
#define mp make_pair
bool compare(Pi a, Pi b) {
    if(a.first != b.first){
        return a.first < b.first;

    }else{
        return a.second < b.second;
    }
}

bool In_map(ll y,ll x,ll h,ll w){
    if(y<0 || x<0 || y>=h || x>=w){
        return 0;
    }else{
        return 1;
    }
}
const vector<ll> dx{1,0,-1,0};
const vector<ll> dy{0,1,0,-1};

int main() {
    ll N,M,X,Y;
    cin >>N>>M>>X>>Y;
    ll XZ=X;
    ll YZ=Y;
    rep(i,N){
        ll x;
        cin>>x;
        chmax(XZ,x);
    }

    rep(i,M){
        ll y;
        cin>>y;
        chmin(YZ,y);
    }

    if(XZ<YZ){
        cout<<"No War"<<endl;
    }else{
        cout<<"War"<<endl;
    }

    return 0;

}

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

int main()
{
	int a,b,c;
	cin>>a>>b>>c;
	if(min(a,b)<c&&c<max(a,b))cout<<"Yes"<<endl;
	else cout<<"No"<<endl;
	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <cctype>
#include <random>
#include <cassert>
#include <numeric>
#define ll long long int
#define LL unsigned long long
#define pb push_back
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,n) for(int i=1;i<=(n);i++)
using namespace std;

int mx8[] = {0,0,1,-1,-1,1,-1,1};
int my8[] = {-1,1,0,0,-1,-1,1,1};
int mx4[] = {1,-1,0,0};
int my4[] = {0,0,-1,1};
const int MOD = 1000000007;

int main() {
    int n, x;
    cin >> n >> x;
    vector<int> a(n), sum(n);
    rep(i, n) cin >> a[i];
    sort(a.begin(), a.end());

    sum[0] = a[0];
    rep(i,n-1) sum[i+1] = sum[i] + a[i+1];

    rep(i,n) {
        if(sum[i] == x){
            cout << i + 1 << endl;
            return 0;
        }else if(sum[i] > x) {
            cout << i << endl;
            return 0;
        }
    }
    cout << n - 1 << endl;
}

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

int main() {
  int N,x;
  int ans = 0;
  bool flag = false;

  cin >> N >> x;
  vector<int> vec(N);
  for(int i=0;i<N;++i){
    cin >> vec.at(i);
  }
  sort(vec.begin(),vec.end());
  for(int i=0;i<N-1;++i){
    if(x - vec.at(i) >= 0){
      x -= vec.at(i);
      ans += 1;
    }else{
      flag = true;
    }
    if(flag) break;
  }
  if(!flag && vec.at(N-1) == x) ans += 1;
  cout << ans << endl;
  return 0;
}


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

int m;
int d;
string days[] = {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};

int GetNumDay(int m) {
  switch (m) {
  case 2:
    return 29;
  case 4:
  case 6:
  case 9:
  case 11:
    return 30;
  default:
    return 31;
  }
}

int main() {
  while (1) {
    scanf("%d%d", &m, &d);
    if (m == 0 && d == 0) return 0;

    int tot = d-1;
    for (int i=1; i<m; i++) {
      tot += GetNumDay(i);
    }

    puts(days[(3+tot)%7].c_str());
  }
}

#include <iostream>
using namespace std;

int main(){
    int n;
    int count[30];
    while(cin >> n, n){
        for(int i = 0;i < n;++i){
            count[i] = i < 3 ? 1 : 0;
        }
        for(int i = 0;i < n;++i){
            for(int j = 1;j < 4;++j){
                if(i + j >= n) break;
                count[i + j] += count[i];
            }
        }
        cout << count[n - 1] / 3650 + 1 << endl;
    }
}


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

int main() {
    Int H, W, A, B;
    cin >> H >> W >> A >> B;

    vector<string> s(H);

    if (B > 0) {
        rep(i,B) {
            if (A > 0) {
                rep(j,A) {
                    s[i] += '1';
                }
            }
            rep(j,W-A) s[i] += '0';
        }
    }
    for (int i = B; i<H; i++) {
        if (A > 0) {
                rep(j,A) {
                    s[i] += '0';
                }
            }
            rep(j,W-A) s[i] += '1';
    }

    rep(i,H) cout << s[i] << endl;
}

#include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<stack>
#include<climits>
#include<cstring>
#include<queue>
using namespace std;

typedef unsigned long long ull;

const ull INF = ULLONG_MAX/3;

void latentEvaluation(int ss, vector<ull> &xList, vector<ull> &sxList){
    int sn = sxList.size();
    if(sxList[ss] != INF){
        for(int i=0;i<sn;i++){
            xList[sn*ss+i] = sxList[ss];
        }
        sxList[ss] = INF;
    }
}

void update(int s, int t, ull x, vector<ull> &xList, vector<ull> &sxList){
    int nn = xList.size();
    int sn = sxList.size();
    int ss = s/sn;
    int tt = t/sn;
    if(ss == tt){
        latentEvaluation(ss, xList, sxList);
        for (int i=0;i<t-s+1;i++){
            xList[s+i] = x;
        }
    }else{
        latentEvaluation(ss, xList, sxList);
        for(int i=s;i<(ss+1)*sn;i++){
            xList[i] = x;
        }

        latentEvaluation(tt, xList, sxList);
        for(int i=tt*sn;i<t+1;i++){
            xList[i] = x;
        }

        for(int i=ss+1;i<tt;i++){
            sxList[i] = x;
        }
    }
}

void find(int ind, vector<ull> &xList, vector<ull> &sxList){
    int nn = xList.size();
    int sn = sxList.size();
    latentEvaluation(ind/sn, xList, sxList);
    cout << xList[ind] << endl;
}

int main(){
    int n, q, qType, s, t,ind;
    ull x;
    cin >> n >> q;
    int sn = sqrt(n)+1;

    vector<ull> xList(sn*sn, (1ull << 31) -1);

    vector<ull> sxList(sn, INF);

    for (int i=0;i<q;i++){
        cin >> qType;
        if (qType == 0){
            cin >> s >> t >> x;
            update(s,t,x,xList,sxList);
        }else{
            cin >> ind;
            find(ind, xList, sxList);
        }
        /*
        for (int i=0;i<sn*sn;i++){
            cout << xList[i]  << " ";
        }
        cout << endl;
        for (int i=0;i<sn;i++){
            cout << sxList[i] << " ";
        }
        cout << endl;
        */
    }
    return 0;
}


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

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

using ll = long long;
using ld = long double;

const int N = 1e5 + 69;

priority_queue<pii, vector<pii>, greater<pii>> edges;
vector<pair<int,int>> a, b;
ll ans;
int n, cnt;
int par[N], sz[N];

int fpar(int a) {
  return (a == par[a] ? a : par[a] = fpar(par[a]));
}

void merge(int a, int b) {
  int pa, pb;
  pa = fpar(a);
  pb = fpar(b);
  if(pa == pb)
    return;
  if(sz[a] > sz[b])
    swap(a, b);
  par[pa] = pb;
  sz[pb] += sz[pa];
}

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

  cin >> n;
  for(int i = 1; i <= n; i++)
    par[i] = i, sz[i] = 1;
  for(int i = 1; i <= n; i++) {
    int x, y;
    cin >> x >> y;
    a.emplace_back(x, i);
    b.emplace_back(y, i);
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  for(int i = 1; i < n; i++) {
    edges.emplace(a[i].first - a[i - 1].first, make_pair(a[i].second, a[i - 1].second));
    edges.emplace(b[i].first - b[i - 1].first, make_pair(b[i].second, b[i - 1].second));
  }
  while(cnt < n - 1) {
    pii cur = edges.top();
    edges.pop();
    if(fpar(cur.second.first) == fpar(cur.second.second))
      continue;
    merge(cur.second.first, cur.second.second);
    ans += cur.first;
    cnt++;
  }
  cout << ans << '\n';
  return 0;
}

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

typedef pair<int, ll> p;

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

    int N;
    cin >> N;

    vector<vector<p> > x(N, vector<p>());
    vector<bool> C(N);
    vector<ll> L(N);

    for(int i = 0; i < N - 1; i++) {
        int a, b;
        ll c;
        cin >> a >> b >> c;
        a--;
        b--;
        x[a].push_back(make_pair(b, c));
        x[b].push_back(make_pair(a, c));
    }

    stack<p> s;

    int Q, K;
    cin >> Q >> K;
    K--;

    s.push(make_pair(K, 0));

    while(!s.empty()) {
        p y = s.top();
        s.pop();
        int n = y.first;
        ll l = y.second;

        C[n] = true;
        L[n] = l;

        for(int i = 0; i < x[n].size(); i++) {
            if(C[x[n][i].first]) continue;
            s.push(make_pair(x[n][i].first, l + x[n][i].second));
        }
    }

    vector<int> a(Q);
    vector<int> b(Q);

    for(int i = 0; i < Q; i++) cin >> a[i] >> b[i];

    for(int i = 0; i < Q; i++) {
        a[i]--;
        b[i]--;
        cout << L[a[i]] + L[b[i]] << endl;
    }
}


#include <bits/stdc++.h>
#include <math.h>
using namespace std;
#define rep(i,n) for (ll i=0; i<(n); ++i)
#define rep1(i,n) for (ll i=1; i<=(n); ++i)
typedef long long ll;
bool debug=false;
string yes = "Yes";
string no = "No";
const ll mod = 998244353;
const double e = 0.000000001;
int main(){
  ll n; cin >> n;
  cout << (n<1200 ? "ABC" : (n<2800 ? "ARC" : "AGC"));
  return 0;
}

#include <bits/stdc++.h>

using namespace std;

#define repr(i,a,b) for (int i=a; i<b; i++)
#define rep(i,n) for (int i=0; i<  n; i++)

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

const long long INF = 1LL << 60;
#define PI 3.14159265359

template<typename T>
void remove(std::vector<T>& vector, unsigned int index)
{
    vector.erase(vector.begin() + index);
}

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

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

long long GCD(long long a, long long b) { return b ? GCD(b, a%b) : a; }

int collatz(int n){
    int a=n;
    if(a%2 == 0)a/=2;
    else a = a*3+1;
    return a;
}

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

    if(R<1200)cout << "ABC" << endl;
    else if (R>=1200 && R<2800)cout << "ARC" << endl;
    else cout << "AGC" << endl;

}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;
const long long MAXN = 2e5+5;
long long q[MAXN];
struct segtree{
    long long seg[5*MAXN];
    long long lazy[5*MAXN];
    void push(long long curr){
        if(lazy[curr]){
            seg[2*curr]+=lazy[curr];
            seg[2*curr+1]+=lazy[curr];
            lazy[2*curr] += lazy[curr];
            lazy[2*curr+1]+=lazy[curr];
            lazy[curr] = 0;
        }
    }
    void build(long long curr,long long l,long long r){
        if(l==r){
            seg[curr] = 1e18;
            lazy[curr] = 0;
            return;
        }
        long long mid = (l+r)/2;
        build(2*curr,l,mid);
        build(2*curr+1,mid+1,r);
        seg[curr] = min(seg[2*curr],seg[2*curr+1]);
    }
    void insert(long long curr,long long l,long long r,long long ind,long long val){
        if(l==r){
            seg[curr] = min(seg[curr],val);
            return;
        }
        if(l!=r){
            push(curr);
        }
        long long mid = (l+r)/2;
        if(ind<=mid){
            insert(2*curr,l,mid,ind,val);
        }else{
            insert(2*curr+1,mid+1,r,ind,val);
        }
        seg[curr] = min(seg[2*curr],seg[2*curr+1]);
    }
    void update(long long curr,long long l,long long r,long long tl,long long tr,long long val){
        if(l>tr||r<tl){
            return;
        }
        if(l!=r){
            push(curr);
        }
        if(l>=tl && r<=tr){
            seg[curr] += val;
            lazy[curr]+=val;
            return;
        }
        long long mid = (l+r)/2;
        update(2*curr,l,mid,tl,tr,val);
        update(2*curr+1,mid+1,r,tl,tr,val);
        seg[curr] = min(seg[2*curr],seg[2*curr+1]);
    }
    long long query(long long curr,long long l,long long r,long long tl,long long tr){
        if(l>tr||r<tl){
            return 1e18;
        }
        if(l!=r){
            push(curr);
        }
        if(l>=tl && r<=tr){
            return seg[curr];
        }
        long long mid = (l+r)/2;
        return min(query(2*curr,l,mid,tl,tr),query(2*curr+1,mid+1,r,tl,tr));
    }
};
segtree seg1,seg2,seg3;
int main(){
    long long n,Q,a,b;
    cin>>n>>Q>>a>>b;
    for(long long i=1;i<=Q;i++){
        cin>>q[i];
    }
    seg1.build(1,1,n);
    seg2.build(1,1,n);
    seg3.build(1,1,n);
    seg1.insert(1,1,n,a,abs(b-q[1]));
    seg2.insert(1,1,n,a,abs(b-q[1])+a);
    seg3.insert(1,1,n,a,abs(b-q[1])-a);
    seg1.insert(1,1,n,b,abs(a-q[1]));
    seg2.insert(1,1,n,b,abs(a-q[1])+b);
    seg3.insert(1,1,n,b,abs(a-q[1])-b);
    //cout<<seg1.query(1,1,n,1,n)<<endl;
    for(long long i=2;i<=Q;i++){
        long long val = seg2.query(1,1,n,q[i],n)-q[i];
        val = min(val,seg3.query(1,1,n,1,q[i]-1)+q[i]);
        seg1.update(1,1,n,1,n,abs(q[i]-q[i-1]));
        seg2.update(1,1,n,1,n,abs(q[i]-q[i-1]));
        seg3.update(1,1,n,1,n,abs(q[i]-q[i-1]));
        seg1.insert(1,1,n,q[i-1],val);
        seg2.insert(1,1,n,q[i-1],val+q[i-1]);
        seg3.insert(1,1,n,q[i-1],val-q[i-1]);
    }
    cout<<seg1.query(1,1,n,1,n)<<endl;
}

#include <bits/stdc++.h>
#define mset(a, b) memset(a, b, sizeof(a))
#define mcpy(a, b) memcpy(a, b, sizeof(a))
#define rg register
using namespace std;
typedef long long LL;
const int MAXN = 200005;
const LL INF = 0x3f3f3f3f3f3f3f3f;

template <typename T> inline void read(T &AKNOI) {
	T x = 0, flag = 1;
	char ch = getchar();
	while (!isdigit(ch)) {
		if (ch == '-') flag = -1;
		ch = getchar();
	}
	while (isdigit(ch)) {
		x = x * 10 + ch - '0';
		ch = getchar();
	}
	AKNOI = flag * x;
}

template <typename T> inline void cmin(T &x, T y) {
	if (x > y) x = y;
}

template <typename T> inline void cmax(T &x, T y) {
	if (x < y) x = y;
}

int n, q, A, B, p[MAXN];
LL csum[MAXN], dp[MAXN];

struct BIT {
	LL tr[MAXN];
	BIT() {
		mset(tr, 0x3f);
	}
	inline int lowbit(int x) {
		return x & (-x);
	}
	inline void update(int x, LL val) {
		for (int i=x;i<=n;i+=lowbit(i)) {
			cmin(tr[i], val);
		}
	}
	inline LL query(int x) {
		LL ret = INF;
		for (int i=x;i;i-=lowbit(i)) {
			cmin(ret, tr[i]);
		}
		return ret;
	}
}t1, t2;

void init() {
	read(n); read(q);
	read(A); read(B);
	for (int i=1;i<=q;++i) {
		read(p[i]);
	}
	for (int i=2;i<=q;++i) {
		csum[i] = csum[i-1] + abs(p[i]-p[i-1]);
	}
}

void solve() {
	dp[0] = min(abs(A-p[1]), abs(B-p[1]));
	t1.update(A, abs(B-p[1]) - A);
	t2.update(n-A+1, abs(B-p[1]) + A);
	t1.update(B, abs(A-p[1]) - B);
	t2.update(n-B+1, abs(A-p[1]) + B);
	for (int i=1;i<q;++i) {
		dp[i] = min( t1.query(p[i+1]) + csum[i] + p[i+1],
					 t2.query(n-p[i+1]+1) + csum[i] - p[i+1] );
		t1.update(p[i], dp[i] - csum[i+1] - p[i]);
		t2.update(n-p[i]+1, dp[i] - csum[i+1] + p[i]);
	}
	LL ans = INF;
	for (int i=0;i<q;++i) {
		cmin(ans, dp[i] + csum[q] - csum[i+1]);
	}
	printf("%lld\n", ans);
}

int main() {
	init();
	solve();
	return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define yorn(f) cout<<((f)?"Yes":"No")<<endl;
#define YORN(f) cout<<((f)?"YES":"NO")<<endl;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, n) for (int i = 1; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define put(x) cout << x << endl;
#define println(x) cout << x << endl;
using namespace std;
signed main()
{
    int n;
    ll Y;
    cin >> n >> Y;

    for (int i = n; i >= 0; i--) {
        for (int j = 0; j <= n - i; j++) {
            int k = n - j - i;
            if (k < 0) {
                continue;
            }
            if (Y == 10000 * i + 5000 * j + 1000 * k) {
                cout << i << " " << j << " " << k << endl;
                return 0;
            }
        }
    }

    put("-1 -1 -1");

    return 0;
}

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
using ll = long long;
using p = pair<int,int>;
using Graph = vector<vector<int>>;
int main(int argc, const char * argv[]) {
    cin.tie(0);
    ios::sync_with_stdio(false);

    double r,g;
    cin>>r>>g;
    cout<<2*g-r<<endl;

    return 0;
}


#include <bits/stdc++.h>

int main() {
    using namespace std;

    constexpr unsigned long MOD = 1000000007;

    unsigned long N;
    cin >> N;

    auto pw = [&MOD](unsigned long a, unsigned long n = 1000000005){
        unsigned long ret = 1;
        while(n){
            if(n & 1)(ret *= a) %= MOD;
            (a *= a) %= MOD;
            n >>= 1;
        }
        return ret;
    };

    vector<unsigned long> fact(N + 1), ifact(N + 1);
    fact[0] = 1;
    for(unsigned long i = 1; i <= N; ++i)fact[i] = fact[i - 1] * i % MOD;
    ifact[N] = pw(fact[N]);
    for(unsigned long i = N; i > 0; --i)ifact[i - 1] = ifact[i] * i % MOD;

    unsigned long A, B, C, D;
    cin >> A >> B >> C >> D;

    vector<vector<unsigned long>> dp(B + 1, vector<unsigned long>(N + 1));
    dp[A - 1][N] = 1;

    for(unsigned long i = A; i <= B; ++i){
        for(unsigned long j = 0; j <= N; ++j){
            dp[i][j] = dp[i - 1][j];
            for(unsigned long k = C, l = j + C * i, m = pw(ifact[i], C); k <= D && l <= N; ++k, l += i, (m *= ifact[i]) %= MOD)dp[i][j] += ((((dp[i - 1][l] * fact[l]) % MOD * ifact[j]) % MOD * m) % MOD * ifact[k]) % MOD;
            dp[i][j] %= MOD;
        }
    }

    cout << dp[B][0] << endl;

    return 0;
}

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

using namespace std;

struct edge{
	int u;	//?????????
	int v;	//??°??????
	int cost;	//??????
	//???????????????????????§?????§?°???¢????????????
	bool operator<(const edge& another) const {
			return cost < another.cost;
	}
};

const int MAX_N = 100;
const int MAX_M = 10000;

edge es[MAX_M];
int par[MAX_N];
int rank[MAX_N];

void initial(int n){		//?????????
	for(int i=0;i<n;i++){
		par[i] = i;
		rank[i] = 0;
	}
}

int find(int x){		//??¨??????????±???????
	if(par[x] == x){
		return x;
	}else{
		return par[x] = find(par[x]);
	}
}

void unite(int x,int y){	//x???????????¨??¨y???????????¨?????????
	x = find(x);	//x???????????¨??????????????????x??¨??????
	y = find(y);	//y???????????¨??????????????????y??¨??????
	if(x == y) return;
	if(rank[x] < rank[y]){
		par[x] = y;
	}else{
		par[y] = x;
		if(rank[x] == rank[y])
			rank[x]++;
	}
}

bool same(int x,int y){	//x,y???????????¨???????????§?????????
	return find(x) == find(y);
}

bool comp(const edge& e1,const edge& e2){
	return e1.cost < e2.cost;
}

int kruskal(int n,int m){
	initial(n);
	int res = 0;
	for(int i=0;i<m;i++){
		edge e = es[i];
		if(!same(e.u,e.v)){
			unite(e.u,e.v);
			res += e.cost;
		}
	}
	return res;
}

int main(){
	int n,m=0,k,l,x;
	cin >> n;	//????????°?????\???
	int cnt=0;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			cin >> x;
			if(x != -1){
				edge e;
				e.u = i; e.v = j; e.cost = x;
				es[m] = e;
				m++;
			}
		}
	}
	sort(es,es+m);
	cout << kruskal(n,m) << "\n";
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+5;
int n,a[N],b[N];
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for(int i=n;i>=1;i--) scanf("%d",&b[i]);
    for(int i=1;i<=n;i++){
        if(a[i]==b[i]){
            bool flag=0;
            for(int j=1;j<=n;j++){
                if(b[j]!=a[i]&&b[i]!=a[j]){
                    swap(b[i],b[j]);
                    flag=1;
                    break;
                }
            }
            if(!flag){
                printf("No");
                return 0;
            }
        }
    }
    printf("Yes\n");
    for(int i=1;i<=n;i++) printf("%d ",b[i]);
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

#define ll  long long
#define ld long double
#define f first
#define s second
#define matrix vector<vector<char>>
#define zero(n,m) matrix(n , vector<char>(m,'0'))
#define one(n,m) matrix(n , vector<ll>(m,1))
#define pii pair<int,int>
#define point complex<ld>
#define angle(a) (atan2((a).imag() , (a).real() ))
#define pi acos(-1)
#define X real()
#define Y imag()
const int N = 2e5+5;

ll n , a[N] , b[N] , seg[4*N] , lazy[4*N];
ll st[N] , en[N] , st2[N] , en2[N];
set<ll> stt;

void probagate(int node , int st , int en)
{
    if (lazy[node] != 0)
    {
        seg[node] += lazy[node];
        if (st != en)
        {
            lazy[2*node] += lazy[node];
            lazy[2*node+1] += lazy[node];
        }
        lazy[node] = 0;
    }
}

void update(int node , int st , int en , int l , int r , int val)
{
    probagate(node , st , en);

    if (st > en || st > r || en < l)
        return;

    if (st >= l && en <= r)
    {
        lazy[node] += val;
        return probagate(node , st , en);
    }

    int mid = (st+en)/2;
    update(2*node,st,mid,l,r,val);
    update(2*node+1,mid+1,en,l,r,val);
    seg[node] = max(seg[2*node] , seg[2*node+1]);
}

ll query(int node , int st , int en , int l , int r)
{
    if (st > en || st > r || en < l) return 0;

    probagate(node , st , en);

    if (st >= l && en <= r)
    {
        return seg[node];
    }

    int mid = (st+en)/2;

    return max(query(2*node , st , mid , l , r) , query(2*node+1 , mid+1 , en , l , r));
}

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

    cin >> n;
    for(int i=1; i<=n; i++)
    {
        cin >> a[i];
        if (!st[a[i]]) st[a[i]] = i;
        en[a[i]] = i;
    }

    for(int i=1; i<=n; i++)
    {
        cin >> b[i];
        if (!st2[b[i]]) st2[b[i]] = i;
        en2[b[i]] = i;
        stt.insert(b[i]);
    }

    for(ll i=1; i<=n; i++)
    {
        ll l = st[b[i]] , r = en[b[i]];
        if (l > i)
        {
            update(1,0,n-1,0,l-i-1,1);
        }
        l = max(en[b[i]]+1,i) , r = n;
        if (l<=r)
        {
            ll l2 = l-i , r2 = r-i;
            if (l2 <= r2)
            {
                update(1,0,n-1,l2,r2,1);
            }
        }

        //from one to st[b[i]]-1
        l = 1 , r = min(st[b[i]]-1 , i-1);
        if (l<=r)
        {
            ll l2 = n-i+l , r2 = n-i+r;
            if (l2 <= r2)
            {
                update(1,0,n-1,l2,r2,1);
            }
        }

        if (i > en[b[i]])
        {
            l = en[b[i]]+1 , r = i-1;
            if (l<=r)
            {
                ll l2 = n-i+l , r2 = n-i+r;
                if (l2 <= r2)
                {
                    update(1,0,n-1,l2,r2,1);
                }
            }
        }
    }

    for(int i=0; i<n; i++)
    {
        if (query(1,0,n-1,i,i) == n)
        {
            cout << "Yes\n";
            for(int j=1; j<=n; j++)
            {
                int newIdx = j + i;
                if (newIdx > n) newIdx -= n;
                a[newIdx] = b[j];
            }
            for(int j=1; j<=n; j++)
                cout << a[j] << ' ';
            cout << '\n';
            return 0;
        }
    }
    cout << "No\n";

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1e16;
const ll mod = 1000000007;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)

int main() {
  ll n; cin >> n;
  ll tmp = INF;
  rep(i, 5) {
    ll a; cin >> a;
    tmp = min(tmp, a);
  }
  if (n%tmp == 0) cout << n/tmp + 4 << endl;
  else cout << n/tmp + 5 << endl;
}

#pragma region header
#include <bits/stdc++.h>
#define int long long
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rev(i, n) for(int i = (int)(n - 1); i >= 0; i--)
#define rev1(i, n) for(int i = (int)(n); i > 0; i--)
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define resort(v) sort((v).rbegin(), (v).rend())
#define vi vector<int>
#define vvi vector<vector<int>>
#define vc vector<char>
#define vvc vector<vector<char>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
using ll = long long;
using P = pair<int, int>;
/* ----------------よく使う数字や配列----------------- */
int dx[] = {1,0,-1,0};
int dy[] = {0,1,0,-1};
constexpr ll mod = 1e9+7;
constexpr int inf = INT32_MAX/2;
constexpr ll INF = LLONG_MAX/2;
constexpr long double eps = DBL_EPSILON;
constexpr long double pi = 3.141592653589793238462643383279;
/* ----------------------end----------------------- */

/* --------------------テンプレート------------------ */
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
/* ----------------------end----------------------- */

/* --------------------ライブラリ-------------------- */
ll fact(int i) {       //階乗
    if (i == 0) return 1;
    return (fact(i - 1)) * i % mod;
}
ll gcd(ll a, ll b) {        //最大公約数
    if(b == 0) return a;
    return gcd(b, a % b);
}
ll lcm(ll a, ll b) {      //最小公倍数
    return a * b / gcd(a, b);
}
int keta(ll n) {        //桁数を求める
    if(n == 0) return 1;
    int count = 0;
    while(n != 0) {
        n /= 10;
        count++;
    }
    return count;
}
ll ketasum(ll n) {    //各桁の和
    ll sum = 0;
    while(n != 0) {
        sum += n % 10;
        n /= 10;
    }
    return sum;
}
/* ----------------------end----------------------- */
#pragma endregion
signed main() {
    int n,a,b,c,d,e;cin >> n >> a >> b >> c >> d >> e;
    int k = min({a,b,c,d,e});
    cout << (n+k-1)/k + 4 << endl;
    return 0;
}


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

int main()
{
    char ch[20];
	int i=0;
	while(cin >> ch[i]){
		i++;
	}
	for(int j=i-1 ; j>=0 ; j--){
		cout<<ch[j];
	}
	cout<<endl;
	return 0;
}

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

int main() {
	string s;
	cin >> s;
	cout << string(s.rbegin(),s.rend()) << endl;
	return 0;
}


#include<bits/stdc++.h>
#define forr(i, a, b) for(int i = (a); i < (b); i++)
#define rep(i,n) for(int i = 0; i < (n); i++)
#define rrep(i,n) for(int i = 1; i <= (n); i++)
#define ALL(a) (a.begin()),(a.end())
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<ll, ll> LP;
const ll LINF = 1LL<<60;
const int INF = 1001001001;

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

int main() {
    int a, b, c;
    cin >> a >> b >> c;
    if(a < c && c < b || a > c && c > b) cout << "Yes" << endl;
    else cout << "No" << endl;

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, from, to) for (int i = from; i < (to); ++i)
#define mp(x,y) make_pair(x,y)
#define all(x) (x).begin(),(x).end()
#define pb push_back
using ll = long long;
using vin=vector<int>;
using vll=vector<ll>;
using vst=vector<string>;
using P = pair<int, int>;
const int inf=1e9+7;
const ll INF=1e18;
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template<class T> inline void Yes(T condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; }
template<class T> inline void YES(T condition){ if(condition) cout << "YES" << endl; else cout << "NO" << endl; }
const int dx[4] = { 1, 0, -1, 0 };
const int dy[4] = { 0, 1, 0, -1 };

int main(){cout<<fixed<<setprecision(10);
		   int a,b,c;
           cin>>a>>b>>c;
           Yes((a-c)*(b-c)<0);
}


#include <iostream>
#include <cstdio>
#include <cstring>
typedef long long lint;
const int N=1e5+5;
int n,m,val[N],nxt[N],ans[N];
lint q;
bool vis[N];

template <class T> inline T nxi(){
	T x=0;
	char c;
	while(((c=getchar())>'9'||c<'0')&&c!='-');
	const bool f=c=='-'&&(c=getchar());
	while(x=x*10-48+c,(c=getchar())>='0'&&c<='9');
	return f?-x:x;
}

inline void solve(const int st){
	static int cyc[N];
	int pt=0;
	for(int x=st; !vis[x]; x=nxt[x]){
		vis[x]=1;
		cyc[++pt]=x;
	}
	for(int i=1; i<=pt; ++i){
		ans[cyc[i]]=val[cyc[(i+q%pt-1)%pt+1]];
	}
}

int main(){
	n=nxi<int>();
	for(int lst=0,x,i=1; i<=n; lst=x,++i){
		nxt[i]=i;
		x=nxi<int>();
		val[i]=x-lst;
	}
	m=nxi<int>(),q=nxi<lint>();
	for(int i=1; i<=m; ++i){
		const int x=nxi<int>();
		std::swap(nxt[x],nxt[x+1]);
	}
	for(int i=1; i<=n; ++i){
		if(!vis[i]) solve(i);
	}
	lint sum=0;
	for(int i=1; i<=n; ++i){
		printf("%lld\n",sum+=ans[i]);
	}
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
int main() {
  int L,R;
  cin >> L >> R;
  int modL = L % 2019;
  int modR = R % 2019;
  if (R - L >= 2018){
    cout << 0 << endl;
    return 0;
  }
  else if (modL > modR){
    cout << 0 << endl;
    return 0;
  }
  int N = 2019;
  for (int i = modL; i < modR; i++) {
    for (int j = i+1; j <= modR; j++) {
      if (i != j){
        int M = ((i*j) % 2019) % 2019;
        if (M < N){
          N = M;
        }
      }
    }
  }
  cout << N << endl;
}


#include<iostream>
#include<vector>
#include<queue>
#include<map>
using namespace std;
int n,m;
map<int,vector<pair<int,int> > >G[1<<17];
map<int,int>d[1<<17];
main()
{
	cin>>n>>m;
	for(int i=0;i<m;i++)
	{
		int a,b,c;cin>>a>>b>>c;
		G[a][c].push_back(make_pair(b,c));
		G[b][c].push_back(make_pair(a,c));
		G[a][c].push_back(make_pair(a,0));
		G[b][c].push_back(make_pair(b,0));
		G[a][0].push_back(make_pair(b,c));
		G[b][0].push_back(make_pair(a,c));
	}
	priority_queue<pair<int,pair<int,int> > >P;
	d[1][0]=0;
	P.push(make_pair(0,make_pair(1,0)));
	while(!P.empty())
	{
		int dis=-P.top().first;
		int u=P.top().second.first;
		int c=P.top().second.second;
		P.pop();
		if(u==n&&c==0)
		{
			cout<<dis<<endl;
			return 0;
		}
		if(d[u][c]<dis)continue;
		for(pair<int,int>&p:G[u][c])
		{
			int v=p.first;
			int cost=dis+!c;
			int col=p.second;
			if(d[v].find(col)==d[v].end()||d[v][col]>cost)
			{
				d[v][col]=cost;
				P.push(make_pair(-cost,make_pair(v,col)));
			}
		}
	}
	cout<<-1<<endl;
}

#include<bits/stdc++.h>
#define ll long long
#define mp make_pair
using namespace std;
const int N=5e6;
const int INF=0x3f3f3f3f;
int n,m,cnt;
int len=0;
int lin[N];
int dis[N];
bool vis[N];
map<pair<int,int>,int> id;
struct edge
{
	int next,y,v;
}e[N];
inline int read()
{
    int 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<<1)+(x<<3)+(ch^48);
        ch=getchar();
    }
    return x*f;
}
int Getid(int x,int y)
{
	if(id.find(mp(x,y))!=id.end()) return id[mp(x,y)];
	return id[mp(x,y)]=++cnt;
}
void insert(int xx,int yy,int v)
{
	e[++len].next=lin[xx];
	lin[xx]=len;
	e[len].y=yy;
	e[len].v=v;
}
void add(int xx,int yy,int val)
{
	insert(xx,yy,val),insert(yy,xx,val);
}
void SPFA()
{
	queue<int> q;
	memset(dis,0x3f,sizeof(dis));
	memset(vis,false,sizeof(vis));
	dis[1]=0,vis[1]=true;
	q.push(1);
	while(!q.empty())
	{
		int x=q.front();
		q.pop();
		vis[x]=false;
		for(int i=lin[x];i;i=e[i].next)
		{
			int v=e[i].y;
			if(dis[v]>dis[x]+e[i].v)
			{
				dis[v]=dis[x]+e[i].v;
				if(!vis[v])
				{
					q.push(v);
					vis[v]=true;
				}
			}
		}
	}
}
int main()
{
//    freopen("input.in", "r", stdin);
//    freopen("output.out", "w", stdout);
	n=read(),m=read();
	cnt=n;
	for(int i=1;i<=m;i++)
	{
		int u=read(),v=read(),c=read();
		int xx=Getid(u,c);
		int yy=Getid(v,c);
		add(xx,yy,0);
		add(u,xx,1);
		add(v,yy,1);
	}
	SPFA();
	printf("%d",dis[n]!=INF?dis[n]/2:-1);
	return 0;
}


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

using ll = long long;
using ull = unsigned long long;
using ld = long double;

#define MOD 1000000007LL

#define rep(i, n) for(ll (i) = 0LL;(i) < (ll)(n);(i)++)
#define rep2(i, s, e) for(ll (i) = (ll)(s);(i) < (ll)(e);(i)++)
#define repi(i, n) for(ll (i) = 0LL;(i) <= (ll)(n);(i)++)
#define repi2(i, s, e) for(ll (i) = (ll)(s);(i) <= (ll)(e);(i)++)
#define per(i, n) for(ll (i) = (ll)(n) - 1LL;(i) >= 0LL;(i)--)
#define per2(i, s, e) for(ll (i) = (ll)(s) - 1LL;(i) >= (ll)(e);(i)--)
#define peri(i, n) for(ll (i) = (ll)(n);(i) >= 0LL;(i)--)
#define peri2(i, s, e) for(ll (i) = (ll)(s);(i) >= (ll)(e);(i)--)
#define iter(i, it) for(auto &(i): (it))

template<typename T, typename U> ostream& operator<<(ostream &s, const pair<T, U> m) {
    cout << "(" << m.first << ", " << m.second << ")";
    return s;
}
template<typename T, typename U> ostream& operator<<(ostream &s, const map<T, U> m) {
    ll c = 0;
    cout << "{ ";
    iter(i, m) cout << i << (c++ == m.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}
template<typename T> ostream& operator<<(ostream &s, const vector<T> &v) {
    cout << "{ ";
    rep(i, v.size()) cout << v[i] << (i == v.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}
template<typename T> ostream& operator<<(ostream &s, const list<T> &v) {
    ll c = 0;
    cout << "{ ";
    iter(i, v) cout << i << (c++ == v.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}

int main(void) {
    vector<string> A{"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};
    string S;
    cin >> S;
    auto iter = find(A.begin(), A.end(), S);
    cout << 7 - distance(A.begin(), iter) << endl;
    return 0;
}

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

int main(){
  string S;
  cin >> S;
  bool a=false;
  for(int i=0;i<S.size()-1;i++){
    if(S.at(i)=='A' && S.at(i+1)=='C'){
      cout << "Yes" << endl;
      a=true;
      break;
    }
  }
  if(!a){
    cout << "No" << endl;
  }
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define ll long long
int main(void)
{
    ll n, m;
    cin >> n >> m;
    vector<ll> s(m),c(m);
    rep(i,m) cin >> s[i] >> c[i];
    rep(i,1000){
        string t = to_string(i);
        if(t.size()==n){
            bool ok=true;
            rep(j,m){
                if(t[s[j]-1] != c[j]+'0') ok = false;
            }
            if(ok){
                cout << i << endl;
                return 0;
            }
        }
    }
    cout << -1 << endl;
    return 0;
}


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

#ifdef LOCAL
#define debug(var) cerr << (#var) << " = " << (var) << endl;
#else
#define debug(var)
#endif

void init() {
  ios_base::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);
}

const int N = 501;

int a[N][N];

struct Move {
  int x1, y1, x2, y2;
};

            // up, left, down, right
int dx[4] = {0, -1, 0, 1}, dy[4] = {-1, 0, 1, 0};

void solve() {
  int h, w; scanf("%d%d", &h, &w);
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      scanf("%d", a[i]+j);
    }
  }
  vector<Move> v;
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (a[i][j]%2 == 0) continue;
      for (int k = 0; k < 4; ++k) {
	if (i+dy[k] < 0 || i+dy[k] >= h || j+dx[k] < 0 || j+dx[k] >= w) continue;
	if (a[i+dy[k]][j+dx[k]]%2==0) continue;
	v.push_back({i+1, j+1, i+dy[k]+1, j+dx[k]+1});
	--a[i][j];
	++a[i+dy[k]][j+dx[k]];
	break;
      }
      if (a[i][j]%2 == 0) continue;
      for (int k = 2; k < 4; ++k) {
	if (i+dy[k] < 0 || i+dy[k] >= h || j+dx[k] < 0 || j+dx[k] >= w) continue;
	v.push_back({i+1, j+1, i+dy[k]+1, j+dx[k]+1});
	--a[i][j];
	++a[i+dy[k]][j+dx[k]];
	break;
      }
    }
  }
  printf("%d\n", v.size());
  for (const auto &e: v) {
    printf("%d %d %d %d\n", e.x1, e.y1, e.x2, e.y2);
  }
}

int main() {
  init();
  int t = 1; //scanf("%d", &t);
  while (t--) {
    solve();
  }
  return 0;
}

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < n; ++i)
#define ALL(c) (c).begin(), (c).end()
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
using namespace std;

using ll = long long;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
constexpr int INF = 1e9;
constexpr long long INFLL = 1LL<<60;

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

  int h, w; cin >> h >> w;
  vector<vector<int>> a(h, vector<int>(w));
  rep(i, h) rep(j, w) cin >> a[i][j];

  vector<vector<int>> ans;

  rep(i, h) {
    rep(j, w) {
      if (a[i][j] % 2 == 0) continue;
      if (j < w-1) {
        ans.push_back({i, j, i, j+1});
        a[i][j+1]++;
      } else if (i < h-1) {
        ans.push_back({i, j, i+1, j});
        a[i+1][j]++;
      }
    }
  }

  cout << ans.size() << endl;
  for (auto& v : ans) {
    for (int j = 0; j < v.size(); j++) {
      cout << v[j] + 1;
      if (j == v.size() - 1) cout << endl;
      else cout << " ";
    }
  }

  return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair< ll, ll > Pi;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep2(i,n) for(int i=1;i<=(n);i++)
#define rep3(i,i0,n) for(int i=i0;i<(n);i++)
#define pb push_back
#define mod 1000000007
const ll INF = 1LL << 60;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) {return a/gcd(a,b)*b;}
#define all(x) x.begin(), x.end()
#define mp make_pair
bool compare(Pi a, Pi b) {
    if(a.first != b.first){
        return a.first < b.first;

    }else{
        return a.second < b.second;
    }
}

bool In_map(ll y,ll x,ll h,ll w){
    if(y<0 || x<0 || y>=h || x>=w){
        return 0;
    }else{
        return 1;
    }
}
const vector<ll> dx{1,0,-1,0};
const vector<ll> dy{0,1,0,-1};

int main() {
    ll N;
    cin >>N;
    vector<ll>A(N);
    rep(i,N)cin>>A[i];
    ll ans=0;
    ll flag=0;
    while(1){
        rep(i,N){
            if(A[i]%2==0){
                A[i]/=2;

            }else{
                flag=1;
                break;
            }
        }
        if(flag==1) break;
        ans++;

    }
    cout << ans << endl;

    return 0;

}

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

int main(){
  int n = 0;
  cin >> n;
  int count = 200;
  for (int i = 0;i < n;i++) {
    int subcount = 0;
    int a =0;
    cin >> a;
    for (int j = a;j > 1;j /= 2){
      if( (j % 2) == 1) {
        break;
      }
      else {
        subcount++;
      }
    }
    if (count > subcount){
      count = subcount;
    }
  }
  cout << count << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
	int a,b;cin >> a >> b;
	cout << (a > b ? 2*a-1 : b > a ? 2*b -1 : 2*a);
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define rep(i,s, n) for(int i = s; i < (int)(n); i++)
#define co(x) cout<<x<<endl
#define SQvec(i,j) vector<vector<int>> data(i, vector<int>(j))
#define mod 1000000007
typedef long long int lint;

int main() {
  int N,K;
  cin >> N >> K;
  if(N%K == 0){co(0);}
  else{co(1);}

  return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
const int INF = 1 << 25;

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

	int n, m, query;
	while (cin >> n >> m , n) {
		int cost[101][101], time[101][101];
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < m; j++) {
				cost[i][j] = (i == j) ? 0 : INF;
				time[i][j] = (i == j) ? 0 : INF;
			}
		}

		while (n--) {
			int a, b, c, t;
			cin >> a >> b >> c >> t;
			cost[a - 1][b - 1] = cost[b - 1][a - 1] = c;
			time[a - 1][b - 1] = time[b - 1][a - 1] = t;
		}

		for (int k = 0; k < m; k++) {
			for (int i = 0; i < m; i++) {
				for (int j = 0; j < m; j++) {
					cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
					time[i][j] = min(time[i][j], time[i][k] + time[k][j]);
				}
			}
		}

		cin >> query;
		while (query--) {
			int p, q, r;
			cin >> p >> q >> r;
			if (r == 0) {
				cout << cost[p - 1][q - 1] << endl;
			}
			else {
				cout << time[p - 1][q - 1] << endl;
			}
		}
	}
}

#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

vector<int> mato;
vector<int> a;

int main(){
	int n,m;
	int self;
	while(scanf("%d %d",&n,&m) && n && m){
		mato.clear();
		a.clear();

		for(int i=0;i<n;i++){
			scanf("%d",&self);
			mato.push_back(self);
		}
		mato.push_back(0);
		sort(mato.begin(),mato.end());
		mato.erase(unique(mato.begin(),mato.end()),mato.end());
		int size = mato.size();
		for(int i=0;i<size;i++){
			for(int j=0;j<size;j++){
				a.push_back(mato[i] + mato[j]);
			}
		}
		sort(a.begin(),a.end());
		a.erase(unique(a.begin(),a.end()),a.end());
		int max = 0;
		size = a.size();
		int s,ss;
		for(int i=0;i<size;i++){
			s = m - a[i];
			if(s > 0){
				ss = *(lower_bound(a.begin(),a.end(),s) - 1);
				//printf("find:%d -> %d\n",s,ss);
				if(a[i] + ss > max){ max = a[i] + ss; }
			}
		}
		printf("%d\n",max);
	}
}


#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#include<bits/stdc++.h>
using namespace std;
int f[105];
int main()
{
	ios::sync_with_stdio(false);
	int n,m,a,b,t,p=0;
	cin>>n>>m;
    a=-1;
	b=-1;
    for(int i=1;i<=m;i++)
    {
    	cin>>f[i];
        if(~f[i]&1)
		{
			continue;
		}
        else if(p==0)
		{
			a=i;
		}
		else if(p==1)
		{
			b=i;
		}
        else
		{
			cout<<"Impossible"<<endl;
			exit(0);
		}
		p++;
    }
    if(a==-1) a=1;
    if(b==-1) b=m;
    t=f[1];
    f[1]=f[a];
    f[a]=t;
    t=f[m];
    f[m]=f[b];
    f[b]=t;
    if(m==1)
	{
		if(f[1]==1)
		{
			cout<<1;
			cout<<endl;
			cout<<1;
			cout<<endl;
			cout<<1;
			cout<<endl;
			exit(0);
		}
	}
    for(register int i=1;i<=m;i++)
    {
    	cout<<f[i]<<' ';
	}
    if(m==1)
	{
		f[1]--;
		cout<<endl;
		cout<<2;
		cout<<endl;
		cout<<f[1];
		cout<<' ';
		cout<<1;
		cout<<endl;
		exit(0);
	}
	cout<<endl;
	if(f[m]>1)
	{
		cout<<m;
	}else{
		cout<<m-1;
	}
	cout<<endl;
	cout<<f[1]+1;
	cout<<' ';
    for(register int i=2;i<m;i++)
	{
		cout<<f[i]<<' ';
	}
    if(f[m]>1)
	{
		cout<<f[m]-1<<endl;
	}
    return 0;
}

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=105;
int n,m,a[maxn],b[maxn];
inline char nc(){
	static char buf[100000],*p1,*p2;
	return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline int read(){
	int ret=0;bool f=0;char ch=nc();
	while(ch>'9'||ch<'0') f^=ch=='-',ch=nc();
	while(ch<='9'&&ch>='0') ret=ret*10+ch-'0',ch=nc();
	return f?-ret:ret;
}
int main(){
	n=read(),m=read();
	for (int i=1;i<=m;i++) a[i]=read();
	for (int i=2;i<=m;i++)
	if(a[i]&1) swap(a[i],a[1]);
	for (int i=m-1;i>1;i--)
	if(a[i]&1) swap(a[i],a[m]);
	for (int i=2;i<m;i++)
	if(a[i]&1) return puts("Impossible")&0;
	for (int i=1;i<=m;i++)
	printf("%d",a[i]),putchar(i==m?'\n':' ');
	if(a[1]>1) b[++b[0]]=a[1]-1;
	for (int i=2;i<m;i++) b[++b[0]]=a[i];
	if(m>1){
		b[++b[0]]=2;
		if(a[m]>1) b[++b[0]]=a[m]-1;
	}else b[++b[0]]=1;
	for (int i=0;i<=b[0];i++)
	printf("%d",b[i]),putchar(!i||i==b[0]?'\n':' ');
	return 0;
}


#include<iostream>
#include<stdio.h>
using namespace std;
#include<algorithm>
#include<set>
#include<vector>
#define rep(i,n) for(int i=0;i<(n);i++)

const int mod=1000000007;

int main(){
  int d,t,s;
  cin>>d>>t>>s;

if(d-t*s<=0)cout<<"Yes"<<endl;
else cout<<"No"<<endl;
return 0;
}

#include<bits/stdc++.h>
#include<string.h>
using namespace std;
int main()
{
    int x,y;
    double z;
    cin>>x>>y>>z;
    (double(x/z)<=y)?cout<<"Yes":cout<<"No";
}


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

int main(){
  ll N;
  cin>>N;
  vector
Sup;
  vector
Sdown;
  rep(i,N){
  	string s;
  	cin>>s;
    ll total=0,mini=0;
    rep(j,s.size()){
      if(s[j]=='('){total++;}
      else{total--;}
      mini=min(mini,total);
    }
    if(total>=0){Sup.push_back(P(mini,total));}
    else{Sdown.push_back(P(mini-total,-total));}
  }
  sort(Sup.begin(),Sup.end());
  reverse(Sup.begin(),Sup.end());

  sort(Sdown.begin(),Sdown.end());
  reverse(Sdown.begin(),Sdown.end());

  int flag=0;
  ll sum1=0;
  ll sum2=0;
  rep(i,Sup.size()){
  	if(Sup[i].first<0&&sum1+Sup[i].first<0){flag=1;}
  	else{sum1+=Sup[i].second;}
    if(sum1<0){flag=1;}
  }
  rep(i,Sdown.size()){
  	if(Sdown[i].first<0&&sum2+Sdown[i].first<0){flag=1;}
  	else{sum2+=Sdown[i].second;}
    if(sum2<0){flag=1;}
  }
  if(sum1!=sum2){flag=1;}
  if(flag==1){cout<<"No";}
  if(flag==0){cout<<"Yes";}
}

#include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 1000000000000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

#define SIZE 300005

char buf[SIZE];
int num_E[SIZE],num_W[SIZE];

int main(){

	int N;
	scanf("%d",&N);
	scanf("%s",buf);

	num_W[0] = 0;
	for(int i = 1; i <= N; i++){

		num_W[i] = num_W[i-1];
		if(buf[i-1] == 'W'){
			num_W[i]++;
		}
	}
	num_E[N+1] = 0;
	for(int i = N; i >= 1; i--){

		num_E[i] = num_E[i+1];
		if(buf[i-1] == 'E'){

			num_E[i]++;
		}
	}

	int ans = N;
	for(int i = 1; i <= N; i++){

		ans = min(ans,num_W[i-1]+num_E[i+1]);
	}

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

	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <map>

using namespace std;

int main() {
	int A, B, C, D;
	cin >> A >> B >> C >> D;
	int budget = min(A, B) + min(C, D);
	cout << budget << endl;
	return 0;
}

#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
typedef long long ll;
#define rep(i, s, e) for (int i = s; i < e; i++)
#define endl '\n'
int main()
{
    int n, x, t;
    cin >> n >> x >> t;
    int ans = (n +x-1)/x * t;
    cout << ans << endl;
}


#include<bits/stdc++.h>
#define ll long long
#define MOD 1000000007
#define MAX 1000000007
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define zoom ios_base::sync_with_stdio(false);cin.tie(NULL);
using namespace std;

bool cmp(pair<ll, ll> a, pair<ll, ll> b) {
    return (b.ff + b.ss > a.ff + a.ss);
}

int main() {

    zoom;
    ll n, i, j;
    cin>>n;
    vector<pair<ll, ll> >A(n);
    for(i = 0; i < n; i += 1)
        cin>>A[i].ff>>A[i].ss;
    sort(A.begin(), A.end(), cmp);
    vector<vector<ll> > dp(n, vector<ll>());
    dp[0].pb(0);
    dp[0].pb(A[0].ss);
    for(i = 1; i < n; i += 1) {
        dp[i].pb(0);
        for(j = 1; j <= i; j += 1) {
            ll temp1 = dp[i - 1][j];
            ll temp2 = -1;
            if(dp[i - 1][j - 1] != -1 && dp[i - 1][j - 1] <= A[i].ff)
                temp2 = dp[i - 1][j - 1] + A[i].ss;
            if((temp1 == -1 && temp2 == - 1))
                dp[i].pb(-1);
            else if(temp1 == -1)
                dp[i].pb(temp2);
            else if(temp2 == -1)
                dp[i].pb(temp1);
            else
                dp[i].pb(min(temp1, temp2));
        }
        if(dp[i - 1][j - 1] != -1 && dp[i - 1][j - 1] <= A[i].ff)
            dp[i].pb(dp[i - 1][j - 1] + A[i].ss);
        else
            dp[i].pb(-1);
    }
    ll ans = -1;
    for(i = n - 1; i >= 0; i -= 1) {
        for(j = i + 1; j >= 1; j -= 1) {
            if(dp[i][j] != -1)
            ans = max(ans, j);
        }
    }
    cout<<ans;
    return 0;
}

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

#define REP(i, n) for (ll (i) = 0 ; (i) < (ll)(n) ; ++(i))
#define REPN(i, m, n) for (ll (i) = m ; (i) < (ll)(n) ; ++(i))
#define REP_REV(i, n) for (ll (i) = (ll)(n) - 1 ; (i) >= 0 ; --(i))
#define REPN_REV(i, m, n) for (ll (i) = (ll)(n) - 1 ; (i) >= m ; --(i))

#define INF             2e9
#define INF_LL          1LL<<60
#define ll              long long
#define Ceil(x, n)      (((((x))+((n)-1))/n))
#define CeilN(x, n)     (((((x))+((n)-1))/n)*n)
#define FloorN(x, n)    ((x)-(x)%(n))
#define ALL(x)          (x).begin(),(x).end()
typedef pair<ll, ll>    P;

int main() {
    int n;
    cin >> n;
    vector<ll> a(n), b(n);
    vector<P> c(n);

    REP(i, n) cin >> a[i] >> b[i];
    REP(i, n) {
        c[i].first = a[i] + b[i];
        c[i].second = i;
    }
    sort(ALL(c));
    reverse(ALL(c));

    ll aoki = 0, takahashi = 0;
    REP(i, n) {
        if(i % 2 == 0) {
            aoki += a[c[i].second];
        } else {
            takahashi += b[c[i].second];
        }
    }

    cout << aoki - takahashi << endl;

    return 0;
}


#pragma GCC optimize("Ofast","inline")
#include <bits/stdc++.h>
using namespace std;
template<class t> inline t read(t &x){
	char c=getchar();bool f=0;x=0;
	while(!isdigit(c)) f|=c=='-',c=getchar();
	while(isdigit(c)) x=(x<<1)+(x<<3)+(c^48),c=getchar();
	if(f) x=-x;return x;
}
template<class t,class ...A> inline void read(t &x,A &...a){
	read(x);read(a...);
}
template<class t> inline void write(t x){
	if(x<0) putchar('-'),write(-x);
	else{if(x>9) write(x/10);putchar('0'+x%10);}
}

#define int long long

int n,cnt[100][100],ma2,ma5,ans1,ans2;

signed main(){
	read(n);
	for(int i=1;i<=n;i++){
		double x;
		scanf("%lf",&x);
		int y=floor(x*1e9+0.5),cnt2=0,cnt5=0;
		while(y%2==0) y/=2,cnt2++;
		while(y%5==0) y/=5,cnt5++;
		cnt[cnt2][cnt5]++;
		ma2=max(ma2,cnt2);
		ma5=max(ma5,cnt5);
	}
	for(int i=0;i<=ma2;i++) for(int j=0;j<=ma5;j++) if(cnt[i][j])
		for(int x=0;x<=ma2;x++) for(int y=0;y<=ma5;y++) if(cnt[x][y]&&min(i+x,j+y)>=18)
			i==x&&j==y?ans1+=cnt[i][j]*(cnt[i][j]-1)/2:ans2+=cnt[i][j]*cnt[x][y];
	write(ans2/2+ans1);
}

#include<iostream>
using namespace std;
int main()
{
    int n, a, b, c, d, i, j, team[100], score[100];
    cin >> n;
    for (i = 1; i <= n; i++) {
        team[i] = 0;
    }
    for (i = 0; i < n*(n - 1) / 2; i++) {
        cin >> a >> b >> c >> d;
        if (c > d)
            team[a] += 3;
        else if (c < d)
            team[b] += 3;
        else {
            team[a]++;
            team[b]++;
        }
    }
    for(i=1;i<=n;i++){
        score[i] = 1;
        for (j = 1; j <= n; j++) {
            if (team[i] < team[j])
                score[i]++;
        }
    }
    for (i = 1; i <= n; i++) {
        cout << score[i] << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using ll = long long;
using namespace std;
const int INFint = 1e9+1;
const ll INFll = (ll)1e18+1;
ll MOD=1e9+7;

int main(){
  int K,T;
  cin>>K>>T;
  vector<int> A(T);
  for(int i(0);i<T;i++){
    cin>>A[i];
  }
  sort(A.rbegin(),A.rend());
  int sum(0);
  if(T == 1){
    cout << A[0] - 1 << endl;
  }else{
    for(int i(1);i<T;i++){
      sum += A[i];
    }
    cout << max(A[0]-sum-1,0) << endl;
  }
}

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

int main(){
    int k,t;
    cin >> k >> t;
    multiset<int> s;
    rep(i,t){
        int temp;
        cin >> temp;
        s.insert(temp);
    }
    int st = 0;
    rep(i,k){
        if(s.size()==0){
            cout << st << endl;
            return 0;
        }

        int temp = *(--s.end());
        --temp;
        //cout << " " << temp << endl;
        s.erase(--s.end());
        //cout << "  " << s.size() << endl;
        if(st>0&&i>0) s.insert(st);
        st = temp;
    }

    cout << 0 << endl;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
  int n,ans;
  cin>>n;
  ans=((n-2)*180);
  cout<<ans<<endl;
  return 0;
}

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

//ライブラリ始まり

//定数

//円周率
const double PI = 3.1415926535897932384;

//天井
const int INF = 1000000000; // = 10^9
const ll LINF = 1000000000000000; // = 10^15

//ABC文字列
const string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZABC";
const string abc = "abcdefghijklmnopqrstuvwxyzabc";

//よくあるmodくん
const ll MOD = 1000000007; // = 10^9 + 7

//データ構造

//Union_Find木
struct UnionFind {
  vector<int> UF; // UF.at(i) : iの親の番号
  vector<int> SIZE; // SIZE.at(root(i)) : iと連結されてる要素の数
  UnionFind(int N) : UF(N), SIZE(N, 1) { // 最初は全てが根であるとして初期化
    for(int i = 0; i < N; i++) {
      UF.at(i) = i;
    }
  }
  int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
    if (UF.at(x) == x) {
      return x;
    }
    return UF.at(x) = root(UF.at(x));
  }
  void unite(int x, int y) { // xとyの木を併合
    int rx = root(x); // xの根をrx
    int ry = root(y); // yの根をry
    if (rx == ry) {
      return; // xとyの根が同じ(=同じ木にある)時はそのまま
    }
    // xとyの根が同じでない(=同じ木にない)時：小さい方の根を大きい方の根につける
    if (SIZE.at(rx) < SIZE.at(ry)) {
      UF.at(rx) = ry;
      SIZE.at(ry) += SIZE.at(rx);
      SIZE.at(rx) = 0;
    }
    else {
      UF.at(ry) = rx;
      SIZE.at(rx) += SIZE.at(ry);
      SIZE.at(ry) = 0;
    }
  }
  bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
  int size(int x) { // xと連結されてる要素の数を返す
    return SIZE.at(root(x));
  }
};

//関数

//ctoi
int ctoi(char c){
  if (c == '0') {
    return 0;
  }
  if (c == '1') {
    return 1;
  }
  if (c == '2') {
    return 2;
  }
  if (c == '3') {
    return 3;
  }
  if (c == '4') {
    return 4;
  }
  if (c == '5') {
    return 5;
  }
  if (c == '6') {
    return 6;
  }
  if (c == '7') {
    return 7;
  }
  if (c == '8') {
    return 8;
  }
  if (c == '9') {
    return 9;
  }
  return -1;
}

//素数判定
bool PN(int x) {
  if (x <= 1) {
    return false; // 1や0,-1は素数ではない
  }
  if (x == 2) {
    return true; // √2 + 1 > 2 なので下でやると 2 ％ 2 = 0 となり判定できない
  }
  for (int i = 2; i < sqrt(x) + 1; i++) {
    if (x % i == 0) {
      return false; // 割れたら素数じゃない
    }
  }
  return true;
}

//ライブラリ終わり

int main() {
  int N;
  cin >> N;
  vector<int> T(N, 1);
  for (int i = 2; i <= N; i++) {
    int M = i;
    for (int j = 2; j < sqrt(i) + 1; j++) {
      while (M % j == 0) {
        T.at(j - 1)++;
        M /= j;
      }
    }
    if (M != 1) {
      T.at(M - 1)++;
    }
  }
  ll ans = 1;
  for (int i = 0; i < N; i++) {
    ans *= T.at(i);
    ans %= MOD;
  }
  cout << ans << endl;
}


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

char ss[12][12];

void dfs(int x, int y) {
  if (x < 0 || y < 0 || x >= 12 || y >= 12 || ss[y][x] == '0')
    return;
  ss[y][x] = '0';
  dfs(x + 1, y);
  dfs(x - 1, y);
  dfs(x, y + 1);
  dfs(x, y - 1);
}
int main() {
  int n, ans = 0;
  while (cin >> ss[0][0]) {
    ans = 0;
    rep(i, 12) rep(j, 12) {
      if (i == 0 && j == 0)
        ;
      else
        cin >> ss[i][j];
    }
    rep(i, 12) rep(j, 12) {
      if (ss[i][j] == '1') {
        ans++;
        dfs(j, i);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

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

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

struct UnionFind{
    vector<int> data;
    int __size;
    UnionFind(int sz) {
    data.assign(sz, -1);
    __size = sz;
  }

  bool unite(int x, int y) {
    x = find(x), y = find(y);
    if(x == y) return (false);
    if(data[x] > data[y]) swap(x, y);//親は負でサイズを保存
    __size--;
    data[x] += data[y];
    data[y] = x;
    return (true);
  }

  int find(int k) {
    if(data[k] < 0) return (k);
    return (data[k] = find(data[k]));
  }

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

  int size(int k) {
    return (-data[find(k)]);
  }

  int unum() {
    return (__size);
  }
};

int main(){
  int h, w;
  cin >> h >> w;
  vector<string> s(h);
  rep(i,h) cin >> s[i];
  map<int,ll> mp;
  UnionFind uf(h*w);
  vector<int> b;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      for (int k = 0; k < 2; k++) {
        int ny = i + dy[k], nx = j + dx[k];
        if (ny >= h || nx >= w) continue;
        if (uf.same(i*w+j,ny*w+nx)) continue;
        if (s[i][j] != s[ny][nx]) uf.unite(i*w+j, ny*w+nx);
      }
    }
  }
  ll res = 0;
  rep(i,h) rep(j,w) if (s[i][j] == '#') mp[uf.find(i*w+j)]++;
  rep(i,h) rep(j,w) if (s[i][j] == '#') res += uf.size(i*w+j) - mp[uf.find(i*w+j)];
  cout << res << endl;
  return 0;
}


#include <iostream>

using namespace std;

int main(){
   int i;

       cin >> i;
   if(i >=0 && i <= 1000) {
       cout << i * i * i << endl;
   }else{
       return -1;
   }
   return 0;
}

//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
//end of libraries ;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// ll lmax(ll o , ll w) {if(o>w)return o; return w;}
// ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 3000003
#define PI 3.14159265359
#define endl "\n"
#define F first
#define S second
#define pb push_back
#define ll long long
#define all(c) (c).begin(),(c).end()
#define sz(c) (int)(c).size()
#define mkp(a,b) make_pair(a,b)
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
ll n;
string ans;
int main(){
fcin;
    cin >> n;
    ll p = 1;
    while(n){
        n-=p;
        char x = ('a' + (n/p)%26);
        ans = x + ans;
        p*=26;
        if(p>n) break;
    }
    cout << ans << '\n';

}


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

#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define INF 1e9
typedef long long ll;

int main() {
    int A,B; cin >> A >> B; // Aが白

    bool changed = false;
    if (A>B){
        swap(A,B);
        changed = true;
    }

    vector<vector<int>> T(100,vector<int>(100,0));

    int b = 1;
    while(b<=B){
        int yoko = (b-1)%25;
        yoko *= 4;
        int tate = (b-1)/25;
        tate *= 4;

        T[tate][yoko] = 1;
        T[tate][yoko+1] = 1;
        T[tate][yoko+2] = 1;
        T[tate+1][yoko] = 1;
        T[tate+1][yoko+1] = 1;
        T[tate+1][yoko+2] = 1;
        T[tate+2][yoko] = 1;
        T[tate+2][yoko+1] = 1;
        T[tate+2][yoko+2] = 1;

        b++;
    }

    int a = 1;
    while(a<=A-1){
        int yoko = (a-1)%25;
        yoko *= 4;
        int tate = (a-1)/25;
        tate *= 4;

        T[tate+1][yoko+1] = 0;

        a++;
    }

    cout << 100 << " " << 100 << endl;
    REP(i,100){
        REP(j,100){
            if(!changed){
                if(T[i][j]==0)cout << ".";
                else cout << "#";
            }else{
                if(T[i][j]==0)cout << "#";
                else cout << ".";
            }

        }
        cout << endl;
    }

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
int main(){
  long long a,b;	cin >> a >> b;
  long long x=__gcd(a,b);

  int ans=1;
  for(long long i=2;i*i<x;i++){
    if(x%i!=0)	continue;
    ans++;
    while(x%i==0){
      x/=i;
    }
  }
  if(x!=1)	ans++;
  cout << ans;
}


#include<bits/stdc++.h>
#define pii pair<int, int>
using namespace std;
const int MAXN = 2e5 + 7;
int a[MAXN], b[MAXN], ans[MAXN];
int L[MAXN], R[MAXN];
int main() {
    int n; scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    for (int i = 1; i <= n; i++) scanf("%d", &b[i]);
    for (int l = 1, r; l <= n; l = r + 1) {
        r = l; while (a[r + 1] == a[l] && r < n) r++;
        L[a[l]] = l; R[a[l]] = r;
    }
    int MAX = 0;
    for (int l = 1, r; l <= n; l = r + 1) {
        r = l; while (b[r + 1] == b[l] && r < n) r++;
        if (L[b[l]]) {
            MAX = max(MAX, R[b[l]] - l + 1);
        }
    }
    bool fail = 0;
    for (int i = 1; i <= n; i++) {
        ans[(i + MAX - 1) % n + 1] = b[i];
    }
    for (int i = 1; i <= n; i++)
        if (ans[i] == a[i]) fail = 1;
    if (fail) puts("No");
    else {
        puts("Yes");
        for (int i = 1; i <= n; i++) printf("%d%c", ans[i], " \n"[i == n]);
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize("Ofast")
#define rep(i,n) for(long long i=0;i<(long long)(n);i++)
#define REP(i,k,n) for(long long i=k;i<(long long)(n);i++)
#define all(a) a.begin(),a.end()
#define eb emplace_back
#define lb(v,k) (lower_bound(all(v),k)-v.begin())
#define ub(v,k) (upper_bound(all(v),k)-v.begin())
#define fi first
#define se second
#define pi M_PI
#define PQ(T) priority_queue<T>
#define SPQ(T) priority_queue<T,vector<T>,greater<T>>
using ll=long long;
using P=pair<ll,ll>;
using PP=tuple<ll,ll,ll>;
using S=multiset<ll>;
using vi=vector<ll>;
using vvi=vector<vi>;
using vvvi=vector<vvi>;
const ll inf=1001001001001001;
const int INF=1001001001;
const int mod=1000000007;
const double eps=1e-10;
template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<class T> void out(T a){cout<<a<<'\n';}
template<class T> void outp(T a){cout<<'('<<a.fi<<','<<a.se<<')'<<'\n';}
template<class T> void outvp(T v){rep(i,v.size())cout<<'('<<v[i].fi<<','<<v[i].se<<')';cout<<'\n';}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';}
template<class T> void outvv(T v){rep(i,v.size())outv(v[i]);}
template<class T> bool isin(T x,T l,T r){return (l)<=(x)&&(x)<=(r);}
template<class T> void YesNo(T b){if(b)out("Yes");else out("No");}
void decimal(int x){cout<<fixed<<setprecision(x);}
ll GCD(ll a,ll b){if(b==0) return a; return GCD(b,a%b);}
ll LCM(ll a,ll b){return(a/GCD(a,b))*(b/GCD(a,b))*GCD(a,b);}
ll POW(ll a,ll b){a%=mod;if(b==0)return 1;if(b&1)return a*POW(a,b-1)%mod;ll k=POW(a,b/2);return k*k%mod;}
vi calc(ll x){vi res;while(x>0){res.eb(x%10);x/=10;}reverse(all(res));return res;}

void solve(){
  ll n; cin>>n;
  vi a(n),b(n);
  rep(i,n) cin>>a[i];
  rep(i,n) cin>>b[i];
  //Yes or No
  vi c(200001,0),d(200001,0);
  rep(i,n) c[a[i]]++;
  rep(i,n) d[b[i]]++;
  bool f=true;
  rep(i,200001) if(c[i]+d[i]>n) f=false;
  YesNo(f);
  ll p=0;
  vi ans(n);
  //show example
  if(f){
    REP(i,1,200001){
      c[i]+=c[i-1];
      d[i]+=d[i-1];
    }
    REP(i,1,200001) chmax(p,c[i]-d[i-1]);
    rep(i,n) ans[(i+p)%n]=b[i];
    outv(ans);
  }
}

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


#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
using namespace std;
#define INF ((1<<30)-1)
#define LINF (1LL<<60)
#define EPS (1e-10)
typedef long long ll;
typedef pair<ll, ll> P;
const int MOD = 1000000007;
const int MOD2 = 998244353;

ll v[55];

int main(){
    int n, k;
    cin >> n >> k;
    rep(i, n) cin >> v[i];

    int m = min(n, k);

    ll ans = 0;

    rep(i, m+1){ // i個取り出す時を考える
        rep(l, i+1){
            vector<ll> tmp;
            rep(j, l) tmp.push_back(v[j]);
            int r = i - l;
            // cout << l << " " << r << endl;
            for(int j = n-1; j >= n - r; j--) tmp.push_back(v[j]);
            // rep(j, tmp.size()) cout << tmp[j] << " ";
            // cout << endl;

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

            int res = k - (l+r);
            ll s = 0;
            rep(j, tmp.size()){
                if (tmp[j] < 0 && res > 0){
                    res--;
                }
                else s += tmp[j];
            }
            ans = max(s, ans);
        }
    }

    cout << ans << endl;

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
ll ans = 1;
const ll mo = 1000000007;

int main()
{
ll n , m ;
cin >> n >> m ;
vector<ll> a(n);
rep(i, n){
  cin >> a[i];
}
vector<ll> b(m);
rep(i, m){
  cin >> b[i];
}
sort(a.rbegin(),a.rend());
sort(b.rbegin(),b.rend());
rep(i,n-1)if(a[i]==a[i+1])ans=0;
rep(i,m-1)if(b[i]==b[i+1])ans=0;
a.push_back(-1);
b.push_back(-1);
if(a[0]!=n*m||b[0]!=n*m)ans=0;
ll in=0,im=0;
for (ll i = n*m; i > 0; i--)
{
  while(i<a[in]&&in!=n)in++;
  while(i<b[im]&&im!=m)im++;
  if(a[in]==i&&b[im]==i)continue;
  else if(a[in]==i)ans=(ans*im)%mo;
  else if(b[im]==i)ans=(ans*in)%mo;
  else{
    if(in*im-n*m+i<=0)ans=0;
    else ans=(ans*(in*im-n*m+i))%mo;
  }
}

cout << ans << endl;
return 0;
}


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

int main(){
  string str;
  while(cin >> str){
    vector<char> v;
    int ci, cj, co;
    int lv = 0;
    for(int i=0; i<str.size(); ++i) v.push_back(str[i]);
    ci = 0;
    cj = 0;
    co = 0;
    for(int i=0; i<v.size(); ++i){
      if(v[i] == 'J'){
	++cj;
      }else if(cj>0){
	for(;i<v.size(); ++i){
	  if(v[i] == 'O'){
	    ++co;
	  }else if(co>0){
	    for(;i<v.size(); ++i){
	      if(v[i] == 'I'){
		++ci;
	      }
	      if(v[i] != 'I' || i == v.size()-1){
		if(co <= cj && co <= ci && co > lv)lv = co;
		co = 0;
		ci = 0;
		cj = 0;
		//cout << "cj:co:ci:i = " <<cj<<":"<<co<<":"<<ci<<":"<<i<< endl;
 		//--i;
		break;
	      }
	    }
	  }
	  if(v[i]!='O'){
	    co = 0;
	    cj = 0;
	    ci = 0;
	    i -=1;
	    break;
	  }
	}
      }
    }
    //cout << "-----------------------------"<< endl;
    cout << lv << endl;
  }
}

#include <bits/stdc++.h>

using namespace std;

int CountOfJOI[3];
const string target = "JOI";

int main() {
	int len, curPos, k;
	string input;

	k = 0;
	cin >> input;
	len = input.size();
	curPos = 0;
	while (curPos < len) {
		if (input[curPos] != 'J') {
			++curPos;
			continue;
		}
		memset(CountOfJOI, 0, sizeof(CountOfJOI));
		for (int i = 0; i < 3; ++i) {
			while (curPos < len && input[curPos] == target[i]) {
				++curPos;
				++CountOfJOI[i];
			}
		}
		if (CountOfJOI[1] <= CountOfJOI[0] && CountOfJOI[1] <= CountOfJOI[2]) {
			k = max(k, CountOfJOI[1]);
		}
	}
	cout << k << endl;

	return 0;
}


#include <iostream>
#include <string>

using namespace std;

int main(){
	int n;
	cin >> n;
	while(n--){
		int runner = 0, out = 0, score = 0;
		while(out < 3){
			string e;
			cin >> e;
			if(e == "HIT"){
				runner = (runner << 1) + 1;
				if(runner & 0xf8){ ++score; }
			}else if(e == "HOMERUN"){
				++score;
				if(runner & 0x01){ ++score; }
				if(runner & 0x02){ ++score; }
				if(runner & 0x04){ ++score; }
				runner = 0;
			}else if(e == "OUT"){
				++out;
			}
			runner &= 0x07;
		}
		cout << score << endl;
	}
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

using i64 = long long;

const i64 MOD = 998244353;
const i64 INF = 998244353;

template <typename T>
bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

template <typename T>
bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}

template <i64 mod = MOD>
struct ModInt{
    i64 p;

    ModInt() : p(0){}
    ModInt(i64 x){p = x >= 0 ? x % mod : x + (-x + mod - 1) / mod * mod;}

    ModInt& operator+=(const ModInt& y){p = p + *y - ((p + *y) >= mod ? mod : 0); return *this;}
    ModInt& operator-=(const ModInt& y){p = p - *y + (p - *y < 0 ? mod : 0); return *this;}
    ModInt& operator*=(const ModInt& y){p = (p * *y) % mod; return *this;}
    ModInt& operator%=(const ModInt& y){if(y)p %= *y; return *this;}

    ModInt operator+(const ModInt& y) const{ModInt x = *this; return x += y;}
    ModInt operator-(const ModInt& y) const{ModInt x = *this; return x -= y;}
    ModInt operator*(const ModInt& y) const{ModInt x = *this; return x *= y;}
    ModInt operator%(const ModInt& y) const{ModInt x = *this; return x %= y;}

    friend ostream& operator<<(ostream& stream, const ModInt<mod>& x){
        stream << *x;
        return stream;
    }

    friend ostream& operator>>(ostream& stream, const ModInt<mod>& x){
        stream >> *x;
        return stream;
    }

    ModInt& operator++(){p = (p + 1) % mod; return *this;}
    ModInt& operator--(){p = (p - 1 + mod) % mod; return *this;}

    bool operator==(const ModInt& y) const{return p == *y;}
    bool operator!=(const ModInt& y) const{return p != *y;}

    const i64& operator*() const{return p;}
    i64& operator*(){return p;}

};

using mint = ModInt<>;

auto mpow = [](auto x, i64 y){
    auto z = x;
    decltype(x) val = y & 1 ? x : decltype(x)(1);
    while(z *= z, y >>= 1)
        if(y & 1)
            val *= z;
    return val;
};

signed main(){
    int n;
    cin >> n;
    vector<int> a(n);
    map<int,int> v;
    for(int i = 0; i < n; ++i){
        cin >> a[i];
        ++v[a[i]];
    }
    if(a[0] != 0 || v[0] != 1){
        cout << 0 << endl;
        return 0;
    }
    int ma = *max_element(a.begin(), a.end());

    mint ans = 1;
    for(int i = 1; i <= ma; ++i){
        if(v[i] == 0){
            cout << 0 << endl;
            return 0;
        }
        ans *= mpow(mint(v[i - 1]), v[i]);
    }
    cout << ans << endl;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define EPS 1e-10

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

ll n;
ll index, a, b;
ll sum[5000];
V vi;

int main(){
	while (cin >> n&&n){
		vi.clear();
		fill(sum, sum + 5000, 0);
		REP(i, n){
			cin >> index >> a >> b;
			if (find(ALL(vi), index) == vi.end()){
				vi.push_back(index);
			}
			if (sum[index] >= 1000000)continue;
			sum[index] += a*b;
		}
		bool f = 1;
		REP(i, vi.size()){
			if (sum[vi[i]] >= 1000000){
				cout << vi[i] << endl;
				f = 0;
			}
		}
		if (f)cout << "NA" << endl;
	}
}

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

using namespace std;

int main(){

	int n;
	while(cin>>n,n){

		map<long double,set<string> > ans;
		for(int i=0;i<n;i++){
			string name;
			long double p,a,b,c,d,e,f,s,m;
			cin>>name>>p>>a>>b>>c>>d>>e>>f>>s>>m;
			ans[((f*s*m)-p)/(a+b+c+m*(d+e))].insert(name);
		}

		for(auto it=ans.rbegin();it!=ans.rend();it++){
			set<string> ss =it->second;
			for(auto it2=ss.begin();it2!=ss.end();it2++) cout<<(*it2)<<endl;

		}
		cout<<"#"<<endl;
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<n; i++)
#define rep2(i,a,n) for(int i=(a); i<(n); i++)
#define all(vec) vec.begin(),vec.end()
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
int main() {
  int N,T; cin>>N>>T;
  vi time(N);
  rep(i,N) cin>>time[i];
  ll ans = 0;
  int ct=0;
  rep(i,N){
    if(time[i]>=ct){
      ans += T;
    }
    else {
      ans = ans + time[i] - ct + T;
    }
    ct = time[i]+T;
  }
  cout << ans << endl;
}

#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
#define get(A,B,C,D) (rui[B][D]-rui[A-1][D]-rui[B][C-1]+rui[A-1][C-1])
short dp[102][102][102][102];
int map[100][100];
short rui[101][101];
int main()
{
	int mx,my;
	scanf("%d%d",&mx,&my);
	int sx,sy;
	for(int i=0;i<mx;i++)
	{
		for(int j=0;j<my;j++)
		{
			char z;
			scanf(" %c",&z);
			if(z=='o')map[i][j]=1;
			if(z=='E')sx=i,sy=j;
		}
	}
	for(int i=0;i<mx;i++)for(int j=0;j<my;j++)rui[i+1][j+1]=rui[i+1][j]+rui[i][j+1]-rui[i][j]+map[i][j];
	short maxi=0;
	for(int i=1;i<=mx;i++)
	{
		for(int j=mx;j>=i-1;j--)
		{
			for(int k=1;k<=my;k++)
			{
				for(int l=my;l>=k-1;l--)
				{
					short t=dp[i][j][k][l];
					maxi=max(maxi,t);
					if(i>j||k>l)continue;
					int si=1+sx-(mx-j);
					int sj=1+sx+(i-1);
					int sk=1+sy-(my-l);
					int sl=1+sy+(k-1);
					dp[i+1][j][k][l]=max(dp[i+1][j][k][l],t);
					dp[i][j-1][k][l]=max(dp[i][j-1][k][l],t);
					dp[i][j][k+1][l]=max(dp[i][j][k+1][l],t);
					dp[i][j][k][l-1]=max(dp[i][j][k][l-1],t);
					if(si<=i)dp[i+1][j][k][l]=max(dp[i+1][j][k][l],short(t+get(i,i,max(sk,k),min(sl,l))));
					if(sj>=j)dp[i][j-1][k][l]=max(dp[i][j-1][k][l],short(t+get(j,j,max(sk,k),min(sl,l))));
					if(sk<=k)dp[i][j][k+1][l]=max(dp[i][j][k+1][l],short(t+get(max(si,i),min(sj,j),k,k)));
					if(sl>=l)dp[i][j][k][l-1]=max(dp[i][j][k][l-1],short(t+get(max(si,i),min(sj,j),l,l)));
				}
			}
		}
	}
	printf("%d\n",maxi);
}


#include <bits/stdc++.h>

using namespace std;

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<vector<bool>> a(n, vector<bool>(m));
  for (int i = 0; i < n; i++) {
    string foo;
    cin >> foo;
    for (int j = 0; j < m; j++) {
      a[i][j] = foo[j] == '#';
    }
  }
  queue<pair<int, int>> que;
  vector<vector<int>> dist(n, vector<int>(m, -1));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i][j]) {
        que.push({i, j});
        dist[i][j] = 0;
      }
    }
  }
  while (!que.empty()) {
    auto u = que.front();
    que.pop();
    int x = u.first, y = u.second;
    for (int i = 0; i < 4; i++) {
      int tx = x + dx[i], ty = y + dy[i];
      if (0 <= tx && tx < n && 0 <= ty && ty < m && dist[tx][ty] == -1) {
        dist[tx][ty] = dist[x][y] + 1;
        que.push({tx, ty});
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      ans = max(ans, dist[i][j]);
    }
  }
  cout << ans << '\n';
  return 0; // :)
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);++i)
#define FOR(i,n,j) for(int i=(j);i<(n);++i)
#define ssort(n) sort((n).begin(),(n).end())
#define rsort(n) sort((n).begin(),(n).end(),greater<int>())
using ll=long long;
using ld=long double;
typedef pair<int,int> P;
typedef pair<P,int> COST;
#define repl(i,n) for(ll i=0;i<(n);++i)
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
using Graf=vector<vector<int>>;
#define MAX 10000000

int main()
{
    int n;
    cin >> n;
    vector<ll> a(n);
    rep(i,n){
        cin >> a[i];
    }
    ll sum=0;
    ll tmp=0;
    int count=0;

    rep(i,n){
        if(a[i]<0)count++;
        a[i]=abs(a[i]);
        sum+=a[i];
    }
    ssort(a);
    if(count%2==0){
        cout << sum << endl;
    }else{
        cout << sum-2*a[0] << endl;
    }
}

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<queue>
#include<deque>
#include<cmath>
#include<map>
#include<unordered_map>
#include<set>
#include<cstring>
#include<iomanip> //cout << fixed << setprecision(15) << x << endl;

using namespace std;
typedef long long ll;
const ll INF = 1e9 + 6;
const ll MOD = 1e9 + 7;
const ll LLINF = 1LL<<60;
#define Pint pair<int, int>
#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
/* -- template -- */
const int mod = 998244353;
long long modpow(long long a, long long n) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

int main() {
    int N; cin >> N;
    map<int, ll> m;
    int D[N]; rep(i, N) cin >> D[i], m[D[i]]++;
    bool ok = true;
    if(D[0] != 0) ok = false;
    if(m[0] != 1) ok = false;
    ll ans = 1;
    ll prev = m[0], prev_num = -1;
    for(auto itr = m.begin(); itr != m.end(); ++itr) {
        if(prev_num != itr -> first - 1) ok = false;
        ans = (ans * modpow(prev, itr -> second)) % mod;
        prev = itr -> second;
        prev_num = itr -> first;
    }
    cout << ((ok) ? ans : 0) << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define run ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0);
#define MOD 1000000007
#define ms(a,x) memset(a,x,sizeof(a))

int main()
{
    run;
   ll n,i,ans=0;
   cin>>n;
   ll a[n], b[n+1]={0};
   for(i=0;i<n;i++)
   {
      cin>>a[i];
     b[i+1]+=b[i]+a[i];
    }
      for(i=0;i<n;i++)
      {
     ll sum=(b[n]-b[i+1])%MOD;
    	ans+=a[i]*sum;
    	ans%=MOD;
      }
      cout<<ans<<endl;

}

#include <iostream>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

bool is_prime(int n) {
  for (int p = 2; p * p <= n; p++) {
    if (n % p == 0) {
      return false;
    }
  }

  return true;
}

vector<pair<int, int>> factorize(int n) {
  vector<pair<int, int>> result;
  for (int p = 2; p * p <= n; p++) {
    if (is_prime(p)) {
      int cnt = 0;
      while (n % p == 0) {
        n /= p;
        cnt++;
      }
      if (cnt > 0) {
        result.push_back(make_pair(p, cnt));
      }
    }
  }
  if (n != 1) {
    result.push_back(make_pair(n, 1));
  }
  return move(result);
}

int main() {
  int n;
  cin >> n;
  vector<int> e(n + 1, 0);
  for (int i = 1; i <= n; i++) {
    auto factor = factorize(i);
    for (const auto f : factor) {
      int p, j;
      tie(p, j) = f;
      e[p] += j;
    }
  }

  const int64_t MOD = 1e9 + 7;
  int64_t result = 1;
  for (int i = 1; i <= n; i++) {
    result *= e[i] + 1;
    result %= MOD;
  }
  cout << result << endl;

  return 0;
}


#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

bool block[6][110][110];
bool visit[110][110];

int main(){
	int w, h;
	int xs, ys, xg, yg;
	int n, c, d, x, y;
	while(scanf("%d%d%d%d%d%d%d", &w, &h, &xs, &ys, &xg, &yg, &n) == 7){
		memset(block, 0, sizeof block);
		memset(visit, 0, sizeof visit);

		for(int i = 0; i < n; ++i){
			scanf("%d%d%d%d", &c, &d, &x, &y);

			int dx, dy;
			if(d){ dx = 2; dy = 4; }
			else{  dy = 2; dx = 4; }

			for(int j = 0; j < dy; ++j)
			for(int k = 0; k < dx; ++k){
				block[c][y + j][x + k] = true;
			}
		}

		for(c = 5; c > 1 && !block[c][ys][xs]; --c);

		if(c){
			queue<int> q;
			q.push(ys << 16 | xs);
			visit[ys][xs] = true;
			while(!q.empty()){
				y = q.front() >> 16;
				x = q.front() & 0xffff;
				q.pop();

				for(int dy = -1; dy <= 1; ++dy)
				for(int dx = -1; dx <= 1; ++dx){
					if(block[c][y+dy][x+dx] && !visit[y+dy][x+dx]){
						visit[y+dy][x+dx] = true;
						q.push(y+dy << 16 | x+dx);
					}
				}
			}
		}

		puts(visit[yg][xg] ? "OK" : "NG");
	}
}

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

string ans;
int w,h,gx,gy;
int map[110][110];
int dx_[4] = {0,1,0,-1};
int dy_[4] = {1,0,-1,0};
int dx[2][8] = {
	{0,1,2,3,0,1,2,3},
	{0,1,0,1,0,1,0,1}
};
int dy[2][8] = {
	{0,0,0,0,1,1,1,1},
	{0,0,1,1,2,2,3,3},
};
void solve(int x,int y,int color){
	if( ans == "OK" || color == 0 ) return;

	for(int i=0 ; i<4 ; ++i ){
		int mx = x + dx_[i];
		int my = y + dy_[i];
		if( mx<0 || mx>w || my<0 || my>h ) continue;

		if( map[my][mx] == color ){
			if( mx == gx && my == gy ){
				ans = "OK";
			}else{
				map[my][mx] = 0;
				solve( mx , my , color );
			}
		}
	}
}

int main(){
	int sx,sy,n;

	while( cin >> w >> h , w|h ){
		ans = "NG";
		for(int y=0 ; y<110 ; ++y )
			for(int x=0 ; x<110 ; ++x )
				map[y][x] = 0;

		cin >> sx >> sy >> gx >> gy >> n ;
		for(int i=0 ; i<n ; ++i ){
			int c,d,x,y;
			cin >> c >> d >> x >> y;
			for(int j=0 ; j<8 ; j++){
				int mx = x + dx[d][j];
				int my = y + dy[d][j];
				map[my][mx] = c;
			}
		}
		solve( sx , sy , map[sy][sx] );
		cout << ans << endl;
	}
}


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

int main(){
  int h,w;
  cin >> h >> w;
  vector<vector<int>> a(h,vector<int>(w,0));
  for(int i=0;i<h;++i){
    for(int j=0;j<w;++j){
      cin >> a[i][j];
    }
  }
  vector<tuple<int,int,int,int>> ans;
  for(int i=0;i<h;++i){
    for(int j=0;j<w-1;++j){
      if(a[i][j]%2==1){
        a[i][j]--;
        a[i][j+1]++;
        ans.push_back(make_tuple(i,j,i,j+1));
      }
    }
  }
  for(int i=0;i<h-1;++i){
    if(a[i][w-1]%2==1){
      a[i][w-1]--;
      a[i+1][w-1]++;
      ans.push_back(make_tuple(i,w-1,i+1,w-1));
    }
  }
  int n=ans.size();
  cout << n << endl;
  for(auto p : ans){
    cout << get<0>(p)+1 << " " << get<1>(p)+1 << " " << get<2>(p)+1 << " " << get<3>(p)+1 << endl;
  }
  return 0;
}

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

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

#define rep(i,n) for(ll (i)=0; (i)<(ll)(n); (i)++)
#define frep(i,m,n) for(ll (i)=(m); (i)<=(ll)(n); (i)++)
#define rrep(i,n) for(ll (i)=(n)-1; (i)>-1; (i)--)
#define frrep(i,m,n) for(ll (i)=(n); (i)>(ll)(m); (i)--)
#define ALL(x) (x).begin(), (x).end()

const ll INF = 100100100100100100;
const ll MOD = 1000000007;

// get abs
ll my_abs(ll a);
// a^n
ll a_n(ll a, ll n);
// get gcd
ll my_gcd(ll a, ll b);
// a^(-1) % MOD
ll inv(ll a);
// (a+b+c)%MOD
ll madd(ll a, ll b, ll c);
// (a-b)%MOD
ll msub(ll a, ll b);
// (a*b*c)%MOD
ll mtime(ll a, ll b, ll c);

int main() {
    ll h, w;
    cin >> h >> w;
    vector<vector<bool>> odd(h, vector<bool>(w, false)); // 奇数ならtrue;
    vector<pair<P,P>> ans;
    rep(i, h) {
        rep(j, w) {
            ll a; cin >> a;
            if((a%2) == 1) odd[i][j] = true;
        }
    }
    rep(i, h) {
        bool ok = false; // 奇数から取ったコインを移動中
        rep(j, w) {
            if(odd[i][j]) {
                if(ok) {
                    ok = false;
                }
                else {
                    ok = true;
                    if(j != w-1) ans.push_back(make_pair(P(i+1,j+1), P(i+1,j+2)));
                }
            }
            else {
                if(ok && (j != w-1)) {
                    ans.push_back(make_pair(P(i+1,j+1), P(i+1,j+2)));
                }
            }
        }
        if(ok && i != h-1) {
            ans.push_back(make_pair(P(i+1,w), P(i+2,w)));
            odd[i+1][w-1] = !(odd[i+1][w-1]);
        }
    }
    cout << ans.size() << endl;
    rep(i, ans.size()) {
        cout << ans[i].first.first << ' ';
        cout << ans[i].first.second << ' ';
        cout << ans[i].second.first << ' ';
        cout << ans[i].second.second << endl;
    }

    return 0;
}

ll my_abs(ll a) {
    if(a >= 0) return a;
    else return -1 *a;
}

ll a_n(ll a, ll n) {
    if(n == 0) return 1;

    ll ret = a, count = 1;
    while(count * 2 < n) {
        ret *= ret;
        count *= 2;
    }
    if(count == n) return ret;
    else return (ret * a_n(a, n-count));
}

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

ll inv(ll a) {
    return a_n(a, MOD-2);
}

ll madd(ll a, ll b, ll c) {
    ll ret = (a+b) % MOD;
    return (ret+c) % MOD;
}

ll msub(ll a, ll b) {
    if(a < b) return (a-b+MOD) % MOD;
    else return (a-b) % MOD;
}

ll mtime(ll a, ll b, ll c) {
    ll ret = (a*b) % MOD;
    return (ret*c) % MOD;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

const int mod = 1000000007;
//const int mod = 998244353;

//#define int long long
//signed main(){
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n;
    cin >> n;
    ll a[n];
    for(ll i = 0; i < n; i++) {
        cin >> a[i];
    }
    ll cnt[3][n+1];
    cnt[0][0]=0;
    cnt[1][0]=0;
    cnt[2][0]=0;

    for(ll i = 0; i < n; i++) {
        ll c=0;
        while(cnt[c][i]!=a[i]){
            c++;
	        if(c==3){
    	        cout << "0" << "\n";
        	    return 0;
            }
        }
        for(ll j = 0; j < 3; j++) cnt[j][i+1]=cnt[j][i];
        cnt[c][i+1]++;
    }

    for(ll i = 0; i < 3; i++) {
        for(ll j = 0; j < n; j++) {
            cerr << std::right << std::setw(2) << cnt[i][j];
        }
        cerr << "\n";
    }

    ll ans=1;
    for(ll i = 0; i < n; i++) {
        ll tmp=0;
        for(ll j = 0; j < 3; j++) if(a[i]==cnt[j][i]) tmp++;
        cerr << std::right << std::setw(2) << tmp;
        ans=(ans*tmp)%mod;
    }
    cout << ans << "\n";
    return 0;
}

#include<iostream>
#include<algorithm>
#include<vector>
#include<stdio.h>
#include<cstring>
#include<math.h>
#include<map>
#include<bitset>
#include<iomanip>
#include<queue>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
using P = pair<int, int>;

const long long MOD = 1000000007;
long long N;
long long a[100000] = {0};

int main() {
  cin >> N;
  long long ans = 1;
  rep(i, N) {
    long long A;
    cin >> A;
    long long temp;
    if(A == 0) {
      temp = 3 - a[A];
    } else {
      temp = a[A-1] - a[A];
    }
    ans =  ans * temp % MOD;
    a[A]++;
  }
  if(ans <= 0) cout << 0 << endl;
  else cout << ans << endl;
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
int gcd(int x, int y) { return (x % y)? gcd(y, x % y): y; }    //最大公約数
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }     //最小公倍数
using Graph = vector<vector<ll>>;
ll inf=300000000000000000;
const double PI = 3.14159265358979323846;
int main(){
  ll n;
  cin >> n;
  string s="";
  ll m=1;
  if(n==0){
    cout << 0 << endl;
    return 0;
  }
  while(n!=0){
    if(abs(n)%(abs(m)*2)!=0){
      s='1'+s;
      n-=m;
    }
    else s='0'+s;
    m*=-2;
  }
  cout << s << endl;
}

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#include <bits/stdc++.h>
using namespace std;
#define getchar getchar_unlocked
template <class T>
inline void _read(T &x)
{
    x = 0;
    char t = getchar();
    while (!isdigit(t)) t = getchar();
    while (isdigit(t))
    {
        x = x * 10 + t - '0';
        t = getchar();
    }
}
constexpr int MAXN = 1e6 + 10, mo = 998244353;
int n, m, ans, sum, fac[MAXN], inv[MAXN];
inline int C(int n, int m)
{
    return 1ll * fac[n] * inv[m] % mo * inv[n - m] % mo;
}
int main()
{
    _read(n), _read(m);
    if (n < m)
        swap(n, m);
    ans = n;
    fac[0] = fac[1] = inv[0] = inv[1] = 1;
    for (int i = 2; i <= n + m; ++i)
        fac[i] = 1ll * fac[i - 1] * i % mo, inv[i] = 1ll * (mo - mo / i) * inv[mo % i] % mo;
    for (int i = 2; i <= n + m; ++i)
        inv[i] = 1ll * inv[i] * inv[i - 1] % mo;
    for (int i = 1; i <= m; ++i)
        sum = (1ll * C(i << 1, i) * C(n + m - (i << 1), n - i) + sum) % mo;
    printf("%d", (1ll * sum * inv[2] % mo * inv[n + m] % mo * fac[n] % mo * fac[m] + ans) % mo);
    return 0;
}


#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;
int main(){
    int n;
    cin>>n;
    vector<int> l(n*2);
    rep(i,n*2) cin>>l.at(i);
    sort(l.begin(),l.end());
    int ans=0;
    rep(i,n){
        ans+=l.at(2*i);
    }
    cout<<ans;
}

// C - 2D Plane 2N Points

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// const int INF = 2147483647;
// const ll INF = 9223372036854775807;
// const ll MOD = 1e9 + 7;

pair<int, int> red[100];
pair<int, int> blue[100];
bool used_red[100];

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

	for (int i=0; i<N; i++) {
		int a, b;
		cin >> a >> b;
		red[i] = make_pair(a, b);
	}

	for (int i=0; i<N; i++) {
		int c, d;
		cin >> c >> d;
		blue[i] = make_pair(c, d);
	}

	sort(blue, blue+N);

	int ans = 0;

	for (int i=0; i<N; i++) {
		int bx = blue[i].first;
		int by = blue[i].second;

		int max_ry = -1;
		int max_red_idx = -1;
		for (int j=0; j<N; j++) {
			int rx = red[j].first;
			int ry = red[j].second;

			if (rx < bx && ry < by && ry > max_ry && used_red[j] == false) {
				max_red_idx = j;
				max_ry = ry;
			}
		}

		if (max_red_idx != -1) {
			ans++;
			used_red[max_red_idx] = true;
		}
	}

	cout << ans << endl;

	return 0;
}