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

int main() {
  string S;
  cin>>S;
  int Ans=0;
  if(S=="SUN"){Ans=7;}
  else if(S=="MON"){Ans=6;}
  else if(S=="TUE"){Ans=5;}
  else if(S=="WED"){Ans=4;}
  else if(S=="THU"){Ans=3;}
  else if(S=="FRI"){Ans=2;}
  else {Ans=1;}
  cout<<Ans<<endl;
}

#if __has_include("debug.h")
    #include "debug.h"
#else
#include <bits/stdc++.h>
using namespace std;
#define d(...) 2;
#endif

#define int long long int
#define ld long double

#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl "\n"

#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define ff first
#define ss second
#define eps 1e-10
#define PI acos(-1LL)

#define gcd(a,b)            __gcd((a),(b))
#define lcm(a,b)            ((a*b)/gcd(a,b))

void solve();

int32_t main()
{
    #ifdef DEBUG
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
    #endif

    fast

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

	while(t--)
	    solve();
	return 0;
}

void solve()
{
	int i,j;
	string s;
	cin>>s;

	vector<string>v{"MON","TUE","WED","THU","FRI","SAT","SUN"};

	int cnt=0;

	while(1)
	{
		for(i=0;i<v.size();i++)
		{
			if(v[i]==s)
			{
				j=(i+1)%7;
				cnt++;
				while(v[j]!="SUN")
				{
					cnt++;
						j=(j+1)%7;
				}
				cout<<cnt<<endl;
				return;
			}
		}
	}

}


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

string a(int aa, int b) {
  if(aa <= 8 && b <= 8) {
    return "Yay!";
  }
  return ":(";

}

int main() {
  int aa, b;
  cin >> aa >> b;
  cout << a(aa, b);
}

#include <bits/stdc++.h>
#define rep(i, n) for(int i= 0; i < (n); i++)
using ll= long long int;
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
ll mod= 1e9 + 7;

int main(){
  int a,b;
  cin >> a >>b;
  if(a<=8 && b<=8){cout << "Yay!" << endl;}else{cout << ":(" << endl;}
}


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

int main(){
    string s;
    cin >> s;
    int i=0;
    int ac=0;

    while((i<s.size()-1)&&(ac==0)){
        if((s.at(i)=='A')&&(s.at(i+1)=='C')){
            ac++;
        }
        i++;
    }
    if(ac==1){
        cout << "Yes" << endl;
    }
    else{
        cout << "No" << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define INF 114514810
#define ll long long
#define ALL(a) (a).begin(),(a).end()
#define SORT(v) sort(ALL(v))
//#define scanf scanf_s
typedef pair<int, int> P;

int n,m,q;
int a[105];
int main()
{
	while (cin >> n >> q, n)
	{
		memset(a, 0, sizeof(a));
		REP(i, n)
		{
			cin >> m;
			int d;
			REP(j, m)
			{
				cin >> d;
				a[d]++;
			}
		}
		int* max = max_element(a,a+104);
		if (*max >= q) cout << max-a << endl;
		else cout << 0 << endl;
	}
	return 0;
}

#include<iostream>
#include<string>
#include<vector>
#include<cstdio>
#include<sstream>
#include<algorithm>
#include<cmath>
#include<map>
using namespace std;
int main(){
int n, dp[50];
while(cin>>n,n!=0){
fill(dp,dp+50,0);
dp[0]=0,dp[1]=1,dp[2]=1,dp[3]=2;
for(int i=3;i<=n+1;i++)dp[i]=dp[i-1]+dp[i-2]+dp[i-3];
int ans=dp[n+1];
ans/=3650;
if(dp[n]%3650!=0)ans++;
cout<<ans<<endl;
}
return 0;
}


#include<bits/stdc++.h>
#define mems(a,x) memset(a,x,sizeof(a))
#define first fi
#define second se
using namespace std;
typedef long long ll;
const int mod=1e9+7,N=105;
const double eps=1e-8,pi=3.1415926535898;
ll inv(ll x){return x==1?x:(mod-mod/x)*inv(mod%x)%mod;}
ll inv(ll x,ll mod){return x==1?x:(mod-mod/x)*inv(mod%x,mod)%mod;}
ll qpow(ll a,ll n){ll ans=1;while(n){if(n&1) ans=ans*a%mod;a=a*a%mod;n>>=1;}return ans;}
ll mul(ll a,ll b){ll ans=0;while(b){if(b&1) ans=(ans+a)%mod;a=(a+a)%mod;b>>=1;};return ans;}
ll qpow(ll a,ll n,ll mod){ll ans=1;while(n){if(n&1) ans=ans*a%mod;a=a*a%mod;n>>=1;}return ans;}
ll mul(ll a,ll b,ll mod){ll ans=0;while(b){if(b&1) ans=(ans+a)%mod;a=(a+a)%mod;b>>=1;};return ans;}
struct vec
{
    double x,y;
    int id;
    vec(double x=0,double y=0):x(x),y(y){}
    vec operator+(const vec&o)const{ return vec(x+o.x,y+o.y);}
    vec operator-(const vec&o)const{ return vec(x-o.x,y-o.y);}
    double operator*(const vec&o)const{ return x*o.x+y*o.y;}
    double operator^(const vec&o)const{ return x*o.y-y*o.x;}
    vec operator/(const double&o)const{ return vec(x/o,y/o);}
    vec operator*(const double&o)const{ return vec(x*o,y*o);}
    void sc(){scanf("%lf%lf",&x,&y);}
    double len(){return sqrt(x*x+y*y);}
}a[N],s[N];
int n,top;
bool cmp(vec x,vec y)
{
    double k1=atan2(x.y-a[1].y,x.x-a[1].x),k2=atan2(y.y-a[1].y,y.x-a[1].x);
    if(fabs(k1-k2)<eps) return x.x<y.x;
    return k1<k2;
}
double ans[N];
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++) a[i].sc(),a[i].id=i;
    for(int i=1;i<=n;i++)
        if(a[i].y<a[1].y||a[i].y==a[1].y&&a[i].x<a[1].x)
        swap(a[i],a[1]);
    sort(a+2,a+1+n,cmp);
    for(int i=1;i<=n;i++)
    {
        while(top>=2&&((s[top-1]-a[i])^(s[top]-a[i]))<=0) top--;
        s[++top]=a[i];
    }
    if(top==1) ans[s[1].id]=1;
    else if(top==2) ans[s[1].id]=ans[s[2].id]=0.5;
    else
        for(int i=1;i<=top;i++)
        {
            vec l,r;
            if(i==1) l=s[top];
            else l=s[i-1];
            if(i==top) r=s[1];
            else r=s[i+1];
            double res=(r-s[i])*(l-s[i]);
            res/=(r-s[i]).len()*(l-s[i]).len();
            res=pi-acos(res);
            res/=2*pi;
            ans[s[i].id]=res;
        }
    for(int i=1;i<=n;i++)
        printf("%.10f\n",ans[i]);
}

#include <bits/stdc++.h>

using namespace std;

#define DBG cerr << '!' << endl;
#define REP(i,n) for(ll (i) = (0);(i) < (n);++i)
#define rep(i,s,g) for(ll (i) = (s);(i) < (g);++i)
#define rrep(i,s,g) for(ll (i) = (s);i >= (g);--(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {for(int W = 0;W < (n);W++)cerr << v[W] << ' ';cerr << endl << endl;}
#define SHOW2d(v,i,j) {for(int aaa = 0;aaa < i;aaa++){for(int bbb = 0;bbb < j;bbb++)cerr << v[aaa][bbb] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(10)
#define INF 1000000000
#define PI 3.14159265359

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

int main()
{
	int n;cin >> n;
	vector<double> x(n),y(n);

	REP(i,n)
	{
		cin >> x[i] >> y[i];
	}

	REP(i,n)
	{
		vector<double> arg;
		REP(j,n)
		{
			if(i != j)
			{
				arg.PB(atan2(y[i] - y[j],x[i] - x[j]));
			}
		}

		sort(ALL(arg));

		double ma = 0;

		REP(j,arg.size()-1)
		{
			ma = max(ma,arg[j+1] - arg[j]);
		}
		ma = max(ma,arg[0]+2*PI - arg[arg.size()-1]);

		if(ma < PI)ma = 0;
		else
		{
			ma -= PI;
		}

		cout << Decimal << ma / (2*PI) << endl;
	}

	return 0;
}


#include <bits/stdc++.h>
#include <ext/rope>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#define int long long
#define pb push_back
#define x first
#define y second
#define mk(a,b) make_pair(a,b)
#define rr return 0
#define sqr(a) ((a)*(a))
#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()

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

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

template<class value, class cmp = less<value> >
using ordered_set = tree<value, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
template<class value, class cmp = less_equal<value> >
using ordered_multiset = tree<value, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
template<class key, class value, class cmp = less<key> >
using ordered_map = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;

/// find_by_order()
/// order_of_key()
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
inline int randll(int l = INT_MIN, int r = INT_MAX) {
    return uniform_int_distribution<int>(l, r)(rng);
}

const int INF = 1e9, MOD = 998244353; /// think
const ll LINF = 1e15;

const int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
inline bool inside (int x, int y, int n, int m) {
    return 0 <= x && 0 <= y && x < n && y < m;
}

template<class T> bool umin (T &a, T b) {return a > b ? (a = b, true) : false; }
template<class T> bool umax (T &a, T b) {return a < b ? (a = b, true) : false; }

vector <vector <int> > edge;
main()
{
    ios::sync_with_stdio(0);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n, m;
    cin >> n >> m;
    edge.resize(n);
    vector <pii> e(m);
    vector <int> res;
    for (int i = 0, a, b; i < m; i++) {
        cin >> a >> b;
        --a, --b;
        e[i] = {a, b};
        edge[a].pb(b);
    }
    auto solve = [&] (int v) {
        vector <int> d(n, INF), pr(n);
        queue <int> q;
        for (int to : edge[v]) {
            d[to] = 1;
            pr[to] = v;
            q.push(to);
        }
        while (!q.empty()) {
            int V = q.front();
            q.pop();
            for (int to : edge[V]) {
                if (d[to] == INF) {
                    d[to] = d[V] + 1;
                    pr[to] = V;
                    q.push(to);
                }
            }
        }
        if (d[v] != INF) {
            vector <int> ans;
            int from = v;
            while (d[from] != 1) {
                ans.pb(from);
                from = pr[from];
            }
            ans.pb(from);
            if (sz(ans) < sz(res) || res.empty()) {
                res = ans;
            }
        }
    };
    for (int i = 0; i < n; i++) {
        solve(i);
    }
    bool ok = !res.empty();
    vector <int> tin(n), tout(n);
    for (int i = 0; i < m; i++) {
        int f = 0;
        for (auto &j : res) {
            if (e[i].x == j) {
                ++f;
                break;
            }
        }
        for (auto &j : res) {
            if (e[i].y == j) {
                ++f;
                break;
            }
        }
        if (f == 2) {
            ++tin[e[i].x];
            ++tout[e[i].y];
        }
    }
    for (int i : res) {
        if (tin[i] != 1 || tout[i] != 1) {
            ok = false;
        }
    }
    if (ok) {
        cout << sz(res) << '\n';
        for (auto &i : res) {
            cout << i + 1 << '\n';
        }
    }
    else cout << "-1\n";
}

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

using namespace std;

namespace SCC {
	const vector<vector<size_t>> *map, *revMap;
	vector<size_t> parent, topological;
	vector<bool> visit;
	size_t n, current;

	size_t find(size_t p) {
		size_t &q = parent[p];
		if (p == q) return p;
		return q = find(q);
	}

	inline void merge(size_t p, size_t q) {
		p = find(p);
		q = find(q);
		parent[q] = p;
	}

	inline void init(const size_t len) {
		parent.resize(len);
		for (size_t i = 0; i < len; i++) parent[i] = i;
	}

	void topologicalSort(size_t idx) {
		if (visit[idx]) return;
		visit[idx] = true;

		for (auto v : map->at(idx)) topologicalSort(v);

		topological.emplace_back(idx);
	}

	void getSCC(size_t idx) {
		if (visit[idx]) return;
		visit[idx] = true;

		for (auto v : revMap->at(idx)) getSCC(v);
		merge(current, idx);
	}

	vector<size_t>& SCC(const vector<vector<size_t>> &m, const vector<vector<size_t>> &revM) {
		map = &m;
		revMap = &revM;
		n = m.size();
		init(n);

		topological.clear();
		visit.resize(n);

		fill(visit.begin(), visit.end(), false);
		for (size_t i = 0; i < n; i++) topologicalSort(i);

		fill(visit.begin(), visit.end(), false);
		for (auto i = topological.rbegin(); i != topological.rend(); i++) {
			current = *i;
			getSCC(*i);
		}
		for (size_t i = 0; i < n; i++) find(i);

		return parent;
	}
}

int main() {
	vector<vector<size_t>> inM, outM;
	size_t n, m, a, b;
	scanf("%zd%zd", &n, &m);
	inM.resize(n);
	outM.resize(n);

	while (m--) {
		scanf("%zd%zd", &a, &b);
		inM[b].emplace_back(a);
		outM[a].emplace_back(b);
	}

	SCC::SCC(inM, outM);
	scanf("%zd", &m);
	while (m--) {
		scanf("%zd%zd", &a, &b);
		puts(SCC::parent[a] == SCC::parent[b] ? "1" : "0");
	}

	/*
	map<size_t, vector<size_t>> s;
	SCC::SCC(inM, outM);
	for (size_t i = 0; i < n; i++) {
		s[SCC::parent[i]].emplace_back(i);
	}
	printf("%zd\n", s.size());
	for (auto &v : s) {
		for (auto t : v.second) printf("%zd ", ++t);
		puts("-1");
	}
	*/
}


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

int main(){
    int n,m;
    cin>>n>>m;
    vector<int>a(n);
    rep(i,n)cin>>a[i];
    vector<ll>sum(n+1);
    for(int i=1; i<=n; ++i){
        sum[i]=sum[i-1]+a[i-1];
    }
    map<ll,int>mp;
    ll ans=0;
    rep(i,n+1){
        ans+=mp[sum[i]%m];
        mp[sum[i]%m]++;
    }
    cout<<ans<<endl;
}

#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll mod = 1e9+7;
int main()
{
    int n;
    cin>>n;
    vector<ll> a(n);
    ll s1 = 0, s2 = 0;
    for (int i=0; i<n; i++) {
        cin>>a[i];
        s1 += a[i];
        s2 += (a[i] * a[i]);
        s1 %= mod;
        s2 %= mod;
    }
    ll res = (s1 * s1 - s2);
    res %= mod;
    while (res < 0) res += mod;
    ll ans;
    if (res % 2) ans = (res + mod) / 2;
    else ans = res / 2;

    cout << ans << '\n';
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
ll factorial(int i) {       //階乗
    if (i == 0) return 1;
    return (factorial(i - 1)) * i;
}

int main() {
    int n, q;
    cin >> n >> q;
    string s;
    cin >> s;
    vector<int> ac(n);
    vector<int> sum(n + 1, 0);
    rep(i, n) {
        if(i != n) {
            if(s[i] == 'A' && s[i + 1] == 'C') {
                ac[i]++;
                i++;
            }
        }
    }
    rep(i, n) {
        sum[i + 1] += sum[i] + ac[i];
    }
    /*cout << endl;
    rep(i, n) cout << ac[i] << ' ';
    cout << endl;
    rep(i, n + 1) cout << sum[i] << ' ';
    cout << endl;
    //確認済*/
    vector<vector<int>> l(q, vector<int>(2));
    rep(i, q) {
        rep(j, 2) cin >> l[i][j];
    }
    rep(i, q) {
        cout << sum[l[i][1] - 1] - sum[l[i][0] - 1] << endl;
    }

}

#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <math.h>
#include <iomanip>
using namespace std;
#define int long long

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

    int n,q; cin >> n >> q;
    string s; cin >> s;
    vector<int> l(q),r(q);
    for(int i = 0;i < q;i ++) cin >> l.at(i) >> r.at(i);

    for(int i = 0;i < q;i ++){
        l.at(i) --;
        r.at(i) --;
    }

    vector<int> ans(n);
    for(int i = 1;i < n;i ++){
        ans.at(i) = ans.at(i-1);
        if(s.at(i-1) == 'A' && s.at(i) == 'C') ans.at(i) ++;
    }

    for(int i = 0;i < q;i ++){
        cout << ans.at(r.at(i)) - ans.at(l.at(i)) << endl;
    }
}


#include <bits/stdc++.h>
#define rep(i, s, n) for (int i = s; i < n; i++)
#define rp(i,n) for (int i=0;i<n;i++)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define all_acc(x) (x).begin(), (x).end(), 0LL
#define int long long
#define absSort(v) sort(a.begin(), a.end(), [](int i, int j) -> bool { return abs(i) < abs(j); });
using namespace std;
using P = pair<int, int>;
using Graph = vector<vector<int>>;
using ll = long long;
double pi=3.14159265359;

//出力するよ
void output_tate(vector<int> s){
    int n = s.size();
    for (int i = 0; i < n; i++){
        //if (i != 0)
        //    cout << " ";
        cout << s[i]<<endl;
    }
    //cout << endl;
}

void output_yoko(vector<int> s){
    int n = s.size();
    for (int i = 0; i < n; i++){
        if (i != 0)
            cout << " ";
        cout << s[i];//<<endl;
    }
    cout << endl;
}

int MOD = pow(10, 9) + 7;

int kaijou(int n){
    if(n==0) return 1;
    else{
        return ((n%MOD)*(kaijou(n-1)%MOD))%MOD;
    }
}

bool ispow(int n){
    int rn=sqrt(n);
    if(rn*rn==n) return true;
    else return false;
}

/*
vector<int> fn(1000+1);

int f(int n){
    //if(n%2==1) return
    n*=2;
    double sqrtNum=sqrt(n);
    set<tuple<int,int,int>> ans;

    for(int X=1;X<=n;X++){
        for(int Y=1;Y<=n-X;Y++){
            int Z=n-X-Y;
            //cout<<X<<" "<<Y<<" "<<Z<<endl;
            if(Z<0) continue;

            if(ispow(X) && ispow(Y) && ispow(Z)){
                int rx=sqrt(X),ry=sqrt(Y),rz=sqrt(Z);
                int x=(rx-ry+rz);
                int y=(rx+ry-rz);
                int z=(-rx+ry+rz);

                if(x%2==0 && y%2==0 && z%2==0 && x>0 && y>0 && z>0){
                    x/=2;
                    y/=2;
                    z/=2;
                    ans.insert({x,y,z});
                    //cout<<x<<" "<<y<<" "<<z<<endl;
                }
            }else continue;

        }
    }

    for(int X=1;X<=sqrtNum;X++){
        for(int Y=1;Y<=sqrtNum;Y++){
            int Z2=n-X*X-Y*Y;
            if(Z2<0 || ispow(Z2)==false) continue;

            int Z=sqrt(Z2);
            int x=(X-Y+Z);
            int y=(X+Y-Z);
            int z=(-X+Y+Z);

            if(x%2==0 && y%2==0 && z%2==0 && x>0 && y>0 && z>0){
                    x/=2;
                    y/=2;
                    z/=2;
                    ans.insert({x,y,z});
                    //cout<<x<<" "<<y<<" "<<z<<endl;
            }
        }
    }

    for(int x=2;x<=sqrtNum;x++){
        for(int y=2;y<=sqrtNum;y++){
            int z2=n-x*x-y*y;

            //if((z2&(z2-1))!=0) break;
            int z=sqrt(z2);
            if((z*z)!=z2) break;
            cout<<z2<<endl;

            if(z<2||z>sqrtNum) break;
            cout<<x<<" "<<y<<" "<<z<<endl;
            cout<<x*x+y*y+z*z<<endl;

            if((x-y+z)%2!=0||(x+y-z)%2!=0||(-x+y+z)%2!=0) break;
            int x_t=(x-y+z)/2;
            int y_t=(x+y-z)/2;
            int z_t=(-x+y+z)/2;

            if((x_t)<1||(y_t)<1||(z_t)<1) break;

            cout<<x_t<<" "<<y_t<<" "<<z_t<<endl;
            ans.insert({x_t,y_t,z_t});
        }
    }

    return ans.size();
}*/

signed main() {
    int x,y,z; cin>>x>>y>>z;
    cout<<z<<" "<<x<<" "<<y<<endl;
}

#include<bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define each(it,v) for(auto &it : v)
#define mod 1000000007
#define all(v) (v).begin(),(v).end()
#define vi vector<int>
#define vl vector<long>
#define P pair<int,int>
using namespace std;

#define pi 3.1415926535897932384

main()
{
        int n; cin>>n;
        string a,b,c; cin>>a>>b>>c;

        int ans=0;

        rep(i,n)
        {
                set<char> s;
                s.insert(a[i]);
                s.insert(b[i]);
                s.insert(c[i]);

                if(s.size()==2)ans++;
                if(s.size()==3)ans+=2;
        }
        cout<<ans<<endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL MOD = 1e9 + 7;
const int N = 200000 + 10;
char s[N]; int n;
int lef[N];
int main() {
	scanf("%d%s", &n,s+1);
	if(s[1] == 'W') return !printf("0\n");
	lef[1]=1;
	for(int i=2;i<=2*n;i++){
		if(s[i]==s[i-1]) lef[i]=1^lef[i-1];
		else lef[i]=lef[i-1];
	}

	int cnt=0,ans=1;
	for(int i=1;i<=2*n;i++){
		//printf("# %d\n", lef[i]);
		if(lef[i]){cnt++;}
		else {
			ans=1LL*ans*cnt%MOD;
			if(cnt==0)return !printf("0\n");
			cnt--;
		}
	}
	if(cnt)return !printf("0\n");
	for(int i=1;i<=n;i++)ans=1LL*ans*i%MOD;
	cout<<ans<<endl;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long LL;
typedef long double ld;
typedef vector<ll>VI;
typedef pair<ll,ll>P;
#define VV(T) vector<vector<T>>
#define sz(x) int(x.size())
#define PI 3.1415926535897932384626433832795
#define rep(i, n) for (ll i = 0; i < (ll)n; i++)
#define ALL(a)  (a).begin(),(a).end()
#define rALL(a) (a).rbegin(),(a).rend()
#define c_max(a, b) (((ll)a)>((ll)b)?(a):(b))
#define c_min(a,b) (((ll)a)<((ll)b)?(a):(b))
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
#define vmax(v) *max_element(ALL(v))
#define vmin(v) *min_element(ALL(v))
#define $(x) {cout<<#x<<" = " <<(x)<<endl;}
#define fi first
#define se second
#define MAX 100100//5
#define MAX2 200100
#define MAX6 1001001//6
#define MAX7 10010010//7
#define SENTINEL 2000000000//9
const int MOD = 1000000007;
#define INF 1<<30
#define INFTY 1LL<<61
#define MAX_INT INT_MAX
#define Endl '\n'
#define CLR(mat) memset(mat, 0, sizeof(mat))
inline ll GCD(ll a,ll b){return b?GCD(b,a%b):a;}
inline ll lcm(ll a,ll b){return a*b/GCD(a,b);}
template<class T> bool contain(const std::string& s, const T& v) {
   return s.find(v) != std::string::npos;
}
using Edge=pair<int,ll>;
using Graph=vector<vector<ll> >;//ll or Edge
//const int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
// sort_reverse -> greater<long long>()
// vector型から重複を削除 list.erase(unique(ALL(list)),list.end());
// g++ -o a a.cpp -Wall -lm -std=c++17
//push_back -> emplace_back

int main(){
   ll n;cin>>n;
   VI a(n);
   rep(i,n)cin>>a[i];
   VI cnt(3,0);
   ll ans=1;
   rep(i,n){
      int mul=0;
      rep(j,3){
         if(cnt[j]==a[i]){
            mul++;
         }
      }
      rep(j,3){
         if(cnt[j]==a[i]){
            cnt[j]++;
            break;
         }
      }
      ans=(ans*mul)%MOD;
   }
   cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=1000005;
char a[maxn],b[maxn];
int q[maxn],head,tail;
int main()
{
    int n;
    scanf("%d",&n);
    scanf("%s%s",a+1,b+1);
    if(strcmp(a+1,b+1)==0)
    {
        puts("0");
        return 0;
    }
    int ans=0;
    head=tail=maxn-2;
    //q[tail++]=n+1;
    int last=n;
    int tag=0;
    for(int l=n,r;l;l--)
    {
        r=l;
        while(b[l]==b[l-1]) l--;
        while(last){
            if(last>l||a[last]!=b[l]) last--;
            else break;
        }
        //printf("%d %d %d\n",l,r,last);
        if(last==0){
            puts("-1");return 0;
        }
        while(head<tail){
            if(q[tail-1]-tag>r) tail--;
            else break;
        }
        if(head==tail){
            tag=0;
            q[--head]=last;
        }
        else{
            tag++;
            q[--head]=last+tag;
        }
        //printf("%d %d %d\n",l,head,tail);
        ans=max(tail-head,ans);
    }
    printf("%d\n",ans);
    return 0;
}

#include "bits/stdc++.h"

#define REP(i,num) for(ll i=0;i<(num);++i)
#define LOOP(i) while(i--)
#define ALL(c) c.begin(),c.end()
#define PRINTALL(c) for(auto pitr=c.begin();pitr!=c.end();++pitr){cout<<*pitr;if(next(pitr,1)!=c.end())cout<<' ';}cout<<endl;
#define PAIRCOMP(c,comp) [](const pair<ll,ll>& lhs,const pair<ll,ll>& rhs){return lhs.c comp rhs.c;}

using namespace std;
using ll = long long;

constexpr ll atcoder_mod = 1e9+7;

template<typename T=ll>
T in(){T x; cin >> x; return (x);}
template<typename T=ll,typename C=vector<T>>
C vecin(int N){C x(N);REP(i,N){x[i]=in<T>();}return move(x);}

void vout(){cout << endl;}
template<typename Head,typename... Tail>
void vout(Head&& h,Tail&&... t){cout << ' ' << h;vout(forward<Tail>(t)...);}
void out(){cout << endl;}
template<typename Head,typename... Tail>
void out(Head&& h,Tail&&... t){cout << h;vout(forward<Tail>(t)...);}

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

	string S=in<string>();
	map<char,ll> M;
	for(auto& x:S) M[x]++;
	ll N=S.size();
	ll A=N*(N-1LL)/2LL;
	for(auto& x:M){
		A -= x.second*(x.second-1LL)/2LL;
	}
	out(A+1LL);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cmath>
#include <complex>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cassert>
#include <iomanip>
using namespace std;

#define Rep(b, e, i) for(int i = b; i <= e; i++)
#define Repr(e, b, i) for(int i = e; i >= b; i--)
#define rep(n, i) Rep(0, n-1, i)
#define repr(n, i) Repr(n-1, 0, i)
#define all(v) (v).begin(), (v).end()
#define pb(v) push_back(v)
#define uniq(v) (v).erase(unique(all(v)),(v).end())
#define bitcnt(x) __builtin_popcount(x)
#define fst first
#define snd second
#define Pqaz(T) priority_queue<T,vector<T>,greater<T>>
#define Pqza(T) priority_queue<T>
#define put(x) cout << x;
#define putsp(x) cout << x << ' ';
#define putbd cout << "---------------------------------------------" << endl;
#define putln(x) cout << x << endl;
#define debug(x) cerr << #x << "=" << x << endl;
#define ENJYU std::ios::sync_with_stdio(false);std::cin.tie(0);

typedef long long ll;
typedef pair<ll, ll> llP;
typedef pair<int, int> intP;
typedef complex<double> comp;
typedef vector <int> vec;
typedef vector <ll> vecll;
typedef vector <double> vecd;
typedef vector <vec> mat;
typedef vector <vecll> matll;
typedef vector <vecd> matd;

//vector の中身を出力
template <class T>ostream &operator<<(ostream &o,const vector<T>&v)
{o<<"{";for(int i=0;i<(int)v.size();i++)o<<(i>0?", ":"")<<v[i];o<<"}";return o;}

const int MAX = 200020;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = 1<<29;
const ll INFL = 1e18;
const ll MOD = 1000000007;

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

//********************************template END****************************************//

struct UnionFind
{
	//number of parents(par[i] = i => root)
	vector <int> par;
	//rank of tree
	vector <int> rank;

	UnionFind(int n) : par(n), rank(n, 1) {
		//initialize
		rep(n, i) par[i] = i;
	}

	//find root (keiroasshuku at the same time)
	int root(int x){
		if (par[x] == x) return x;
		else return par[x] = root(par[x]);
	}

	//unite
	void unite(int x, int y){
		x = root(x);
		y = root(y);
		if (x == y) return;
		if (rank[x] > rank[y]) swap(x, y);
		par[x] = y;
		rank[y] += rank[x];
	}

	//same tree?
	bool same(int x, int y){
		return root(x) == root(y);
	}
};

void solve(void)
{
	int N, K, L;
	cin >> N >> K >> L;

	UnionFind uf1(N), uf2(N);

	while(K--)
	{
		int u, v;
		cin >> u >> v;
		uf1.unite(u-1, v-1);
	}

	while(L--)
	{
		int u, v;
		cin >> u >> v;
		uf2.unite(u-1, v-1);
	}

	map<intP, int> mp;

	rep(N, i)
	{
		mp[intP(uf1.root(i), uf2.root(i))]++;
	}

	rep(N, i)
	{
		cout << mp[intP(uf1.root(i), uf2.root(i))] << (i == N-1 ? '\n' : ' ');
	}

}

int main(void){
	solve();
	//cout << "yui(*-v・)yui" << endl;
	return 0;
}

//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define N 100005
//#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<22,stdin),p1 == p2)?EOF:*p1++)
using namespace std ;
//char buf[(1 << 22)] , *p1 = buf , *p2 = buf ;
inline int read ()
{
    char c = getchar () ; int x = 0 , f = 1 ;
    while (c < '0' || c > '9') { if (c == '-') f = -1 ; c = getchar () ; }
    while (c >= '0' && c <= '9'){ x = x * 10 + c - '0' ; c = getchar () ; }
    return x * f ;
}
map <int , int> mp[N << 3] ; //mp[u][w]:u属性为w的虚点编号
vector <pair <int , int> > edge[N << 3] ;
int dis[N << 3] , vis[N << 3] , n , m ;
queue <int> q ;
void spfa (int s)
{
    int tim = 0 ;
    for (int i = 0 ; i <= (N << 3) - 5 ; i ++) dis[i] = 1e9 ;
	// fill (dis , dis + (N << 2) , 1e9) ;
	dis[s] = 0 ;
	vis[s] = 1 ;
	q.push (s) ;
	while (! q.empty ())
	{
        // printf ("The %d time\n" , ++ tim) ;
		int x = q.front () ;
		q.pop () ;
		for (int i = 0 ; i < edge[x].size () ; i ++)
		{
			int y = edge[x][i].first ;
			if (dis[y] > dis[x] + edge[x][i].second)
			{
				dis[y] = dis[x] + edge[x][i].second ;
				if (! vis[y])
				{
					vis[y] = 1 ;
					q.push (y) ;
				}
			}
		}
        vis[x] = 0 ;
	}
}
int main ()
{
//    freopen (".in" , "r" , stdin) ; freopen (".out" , "w" , stdout) ;
	n = read () , m = read () ;
    int tot = n ;
	for (int i = 1 ; i <= m ; i ++)
	{
		int u = read () , v = read () , w = read () ;
        if (! mp[u].count (w))
        {
            mp[u][w] = ++ tot ;
            edge[u].push_back (make_pair (tot , 1)) ;
            edge[tot].push_back (make_pair (u , 1)) ;
        }
        if (! mp[v].count (w))
        {
            mp[v][w] = ++ tot ;
            edge[v].push_back (make_pair (tot , 1)) ;
            edge[tot].push_back (make_pair (v , 1)) ;
        }
        edge[mp[u][w]].push_back (make_pair (mp[v][w] , 0)) ;
        edge[mp[v][w]].push_back (make_pair (mp[u][w] , 0)) ;
    }
    spfa (1) ;
    if (dis[n] == 1e9) return puts ("-1") , 0 ;
    cout << dis[n] / 2 ;
}


#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<n; i++)
#define PI 3.14159265359
#define INF 1000100100
#define MOD 1000000007
#define all(x) (x).begin(),(x).end()
typedef long long ll;
#define P pair<int, int>
#define PP pair<P,int>
#define T tuple<int,int,int>
using namespace std;

int main(){
    int n,q; cin >> n >> q;
    vector<T> rw(n);
    rep(i,n){
        int a,b,c; cin >> a >> b >> c;
        rw[i]=T(c,a,b);
    }
    sort(all(rw));
    map<int,int> index;

    vector<int> t(q+2);
    t[0]=-INF;
    t[q+1]=INF;
    rep(i,q){
        cin >> t[i+1];
        index[t[i+1]]=i;
    }

    vector<int> ans(q,-1);

    rep(i,n){
        int a,b,c;
        tie(a,b,c)=rw[i];
        b-=a;
        c=c-a;
        int left,right;
        int l,r;
        l=0;
        r=t.size()-1;
        while(r!=l){
            int cen=(r+l)/2;
            if(t[cen]>=b) r=cen;
            else l=cen+1;
        }
        left=l;
        l=0;
        r=t.size()-1;
        while(r!=l){
            int cen=(r+l)/2;
            if(t[cen]>=c) r=cen;
            else l=cen+1;
        }
        right=l-1;
        for(int j=left; j<=right; j++){
            ans[index[t[j]]]=a;
        }
        while(right-left+1>0){
            t.erase(t.begin()+left);
            right--;
        }
    }

    rep(i,q) cout << ans[i] << endl;
    return 0;
}


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

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

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

using ll = long long;

const vector<int> dx = {1, 0, -1, 0};
const vector<int> dy = {0, 1, 0, -1};
const double PI = 3.1415926535;
const ll inf = 1001001001;
const ll e9  = 1000000000;
const ll mod = 1000000007;

void dfs(vector<vector<pair<int,ll>>> &graph,
 int now, int past,ll sum, vector<ll> &dist){
    dist[now]=sum;
    for(auto next:graph[now]){
       if(next.first==past)continue;
       dfs(graph, next.first, now, sum+next.second, dist);
    }
}
int main(){
   int n;
   cin >> n;
   vector<vector<pair<int,ll>>> graph(n);
   vector<ll> dist(n);
   rep(i, 0, n-1){
      int a, b;
      ll c;
      cin >> a >> b >> c;
      a--, b--;
      graph[a].push_back({b, c});
      graph[b].push_back({a, c});
   }
   int q, k;
   cin >> q >> k;
   k--;
   dfs(graph, k, -1, 0, dist);
   vector<ll>ans(q);
   rep(i, 0, q){
      int x, y;
      cin >> x >> y;
      ans[i] = dist[x-1]+dist[y-1];
   }
   rep(i, 0, q)cout<< ans[i] << endl;
   return 0;
}

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

const int N = 1010;
int n, m, a, b;
vector<int> g[N], ans(N + 1);

vector<int> minCircle(int s) {
    int d[N] = {0}, f[N] = {0};
    queue<int> q;
    q.push(s);
    while(!q.empty()) {
        int p = q.front();q.pop();
        for(int c : g[p]) {
            if(!d[c]) {
                f[c] = p;
                d[c] = 1;
                q.push(c);
            }
        }
        if(d[s]) break;
    }
    int p = f[s];
    vector<int> ret;
    ret.push_back(s);
    while(p && p != s) {
        ret.push_back(p);
        p = f[p];
    }
    return ret;
}

int main() {
    cin >> n >> m;
    while(m--) {
        cin >> a >> b;
        g[a].push_back(b);
    }
    for(int i = 1; i <= n; i++) {
        vector<int> t = minCircle(i);
        if(t.size() > 1 && t.size() < ans.size()) ans = t;
    }
    if(ans.size() == N + 1) cout << -1;
    else {
        cout << ans.size() << endl;
        for(int i = 0; i < ans.size(); i++) cout << ans[i] << endl;
    }
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace std;
using namespace __gnu_pbds;

#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

#define lsc(x)      scanf("%lld",&x)
#define sc(x)       scanf("%d",&x)
#define lpr(x)      printf("%lld ",(long long)x)
#define pr(x)       printf("%d ",x)
#define n_l         printf("\n")
#define VI          vector<int>
#define VII         vector<long long int>
#define m_p         make_pair
#define pb          push_back
#define fi           first
#define se           second
#define mset(x,y)   memset(x,y,sizeof(x))
#define sz(v)       (int)v.size()
#define all(v)      v.begin(), v.end()
#define fr(i, a, n) for(int i=a;i<=n;i++)
#define FIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
mt19937 rng32(chrono::steady_clock::now().time_since_epoch().count());
const int N=(int)1e6+5;
const int mod = 1000000007;
typedef long long ll;
// order_of_key (val): returns the no. of values strictly less than val
// find_by_order (k): returns the kth largest element iterator.(0-based)
// vector<int>::iterator itr=lower_bound(v.begin(),v.end(),x);
// s.substr(pos[0-indexed], len(default=till end))
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;

ll fmod(ll x){if (x<mod) return x;return x%mod;}
ll mul(ll a, ll b, ll c){
	ll ret=0;while(b){if (b%2) ret=(ret+a)%c;a=(a*2)%c;b>>=1;}
    return ret;
}
int modpow(ll a, ll b){
    ll ret=1;while(b){if (b%2) ret=(ret*a)%mod;a=(a*a)%mod;b>>=1;}
    return (int)ret;
}
inline int inv(int x){ return modpow(x, mod-2);}
int isprime[N];
void calc_prime(){
	isprime[1]=1;for(ll i=2;i<N;i++) if (!isprime[i]) for(ll j=i*i;j<N;j+=i) isprime[j]=1;
}
int ans[N];
int a[N], added[N];
#define pii pair<int, int>
vector<pii> v[N];
int BIT[N];
void upd(int idx, int val){
    //trace(idx, val);
    while(idx<N){
        BIT[idx] += val;
        idx += idx & (-idx);
    }
}
int query(int idx){
    if (idx<1) return 0;
    int ret = 0;
    int idx1 = idx;
    while(idx){
        ret += BIT[idx];
        idx -= idx & (-idx);
    }
    return ret;
}
int main(){
    int n, q;
    sc(n);sc(q);
    fr(i, 1, n) sc(a[i]);
    fr(i, 1, q){
        int l, r;sc(l);sc(r);
        v[r].pb({l, i});
    }
    fr(i, 1, n){
        //trace(i);
        if (added[a[i]]){
            upd(added[a[i]], -1);
        }
        added[a[i]] = i;
        upd(added[a[i]], 1);
        for(auto pp: v[i]){
            int l = pp.fi;
            int idx = pp.se;
            ans[idx] = query(i) - query(l-1);
        }
    }
    fr(i, 1, q){
        pr(ans[i]);
    }n_l;
    return 0;
}

#include "bits/stdc++.h"

using namespace std;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<VVI> VVVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
typedef long long LL;
typedef priority_queue<int> PQ_DESC;
typedef priority_queue<int, vector<int>, greater<int>> PQ_ASC;
typedef priority_queue<PII> PQ_DESC_PII;
typedef priority_queue<PII, vector<PII>, greater<PII>> PQ_ASC_PII;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<VVLL> VVVLL;

#define SORT_ASC(c) sort((c).begin(), (c).end())
#define SORT_DESC(c) sort((c).begin(), (c).end(), greater<typeof((c).begin())>())

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define FORL(i,a,b) for(LL i=(a);i<(b);++i)
#define REPL(i,n)  FORL(i,0,n)
#define SIZE(a) int((a).size())
#define ALL(a) (a).begin(),(a).end()

const double EPS = 1e-10;
const double PI  = acos(-1.0);

template <typename Iterator>
inline bool next_combination(const Iterator first, Iterator k, const Iterator last)
{
   /* Credits: Thomas Draper */
   if ((first == last) || (first == k) || (last == k))
      return false;
   Iterator itr1 = first;
   Iterator itr2 = last;
   ++itr1;
   if (last == itr1)
      return false;
   itr1 = last;
   --itr1;
   itr1 = k;
   --itr2;
   while (first != itr1)
   {
      if (*--itr1 < *itr2)
      {
         Iterator j = k;
         while (!(*itr1 < *j)) ++j;
         iter_swap(itr1,j);
         ++itr1;
         ++j;
         itr2 = k;
         rotate(itr1,j,last);
         while (last != j)
         {
            ++j;
            ++itr2;
         }
         rotate(k,itr2,last);
         return true;
      }
   }
   rotate(first,k,last);
   return false;
}

inline double get_time_sec(void){
    return static_cast<double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count())/1000000000;
}

template<typename T>
T gcd(T a, T b) {
    if(a > b) swap(a, b);
    if(a == 0) return b;
    else return gcd(b%a, a);
}

template<typename T>
map<T, T> prime_list(T n) {
    map<T, T> ret;
    for(T i = 2; i*i <= n; i++) {
        if(n % i == 0) {
            ret[i] = 0;
            while(n % i == 0) {
                n /= i;
                ret[i]++;
            }
        }
    }
    if(n != 1) ret[n]++;
    return ret;
}

#define MOD 1000000007

LL mypow(LL a, LL n) {
    if(n == 1) return a % MOD;
    if(n % 2 == 1) return (a * mypow(a, n-1)) % MOD;
    LL t = mypow(a, n/2);
    return (t * t) % MOD;
}

int main(void) {
    int n;
    cin >> n;
    VPII ab, cd;
    REP(i, n) {
        int a, b;
        cin >> a >> b;
        ab.push_back(make_pair(a, b));
    }
    REP(i, n) {
        int c, d;
        cin >> c >> d;
        cd.push_back(make_pair(c, d));
    }
    auto cmpx = [](PII x, PII y) {
        return x.first < y.first;
    };
    auto cmpy = [](PII x, PII y) {
        return x.second > y.second;
    };
    sort(ALL(ab), cmpy);
    sort(ALL(cd), cmpx);

    REP(i, n) {
        cerr << "a, b: " << ab[i].first << ", " << ab[i].second << endl;
    }
    REP(i, n) {
        cerr << "c, d: " << cd[i].first << ", " << cd[i].second << endl;
    }

    vector<bool> flgsab(n, true), flgscd(n, true);
    int ans = 0;
    REP(i, n) {
        REP(j, n) {
            if(cd[i].first > ab[j].first && cd[i].second > ab[j].second && flgsab[j] && flgscd[i]) {
                cerr << "a, b: c, d: " << ab[j].first << ", " << ab[j].second << ": " << cd[i].first << ", " << cd[i].second << endl;
                ans++;
                flgsab[j] = false;
                flgscd[i] = false;
                break;
            }
        }
    }
    cout << ans << endl;
}


#define _USE_MATH_DEFINES

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>

using namespace std;

int main(){

	int card[4];
	char c;

	while(cin >> card[0] ){
		for(int i=1;i<5;i++){
			cin >> c >> card[i];
		}
		sort(card, card+5);

		int two = 0;
		bool three = 0;
		bool four = 0;

		for(int i=0;i<5;i++){
			int count = 1;
			for(int j=i+1;j<5;j++){
				if(card[i]==card[j]){
					count++;
				}
				else{
					break;
				}
			}
			if(count==2){
				two+=1;
				i+=1;
			}
			else if(count==3){
				three=1;
				i+=2;
			}
			else if(count==4){
				four=1;
				i+=3;
			}
		}

		if(two && three){
			cout << "full house" << endl;
		}else if(two==2){
			cout << "two pair" <<endl;
		}else if(two==1){
			cout << "one pair" <<endl;
		}else if(three){
			cout << "three card" <<endl;
		}else if(four){
			cout << "four card" <<endl;
		}else{
			bool straight_flag = 1;
			bool ace_flag = 1;
			bool has_13 = 0;
			if(card[4]==13){
				has_13=1;
			}

			for(int i=3;i>=0;i--){
				if(ace_flag && card[i+1]-1==card[i]){
					//cout<<1;
				}else if(ace_flag && has_13 && card[i]==i+1){
					//cout<<0;
					ace_flag=0;
				}else{
					straight_flag=0;
					break;
				}
			}

			if(straight_flag){
				cout << "straight" << endl;
			}else{
				cout << "null" << endl;
			}
		}
	}
	return 0;
}

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

int main(){

  int num, v, n, m, len, cnt;
  vector<long long> vc[2];

  while(cin >> n){
    if(n == 0) break;
    vc[0].clear();
    vc[1].clear();
    len = 0;

    for(int i=0; i<n; i++){
      cin >> num >> v >> m;
      for(int j=0; j<=len; j++){
        if(j == len){
          vc[0].push_back(num);
          vc[1].push_back((long long)v * m);
          len++;
          break;
        }
        if(vc[0][j] == num){
          if(vc[1][j] < 1000000){
            vc[1][j] += (long long)v * m;
          }
          break;
        }
      }
    }

    cnt = 0;
    for(int i=0; i<len; i++){
      if(vc[1][i] >= 1000000){
        cout << vc[0][i] << endl;
        cnt++;
      }
    }
    if(cnt == 0) cout << "NA" << endl;

  }

  return 0;

}


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

int main(){
	double n,m,l;
  cin>>n>>m>>l;

 	if(l==0){
    	cout << std::setprecision(20) <<(m-1)/n<<endl;
    }else{
    	cout << std::setprecision(20) <<(m-1)*(2*(n-l))/(n*n)<<endl;
    }

}

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

int a,b,c;

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


#include <bits/stdc++.h>
using namespace std;
#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 pb emplace_back
#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>>
#define dame(a) {out(a);return 0;}
#define decimal cout<<fixed<<setprecision(15);
#define dupli(a) a.erase(unique(all(a)),a.end())
typedef long long ll;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> PP;
typedef tuple<ll,ll,ll,ll> PPP;
typedef multiset<ll> S;
using vi=vector<ll>;
using vvi=vector<vi>;
using vvvi=vector<vvi>;
using vvvvi=vector<vvvi>;
using vp=vector<P>;
using vvp=vector<vp>;
using vb=vector<bool>;
using vvb=vector<vb>;
const ll inf=1001001001001001001;
const ll INF=1001001001;
const ll 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 outvvp(T v){rep(i,v.size())outvp(v[i]);}
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");}
template<class T> void YesNo(T b){if(b)out("Yes");else out("No");}
template<class T> void YESNO(T b){if(b)out("YES");else out("NO");}
template<class T> void noyes(T b){if(b)out("no");else out("yes");}
template<class T> void NoYes(T b){if(b)out("No");else out("Yes");}
template<class T> void NOYES(T b){if(b)out("NO");else out("YES");}
void outs(ll a,ll b){if(a>=inf-100)out(b);else out(a);}
ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);}
ll modpow(ll a,ll b){a%=mod;if(b==0)return 1;if(b&1)return a*modpow(a,b-1)%mod;ll k=modpow(a,b/2);return k*k%mod;}
ll ma;
string s;
ll dp[305][305][305];
ll dfs(int i,int j,int k){
    if(i==j)return 1;
    if(i>j)return 0;
    if(dp[i][j][k]!=-1)return dp[i][j][k];
    if(s[i]==s[j])chmax(dp[i][j][k],dfs(i+1,j-1,k)+2);
    chmax(dp[i][j][k],dfs(i+1,j,k));
    chmax(dp[i][j][k],dfs(i,j-1,k));
    if(k!=ma)chmax(dp[i][j][k],dfs(i+1,j-1,k+1)+2);
    return dp[i][j][k];
}

int main(){
    cin>>s;
    ll n=s.size();cin>>ma;
    rep(i,n)rep(j,n)rep(t,ma+1)dp[i][j][t]=-1;
    out(dfs(0,n-1,0));
    //rep(i,n)rep(j,n)rep(t,ma+1)if(dp[i][j][t]!=-1)cout<<i<<' '<<j<<' '<<t<<' '<<dp[i][j][t]<<endl;
}

#include<bits/stdc++.h>
#define del(a,i) memset(a,i,sizeof(a))
#define ll long long
#define inl inline
#define il inl void
#define it inl int
#define ill inl ll
#define re register
#define ri re int
#define rl re ll
#define mid ((l+r)>>1)
#define lowbit(x) (x&(-x))
#define INF 0x3f3f3f3f
using namespace std;
template<class T>il read(T &x){
	int f=1;char k=getchar();x=0;
	for(;k>'9'||k<'0';k=getchar()) if(k=='-') f=-1;
	for(;k>='0'&&k<='9';k=getchar()) x=x*10+k-'0';
	x*=f;
}
template<class T>il _print(T x){
	if(x>=10) _print(x/10);
	putchar(x%10+'0');
}
template<class T>il print(T x){
	if(x<0) putchar('-'),x=-x;
	_print(x);
}
ll mul(ll a,ll b,ll mod){long double c=1.;return (a*b-(ll)(c*a*b/mod)*mod)%mod;}
it qpow(int x,int k,int mod){
	int res=1,bas=x;
	while(k){
		if(k&1) res=1ll*res*bas%mod;
		bas=1ll*bas*bas%mod,k>>=1;
	}
	return res;
}
const int N = 2e5+5,mod = 1e9+7;
it add(int x,int y){return x+y>=mod?x+y-mod:x+y;}
it mul(int x,int y){return 1ll*x*y%mod;}
il inc(int &x,int y){x=add(x,y);}
int n,m,k,ans,fac[N],ifac[N];
it C(int n,int m){return mul(mul(ifac[n-m],ifac[m]),fac[n]);}
int main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	read(n),read(m),read(k),fac[0]=1;
	for(ri i=1;i<=n*m;++i) fac[i]=mul(fac[i-1],i);
	ifac[n*m]=qpow(fac[n*m],mod-2,mod);
	for(ri i=n*m-1;i>=0;--i) ifac[i]=mul(ifac[i+1],i+1);
	for(ri i=1;i<=n-1;++i) inc(ans,mul(mul(mul(m,m),n-i),i));
	for(ri i=1;i<=m-1;++i) inc(ans,mul(mul(mul(n,n),m-i),i));
	print(mul(ans,C(n*m-2,k-2)));
	return 0;
}


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

using namespace std;

int main() {
	int n;
	long long int m;
	while (cin >> n >> m) {
		if (n == 0 && m == 0) {
			break;
		}
		long long int b;
		vector<long long int> ss;
		ss.push_back(0);
		for (int i = 0; i < n; i++) {
			cin >> b;
			if (b <= m) {
				ss.push_back(b);
			}
		}
		vector<long long int> ss2;
		for (long long int i = 0; i < ss.size(); i++) {
			ss2.push_back(ss[i]);
			for (long long int j = i; j < ss.size(); j++) {
				if (ss[i]+ss[j] <= m) {
					ss2.push_back(ss[i]+ss[j]);
				}
			}
		}
		sort(ss2.begin(), ss2.end());
		/*for (long long int i = 0; i < ss2.size(); i++) {
			cout << ss2[i] << endl;
		}*/
		long long int MAX = 0;
		long long int sz = ss2.size();
		for (long long int j = 0; j < sz; j++) {
			long long int a = m;
			for (long long int i = 0; i < 2; i++) {
				vector<long long int>::iterator ite = upper_bound(ss2.begin(), ss2.end(), a);
				if (ite != ss.begin()) {
					ite--;
					a -= *(ite);
				}
			}
			vector<long long int>::iterator ite2 = ss2.end();
			ss2.erase(ite2-1);
			MAX = max(MAX, m-a);
		}
		cout << MAX << endl;
	}
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define il inline
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define gc getchar()
#define ll long long
#define int long long
#define ri register int
#define rc register char
#define rb register bool
#define rp(i,x,y) for(ri i=x;i<=y;++i)
#define my(i,x,y) for(ri i=x;i>=y;--i)
using namespace std;

il int read()
{
    rc ch=gc;ri x=0;rb y=1;
    while(ch!='-' && (ch>'9' || ch<'0'))ch=gc;
    if(ch=='-')ch=gc,y=0;
    while(ch>='0' && ch<='9')x=(x<<1)+(x<<3)+(ch^'0'),ch=gc;
    return y?x:-x;
}
ll min1[1000001], min2[1000001], adds[1000001];
inline void down(int nw)
{
    if (!adds[nw])return;
    adds[ls(nw)] += adds[nw];
    adds[rs(nw)] += adds[nw];
    min1[ls(nw)] += adds[nw];
    min2[ls(nw)] += adds[nw];
    min1[rs(nw)] += adds[nw];
    min2[rs(nw)] += adds[nw];
    adds[nw] = 0;
}
inline void up(int nw)
{
    min1[nw] = min(min1[ls(nw)], min1[rs(nw)]);
    min2[nw] = min(min2[ls(nw)], min2[rs(nw)]);
}
void setpt(int l, int r, int nw, int pt, ll num)
{
    if (l == r)
	{
        min1[nw] = min(min1[nw], num - l);
        min2[nw] = min(min2[nw], num + l);
        return;
    }
    int mid = (l + r) >> 1;down(nw);
    if (pt <= mid)setpt(l, mid, ls(nw), pt, num);
    else setpt(mid + 1, r, rs(nw), pt, num);
    up(nw);
}
ll query1(int rl, int rr, int l, int r, int nw)
{
    if (rl == l && rr == r)return (min1[nw]);
    int mid = (l + r) >> 1;down(nw);
    if (rl > mid)return (query1(rl, rr, mid + 1, r, rs(nw)));
    if (rr <= mid)return (query1(rl, rr, l, mid, ls(nw)));
    return (min(query1(rl, mid, l, mid, ls(nw)), query1(mid + 1, rr, mid + 1, r, rs(nw))));
}
ll query2(int rl, int rr, int l, int r, int nw)
{
    if (rl == l && rr == r)return (min2[nw]);
    int mid = (l + r) >> 1;down(nw);
    if (rl > mid)return (query2(rl, rr, mid + 1, r, rs(nw)));
    if (rr <= mid)return (query2(rl, rr, l, mid, ls(nw)));
    return (min(query2(rl, mid, l, mid, ls(nw)), query2(mid + 1, rr, mid + 1, r, rs(nw))));
}
ll query(int l, int r, int nw)
{
    if (l == r)return (min1[nw] + l);
    int mid = (l + r) >> 1;down(nw);return (min(query(l, mid, ls(nw)), query(mid + 1, r, rs(nw))));
}
signed main()
{
//	freopen("2558.in","r",stdin);
    int n = read(), q = read(), pre = read();
    memset(min1, 0x3f, sizeof(min1));memset(min2, 0x3f, sizeof(min2));setpt(1, n, 1, read(), 0);
    for (register int i = 1; i <= q; i++)
	{
        int nw = read();
        ll cjr = min(query1(1, nw, 1, n, 1) + nw, query2(nw, n, 1, n, 1) - nw);
        adds[1] += abs(nw - pre);
        min1[1] += abs(nw - pre);
        min2[1] += abs(nw - pre);
        setpt(1, n, 1, pre, cjr);
        pre=nw;
    }
    printf("%lld\n",query(1,n,1));
    return (0);
}


#include <iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define min(A,B) ((A)<(B)?(A):(B))
int dp[2000][2000];
int EditDistance(char *s,char *t)
{
   int i,j,cost;
   int slength=strlen(s);
   int tlength=strlen(t);
   dp[0][0]=0;
   for(i=1;i<=slength;i++)
   dp[i][0]=i;
   for(j=1;j<=tlength;j++)
   dp[0][j]=j;
   for(i=1;i<=slength;i++)
   {
       for(j=1;j<=tlength;j++)
       {
           if(s[i-1]==t[j-1])
           cost=0;
           else
           cost=1;
           dp[i][j] = min(min(dp[i-1][j]+1,dp[i][j-1]+1),dp[i-1][j-1]+cost);
       }
   }
      return dp[slength][tlength];
}
int main()
{
    char s[1025],t[1025];
     while(cin>>s>>t)
     {
         int minx;
         minx=EditDistance(s,t);
         cout<<minx<<endl;
     }
 return 0;
}

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

int n, m, solve[1010][1010];
string s1, s2;
int l1, l2, tmp;

int dp(int n, int m) {

    if (solve[n][m] >= 0) return solve[n][m];

    else if (n == 0 || m == 0) solve[n][m] = max(n, m);

    else {
        tmp = min(dp(n - 1, m), dp(n, m - 1)) + 1;
        if (s1[n - 1] == s2[m - 1]) solve[n][m] = min(tmp, dp(n - 1, m - 1));
        else solve[n][m] = min(tmp, dp(n - 1, m - 1) + 1);
    }

    return solve[n][m];
}

int main(void){

    cin >> s1;
    cin >> s2;
    l1 = s1.size();  l2 = s2.size();

    for (int i = 0; i <= 1000; i++) {
        for (int j = 0; j <= 1000; j++) solve[i][j] = -1;
    }

    cout << dp(l1, l2) << endl;
}


#include<bits/stdc++.h>

#define ll long long
#define mp make_pair
#define rep(i, a, b) for(int i = (a); i <= (b); i++)
#define per(i, a, b) for(int i = (a); i >= (b); i--)

using namespace std;

typedef pair<int, int> pii;
typedef double db;
const int N = 5020;
int D, d[N], len; //d[]数组存储D的每一位数字。
ll ans, f[N][2][2];
//f[i][j][k]表示从低位到高位填了i位，保证合法，并且第n-i位对第n-i+1的进位贡献为j，第i位对第i+1位的进位贡献为k的方案数。
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<<3)+(x<<1)+(ch^48); ch = getchar();}
	return x*f;
}
ll find(int len) {
	int lim = len/2; ll res = 0;
	memset(f, 0, sizeof(f));
	f[0][0][0] = 1;
	rep(i, 0, lim-1) rep(j, 0, 1) rep(k, 0, 1) {
		if(f[i][j][k] != 0) rep(l, 0, 9) {
			int raw = l + d[i+1] + k, num = raw%10;
			int _k = raw/10, _j = j*10 + l - num - d[len-i];
			if(_j < 0 || _j > 1) continue;
			if(i == 0 && (l == 0 || num == 0)) continue;
			f[i+1][_j][_k] += f[i][j][k];
		}
	}
	if(len % 2) {
		rep(j, 0, 1) rep(k, 0, 1) {
			if(f[lim][j][k] != 0) rep(i, 0, 9) {
				int num = i + d[(len+1)/2] + k;
				if(num%10 == i && num/10 == j) res += f[lim][j][k];
			}
		}
	}
	else rep(j, 0, 1) res += f[lim][j][j];
	return res;
}
void init(){
	D = read();
	while(D) d[++len] = D%10, D /= 10; //对D的各个位数的数字分解
	rep(i, len, 2*len) ans += find(i); //枚举n的长度，可证明n的长度不超过D的长度的二倍
	printf("%lld\n", ans);
}
int main(){
//	freopen("input.in","r",stdin);
//	freopen("output.out","w",stdout);
	init();
	return 0;
}

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#define ll long long
#define For(i, j, k) for(int i = j; i <= (int)k; ++ i)
#define Forr(i, j, k) for(int i = j; i >= (int)k; -- i)
#define INF 0x3f3f3f3f
using namespace std;

ll tar, Ans = 0;
ll p[20];

inline void file(){
	freopen("arc075_F.in", "r", stdin);
	freopen("arc075_F.out", "w", stdout);
}

ll dfs(int l, int r, ll delta){
	if(l >= r)	return delta ? 0 : (l == r ? 10 : 1);
	int t = (10 - delta%10) % 10;
	ll v = delta - (t * p[r-l] - t);
	return ((l ? 10 : 9) - t) * dfs(l + 1, r - 1, abs(v) / 10);
}

int main(){
	p[0] = 1;
	For(i, 1, 18)	p[i] = p[i-1] * 10;
	scanf("%lld", &tar);

	For(i, 0, 18)	Ans += dfs(0, i,tar);

	printf("%lld", Ans);
	return 0;
}


#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < (b); i++)
#define rrep(i, a, b) for (int i = a; i >= (b); i--)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
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;
}
void hr() {
   cerr << "--------------" << endl;
}
const int INF = 1001001001;
const int MOD = 1000000007;

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

   int n, k;
   cin >> n >> k;
   vector<int> a(n);
   rep(i, 0, n) cin >> a[i];

   int ans = 0;
   rep(l, 0, k + 1) rep(r, 0, k - l + 1) {
      if (l + r > n) continue;
      int d = k - l - r;
      int now = 0;
      vector<int> s;
      rep(i, 0, l) {
         now += a[i];
         s.push_back(a[i]);
      }
      rep(i, n - r, n) {
         now += a[i];
         s.push_back(a[i]);
      }
      sort(s.begin(), s.end());
      rep(i, 0, d) {
         if (i >= s.size()) break;
         if (s[i] >= 0) break;
         now -= s[i];
      }
      ans = max(ans, now);
   }
   cout << ans << endl;
   return 0;
}

// 2020-07-04 23:45:06
#include<bits/stdc++.h>
#ifdef LOCAL
#include "lib/debug.hpp"
#else
#define debug(...) 1
#endif
#define ALL(a) (a).begin(), (a).end()
#define rep(i, n) REP(i, 0, (n))
#define repc(i, n) REPC(i, 0, (n))
#define REP(i, n, m) for (int i = (int)(n); i < (int)(m); i++)
#define REPC(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define REPCM(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
using namespace std;
using ll = long long;
using ld = long double;
using pr = pair<ll, ll>;
using vll = vector<ll>;
using vpr = vector<pr>;
template<class T> inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; } else return false; }
template<class T> inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }

int dp[105][55][55], v[55];
int rec(int k, int l, int r) {
  if(dp[k][l][r] >= 0) return dp[k][l][r];
  chmax(dp[k][l][r], 0);
  if(k == 0) return dp[k][l][r];
  if(l == r) return dp[k][l][r];
  chmax(dp[k][l][r], rec(k-1, l+1, r) + v[l]);
  chmax(dp[k][l][r], rec(k-1, l, r-1) + v[r-1]);
  if(k >= 2) {
    if(v[l] < 0) chmax(dp[k][l][r], rec(k-2, l+1, r));
    if(v[r-1] < 0) chmax(dp[k][l][r], rec(k-2, l, r-1));
  }
  return dp[k][l][r];
}
void answer() {
  int n, k;
  cin >> n >> k;
  repc(ki, k) repc(li, n) repc(ri, n) dp[ki][li][ri] = -1;
  rep(i, n) cin >> v[i];
  cout << max(0, rec(k, 0, n)) << '\n';
}
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  answer();
  return 0;
}


#include<bits/stdc++.h>
#define lowbit(i) ((i)&(-(i)))
#define lson(i) ((i)<<1)
#define rson(i) ((i)<<1|1)
#define sqr(x) ((x)*(x))
#define fi first
#define se second
#define mp(x,y) make_pair((x),(y))
#define pb push_back
using namespace std;

typedef long long ll;
typedef unsigned long long ul;
typedef pair<int,int> pii;
typedef vector<int> vii;
typedef vector<pii> vpi;
typedef queue<int> qii;

const int inf=0x3f3f3f3f;
const int maxn=100007;

ll n,s;
struct node{
	ll x,p;void init(){scanf("%lld%lld",&x,&p);}
}a[maxn];

pair<ll,ll> solve(ll l,ll r){
	if(a[r].x<s) {
		return mp(-1,s-a[l].x);
  	}else if(a[l].x>s) {
  		return mp(1,a[r].x-s);
    }
  	if(l==r){
		if(a[l].x<s) return mp(-1,s-a[l].x);
		else return mp(1,a[l].x-s);
	}
	if(a[l].p<a[r].p){
		a[r].p+=a[l].p;
		pair<ll,ll> t=solve(l+1,r);
		// cout<<l<<' '<<r<<' '<<a[l].p<<' '<<a[r].p-a[l].p<<' ';
		// cout<<t.fi<<' '<<t.se<<'\n';
		if(t.fi==-1) return mp(-1,t.se+a[l+1].x-a[l].x);
		else return mp(-1,t.se+a[r].x-a[l].x);
	}else{
		a[l].p+=a[r].p;
		pair<ll,ll> t=solve(l,r-1);
		// cout<<l<<' '<<r<<' '<<a[l].p-a[r].p<<' '<<a[r].p<<' ';
		// cout<<t.fi<<' '<<t.se<<'\n';
		if(t.fi==1) return mp(1,t.se+a[r].x-a[r-1].x);
		else return mp(1,t.se+a[r].x-a[l].x);
	}
}

int main(){
// 	freopen("","r",stdin);5
// 	freopen("","w",stdout);
	scanf("%lld%lld",&n,&s);
	for(int i=1;i<=n;i++) a[i].init();
	// for(int i=1;i<=n;i++) cout<<a[i].p<<' ';cout<<'\n';
	printf("%lld\n",solve(1,n).se);
	return 0;
}
/*
3 2
1 3
3 4
4 2
*/


#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef string str;
typedef vector<ll> vel;
typedef vector<str> ves;
#define REP(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, b) REP(i, 0, b)
const ll mod = 1000000007;
const double pi = 3.14159265358979;

int main() {
    ll a, b, c, d;
    cin >> a >> b >> c >> d;
    cout << min(a+c, min(a+d, min(b+c, b+d))) << endl;
}

/*

*/
#include<iostream>
#include<iomanip>
#include<ios>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<string>
#include<algorithm>
#include<utility>
#include<math.h>
#include<cstdlib>
#define rep(i, n) for(int i=0;i<n;i++)
#define repb(i, a, b) for(int i=a;i<b;i++)
#define MAX_N 10000

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

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

int main(){
  int x, y, z;cin>>x>>y>>z;
  cout<<z<<" "<<x<<" "<<y<<endl;
  // system("read -n 1 -s -p \"Press any key to continue...\"");
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define FASTIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define inf 1e9
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define mod 1000000007
#define pb push_back
#define all(s) s.begin(),s.end()
#define pie 3.14159265358979323846
#define fr(i,a,b) for(ll i=a;i<b;i++)
#define frr(i,a,b) for(ll i=b-1;i>=a;i--)
#define gcd __gcd
#define con continue
#define pii pair<ll,ll>
const ll N=5e5+6;
ll seg[4*N];
void build(ll s,ll e,ll idx,ll a[]){
    if(s==e){
       //cout<<s<<" ";
       seg[idx]=a[s];
       return;
    }
    ll mid=(s+e)/2;
    build(s,mid,2*idx+1,a);
    build(mid+1,e,2*idx+2,a);
    seg[idx]=seg[2*idx+1]+seg[2*idx+2];
    return ;
}
void update(ll s,ll e,ll idx,ll pos,ll val){
    if(s==e){
       seg[idx]=val;
       return;
    }
    ll mid=(s+e)/2;
    if(pos<=mid)
    update(s,mid,2*idx+1,pos,val);
    else
    update(mid+1,e,2*idx+2,pos,val);
    seg[idx]=seg[2*idx+1]+seg[2*idx+2];
    return ;
}
ll query(ll s,ll e,ll idx,ll l,ll r){
   if(s>=l&&e<=r)return seg[idx];
   if(s>r||e<l)return 0;
   ll mid=(s+e)/2;
   return query(s,mid,2*idx+1,l,r)+query(mid+1,e,2*idx+2,l,r);
}
signed main(){
    FASTIO;
    ll tt=1;
    //cin>>tt;
    while(tt--){
         ll n,q,x,l,r;cin>>n>>q;
         ll a[n];
         for(ll i=0;i<n;i++){
            cin>>a[i];
         }
         build(0,n-1,0,a);
         while(q--){
            cin>>x;
            if(x){
               cin>>l>>r;
               cout<<query(0,n-1,0,l,r-1)<<"\n";
            }
            else{
               cin>>l>>x;
               a[l]+=x;
               update(0,n-1,0,l,a[l]);
            }
         }
    }
}

#include <bits/stdc++.h>
#include <atcoder/all>
#define REP(i, n) for (ll i = 0; i < n; i++)
#define IREP(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define all(v) v.begin(), v.end()
typedef long long ll;
using namespace atcoder;

int main(){
    int n,q;
    std::cin >> n >> q;
    fenwick_tree<ll> fw(n);
    REP(i,n){
        int input;
        std::cin >> input;
        fw.add(i,input);
    }
    REP(i,q){
        int f,p,x;
        std::cin >> f >> p >> x;
        if(f==0){
            fw.add(p,x);
        }
        else{
            std::cout << fw.sum(p,x) << std::endl;
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
int main(){
 int N,X,T;
 cin>>N>>X>>T;

 if(N % X ==0){
  cout<<(N/X)*T<<endl;
 }
 else if(N % X !=0){
  cout<<((N/X)+1)*T<<endl;
 }
}

// Created by new_sm
//
/*#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,
tree_order_statistics_node_update> ordered_set;
s.find_by_order(x) xth element in set
x.order_of_key(x) number of elements <x*/
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define vi vector<int>
#define si set<int>
#define mii map<int,int>
#define pb push_back
#define pf push_front
#define pii pair<int,int>
#define extract_word(s)  stringstream str(s); while(str>>word)
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define SET(s) cout<<fixed<<setprecision(s)
#define chotu 1000000007
#define set0(a) memset(a,0,sizeof(a))
#define endl "\n"
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define lower_string(s) transform(all(s),s.begin(),::tolower())
#define upper_string(s) transform(all(s),s.begin(),::toupper())
#define size(s) s.size()
template<class T> bool umin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool umax(T &a, T b) { return a < b ? (a = b, true) : false; }
template<typename T,typename U>
bool compare(T x,U y){return(abs(x-y)<=1e-9);}
void solve()
{
fastio
int a,b,c;
cin>>a>>b>>c;
if((a<c&&c<b)||(b<c&&c<a))
cout<<"Yes";
else
cout<<"No";
}
int main()
{
//code
fastio
int t;
t=1;
while(t--)
{
	solve();
	cout<<endl;
}
}


#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 K = 50;

char r[K*2][K*2];

void solve() {
    int a, b; scanf("%d%d", &a, &b);
    for (int i = 0; i < K; ++i) {
        for (int j = 0; j < 2*K; ++j) {
            r[i][j] = '.';
            r[i+K][j] = '#';
        }
    }
    --a; --b;
    for (int i = 0; i < K-1 && b; i+=2) {
        for (int j = 0; j < 2*K; j+=2) {
            r[i][j] = '#';
            if (!--b) goto Astuff;
        }
    }
    Astuff:
    for (int i = 0; i < K-1 && a; i+=2) {
        for (int j = 0; j < 2*K; j+=2) {
            r[i+K+1][j] = '.';
            if (!--a) goto end;
        }
    }
    end:
    puts("100 100");
    for (int i = 0; i < 2*K; ++i) {
        for (int j = 0; j < 2*K; ++j) {
            putchar(r[i][j]);
        }
        puts("");
    }
}

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

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1LL<<60;
#define all(x) (x).begin(),(x).end()
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,k,n) for(int i=(k);i<(n);++i)

int main() {
    int A, B; cin >> A >> B; A--, B--;
    vector<string> g(50,string(100,'#'));
    vector<string> g2(50,string(100,'.'));
    g.insert(g.end(),g2.begin(),g2.end());
    int i = 0; int j = 0;
    while(A--){
        g[i][j] = '.';
        j += 2;
        if(j >= 100){
            j -= 100;
            i += 2;
        }
    }
    i = 51; j = 0;
    while(B--){
        g[i][j] = '#';
        j += 2;
        if(j >= 100){
            j -= 100;
            i += 2;
        }
    }
    cout << "100 100\n";
    REP(i,100) cout << g[i] << endl;
}


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

#define REP(i, n) for(int i = 0; i < (int)(n); ++i)
#define FOR(i, m, n) for(int i = (int)(m); i < (int)(n); ++i)

const int MOD = 1e+9 + 7;

int main()
{
    int N, X, Y, Z;
    cin >> N >> X >> Y >> Z;

    long long ans = 1;
    REP(i, N) ans = ans*10 % MOD;

    int S = (1<<(X+Y+Z))-1;  // X+Y+Z-1 個前までの状態を保持しておけば十分
	int C = (1<<(Z-1))|(1<<(Y+Z-1))|(1<<(X+Y+Z-1));

    // dp[i][j]: i-1まで見たとき、直前が j で XYZ を含まない数列の数
    vector<vector<long long>> dp(N+1, vector<long long>((1<<(X+Y+Z))+1));
	dp[0][0] = 1;
    FOR(i, 1, N+1){
        REP(s, S+1){ // s: 直前の状態
            if(dp[i-1][s] == 0) continue;
            FOR(j, 1, 11){  // i 番目が j のとき
                int sn = (s<<j)|(1<<(j-1));
                sn &= S;  // sn の bit 桁数を S 未満にする
                if((sn&C) != C){
                    dp[i][sn] += dp[i-1][s];
                    dp[i][sn] %= MOD;
                }
            }
        }
    }

    REP(i, S+1) ans = (ans-dp[N][i]) % MOD;
    cout << (ans+MOD)%MOD << endl;

    return 0;
}

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
typedef long long ll;
#define N 4000011
const int p=1e9+7;
int n,k,dp[2002][2002],inv[N],f[N],finv[N];
inline int C(int nn,int mm)
{
	if(mm==nn||mm==0)return 1;if(nn<mm)return 0;
	return 1ll*f[nn]*finv[mm]%p*finv[nn-mm]%p;
}
int main()
{
	f[1]=finv[1]=inv[1]=1;
	for(int i=2;i<=4000000;i++)
	{
		inv[i]=1ll*(p-p/i)*inv[p%i]%p;
		f[i]=1ll*f[i-1]*i%p;finv[i]=1ll*finv[i-1]*inv[i]%p;
	}
	scanf("%d%d",&n,&k);
	if(k==1){puts("1");return 0;}
	dp[0][0]=1;
	for(int i=1;i<=n;i++)for(int j=0;j<=i;j++)
	{
		dp[i][j]=dp[i-1][j];
		if(j)(dp[i][j]+=1ll*C(n-i+(n-j+1)*(k-1)-1,k-2)*dp[i][j-1]%p)%=p;
	}
	printf("%d\n",1ll*dp[n][n]*f[n]%p);
}


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

int MAX_N=262144*2;
vector<int64_t> seg(MAX_N*2-1);
int64_t qu(int a,int b,int k,int l, int r){
  if(r<=a||b<=l){
    return 0;
  }
  else if(a<=l&&r<=b){
    return seg[k];
  }
  else{
    int64_t X=qu(a,b,k*2+1,l,(l+r)/2);
    int64_t Y=qu(a,b,k*2+2,(l+r)/2,r);
    return X+Y;
  }
}
void pu(int64_t a,int b){
  int D=MAX_N-1+b;
  while(D>0){
    seg[D]+=a;
    D--;
    D/=2;
  }
  seg[0]+=a;
}

int main() {
  int N,Q;
  cin>>N>>Q;
  vector<int> p(N);
  for(int i=0;i<N;i++){
    cin>>p[i];
    p[i]--;
  }
  vector<tuple<int,int,int>> q(Q);
  for(int i=0;i<Q;i++){
    int a,b;
    cin>>a>>b;
    q[i]=make_tuple(b,a,i);
  }
  sort(q.begin(),q.end());
  vector<int> la(N,-1);
  vector<int> an(Q);
  int W=0;
  for(int i=0;i<Q;i++){
    int a,b,c;
    tie(a,b,c)=q[i];
    while(W<a){
      if(la[p[W]]!=-1){
        pu(-1,la[p[W]]);
      }
      la[p[W]]=W;
      pu(1,W);
      W++;
    }
    an[c]=qu(b-1,a,0,0,MAX_N);
  }
  for(int i=0;i<Q;i++){
    cout<<an[i]<<endl;
  }
}

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

using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using LL = long long;
using VL = vector<LL>;
using VVL = vector<VL>;
using PLL = pair<LL, LL>;
using VS = vector<string>;

#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))

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

#define FF first
#define SS second
template<class S, class T>
istream& operator>>(istream& is, pair<S,T>& p){
  return is >> p.FF >> p.SS;
}
template<class S, class T>
ostream& operator<<(ostream& os, const pair<S,T>& p){
  return os << p.FF << " " << p.SS;
}
template<class T>
void maxi(T& x, T y){
  if(x < y) x = y;
}
template<class T>
void mini(T& x, T y){
  if(x > y) x = y;
}

const double EPS = 1e-10;
const double PI  = acos(-1.0);
const LL MOD = 1e9+7;
struct Edge{
  int to, cost;

  Edge(int t, int c = 0): to(t), cost(c)
  {}
  bool operator>(const Edge& rhs) const{
	return cost > rhs.cost;
  }
  bool operator<(const Edge& rhs) const{
	return cost < rhs.cost;
  }

};
using Graph =  vector< vector<Edge> >;

void add_edge(Graph& graph, int u, int v, int cost = 0){
  graph[u].push_back(Edge(v,cost));
  graph[v].push_back(Edge(u,cost));
}

void lowlink(int u, int p, int& num, const Graph& G, VI& ord, VI& low,
			 VI& art, vector<pair<int,int>>& brd){
  ord[u] = low[u] = num++;
  int chd_num = 0, chd_low_mx = -1;

  for(unsigned int i=0;i<G[u].size();++i){
	auto& e =  G[u][i];
	int to = e.to;
	if(to == p) continue;
	if(ord[to] < 0){
	  lowlink(to, u, num, G, ord, low, art, brd);
	  low[u] = min(low[u], low[to]);
	  if(ord[u] < low[to]){
		brd.push_back(MP(u, i));
	  }
	  ++chd_num;
	  chd_low_mx = max(chd_low_mx, low[to]);
	}
	else{
	  low[u] = min(low[u], ord[to]);
	}
  }

  if(p == -1){
	if(chd_num > 1)
	  art.push_back(u);
  }
  else{
	if(ord[u] <= chd_low_mx)
	  art.push_back(u);
  }
}

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

  int V, E;
  cin >> V >> E;
  Graph G(V);
  REP(i,E){
	int u, v;
	cin >> u >> v;
	add_edge(G, u, v);
  }

  int num = 0;
  VI ord(V, -1);
  VI low(V, -1);
  vector<PII> brd;
  VI art;
  lowlink(0, -1, num, G, ord, low, art, brd);
  SORT(art);
  for(int v: art) cout << v << endl;

  return 0;
}


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

int main(){
  int n,m;
  cin>>n>>m;
  int ans;
  for(int i = m/n; i >= 1; i--){
    if((m-n*i)%i==0) {ans=i; break;}
  }
  cout<<ans<<endl;
}

#include "bits/stdc++.h"
using namespace std;
using ll = long long;
#define rep(i, begin, n) for (int i = begin; i < n; i++)
#define repe(i, begin, n) for (int i = begin; i <= n; i++)
#define repr(i, begin, n) for (int i = begin; i > begin - n; i--)
#define repre(i, begin, end) for (int i = begin; i >= end; i--)

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

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

const int inf = 1000000007;
const int MOD = 1000000007;
const long long INF = 1000000000000000007;

// -------------------------------------------------------
int N, M;

int main() {
  cin >> N >> M;
  vector<int> divs;
  for (int i = 1; i * i <= M; ++i) {
    if (M % i == 0) {
      divs.push_back(i);
      if (M / i != i) {
        divs.push_back(M / i);
      }
    }
  }
  sort(divs.begin(), divs.end());
  auto lb = lower_bound(divs.begin(), divs.end(), N);
  int nn = *lb;
  cout << M / nn << endl;
}


#include<iostream>
#include<vector>
#include<algorithm>
#define MOD 1000000007
using namespace std;
int main(){
  long long int r=0,c=0;
  long long int n,m; cin >> n >> m;
  vector<int> row(n), col(m);
  for(int i=0;i<n;i++) cin >> row[i];
  for(int i=0;i<m;i++) cin >> col[i];
  sort(row.begin(), row.end(), greater<int>());
  sort(col.begin(), col.end(), greater<int>());
  long long int ans = 1;
  int rp=0,cp=0;
  for(int i=0;i<n*m;i++){
    int num = n*m-i;
  	if(rp<n && row[rp]==num && cp<m && col[cp]==num){
    	ans = ans; r++;c++;rp++;cp++;
    }else if(rp<n && row[rp]==num){
    	ans = (c * ans)%MOD; rp++; r++;
    }else if(cp<m && col[cp]==num){
    	ans = (r * ans)%MOD; cp++; c++;
    }else{
      	ans = ((r*c - i)*ans)% MOD;
    }
  }
  cout << ans << endl;
  return 0;
}

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <math.h>
#include <iomanip>
#include <bitset>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <utility>
#include <set>
#include <queue>
using namespace std;
typedef long long int ll;
typedef long double ld;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const long long INF = 1LL << 60;
typedef pair<ll,ll> pairs;
vector<pairs> p;
bool pairCompare(const pair<double,ll>& firstElof, const pair<double,ll>& secondElof){
    return firstElof.first < secondElof.first;
}
bool pairCompareSecond(const pair<double,ll>& firstElof, const pair<double,ll>& secondElof){
    return firstElof.second < secondElof.second;
}
#define MAX_N 100100
#define MOD 1000000007
bool x[MAX_N];
ll num[MAX_N];
ll fibl[MAX_N]={0};
// 四方向への移動ベクトル
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

ll fib(ll a){
    if (fibl[a]!=0)return fibl[a];
    if (a==0){
        return 0;
    }else if (a==1){
        return 1;
    }
    return fibl[a]=fib(a-1)+fib(a-2);
}

ll eratosthenes(ll n) {
    int p = 0;
    for (ll i=0; i<=n; ++i) x[i] = true;
    x[0] = x[1] = false;
    for(int i=2; i<=n; ++i) {
        if(x[i]) {
            p++;
            for(int j=2*i; j<=n; j+=i) x[j] = false;
        }

        num[i] = p;
    }
    return p;
}

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

ll keta(ll N){
    int tmp{};
    while( N > 0 ){
        tmp += ( N % 10 );
        N /= 10;
    }
    N = tmp;
    return N;
}

int main(){
    ld n,ave=0,c;
    cin >> n;
    ld a[(ll)n];
    for (ll i=0;i<n;i++){
        cin >> a[i];
        ave+=a[i];
    }
    ave/=n;
    c=abs(a[0]-ave);
    bool flag=true;
    for (ll i=0;i<n;i++){
        if (c!=abs(a[i]-ave)){
            flag=false;
            c=min(c,abs(a[i]-ave));
        }
    }
    if (flag){
        cout << 0 << endl;
    }else{
        for (ll i=0;i<n;i++){
            if (c==abs(a[i]-ave)){
                cout << i << endl;
                return 0;
            }
        }
    }
    return 0;
}


/**
 *    author:  FromDihPout
 *    created: 2020-06-21
**/

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

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

    int n, x;
    cin >> n >> x;
    vector<long long> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    long long ans = 0;
    for (int i = 1; i < n; i++) {
        long long diff = min(a[i] + a[i - 1] - x, a[i]);
        if (diff > 0) {
            a[i] -= diff;
            ans += diff;
        }
        diff = a[i] + a[i - 1] - x;
        if (diff > 0) {
            a[i - 1] -= diff;
            ans += diff;
        }
    }
    cout << ans << endl;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)
typedef long long int ll;
int main()
{
	int n;
	ll a, b, x, d, op = 0 ;
	cin >> n >> x;
	cin >> a >> b;
	if( a + b > x )
	{
		d = a + b - x;
		if( d <= b ){
	    	b -= d;
        }
	    else
        {
	    	b = 0;
        }
          op += d;
	}
	a = b;
	rep(i,2,n)
	{
	    cin >> b;
	    if( a + b > x )
		{
			d = a + b - x;
			if( d <= b ){
		    	b -= d;
            }
		    else
            {
              b = 0;
            }
			op += d;
		}
		a = b;
	}
	cout << op<< endl;
	return 0;
}


#include <iostream>
using namespace std;
int sports[1001];
int money,ans,maxvote,n,m,k;
int vote[1001];
int main(){
	cin >> n >> m;
	for(int i=1;i<=n;i++){
		cin >>sports[i];
	}
	for(int i=1;i<=m;i++){
		cin >> money;
		k=1;
		while(money<sports[k]){
			k++;
		}
		vote[k]++;
	}
	ans=1;
	maxvote=0;
	for(int i=1;i<=n;i++){
		if(vote[i]>maxvote){
			maxvote=vote[i];
			ans=i;
		}
	}
	cout << ans<<endl;
	return 0;
}

#pragma target("avx")
#pragma optimize("O3")
#include<bits/stdc++.h>
using ll = int_fast64_t;
using P = std::pair<ll,ll>;
using PP = std::pair<ll,P>;
using V = std::vector<ll>;
template<typename T> using pq = std::priority_queue<T>;
template<typename T> using rpq = std::priority_queue<T, std::vector<T>, std::greater<T>>;

#define REP(i,b,e) for(ll i=b; i<e; i++)
#define ALL(vec) vec.begin(),vec.end()
#define PRINT(vec) printf("[ ");for(auto &i:vec)printf("%ld ",i);puts("]");
#define SCAN(vec) for(auto &i:vec)scanf("%ld",&i)
#define fi first
#define se second

const int MOD = 1e9+7;
const ll INF = 1e18;
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1}, dy[] = {1, 0, -1, 0, 1, -1, -1, 1};

int main(){
	int n, m;
	scanf("%d %d", &n, &m);
	int s[m];
	char c[m];
	REP(i, 0, m){
		scanf("%d %c", &s[i], &c[i]);
		s[i]--;
	}

	REP(i, 0, 1000){
		std::string num = std::to_string(i);
		if(num.size()!=n) continue;

		bool ok = true;
		REP(j, 0, m){
			if(num[s[j]]!=c[j]) ok = false;
		}
		if(ok){
			printf("%ld\n", i);
			return 0;
		}
	}
	puts("-1");
	return 0;
}


#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
#define show(x) {for(auto i: x){cout << i << " ";} cout << endl;}
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
    int N;
    cin >> N;
    vector<int> A(N+1);  // 1-indexed
    rep(i, N) cin >> A[i+1];
    vector<int> ans;
    for (int i = N; i >= 1; i--) {
        int temp = 0;
        for (int j = i; j <= N; j += i) {
            temp ^= A[j];
        }
        if (A[i] ^ temp) A[i] ^= 1;
        if (A[i]) ans.push_back(i);
    }
    cout << sz(ans) << '\n';
    rep(i, sz(ans)) printf("%d%c", ans[i], i+1==sz(ans) ? '\n' : ' ');
    return 0;
}

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int Max_N(1000050);

int N, Temp[26], Last[Max_N][26];
char S[Max_N], T[Max_N];

void init()
{
	scanf("%d", &N);
	scanf("%s", S + 1);
	scanf("%s", T + 1);
	memset(Temp, -1, sizeof(Temp));
	for (int i = 1;i <= N;++i)
	{
		Temp[S[i] - 'a'] = i;
		for (int j = 0;j < 26;++j)
			Last[i][j] = Temp[j];
	}
}

//只考虑答案不为0的情况。考虑逆变换
//显然对于连续一串相同的字母，只要考虑第一个即可，因为后面的都可以在最后一次操作中直接达到目的
//考虑把S0变换到T的所有串从下到上写到一起，那么对于T的每个连续段的首字母，都是先向下走一格，然后每次只能向下走或向左走，直到走到S0
//每条路径经过的字母相同，并且路径之间两两不相交
//贪心地考虑，肯定是每个点先向下走，直到碰到其他路径再向左走，如此反复，直到到达目的地
//考虑从后往前维护每个有用的T[i]的路径，记录每个点上最高的路径
//每次找到一个p满足p是最右的，在i的左边（包括i），S[p] = T[i]，并且上方没有路径
//若p = i，那么答案显然是不变的
//若p < i，那么对于所有p <= k <= i - 1，H[k] = H[k + 1] + 1

#define LEFT  (segt[cur].l)
#define RIGHT (segt[cur].r)
#define MID   (segt[cur].mid)
#define MIN   (segt[cur].Min)
#define TAG   (segt[cur].Tag)
#define LCH   (cur << 1)
#define RCH   ((cur << 1) | 1)

struct node
{
	int l, r, mid, Tag, Min;
};
int dt;
node segt[Max_N << 2];

void build_tree(const int &cur, const int &l, const int &r)
{
	LEFT = l, RIGHT = r, MID = l + ((r - l) >> 1), TAG = 0, MIN = 1;
	if (l == r)
		return;
	build_tree(LCH, l, MID), build_tree(RCH, MID + 1, r);
}

inline void pushup(const int &cur)
{
	MIN = min(segt[LCH].Min, segt[RCH].Min);
}

inline void Add(const int &cur, const int &v)
{
	TAG += v, MIN += v;
}

inline void pushdown(const int &cur)
{
	if (TAG)
		Add(LCH, TAG), Add(RCH, TAG), TAG = 0;
}

void insert(const int &cur, const int &l, const int &r, const int &v)
{
	if (LEFT == l && RIGHT == r)
	{
		Add(cur, v);
		return;
	}
	pushdown(cur);
	if (r <= MID)
		insert(LCH, l, r, v);
	else
		if (l > MID)
			insert(RCH, l, r, v);
		else
			insert(LCH, l, MID, v), insert(RCH, MID + 1, r, v);
	pushup(cur);
}

int query(const int &cur, const int &pos)
{
	if (LEFT == RIGHT)
		return MIN;
	pushdown(cur);
	if (pos <= MID)
		return query(LCH, pos);
	else
		return query(RCH, pos);
}

int query(const int &cur, const int &l, const int &r)
{
	if (MIN > 1)
		return -1;
	if (LEFT == l && RIGHT == r)
		if (LEFT == RIGHT)
			return LEFT;
		else
		{
			pushdown(cur);
			if (segt[RCH].Min <= 1)
				return query(RCH, MID + 1, r);
			else
				return query(LCH, l, MID);
		}
	pushdown(cur);
	if (r <= MID)
		return query(LCH, l, r);
	else
		if (l > MID)
			return query(RCH, l, r);
		else
		{
			int v = query(RCH, MID + 1, r);
			if (v == -1)
				return query(LCH, l, MID);
			else
				return v;
		}
}

bool check()
{
	for (int i = 1;i <= N;++i)
		if (S[i] != T[i])
			return false;
	return true;
}

void work()
{
	build_tree(1, 1, N);
	int Ans(1);
	for (int i = N, p, h;i >= 1;--i)
		if (i == 1 || T[i] != T[i - 1])
		{
			if ((p = query(1, 1 + dt, i + dt)) == -1)
			{
				printf("-1");
				return;
			}
			p -= dt;
			if ((p = Last[p][T[i] - 'a']) == -1)
			{
				printf("-1");
				return;
			}
			h = query(1, i + dt);
			if (p <= i - 1)
				Ans = max(Ans, h + 1), ++dt, insert(1, p + dt, (i - 1) + dt, +1);
		}
	printf("%d", Ans);
}

int main()
{
	init();
	if (check())
		printf("0");
	else
		work();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=5e5;
const int mod=1e9+7;
int a[maxn+5],num[maxn+5],dp[maxn+5];
int x[1<<20],y[1<<20];
int n;
void update(int &x,int y)
{
	x=(x+y)%mod;
}
int main()
{

	scanf("%d",&n);
	for(int i=1;i<=n;++i) scanf("%d",&a[i]),a[i]^=a[i-1],num[i]=num[i-1]+(a[i]==0);
	for(int i=1;i<=n;++i) dp[i]=1;
	for(int i=1;i<=n;++i)
	{
		update(dp[i],(1LL*x[a[i]]*num[i]-y[a[i]])%mod);
		update(x[a[i]],dp[i]);
		update(y[a[i]],1LL*dp[i]*num[i]%mod);
	}
	if(dp[n]<0) dp[n]+=mod;
	if(a[n]>0) cout<<dp[n];
	else
	{
		int ans=1;
		for(int i=1;i<=num[n-1];++i) ans=ans*2%mod;
		for(int i=1;i<=n;++i)
			if(a[i]>0) update(ans,dp[i]);
		if(ans<0) ans+=mod;
		cout<<ans<<endl;
	}
	return 0;
}

#include <unordered_map>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <set>
using namespace std;

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

const ll MAX_N = 6e5 + 25, MAX_M = 1500000, MOD = 1e9 + 7;
ll n, ans;
ll Arr[MAX_N];
ll dp[MAX_N];
ll DP[MAX_N];
ll ind;
vector <ll> vec[MAX_N];
vector <pii> P[MAX_M];
unordered_map <ll, ll> Map;

ll _sum (ll a, ll b) {
	ll c = a + b;
	if (MOD <= c)
		c -= MOD;

	return c;
}

ll _mul (ll a, ll b) {
	ll c = 1LL * a * b % MOD;
	return c;
}

ll _pow (ll a, ll b) {
	if (!b)
		return 1;

	ll res = _pow(a, b >> 1);
	res = _mul(res, res);

	if (b & 1)
		res = _mul(res, a);

	return res;
}

void solve1(ll tmp) {
	ll a = 1, b = 0, x = 0;
	for (ll i = 0; i < n; i++) {
		x ^= Arr[i];

		if (x != 0 && x != tmp)
			continue;

		if (x == 0) {
			dp[i] = b;
			a = _sum(a, dp[i]);
		}
		else {
			dp[i] = a;
			b = _sum(b, dp[i]);
		}
	}

	cout << dp[n - 1] << "\n";
}

ll calc1() {
	ll x = 0, cnt = 0;
	for (ll i = 0; i < n; i++) {
		x ^= Arr[i];
		if (!x)
			cnt++;
	}

	return _pow(2, cnt - 1);
}

ll calc2(ll tmp) {
	ll res = P[tmp][0].second;
	DP[0] = 0;
	dp[0] = P[tmp][0].second;

	for (ll i = 1; i < P[tmp].size(); i++) {
		dp[i] = _mul(_mul(dp[i - 1], P[tmp][i].second), P[tmp][i].first - P[tmp][i - 1].first);
		ll f = _mul(dp[i - 1], _pow(P[tmp][i - 1].second, MOD - 2));
		f = _sum(f, _mul(P[tmp][i].first - P[tmp][i - 1].first, DP[i - 1]));
		dp[i] = _sum(dp[i], _mul(f, P[tmp][i].second));

		DP[i] = _sum(DP[i - 1], dp[i - 1]);
		res = _sum(res, dp[i]);
	}

	return res;
}

void process1() {
	ll x = 0;
	for (ll i = 0; i < n; i++) {
		vec[ind].push_back(Arr[i]);
		x ^= Arr[i];
		if (!x)
			ind++;
	}
}

void process2() {
	for (ll i = 0; i < ind; i++) {
		Map.clear();
		ll x = 0;
		for (ll j = 0; j + 1 < vec[i].size(); j++) {
			x ^= vec[i][j];
			Map[x]++;
		}

		for (auto t: Map)
			P[t.first].push_back({i, t.second});
	}
}

int main() {
	ios::sync_with_stdio(false), cin.tie(0);
	cin >> n;
	for (ll i = 0; i < n; i++)
		cin >> Arr[i];

	ll tmp = 0;
	for (ll i = 0; i < n; i++)
		tmp ^= Arr[i];

	if (tmp) {
		solve1(tmp);
		return 0;
	}

	ans = calc1();
	process1();
	process2();

	for (ll i = 0; i < MAX_M; i++)
		if (P[i].size())
			ans = _sum(ans, calc2(i));

	cout << ans << "\n";
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for (int i = 0; i < n; i++)
typedef long long ll;
// Welcome to my source code!

struct Node {
    int key;
    Node *parent, *left, *right;
};

Node *root, *NIL;

void insertKey(int key) {
    Node *y = NIL;
    Node *x = root;
    Node *z;

    z = new Node;
    z->key = key;
    z->left = NIL;
    z->right = NIL;

    while (x != NIL) {
        y = x;
        if (z->key < x->key) {
            x = x->left;
        } else {
            x = x->right;
        }
    }
    z->parent = y;

    if (y == NIL) {
        root = z;
    } else {
        if (z->key < y->key) {
            y->left = z;
        } else {
            y->right = z;
        }
    }
}

Node* findKey(Node *u, int key) {
    while (u != NIL && u->key != key) {
        if (key < u->key) {
            u = u->left;
        } else {
            u = u->right;
        }
    }
    return u;
}

Node* getMinimum(Node *x) {
    while (x->left != NIL) x = x->left;
    return x;
}

Node* getSuccessor(Node *z) {
    if (z->right != NIL) return getMinimum(z->right);
    Node *y = z->parent;
    while (y != NIL && z == y->right) {
        z = y;
        y = y->parent;
    }
    return y;
}

Node* deleteNode(Node *z) {
    Node *y;
    Node *x;

    if (z->left == NIL || z->right == NIL) y = z;
    else y = getSuccessor(z);

    if (y->left != NIL) x = y->left;
    else x = y->right;

    if (x != NIL) x->parent = y->parent;

    if (y->parent == NIL) root = x;
    else if (y == y->parent->left) y->parent->left = x;
    else y->parent->right = x;

    if (y != z) z->key = y->key;
}

void inorder(Node *u) {
    if (u == NIL) return;
    inorder(u->left);
    printf(" %d", u->key);
    inorder(u->right);
}

void preorder(Node *u) {
    if (u == NIL) return;
    printf(" %d", u->key);
    preorder(u->left);
    preorder(u->right);
}

int main() {
    int m, k;
    string com;
    cin >> m;

    REP(i,m) {
        cin >> com;
        if (com == "print") {
            inorder(root);
            cout << '\n';
            preorder(root);
            cout << endl;
        } else {
            cin >> k;
            if (com == "insert") insertKey(k);
            else if (com == "find") {
                if (findKey(root, k) != NIL) cout << "yes" << endl;
                else cout << "no" << endl;
            } else {
                deleteNode(findKey(root, k));
            }
        }
    }
}

#include <iostream>
using namespace std;

int main() {
    int AsPoint, BsPoint, AsCard, BsCard;
    int key;
    while(true){
        cin >> key;
        AsPoint = BsPoint = 0;
        if(key == 0) return 0;
        while(key != 0) {
            cin >> AsCard;
            cin >> BsCard;
            if(AsCard == BsCard) {
                AsPoint += AsCard;
                BsPoint += BsCard;
            } else {
                AsPoint += (AsCard > BsCard) ? AsCard + BsCard : 0;
                BsPoint += (BsCard > AsCard) ? AsCard + BsCard : 0;
            }
            --key;
        }
        cout << AsPoint << " " << BsPoint << endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
using ll=int64_t;
int main(){
  ll n,a,b,c,d,e,f,g;
  cin>>n>>a>>b>>c>>d>>e;
  f=min(min(min(min(a,b),c),d),e);
  g=n/f;
  if(n%f>0) g++;
  cout<<g+4<<endl;
}

// lcmとか__builtin_popcountとかはg++ -std=c++17 default.cppみたいなかんじで

#include <bits/stdc++.h>
#define mod 1000000007
#define INF LLONG_MAX
#define ll long long
#define ln cout<<endl
#define Yes cout<<"Yes"<<endl
#define NO cout<<"NO"<<endl
#define YES cout<<"YES"<<endl
#define No cout<<"No"<<endl
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define rep(i,n) REP(i,0,n)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
using namespace std;
ll dx[4]={1,0,-1,0};
ll dy[4]={0,1,0,-1};

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

    ll  a,b,c,d,e,n,k,maxi=0,f=0,mini=INF,sum=0,q;
	string str,stra,straa;
    ll x,y,z;
    char ca,cb,cc;
    cin>>n;
    sum+=n/500*1000;
    n%=500;
    sum+=n/5*5;
    cout<<sum<<endl;
    return 0;
}


#include <stdio.h>
#include <string.h>

int main() {
	char a[11], b[11], c[11];

	scanf("%s %s %s", a, b, c);
	int l1 = strlen(a);
	int l2 = strlen(b);

	if(a[l1 - 1] == b[0] && b[l2 - 1] == c[0]) {
		puts("YES");
	}
	else puts("NO");

	return 0;
}

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

using ll = long long;

int main() {
    int N, ans = 0;
    string A, B, C;
    cin >> N >> A >> B >> C;
    rep(i, N) {
        if (A[i] == B[i]) {
            if (A[i] != C[i]) ans++;
        } else if (A[i] == C[i]) {
            ans++;
        } else if (B[i] == C[i]) {
            ans++;
        } else {
            ans += 2;
        }
    }
    cout << ans << "\n";
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <iomanip>
#include <limits>
using namespace std;
// 素因数分解
typedef pair<long long, long long> P;
vector<P> prime_factorize(long long n)
{
vector<P> res;
for (long long p = 2; p * p <= n; ++p)
{
if (n % p != 0)
continue;
int num = 0;
while (n % p == 0)
{
++num;
n /= p;
}
res.push_back(make_pair(p, num));
}
if (n != 1)
res.push_back(make_pair(n, 1));
return res;
}
// 因数分解
long long gcd(long long x, long long y)
{
  if (y == 0)
  {
    return x;
  }
  else
  {
    return gcd(y, x % y);
  }
}
const int MOD = 1000000007;
typedef long long unsigned int ll;
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define rep(i, n) for (int i = 0; i < (n); i++)

int main()
{
    int n, r;
    cin >> n >> r;
    if(n>=10){
        cout << r << endl;
    }else{
        cout << r + 1000 - 100 * n << endl;
    }
    return 0;
}

#include<algorithm>
#include<cmath>
#include<iostream>
#include<vector>
#include<string>
using namespace std;
typedef long long ll;

#define REP(i, n) for(ll i=0;i<ll(n); i++)

int main(){
    int N, R;
    cin >> N >> R;
    int ans;
    if(N >= 10){ans = R;}
    else{ans = R + (100 * (10 - N));}
    cout << ans << endl;
    return 0;
}


#include <iostream>
using namespace std;
int main()
{
 double U = 3.14;
 int R;
cin >> R  >> U;
 cout <<  R * 2 * U <<"\n";
}

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
	int r; cin >> r;
  	cout << 2 * r * acos(-1) << endl;
  	return 0;
}


#include "bits/stdc++.h"
using namespace std;
#define ff first
#define ss second
#define pb push_back
typedef long long ll;
#define db(x) cerr<<__LINE__<<": "<<#x<<" = "<<(x)<<'\n'
const int inf = 1e9+7;
const int N = 1e6+9;

int main() {
    ios_base::sync_with_stdio(0);
    int a,b,n;
    cin>>n;
    // set<int> st;
    vector<int> v;
    for(int i=0;i<n;++i) {
        cin>>a;
        // st.insert(a);
        v.pb(a);
    }
    // for(int p: st) {
        // v.pb(p);
    // }
    sort(v.begin(), v.end());
    int cnt=0;
    for(int i=0;i<v.size();++i) {
        // cout<<v[i]<<endl;
        for(int j=i+1;j<v.size();++j) if(v[j]!=v[i]) {
            for(int k=j+1;k<v.size();++k) if(v[k]!=v[i] && v[k]!=v[j]) {
                if(v[i]+v[j]>v[k]) {
                    // cerr<<v[i]<<" "<<v[j]<<" "<<v[k]<<endl;
                    // cerr<<" "<<i+1<<" "<<j+1<<" "<<k+1<<endl;
                    ++cnt;
                }
            }
        }
    }
    cout<<cnt<<endl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
#define rep(i,n) for (ll i=0; i<n; ++i)
#define all(c) begin(c),end(c)
template<typename T1, typename T2>
bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;}
template<typename T1, typename T2>
bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;}
/*

【Yesになるべき。NoでもACしたパターン】
4
(
)((
))((((
))))
*/

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

  ll N; cin >> N;
  ll L=0, R=0;
  vector<pair<ll,ll>> plus(0, {0,0}), minus(0, {0,0});
  rep(i, N){
    string S; cin>>S;
    ll q=0, p=0, ssz = S.size();
    for (ll j=0; j<ssz; j++){
      if (S[j] == '(')
        p++;
      else{
        if (p) p--;
        else q++;
      }
    }
    if (q==0)
      R += p;
    else if (p==0)
      L += q;
    else{
      ll tmp = p-q; //プラスなら '(' が回復
      if (tmp > 0)
        plus.push_back({q, p});
      else
        minus.push_back({q, p});
    }
  }
  bool ans = true;
  sort(all(plus));
  sort(all(minus));
  reverse(all(minus));
  ll psz = plus.size();
  //まず回復パターン。弱い攻撃から受ける
  rep(i, psz){
    R -= plus[i].first;
    if (R < 0){
      ans = false;
      break;
    }
    R += plus[i].second;
  }
  //防御パターン。大きい攻撃から受ける
  ll msz = minus.size();
  rep(i, msz){
    R -= minus[i].first;
    if (R < 0){
      ans = false;
      break;
    }
    R += minus[i].second;
  }
  R -= L;
  if (R != 0)
    ans = false;
  if (ans)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}


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

int main() {
  // Grid Repainting 2
  int h, w; cin >> h >> w;
  vector<string> s(h);
  vector<vector<bool>> visited(h, vector<bool>(w));
  rep(i,h) cin >> s.at(i);

  vector<int> di = {1, 0, -1, 0};
  vector<int> dj = {0, 1, 0, -1};

  rep(i,h) {
    rep(j,w) {
      if (visited.at(i).at(j)) continue;
      if (s.at(i).at(j) == '#') {
        int cnt = 0;
        rep(k,4) {
          int ni = i + di[k];
          int nj = j + dj[k];
          if (0 <= ni && ni < h && 0 <= nj && nj < w) {
            if (s.at(ni).at(nj) == '.') { cnt++;
            } else {
              visited.at(i).at(j) == true;
              visited.at(ni).at(nj) == true;
            }
          }
        }
        if (cnt == 4) {
          cout << "No" << endl;
          return 0;
        }
      }
    }
  }
  cout << "Yes" << endl;
  return 0;
}

#include<iostream>
using namespace std;

int main(){

	int n,m;
	int judge;
	int max=0;

	cin >> n >> m ;

	int a[n];
	int b[1000]={};

	for( int i=0 ; i<n ; i++ ) cin >> a[i] ;
	for( int i=0 ; i<m ; i++ ){
		cin >> judge ;
		for( int j=0 ; j<n ; j++ ){
			if( a[j] <= judge ){
				b[j]++;
				if( max < b[j]) max=b[j];
				break;
			}
		}
	}

	for( int i=0 ; i<n ; i++ ){
		if( b[i] == max ){
			cout << i+1 << endl ;
			break;
		}
	}

return 0;
}


#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <math.h> // sqrt
using namespace std;
typedef long long ll;

int main() {
    int N, Q;
    string S;
    cin >> N >> Q;
    cin >> S;
    vector<vector<int>> data(Q, vector<int>(2));
    for (int i = 0; i < Q; i++) {
        for (int j = 0; j < 2; j++) {
            cin >> data.at(i).at(j);
        }
    }
    vector<int> rsum(N + 1, 0);
    int sum = 0;
    for (int i = 0; i < N - 1; i++) {
        if (S.at(i) == 'A' && S.at(i + 1) == 'C') {
            sum++;
        }
        rsum.at(i + 1) = sum;
    }
    int ans;
    for (int i = 0; i < Q; i++) {
        ans = rsum.at(data.at(i).at(1) - 1) - rsum.at(data.at(i).at(0) - 1);
        cout << ans << endl;
    }
}

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

int main()
{
	ll n;
	cin >> n;
	vector<ll> M(n + 1);
	cin >> M[0];
	for (int i = 1; i < n; i++) {
		cin >> M[i] >> M[i];
	}
	cin >> M[n];
	vector<vector<ll>> dp(n, vector<ll>(n, INF));
	for (int i = 0; i < n; i++) {
		dp[i][i] = 0;
	}
	for (ll k = 1; k < n; k++) {
		for (ll i = 0; i + k < n; i++) {
			for (ll j = 0; j <= i + k - 1; j++) {
				ll cost = dp[i][j] + dp[j + 1][i + k] + M[i] * M[j + 1] * M[i + k + 1];
				if (cost < dp[i][i + k]) {
					dp[i][i + k] = cost;
				}
			}
		}
	}
	cout << dp[0][n - 1] << endl;
	return 0;
}


#include<iostream>
using namespace std;
#define MAX 100
#define BLANK 0
#define LONG 4
#define SHORT 2
#define HORIZONTAL 0
#define VERTICAL 1
#define VISITED 9

int field[MAX+2][MAX+2];
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
void rec(int x,int y,int c)
{
  int i;
  if(field[x][y]!=c)return;
  field[x][y]=VISITED;
  for(i=0;i<4;i++)rec(x+dx[i],y+dy[i],c);
  return;
}

int main()
{
  int w,h,xs,ys,xg,yg,n,c,d,x,y;
  int i,j;
  while(cin>>w>>h>>xs>>ys>>xg>>yg>>n,w|h){
    for(j=1-1;j<=h+1;j++)for(i=1-1;i<=w+1;i++)field[i][j]=BLANK;
    while(n-->0){
      cin>>c>>d>>x>>y;
      if(d==HORIZONTAL)
	for(i=0;i<LONG;i++)for(j=0;j<SHORT;j++)field[x+i][y+j]=c;
      else
	for(i=0;i<SHORT;i++)for(j=0;j<LONG;j++)field[x+i][y+j]=c;
    }
    if(field[xs][ys]!=BLANK)rec(xs,ys,field[xs][ys]);
    puts(field[xg][yg]==VISITED?"OK":"NG");
  }
}

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int,int> pr;
int main() {
	// your code goes here
	int w,h;
	while(cin >> w >> h && w!=0){
	int xs,ys,xg,yg,n,m,s,sum,c,d,x,y;
	int dx[]={0,1,-1,0},dy[]={1,0,0,-1};
	cin >> xs>>ys>>xg>>yg >> n;
	bool map[102][102]={};
	int color[102][102]={};
	queue<pr> q;
		color[xs][ys]=10;
		for(int i=0;i<n;i++){
			cin >> c >> d >>x >> y;
			for(int j=0;j<2+d*2;j++)
				for(int k=0;k<4-d*2;k++)
					color[x+k][y+j]=c;
		}
		map[xs][ys]=true;
		q.push(pr(xs,ys));
		while(!q.empty()){
			int f=q.front().first,s=q.front().second;
			q.pop();
			for(int i=0;i<4;i++){
					if(color[f][s]==color[f+dx[i]][s+dy[i]] && !map[f+dx[i]][s+dy[i]]){
						q.push(pr(f+dx[i],s+dy[i]));
						map[f+dx[i]][s+dy[i]]=true;
					}
			}
		}
		if(map[xg][yg])
		cout << "OK" << endl;
		else
		cout << "NG" << endl;
	}
	return 0;
}


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

int a(char x){
	switch (x) {
		case 'I': return 1;
		case 'V': return 5;
		case 'X': return 10;
		case 'L': return 50;
		case 'C': return 100;
		case 'D': return 500;
		case 'M': return 1000;
	}
	return 0;
}
int main(void){
	string str;
	int ans=0;
	int p=0;
	while(cin>>str){
		ans=0;
		p=0;
		for(int i=str.length()-1;i>=0;i--){
			int now=a(str[i]);
			if(p>now)ans-=now;
			else ans+=now;
			p=now;
		}
		cout<<ans<<endl;
	}
	return 0;
}

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

int main() {
	char c[101],m[7]={'I','V','X','L','C','D','M'};
	int s,i,j,h,n[7]={1,5,10,50,100,500,1000};
  while(gets(c)!=NULL) {
    s=0; h=10;
	for (i=0;i<strlen(c);i++) {
     for (j=0;j<7;j++) if (c[i]==m[j]) break;
	 s+=n[j];
	 if (h<j) s-=n[h]*2;
	 h=j;
	}
    cout << s << endl;
  }
  return 0;
}