#include<bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define ff first
#define ss second
#define file_se_input  freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#define pp long long int
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define pb push_back
#define mp make_pair
#define pi 3.141592653589793238
#define eb emplace_back
#define rep(i,a,b) for (int i = a; i <= b; i++)
#define zip(i,a,b) for(int i=a;i<b;i++)
#define rzip(i,a,b) for(int i=a;i>=b;i--)
#define ll unsigned long long int
#define test int t;cin>>t; while(t--)
#define um unordered_map
#define en '\n'
#define us unordered_set
typedef pair<int, int>     pii;
typedef pair<char, int>    pci;
typedef pair<char, char>   pcc;
typedef vector<pii>       vii;
typedef long double ld;
#define all(v) v.begin(), v.end()
#define INF (1e18+5)
#define inf (1e9+5)
#define mod 1000000007

bool check_prime(long long n)
{
	int flag = 0;
	for (long long i = 2; i * i <= n; i++)
	{
		if (n % i == 0)
		{
			flag = 1;
			break;
		}
	}
	if (n == 1)
		return false;
	else if (flag == 0 || n == 2 || n == 3)
	{
		return true;
	}
	else
	{
		return false;
	}

}

int BE(int x, int n, int m) //function to calculate x raise to power n modulo m
{
	int result = 1;
	while (n > 0)
	{
		if (n % 2 == 1)
			result = result * x % m;
		x = x * x % m;
		n = n / 2;
	}
	return result;
}

int c[3];
int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(0);
#ifndef ONLINE_JUDGE
	if (fopen("input.txt", "r"))
	{
		freopen ("input.txt" , "r" , stdin);
		freopen ("output.txt" , "w" , stdout);
	}
#endif
	//_______________________________-code starts-_______________________________________________

	int n;
	cin >> n;
	int a[n];
	zip(i, 0, n)
	{
		cin >> a[i];
	}
	int ans = 1;
	zip(i, 0, n)
	{
		int cnt = 0;
		int id = -1;
		if (a[i] == c[0]) cnt++, id = 0;
		if (a[i] == c[1]) cnt++, id = 1;
		if (a[i] == c[2]) cnt++, id = 2;
		if (id == -1)
		{
			cout << 0 << en;
			return 0;
		}
		ans = (ans * cnt) % mod;
		c[id]++;
	}
	cout << ans << en;

	return 0;
}

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

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

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

const int INF = 1001001001;
const int MOD = 1000000007;
const double PI=acos(-1);

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

    ll c[3] = {};
    ll ans = 1;

    rep(i,N) {
        ll a;
        cin >> a;
        bool t = false;
        ll cnt = 0;
        rep(j,3) {
            if (c[j]==a && !t) {
                c[j]++;
                cnt++;
                t = true;
            }
            else if (c[j]==a) {
                cnt++;
            }
        }
        ans*=cnt;
        ans%=MOD;
    }
    cout << ans << endl;
}


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

int main() {
  int n;
  string s, t;
  cin >> n >> s >> t;

  for (int i = n; i >= 0; --i) {
    string u = s;
    for (int j = i; j < n; ++j) u += t[j];
    bool same_t = true;
    rep(j, n) if (u[u.size() - 1 - j] != t[t.size() - 1 - j]) same_t = false;
    if (same_t) {
      cout << 2 * n - i << endl;
      return 0;
    }
  }
  return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main(){
  int a,s,d;
  cin>>a>>s>>d;
  if(a<=s*d){
    cout<<"Yes";
  }else{
    cout<<"No";
  }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e18;
const int inf = 1e9;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define per(i, b, a) for (int i = a - 1; i >= b; i--)
#define int long long
using Graph = vector<vector<int>>;
using pint = pair<int, int>;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int dxx[8] = {1, 1, 1, 0, 0, -1, -1, -1}, dyy[8] = {-1, 0, 1, -1, 1, -1, 0, 1};

signed main() {
    int n;
    cin >> n;
    map<int, int> mp;
    rep (i, 1, n + 1) {
        int k = i;
        for (int j = 2; j * j <= k; j++) {
            while (k % j == 0) {
                k /= j;
                mp[j]++;
            }
        }
        mp[k]++;
    }
    int ans = 1;
    const int mod = 1e9 + 7;
    for (auto p : mp) {
        // cout << p.first << " : " << p.second << "\n";
        if (p.first == 1) continue;
        ans = ans * (p.second + 1) % mod;
        // cout << ans << "\n";
    }
    cout << ans << "\n";
}

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

int main() {
  int x;
  int ans;

  cin >> x;
  ans = pow(x, 3);
  cout << ans << endl;

  return 0;
}


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

using ll = long long;

constexpr ll MOD = 1e9+7;
constexpr ll INF = 1ll<<60;

#define FOR(i,a,b) for (ll i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()

int main(int argc, char **argv)
{
	ll N; cin >> N;
	vector<ll> A(N), B(N);
	REP(i, N)
		cin >> A[i] >> B[i];
	vector<ll> index(N);
	iota(ALL(index), 0ll);

	sort(ALL(index), [&](auto &a, auto &b)
					 {
						 return (A[a] + B[a] > A[b] + B[b]);
					 });

	ll res{0};
	bool ist{true};
	REP(i, N)
	{
		if (ist)
			res += A[index[i]];
		else
			res -= B[index[i]];
		ist ^= true;
	}

	std::cout << res << std::endl;
}

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

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

void ALLIN1_NUMBER(vector<ll>& V)
{
    for(auto& x : V)
    {
        cin >> x;
    }
}

template<class T>
void OUT0(T N)
{
    cout << N << endl;
}

static const double pi = acos(-1.0);

double Cos(double D)
{
    return cos(pi/180 * D);
}

class Dishes
{
    public:
    ll point_takahashi;
    ll point_aoki;
    ll sum;
    ll number;
};

void ALLIN1_DISHES(ll N, vector<Dishes>& dishes)
{
    for(ll i=0;i<N;i++)
    {
        dishes[i].number = i;
        cin >> dishes[i].point_takahashi >> dishes[i].point_aoki;
        dishes[i].sum = dishes[i].point_takahashi + dishes[i].point_aoki;
    }
}

bool Sort_TA(vector<Dishes>& dishesTA)
{
    sort
    (ALL(dishesTA),
    [](Dishes& a, Dishes& b)
    {
        return (a.sum == b.sum) ? (a.point_takahashi > b.point_takahashi) : (a.sum > b.sum);
    });
}

void Sort_AT(vector<Dishes>& dishes)
{
    sort
    (ALL(dishes),
    [](Dishes& a, Dishes& b)
    {
        return (a.sum == b.sum) ? (a.point_aoki > b.point_aoki) : (a.sum > b.sum);
    });
}

ll Ans(ll N, vector<Dishes> dishes_TA, vector<Dishes> dishes_AT)
{
    vector<bool> eaten(N,false);
    ll takahashi_point = 0;
    ll aoki_point = 0;

    ll itr_TA = 0;
    ll itr_AT = 0;

    for(ll i=0;i<N;i++)
    {
        if(i%2==0)
        {
            for(ll& j=itr_TA;j<N;j++)
            {
                if(!eaten[dishes_TA[j].number])
                {
                    eaten[dishes_TA[j].number] = true;
                    takahashi_point += dishes_TA[j].point_takahashi;
                    j++;
                    break;
                }
            }
        }
        else
        {
            for(ll& j=itr_AT;j<N;j++)
            {
                if(!eaten[dishes_AT[j].number])
                {
                    eaten[dishes_AT[j].number] = true;
                    aoki_point += dishes_AT[j].point_aoki;
                    j++;
                    break;
                }
            }
        }
    }
    return takahashi_point - aoki_point;
}

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

    vector<Dishes> dishes(N);
    ALLIN1_DISHES(N,dishes);

    vector<Dishes> dishes_TA = dishes;
    Sort_TA(dishes_TA);

    vector<Dishes> dishes_AT = dishes;
    Sort_AT(dishes_AT);

    OUT0(Ans(N,dishes_TA,dishes_AT));

    return 0;
}


#include <bits/stdc++.h>

#define REP(i, n) for(int i=0;i<(int)(n);i++)
#define ALL(x) (x).begin(),(x).end()
const int INF = 1e9;

using namespace std;
int n;
int res = 0;
struct node {
    int id;
    int parent;
};

node v[100];
vector<tuple<int, int, int>> cost;
vector<int> path;

int find(node x){
    if(x.parent == -1){
        REP(i, path.size())
            v[path[i]].parent = x.id;
        path.clear();
        return x.id;
    }
    else{
        path.push_back(x.id);
        return find(v[x.parent]);
    }
}

void marge(node x, node y){
    int x_id = find(x);
    int y_id = find(y);
    if(x_id == y_id)
        return;
    else
        v[y_id].parent = x_id;
    return;
}

bool judge(node x, node y){
    vector<int> xpath, ypath;
    return find(x) == find(y);
}

int main() {
    cin >> n;
    REP(i, n) REP(j, n) {
        int tmp;
        cin >> tmp;
        if(tmp != -1)
            cost.push_back(make_tuple(tmp, i, j));
    }
    REP(i, n){
        v[i].id = i;
        v[i].parent = -1;
    }
    sort(ALL(cost));
    REP(i, cost.size()){
        node from = v[get<1>(cost[i])];
        node to = v[get<2>(cost[i])];
        if(judge(from, to)){
            continue;
        }
        res += get<0>(cost[i]);
        marge(from, to);
    }

    cout << res << endl;
    return 0;
}

#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;
const int INF = 10000;

template<typename ISTREAM>
void readWeightedGraphMatrix(ISTREAM& cin, vector< vector<int> >& weightedGraphMatrix) {
	size_t n;
	cin >> n;
	int weight;
	weightedGraphMatrix = vector< vector<int> >(n, vector<int>(n));
	for (size_t i = 0; i != n; ++i) {
		for (size_t j = 0; j != n; ++j) {
			cin >> weight;
			weightedGraphMatrix.at(i).at(j) = weight != -1 ? weight : INF;
		}
	}
}

int MST(const vector< vector<int> >& weightedGraphMatrix) {
	enum Color {
		WHITE, GRAY, BLACK//白は未訪問でTと隣接していない、。グレーはTと隣接状態だが未訪問、黒は訪問済み。木は閉路を持たないグラフなので、全域木の計算では
		//各点は1度だけ訪問されるはず。
	};
	const size_t n = weightedGraphMatrix.size();
	vector<Color> color(n,Color::WHITE);
	vector<int> d(n, INF);
	vector<int> p(n, INF);
	color.front() = Color::GRAY;
	d.front() = 0;
	int currentNode = 0;
	while (true) {
		int min = INF;
		int u;
		//TからV-Tへ接続する辺の重みの中で最小のものを求める。
		//同時に、その辺に接続する辺のT内のノードを求める。
		for (size_t i = 0; i != n; ++i) {
			if (color.at(i) == Color::BLACK || color.at(i) == Color::WHITE) continue;//訪問済みはスキップ
			if (d.at(i) >= min)continue;
			min = d.at(i);//TからV-Tの中のノードiへ向かう辺の重みの最小値を更新
			u = i;
		}

		if (min == INF) {
			//ノード０からアクセス可能なすべての点を訪問済みになったらMST構築完了
			break;
		}

		color.at(u) = Color::BLACK;//訪問

		for (int v = 0; v != n; ++v) {
			if (color.at(v) == Color::BLACK) continue;//訪問済みはスキップ
			if (weightedGraphMatrix.at(u).at(v) == INF ) continue;
			if (weightedGraphMatrix.at(u).at(v) >= d.at(v)) continue;
			d.at(v) = weightedGraphMatrix.at(u).at(v);
			p.at(v) = u;
			color.at(v) = Color::GRAY;

		}

	}
	int sum = 0;
	for (int i = 0; i != n; ++i) {
		if (p.at(i) == INF)continue;
		sum += weightedGraphMatrix.at(i).at(p.at(i));
	}
	return sum;
}

template<typename T>
void execute(T& cin) {
	vector< vector<int> > graphList;
	readWeightedGraphMatrix(cin, graphList);
	cout << MST(graphList) << endl;
}

int main()
{

	execute(cin);

	return 0;
}


#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;
const double PI = 3.141592653589793238;
const double EPS = 1e-10;
typedef pair<int, int> P;
typedef pair<ll, P> PP;
int main() {
	int N, M;
	cin >> N >> M;
	map<P, vector<PP>> G;
	map<P, ll> dist;
	for (int i = 0; i < M; i++) {
		int p, q;
		ll c;
		cin >> p >> q >> c;
		p--; q--;
		G[P(p, 0)].emplace_back(1, P(p, c));
		G[P(p, c)].emplace_back(0, P(p, 0));
		G[P(q, 0)].emplace_back(1, P(q, c));
		G[P(q, c)].emplace_back(0, P(q, 0));
		G[P(p, c)].emplace_back(0, P(q, c));
		G[P(q, c)].emplace_back(0, P(p, c));
	}
	priority_queue<PP, vector<PP>, greater<PP>> Q;
	dist[P(0, 0)] = 0;
	Q.push(PP(0, P(0, 0)));
	while (!Q.empty()) {
		PP now = Q.top(); Q.pop();
		if (dist[now.second] < now.first) continue;
		for (auto e : G[now.second]) {
			if (dist.find(e.second) == dist.end() || dist[e.second] > now.first + e.first) {
				dist[e.second] = now.first + e.first;
				Q.push(PP(dist[e.second], e.second));
			}
		}
	}
	if (dist.find(P(N - 1, 0)) == dist.end()) {
		cout << -1 << endl;
		return 0;
	}
	cout << dist[P(N - 1, 0)] << endl;
}

#include <bits/stdc++.h>
#define sc(x) scanf("%d", &x)
#define pr(x) printf("%d\n", x)
#define lowbit(x) (x & -x)
#define ll long long
#define N 500010
using namespace std;
ll tot = 0, head[N], v[N], dis[N], n, m, cnt;
struct Xiao
{
	int next, to, w;
}f[N * 3];
void add(int x, int y, int z)
{
	f[++tot].to = y;
	f[tot].next = head[x];
	f[tot].w = z;
	head[x] = tot;
}
void spfa()
{
	memset(dis, 127, sizeof dis);
	memset(v, 0, sizeof v);
	queue <int> q;
	q.push(1);
	v[1] = 1;
	dis[1] = 0;
	while (q.size())
	{
		int x = q.front();
		q.pop();
		for (int i = head[x]; i; i = f[i].next)
		{
			int y = f[i].to;
			if (dis[y] > dis[x] + f[i].w)
			{
				dis[y] = dis[x] + f[i].w;
				if (!v[y])
				{
					v[y] = 1;
					q.push(y);
				}
			}
		}
		v[x] = 0;
	}
}
map < pair <int, int>, int > p;
int get(int x, int y)
{
	if (p.find(make_pair(x, y)) != p.end()) return p[make_pair(x, y)];
	else return p[make_pair(x, y)] = ++cnt;
}
int main()
{
	//freopen("railway.in", "r", stdin);
	//freopen("railway.out", "w", stdout);
	sc(n);
	sc(m);
	cnt = n;
	for (int i = 1; i <= m; i++)
	{
		int x, y, z;
		sc(x);
		sc(y);
		sc(z);
		int p1 = get(x, z);
		int p2 = get(y, z);
		add(p2, p1, 0);
		add(p1, p2, 0);
		add(x, p1, 1);
		add(p1, x, 1);
		add(y, p2, 1);
		add(p2, y, 1);
	}
	spfa();
	printf("%lld\n", dis[n] > 2 * n ? -1 : dis[n] / 2);
	return 0;
}


#include "bits/stdc++.h"

#define MOD 1000000007
#define rep(i, n) for(ll i=0; i < (n); i++)
#define rrep(i, n) for(ll i=(n)-1; i >=0; i--)
#define ALL(v) v.begin(),v.end()
#define rALL(v) v.rbegin(),v.rend()
#define FOR(i, j, k) for(ll i=j;i<k;i++)
#define DUMP(i, v)for(ll i=0;i<v.size();i++)cerr<<v[i]<<" "

using namespace std;
typedef long long int ll;
typedef vector<ll> llvec;
typedef vector<double> dvec;
typedef pair<ll, ll> P;
typedef long double ld;
struct edge{ll x, c;};

ll mod(ll a, ll mod){
  ll res = a%mod;
  if(res<0)res=res + mod;
  return res;
}

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

ll modinv(ll a, ll mod){
  return modpow(a, mod-2, mod);
}

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

bool is_prime(ll n){
  ll i = 2;
  if(n==1)return false;
  if(n==2)return true;
  bool res = true;
  while(i*i <n){
    if(n%i==0){
      res = false;
    }
    i = i+1;
  }

  //if(i==1)res = false;
  if(n%i==0)res=false;
  return res;
}

struct UnionFind{
  ll N;
  llvec p;
  llvec cnt;
  UnionFind(ll n){
    N = n;
    p=llvec(N);
    cnt=llvec(N, 0);
    rep(i, N){
      p[i] = i;
      cnt[i] = 1;
    }
  }

  void con(ll a, ll b){
    P at = root(a);
    P bt = root(b);
    if(at.second!=bt.second){
      if(at.first>bt.first){
        swap(at, bt);
        swap(a, b);
      }
      p[at.second] = bt.second;
      cnt[bt.second]+=cnt[at.second];
      p[a]=bt.second;
    }
  }

  P root(ll a){
    ll atmp = a;
    ll c=0;
    while(atmp!=p[atmp]){
      atmp = p[atmp];
      c++;
    }
    p[a] = atmp;
    return {c, atmp};
  }

  bool is_con(ll a, ll b){
    P at=root(a);
    P bt=root(b);
    return at.second == bt.second;
  }

};

struct dijkstra{
  ll N;
  llvec d;
  vector<vector<edge>> e;
  dijkstra(ll n){
    N = n;
    //d = llvec(N, 1e18);
    e = vector<vector<edge>>(N);
  }

  void add_edge(ll from, ll to, ll cost){
    e[from].push_back({to, cost});
  }

  void run(ll start){
    priority_queue<P, vector<P>, greater<P>> que;
    que.push({0, start});
    d = llvec(N, 1e18);
    d[start]=0;
    while(!que.empty()){
      P q = que.top();que.pop();
      ll dc = q.first;
      ll x = q.second;
      if(dc>d[x]){
        continue;
      }else{
        for(auto ip: e[x]){
          if(d[ip.x]<=d[x]+ip.c){
            continue;
          }else{
            d[ip.x]= d[x]+ip.c;
            que.push({d[ip.x], ip.x});
          }
        }
      }
    }
  }
};

/**************************************
** A main function starts from here  **
***************************************/
int main(){
  ll N, M, Q;
  cin >> N >> M >> Q;
  vector<llvec> T(N+1, llvec(N+1, 0));
  vector<llvec> S(N+1, llvec(N+1, 0));
  rep(i, M){
    ll a, b;
    cin >> a >> b;
    T[a][b] += 1;
  }
  FOR(i, 1, N+1){
    FOR(j, 1, N+1){
      S[i][j] = S[i][j-1] + T[i][j];
    }
  }
  FOR(i, 1, N+1){
    FOR(j, 1, N+1){
      S[i][j] += S[i-1][j];
    }
  }

  rep(i, Q){
    ll a, b;
    cin >> a >> b;
    a--;
    cout << S[b][b] - S[a][b] - S[b][a] + S[a][a] << endl;
  }

  return 0;
}

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

int main() {
  long h,w,k,mod=1000000007;
  cin >> h >> w >> k;
  if(w==1) {
    cout << 1 << endl;
    return 0;
  }
  vector<long> sayu(w-1),sonomama(w);
  vector<vector<long>> amida(h+1,vector<long>(w));
  amida[0][0]=1;
  for(long bit=0;bit<(1<<(w-1));bit++) {
    bool imp=false;
    for(long i=0;i<w-2;i++) {
      if((bit&(1<<i))&&(bit&(1<<(i+1)))) imp=true;
    }
    if(imp) continue;
    for(long i=0;i<w-1;i++) if(bit&(1<<i)) sayu[i]++;

    if(!(bit&(1<<0))) sonomama[0]++;
    if(!(bit&(1<<(w-2)))) sonomama[w-1]++;
    for(long i=1;i<w-1;i++) if(!(bit&(1<<(i-1))||bit&(1<<i))) sonomama[i]++;
  }

  for(long i=0;i<h;i++) {
    for(long j=0;j<w-1;j++) {
      amida[i+1][j]=(amida[i+1][j]+amida[i][j+1]*sayu[j])%mod;
      amida[i+1][j+1]=(amida[i+1][j+1]+amida[i][j]*sayu[j])%mod;
    }
    for(long j=0;j<w;j++) {
      amida[i+1][j]=(amida[i+1][j]+amida[i][j]*sonomama[j])%mod;
    }
  }

  cout << amida[h][k-1] << endl;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <functional>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <random>
#include <cfloat>
#define norm(X) ((X) * (X))
using namespace std;
template<class T, class Compare = less<T> >
using MaxHeap = priority_queue<T, vector<T>, Compare>;
template<class T, class Compare = greater<T> >
using MinHeap = priority_queue<T, vector<T>, Compare>;
using llong = long long;

const llong s = 1000000;
const double R = 1e11;
llong n;
double px[105];
double py[105];
llong cnt[105];
double x, y;
double r;
double sx, sy;
llong indx;

int main() {

    cin >> n;
    for (llong i = 0; i < n; i++) {
        cin >> px[i];
        cin >> py[i];

        sx += px[i];
        sy += py[i];
    }
    sx /= n;
    sy /= n;

    for (llong i = 1; i <= s; i++) {
        x = R * cos(i * 2.0 * M_PI / s) + sx;
        y = R * sin(i * 2.0 * M_PI / s) + sy;
        indx = 0;

        r = 1e32;

        //cout << x << ' ' << y << endl;
        for (llong j = 0; j < n; j++) {
            double d = norm(x - px[j]) + norm(y - py[j]);

            if (d < r) {
                indx = j;
                r = d;
            }
        }

        cnt[indx]++;
    }

    for (llong i = 0; i < n; i++) {
        printf("%.*lf\n", DBL_DIG, (double)cnt[i] / (double)s);
    }

    return 0;
}

#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define loop(i,n) rep(i,0,n)
using namespace std;
using ll = long long;
constexpr double pi=3.1415926535897932;
int main(){
    int n;
    cin >> n;
    vector<double> x(n),y(n);
    loop(i,n)cin >> x[i] >> y[i];
    loop(i,n){
        vector<double> theta;
        loop(j,n){
            if(i==j)continue;
            theta.push_back(atan2(y[i]-y[j],x[i]-x[j]));
        }
        sort(theta.begin(),theta.end());
        double probability=0;
        if(theta[n-2]-theta[0]<pi)probability=0.5-(theta[n-2]-theta[0])/(2*pi);
        else{
            loop(i,n-2){
                if(theta[i+1]-theta[i]>pi){
                    probability=-0.5+(theta[i+1]-theta[i])/(2*pi);
                    break;
                }
            }
        }
        cout << setprecision(20) << probability << endl;
    }
    return 0;
}


#include <stdio.h>
struct Dice {
  int face1, face2, face3, face4, face5, face6;
};

void roll_for_E(struct Dice *dice);
void roll_for_N(struct Dice *dice);
void roll_for_S(struct Dice *dice);
void roll_for_W(struct Dice *dice);

int main(void)
{
  struct Dice dice;
  char ch;
  scanf("%d%d%d%d%d%d", &(dice.face1), &(dice.face2), &(dice.face3), &(dice.face4), &(dice.face5), &(dice.face6));

  (void)getchar();

  while((ch=getchar())!='\n') {
    if(ch=='E')
      roll_for_E(&dice);
    if(ch=='N')
      roll_for_N(&dice);
    if(ch=='S')
      roll_for_S(&dice);
    if(ch=='W')
      roll_for_W(&dice);
  }
  printf("%d\n", dice.face1);
  return 0;
}

void roll_for_E(struct Dice *dice)
{
  int temp;
  temp = dice->face1;
  dice->face1=dice->face4;
  dice->face4=dice->face6;
  dice->face6=dice->face3;
  dice->face3=temp;
}

void roll_for_N(struct Dice *dice)
{
  int temp;
  temp = dice->face1;
  dice->face1=dice->face2;
  dice->face2=dice->face6;
  dice->face6=dice->face5;
  dice->face5=temp;
}

void roll_for_S(struct Dice *dice)
{
  int temp;
  temp = dice->face1;
  dice->face1=dice->face5;
  dice->face5=dice->face6;
  dice->face6=dice->face2;
  dice->face2=temp;
}

void roll_for_W(struct Dice *dice)
{
  int temp;
  temp = dice->face1;
  dice->face1=dice->face3;
  dice->face3=dice->face6;
  dice->face6=dice->face4;
  dice->face4=temp;
}

#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <utility>
typedef long long ll;
using namespace std;
const int mod=1e9+7;

ll solve(){
    ll sm=0,ans=0;
    ll n,t=0;
    cin>>n;
    int a[n];
    for(int &i:a) cin>>i,sm=(sm+i)%mod;
    for(int i=0;i<n;i++){
        t=(t+a[i])%mod;
        ans=(ans + ((sm-t+mod)%mod)*a[i]%mod )%mod;
    }
    return ans;
}

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


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
#define INF -1000000000000000
#define pb push_back
#define sz(a) int((a).size())
#define all(c) c.begin(), c.end()
#define tr(c, i) for (typeof(c).begin() i = c.begin(); i != c.end(); i++)
#define present(c, x) (c.find(x) != c.end())
#define cpresent(c, x) (find(all(c), x) != c.end())

ll n,ara[200005];
vector< vector<ll> > dp(200005, vector<ll> (5,INF));

ll f(ll i, ll j){

    if(i>n) return 0;

    if(dp[i][j]!=INF) return dp[i][j];

    if(j==0){
        if(i>=n) return 0;
        dp[i][j] = ara[i] + max(max(f(i+2,0),f(i+3,3)),f(i+4,1));
        return dp[i][j];
    }
    if(j==2) return dp[i][j] = ara[i] + max(f(i+2,2),f(i+3,1));
    if(j==3) return dp[i][j] = ara[i] + max(f(i+2,3),f(i+3,1));
    else     return  dp[i][j] = ara[i] + f(i+2,1);

}

int main(){

    ll i;
    cin>>n;
    for(i=1;i<=n;i++) cin>>ara[i];
    if(n%2==1){
    cout<<max(max(f(1,0),f(2,2)),f(3,1))<<endl;
    }
    else{
        cout<<max(f(1,2),f(2,1))<<endl;
    }

	return 0;
}


#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int main(){
int a,b;
  cin>>a>>b;
  int counter=1;
  int cnt=0;
 while(counter<b){
  counter--;
 counter+=a;
 cnt++;
 }
  cout<<cnt<<endl;
}

#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<cstring>
#include<queue>
#include<tuple>
#include<utility>
#include<map>
#include<math.h>
#include<iomanip>
#include<stdio.h>
#include<stdlib.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define range(a) a.begin(), a.end()
#define endl "\n"
#define Yes() cout << "Yes" << endl
#define No() cout << "No" << endl
#define MP make_pair
using Graph = vector<vector<int>>;
const unsigned long long mod = 1e9 + 7;
const long long INF = 1LL<<60;
const int dx[4]={1,0,-1,0};
const int dy[4]={0,1,0,-1};
void chmin(long long &a, long long b) { if (a > b) a = b; }
void chmax(long long &a, long long b) { if (a < b) a = b; }

int main(void){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int a, b, k = 1;
	cin >> a >> b;
	FOR(i, 0, 100)
	{
		if(k>=b){
			cout << i << endl;
			return 0;
		}
		else{
			k += (a - 1);
		}
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define REP(i,m,n) for(int i=(int)(m); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define RREP(i,m,n) for(int i=(int)(m); i>=(int)(n); i--)
#define rrep(i,n) RREP(i,n-1,0)
#define all(v) v.begin(), v.end()
const int inf = 1e9+7;
const ll longinf = 1LL<<60;
const ll mod = 1e9+7;
const ld eps = 1e-10;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int a, b; cin >> a >> b;
  cout << (a <= 8 && b <= 8 ? "Yay!" : ":(") << "\n";
  return 0;
}

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

int main()
{

    /*
    string s1;
    getline( cin, s1 );
    int n1 = s1.size();
    */
    int a, b;
    cin >> a >> b ;

    if( 2*a <= 16 && 2*b <= 16 ){
        cout << "Yay!" << endl;
    }
    else {
        cout << ":(" << endl;
    }

    return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <unordered_set>
#include <unordered_map>
#include <array>
#include <cassert>
#include <bitset>
using namespace std;
using LL = long long;

LL N;
vector<LL>A;
vector<LL>xmos;
const LL MOD = 1000000007;

LL solvedp(vector<pair<LL, int>>& vec, LL lasval) {
	LL zero = 1, notz = 0;
	LL zpar = 1, npar = 0;
	for (auto p : vec) {
		if (p.first == 0) {
			zpar = (notz * p.second) % MOD;
			zero += zpar;
			zero %= MOD;
		}
		else {
			npar = (zero * p.second) % MOD;
			notz += npar;
			notz %= MOD;
		}
	}
	if (lasval == 0) {
		return zpar;
	}
	return npar;
}

int main() {
	cin >> N;
	LL xsum = 0;
	xmos.push_back(0);
	for (int i = 0; i < N; ++i) {
		LL tmp;
		cin >> tmp;
		A.push_back(tmp);
		xsum ^= tmp;
		xmos.push_back(xsum);
	}
	/*vector<LL>dp(N + 1);
	dp[1] = 1;
	LL odd = 1, eve = 0;
	for (int i = 2; i <= N; ++i) {
		if (i % 2) {
			dp[i]++;
			dp[i] += eve;
			dp[i] %= MOD;
			odd += dp[i];
			odd %= MOD;
		}
		else {
			dp[i] += odd;
			eve += dp[i];
			eve %= MOD;
		}
	}*/
	if (xsum != 0) {
		vector<pair<LL, int>>nowarr;
		for (int i = 1; i <= N; ++i) {
			if (xmos[i] == 0) {
				nowarr.push_back({ 0,1 });
			}
			if (xmos[i] == xsum) {
				nowarr.push_back({ xsum,1 });
			}
		}
		LL ans = solvedp(nowarr, xsum);
		cout << ans << endl;
	}
	else {
		map<LL, int>zerolas;
		map<LL, vector<pair<LL, int>>>arrs;
		int zerocnt = 0;
		LL zerotwo = 1;
		for (int i = 1; i <= N; ++i) {
			if (xmos[i] == 0) {
				zerocnt++;
				if (i < N) {
					zerotwo *= 2;
					zerotwo %= MOD;
				}
			}
			else {
				if (zerocnt != zerolas[xmos[i]]) {
					arrs[xmos[i]].push_back({ 0,zerocnt - zerolas[xmos[i]] });
					zerolas[xmos[i]] = zerocnt;
				}
				arrs[xmos[i]].push_back({ xmos[i],1 });
			}
		}
		LL ans = zerotwo;
		for (auto& elm : arrs) {
			if (elm.second.back().second != 0) {
				elm.second.push_back({ 0,1 });
			}
			ans += solvedp(elm.second, 0);
			ans %= MOD;
		}
		cout << ans << endl;
	}
	return 0;
}

#include <iostream>
#include <string>
#include <cstring>
#include <sstream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <chrono>
#include <numeric>
#include <set>
#define _USE_MATH_DEFINES
#include <cmath>
#include <math.h>
#include <iomanip>
#include <list>
#include <limits>

using namespace std;

class cPartition
{
	int mn;
	int mpi;
	vector<int> m_va;

public:
	cPartition(int n, vector<int> &va)
		: mn(n)
	{
		m_va = va;
	}

	void partition();
	void output();
};

void cPartition::partition()
{
	int p = 0;
	int r = m_va.size() - 1;
	int x = m_va[r];
	int i = p - 1;
	for (int j = p; j <= r - 1; ++j)
	{
		if (m_va[j] <= x)
		{
			++i;
			swap(m_va[i], m_va[j]);
		}
	}
	swap(m_va[i + 1], m_va[r]);
	mpi = i + 1;
}

void cPartition::output()
{
	for (int i = 0; i < mn - 1; ++i)
	{
		if (i == mpi)
		{
			cout << "[" << m_va[i] << "]" << " ";
		}
		else
		{
			cout << m_va[i] << " ";
		}
	}
	cout << m_va[mn - 1] << endl;
}

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

	int n;
	cin >> n;
	cin.ignore();

	int a;
	vector<int> va;
	for (int i = 0; i < n; ++i)
	{
		cin >> a;
		va.push_back(a);
	}

	cPartition cp(n, va);
	cp.partition();
	cp.output();

	return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e6+1;
double s[N];
int n,m;ll k;
int a[N],p[N],u[N],tmp[N];
int read(){
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();}
    while(isdigit(ch)){x=x*10+ch-48;ch=getchar();}
    return x*f;
}
int main(){
	n=read();
	for(int i=1;i<=n;i++) a[i]=read();
	m=read();scanf("%lld",&k);
	for(int i=1;i<=n;i++) p[i]=i,u[i]=i;
	for(int i=1;i<=m;i++){
		int x=read();
		swap(p[x],p[x+1]);
	}
	while(k){
		if(k&1){
			for(int i=1;i<=n;i++) tmp[i]=u[p[i]];
			for(int i=1;i<=n;i++) u[i]=tmp[i];
		}
		for(int i=1;i<=n;i++) tmp[i]=p[p[i]];
		for(int i=1;i<=n;i++) p[i]=tmp[i];
		k>>=1;
	}
	for(int i=1;i<=n;i++) s[i]=a[u[i]]-a[u[i]-1]+s[i-1];
	for(int i=1;i<=n;i++) printf("%.1lf\n",s[i]);
	return 0;
}

#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 push_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);
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 vp=vector<P>;
using vvp=vector<vp>;
using vb=vector<bool>;
using vvb=vector<vb>;
const ll inf=1001001001001001;
const int INF=1001001001;
const int mod=1000000007;
const double eps=1e-10;
template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<class T> void out(T a){cout<<a<<'\n';}
template<class T,class U> void outs(T a,U b){if((ll)(a)>=inf-100||(ll)(a)<=-inf+100)out(b);else out(a);}
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");}
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;}
int main(){
    ll n;cin>>n;
    string s;cin>>s;
    vector<pair<string,string>> a,b;
    rep(bit,1<<n){
        string t,u;
        rep(i,n){
            if(bit>>i&1)t+=s[i];
            else u+=s[i];
        }
        a.eb(t,u);
    }
    rep(bit,1<<n){
        string t,u;
        rep(i,n){
            if(bit>>i&1)t+=s[n*2-1-i];
            else u+=s[n*2-1-i];
        }
        b.eb(t,u);
    }
    sort(all(a));
    sort(all(b));
    //outv(a);outv(b);
    ll ans=0;
    for(auto x:a){
        ans+=ub(b,x)-lb(b,x);
    }
    out(ans);
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef tuple<int, int> duo;
//{{{ templates
#define TMPINL_(...) template<__VA_ARGS__>inline
#define TT_ TMPINL_(typename T)
#define TTF_ TMPINL_(typename T,typename F)
#define TTTS_ TMPINL_(typename T,typename...Ts)
#define TITS_ TMPINL_(size_t I=0,typename...Ts)
TT_ T sq(T x){return x*x;}
TT_ T In(){T x;cin>>x;return x;}
TT_ void Out(T&x){cout<<x;}
TT_ void sort(T&v){sort(begin(v),end(v));}
TT_ void revs(T&v){reverse(begin(v),end(v));}
TT_ void uniq(T&v){sort(v);v.erase(unique(begin(v),end(v)),end(v));}
TT_ int ubnd(T&v,typename T::value_type&x){return upper_bound(begin(v),end(v),x)-begin(v);}
TT_ int lbnd(T&v,typename T::value_type&x){return lower_bound(begin(v),end(v),x)-begin(v);}
TTF_ void inpt(T&v,int n,F f){for(v.reserve(n);n--;v.emplace_back(f()));}
TTF_ void show(T&v,F f,string d=" ",string e="\n"){int i=0;for(auto&x:v)i++&&(cout<<d),f(x);cout<<e;}
TITS_ typename enable_if<I==tuple_size<tuple<Ts...>>::value-1,string>::type join(string s,tuple<Ts...>t){return to_string(get<I>(t));}
TITS_ typename enable_if<I<tuple_size<tuple<Ts...>>::value-1,string>::type join(string s,tuple<Ts...>t){return to_string(get<I>(t))+s+join<I+1>(s,t);}
TT_ string join(string s,T t){return to_string(t);}
TTTS_ string join(string s,T t,Ts...ts){return join(s,t)+s+join(s,ts...);}

inline void fast_io(){ios::sync_with_stdio(0);cin.tie(0);}
inline int in(){int x;scanf("%d",&x);return x;}
inline ll pow_mod(ll a,ll k,ll m){ll r=1;for(;k>0;a=a*a%m,k>>=1)if(k&1)r=r*a%m;return r;}
inline ll mod_inv(ll a,ll p){return pow_mod(a,p-2,p);}
inline int puts(const string&s){ return puts(s.c_str()); }
//}}} priority_queue queue deque front stringstream max_element min_element insert count make_tuple
const int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};
const int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};
const int Mod = 1000000000 + 0;

int main()
{
  ll way[128][128];
  for (int i = 0; i < 128; i++){
    for (int j = 0; j < 128; j++){
      way[i][j] = 1ll << 60;
    }
  }
  for (int i = 0; i < 128; i++) way[i][i] = 0;

  int V, E;
  V = in(), E = in();
  for (int i = 0; i < E; i++){
    int s, t, d;
    s = in(), t = in(), d = in();
    way[s][t] = d;
  }
  for (int k = 0; k < V; k++){
    for (int i = 0; i < V; i++){
      for (int j = 0; j < V; j++){
        way[i][j] = min(way[i][j], way[i][k] + way[k][j]);
      }
    }
  }
  for (int i = 0; i < V; i++){
    if (way[i][i] < 0){
      puts("NEGATIVE CYCLE");
      return 0;
    }
  }
  for (int i = 0; i < V; i++){
    for (int j = 0; j < V; j++){
      if (way[i][j] >= (1ll << 59)) cout << "INF";
      else cout << way[i][j];
      if (j == V - 1) cout << "\n";
      else cout << " ";
    }
  }
  return 0;
}

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

/*2進数配列+1*/
vector<int> twoadd(vector<int> v, int N){
    v[N-1]+=1;
    int ind = N-1;
    int j=N-1;
    for(j=N-1;j>=1;j--){
        if(v[j]>1){
            v[j-1]+=1;
            v[j]=0;
        }
    }
    return v;
}
/*フィボナッチ*/
long long fibonatti(long long d){
    long long count = 0;
    long long f1 = 1;
    long long f2 = 1;/*ここを変える*/
    long long temp;
    if(d == 1){
        count = f1;
    }else if(d == 2){
        count = f2;
    }else if(d==0){
        count = 1;
    }else{
        for(int i=0;i<d-2;i++){
            temp = f1+f2;
            f1 = f2;
            f2 = temp;
        }
        count = temp;
    }
    return count;
}

/*ここから*/
int main() {
    int N,L;
    cin >> N >> L;
    int R = N+L-1;
    long ans=0;
    if(L>0){
        for(int i=0;i<N;i++){
            ans += L+i;
        }
        cout << ans - L << endl;
    }
    if(L<=0&&R>=0){
        for(int i=0;i<N;i++){
            ans += L+i;
        }
        cout << ans << endl;
    }
    if(R<0){
        for(int i=0;i<N;i++){
            ans += L+i;
        }
        cout << ans-R << endl;
    }
}


#include<cstdio>
#define int long long
int D;
int calc(int A,int t,int first){
	if(!t)return A==0;
	int v=(A%10+10)%10;
	int res=0;
	res+=(10-v-first)*calc((A-t*v)/10,t/100,0);
	res+=(v-first)*calc((A+(10-v)*t)/10,t/100,0);
	return res;
}
signed main() {
    scanf("%lld",&D);
    if(D%9)return puts("0"),0;
	D/=9;
	int ans=0,p10=10;
	for(int i=2;i<=18;i++){
		int tot=(i%2)*9+1;
		ans+=tot*calc(D,p10/9,1);
		p10*=10;
	}
	printf("%lld\n",ans);
}

#include <iostream>

using namespace std;

int func(int y,int z,int e){
	return y+z+e-y*y-z*z*z;
}

int main(){

	int e;

	while(cin>>e){
		if(e==0)break;

		int min=10000000;

		for(int y=0;y*y<=e;y++){
			for(int z=0;(z*z*z+y*y)<=e;z++){
				int ans = func(y,z,e);

			//	cout<<"y="<<y<<"z="<<z<<endl;
				if(ans<=0)break;

				if(min>=ans){
					min=ans;
				}

			}
		}

		cout<<min<<endl;

	}

	return 0;
}


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

int main(){
	long long int x;
	cin>>x;
	long long int sum;
	sum = (x/500)*1000;
	x -= (x/500)*500;
	sum += (x/5)*5;
	cout<<sum<<endl;
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

#define endl '\n'
#define Opy ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)

typedef long long ll;

int main()
{
    //Opy;

/*-------------------------------------------------
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
-------------------------------------------------*/
    ll a, h = 0;

    cin>> a;

    h += (a - (a % 500)) * 2;

    a %= 500;

    h += a - (a % 5);

    cout<< h<< endl;

    return 0;
}


//
//  main.cpp
//

#include <algorithm>
#include <array>
#include <assert.h>
#include <functional>
#include <iostream>
#include <iomanip>
#include <limits>
#include <map>
#include <math.h>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

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

template<typename T>
vector<T> make_vec_nd(T init, size_t size) {
	return vector<T>(size, init);
}
template<typename T, typename... Args>
auto make_vec_nd(T init, size_t size, Args... rest) {
	auto inner = make_vec_nd(init, rest...);
	return vector<decltype(inner)>(size, inner);
}

#define rep(i,a,b) for(ll i=(a);i<(b);i++)
#define rrep(i,a,b) for(ll i=(a)-1;i>=(b);i--)

vector<vector<ll>> graph;
vector<bool> visited;

void dfs(ll now) {
	visited[now] = true;
	for (ll to : graph[now]) {
		if (!visited[to]) {
			dfs(to);
		}
	}
}

int main() {
	ll N, M;
	cin >> N >> M;
	graph.resize(2*N);
	rep(i,0,M) {
		ll a, b;
		cin >> a >> b;
		a--;
		b--;
		graph[2*a].push_back(2*b+1);
		graph[2*b+1].push_back(2*a);
		graph[2*a+1].push_back(2*b);
		graph[2*b].push_back(2*a+1);
	}
	visited.resize(2*N);
	dfs(0);
	ll cnt = 0;
	rep(i,0,N) {
		if (visited[2*i]) {
			cnt++;
		}
	}
	ll ans = 0;
	rep(i,0,N) {
		bool have_true = false;
		bool have_false = false;
		for (ll to : graph[2*i]) {
			to /= 2;
			if (visited[2*to]) {
				have_true = true;
			} else {
				have_false = true;
			}
		}
		ll ans_loc = 0;
		if (have_true) {
			ans_loc += cnt;
			if (visited[2*i]) {
				ans_loc--;
			}
		}
		if (have_false) {
			ans_loc += N - cnt;
			if (!visited[2*i]) {
				ans_loc--;
			}
		}
		// cout << i+1 << ":" << ans_loc << endl;
		ans += ans_loc;
	}
	cout << ans / 2 - M << endl;
}

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<queue>
#include<stack>
#include<cassert>
#include<numeric>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
const int INF = 1001001001;
const long long LINF = 1001002003004005006ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
typedef long long ll;
typedef pair<int,int> P;

using Graph = vector<vector<int>>;

// 二部グラフ判定
vector<int> color;
bool dfs(const Graph &G, int v, int cur = 0) {
    color[v] = cur;
    for (auto next_v : G[v]) {
        // 隣接頂点がすでに色確定していた場合
        if (color[next_v] != -1) {
            if (color[next_v] == cur) return false; // 同じ色が隣接したらダメ
            continue;
        }

        // 隣接頂点の色を変えて、再帰的に探索 (一回でも false が返ってきたら false)
        if (!dfs(G, next_v , 1 - cur)) return false;
    }
    return true;
}

int main(){
    int n,m;
    cin >> n >> m;
    Graph G(n);
    rep(i,m){
        int a,b;
        cin >> a >> b;
        a--,b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    color.assign(n, -1);
    bool is_bipartite = dfs(G,0);
    if(is_bipartite){
        int b=0;int w=0;
        rep(i,n){
            if(color[i]==0)b++;
            else w++;
        }
        cout << (ll)b*w - m << endl;
    }else{
        cout << (ll)n*(n-1)/2 - m << endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fastread() (ios_base:: sync_with_stdio(false),cin.tie(NULL))
#define fi first
#define se second
#define pb push_back
#define eb emplace_back

int main()
{
    fastread();

    ll n,m;

    cin>>n>>m;
    ll a,b;
    cin>>a>>b;

    for(ll i=0;i<m-1;i++)
    {
        ll a1,b1;

        cin>>a1>>b1;

        a=max(a,a1);
        b=min(b,b1);
    }

    cout<<max(b-a+1,0ll)<<endl;

}

/*
  Motto - try hard to become an expert
*/
#include <bits/stdc++.h>
#include <map>
#include <algorithm>
#include <vector>
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <chrono>
#include <thread>
#include <iomanip>

//define
#define rep(i,n)    for(int i=0;i<n;i++)
#define repu(i,k,n) for(int i=k;i<=n;i++)
#define repd(i,k,n) for(int i=k;i>=n;i--)
#define se second
#define fi first
#define pb push_back
#define mp make_pair
#define INF (int)INT_MAX
#define all(v)  v.begin(), v.end()
#define endl "\n"

//typedef
typedef long long int ll;
typedef long double ld;
typedef std::pair<int, int> pii;
typedef std::vector<int> vi;
typedef std::vector< pii > vii;
typedef std::vector< ll > vll;
typedef std::vector< std::pair < pii, int > > vpp;

const int N = 3*1e5 +5 ;
const long long MOD = 1e9 + 7;
const long long inf = 1e18;

using namespace std;

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

ll modFact(ll n, ll p)
{
    if (n >= p)
        return 0;

    ll result = 1;
    for (ll i = 1; i <= n; i++)
        result = (result * i) % p;

    return result;
}

bool isPrime(int x)
{
    if(x%2==0)
        return 0;
    for(int i=3;i<=sqrt(x);i+=2)
    {
        if(x%i==0)
            return 0;
    }
    return 1;
}

ll binomialCoeff(ll n, ll k)
{
    ll C[n + 1][k + 1];
    ll i, j;

    for (i = 0; i <= n; i++)
    {
        for (j = 0; j <= min(i, k); j++)
        {

            if (j == 0 || j == i)
                C[i][j] = 1;

            else
                C[i][j] = (C[i - 1][j - 1] +
                                        C[i - 1][j])%MOD;
        }
    }

    return C[n][k];
}
bool compare(const pair<int,int> &a , const pair<int,int>&b){
    //set<pair<int,int> , decltype(&compare)>s(&compare) ;
    if(a.fi!=b.fi)return a.se<b.se ;
    return a.fi<b.fi;
}

// struct compare{
//     // for priority_queue
//     bool operator()(const int &a , const int&b){
//         return a<b ;
//     }
// };

void solve()
{
    int n , m ;
    cin>>n>>m;
    vector<int>L(m) , R(m) ;
    rep(i , m){
        cin>>L[i]>>R[i] ;
    }
    int mal = INT_MIN ;
    int mir = INT_MAX ;
    rep(i , m){
        mal = max(L[i] , mal) ;
        mir = min(mir , R[i]) ;
    }
    int ans= 0 ;
    repu(i , 1 , n){
        if(mal<=i && mir>=i){
            ans++;
        }
    }
    cout<<ans<<endl;
}

int main() {
    #ifndef ONLINE_JUDGE
        // for getting input from input.txt
        freopen("input.txt", "r", stdin);
        // for writing output to output.txt
        freopen("output.txt", "w", stdout);
    #endif

    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    int t = 1;
    //cin>>t;
    while(t--)
    {
        solve();
    }
    cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
}


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

int main() {
  int h, w;
  cin >> h >> w;

  string s;
  vector<vector<char>> v(h+2, vector<char>(w+2));
  for(int i=0; i<h+2; i++) {
    if(i==0 || i==h+1) for(int j=0; j<w+2; j++) v[i][j] = '.';
    else {
      cin >> s;
      for(int j=0; j<w+2; j++) {
        if(j==0 || j==w+1) v[i][j] = '.';
        else v[i][j] = s[j-1];
      }
    }
  }

  bool check = true;
  for(int i=0; i<h+2; i++) {
    for(int j=0; j<w+2; j++) {
      if(v[i][j]=='#') {
        if(v[i-1][j]!='#' && v[i+1][j]!='#' && v[i][j-1]!='#' && v[i][j+1]!='#') check = false;
      }
    }
  }

  if(check) cout << "Yes" << endl;
  else cout << "No" << endl;
  return 0;
}

#include <iostream>
#include <vector>
//#define rep(i,n) for(int i = 0; i , (n); ++i)
using ll = long long;
using namespace std;

int sum(int n) {
    if (n == 0) {
        return 0;
    }

    int s = sum(n - 1);
    return s + n;
}
int main() {
    int N, W;
        cin >> N >> W;
        vector<vector<char>>A(N, vector<char>(W));

        for (int i = 0;i < N;i++) {
            for (int j = 0;j < W;j++) {
                cin >> A.at(i).at(j);
            }
        }
        bool hantei = true;
        for (int i = 0;i < N;i++) {
            for (int j = 0;j < W;j++){
                    if (j>0&&j<(W-1)&&i>0&&i<(N-1)&&A.at(i).at(j) == '#'&&A.at(i + 1).at(j) == '.' && A.at(i - 1).at(j) == '.' &&
                        A.at(i).at(j + 1) == '.' && A.at(i).at(j - 1) == '.') {
                         hantei=false;
                             break;
                    }
            }
            if (hantei == false) {
                break;
            }
        }
        if (hantei) {
            cout << "Yes";
        }
        else {
            cout << "No";
        }
}


#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
ll const INF = 1LL << 60;

int main() {
  ll A, B;
  cin >> A >> B;
  cout << max({A - B, A + B, A * B}) << endl;
  return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a,b;
    while(cin >> a >> b )
    {
        cout << max(a+b,max(a-b,a*b)) << endl;
    }
}


#include <iostream>
#include <numeric>
#include <cmath>
#include <limits>
#include <stdio.h>
#include <iomanip>
#include <cstdlib>
#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
using namespace std;
using ll = long long;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
ll Max(ll(a), ll(b), ll(c)) {
	return max(max(a, b), c);
}
ll Min(ll(a), ll(b), ll(c)) {
	return min(min(a, b), c);
}
ll gcd(ll(a), ll(b)) {
	ll c = a;
	while (a % b != 0) {
		c = a % b;
		a = b;
		b = c;
	}
	return b;
}

int main() {
	ll N,K,G,M;
	cin >> N >> K;
	ll A;
	cin >> A;
	G = A;
	M = A;
	string an = "POSSIBLE";
	rep(i, N-1) {
		cin >> A;
		G = gcd(G, A);
		M = max(M, A);
	}
	if (K % G != 0 || K > M) {
		an = "IM" + an;
	}
	cout <<an << endl;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const double pi=3.141592653589793;
typedef unsigned long long ull;
typedef long double ldouble;
const ll INF=1e18;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
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; }

// 最大公約数(ユークリッドの互除法)
// O(log max(a, b))
ll gcd(ll a, ll b){
    if(b == 0) return a;
    return gcd(b, a%b);
}

int main(){
    int n, k, g;
    bool ans = false;
    cin >> n >> k;
    vector<int> a(n), b(n-1);
    rep(i, n){
        cin >> a.at(i);
    }
    sort(a.begin(), a.end());
    g = a.at(0);
    rep2(i, 1, n){
        if(a.at(i) != a.at(i-1)) g = gcd(g, a.at(i) - a.at(i-1));
    }
    rep(i, n){
        if(a.at(i) == k) {
            ans = true;
            break;
        }
        if((a.at(i) - k) > 0 && (a.at(i) - k) % g == 0) {
            ans = true;
            break;
        }
    }
    if(ans) cout << "POSSIBLE" << endl;
    else cout << "IMPOSSIBLE" << endl;

}


#include<bits/stdc++.h>
using namespace std;
#define nl '\n'
int main()
{
    int r,g;
    cin>>r>>g;
    cout<<((g*2)-r);
    cout<<nl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll =long long;
#define all(v) v.begin(),v.end()

int main() {
ll A,B;
  cin>>A>>B;
  ll ans=0;
  for(ll i=A;i<=B;i++) {
string S=to_string(i);
    if(S[0]==S[4]&&S[1]==S[3]) {
ans++;
    }
  }

  cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 1e9+7;
const int INF = 1e9;
const ll INFll = 1e18;
#define put(n) cout<<(n)<<endl
#define Cout(n) cout<<(n)
#define rep(i,num,N) for(int(i)=(num);(i)<(N);++(i))
#define rrep(i,num,N) for(int (i)=(num);(i)>(N);--(i))
#define all(v) (v).begin() , (v).end()
#define rall(v) (v).rbegin() , (v).rend()
#define MP make_pair
#define pb(q) push_back(q)
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};

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

    int N,K;

    while(cin>>N>>K,N!=0&&K!=0){
        set<string> num;
        vector<string> card(N);
        rep(i,0,N) cin>>card[i];
        sort(all(card));

        do{
            string s = "";
            rep(i,0,K) s += card[i];
            num.insert(s);
        }while(next_permutation(all(card)));
        put(num.size());
    }
}

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
#include <cstring>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;

int main()
{
  int n, k;
  while(cin >> n, n) {
    cin >> k;
    set<string> SET;
    vector<string> vs(n); FOR(i,0,n) cin >> vs[i];
    FOR(i,0,1<<n) {
      vector<int> v;
      int cnt = 0;
      FOR(j,0,n) {
        if((i>>j)&1) {
          cnt++;
          v.push_back(j);
        }
      }
      if(cnt != k) continue;
      do {
        string s = "";
        FOR(l,0,k) s = s + vs[v[l]];
        SET.insert(s);
      } while(next_permutation(v.begin(), v.end()));
    }
    cout << SET.size() << endl;
  }

  return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define ar array
#define nl '\n'
#define AC ios_base::sync_with_stdio(0); cin.tie(NULL);
using namespace std;
const int MOD = 998244353;
const int N = 2*1e5;

int main(){
    AC
    int n, m;
    cin>>n>>m;
    if(n%2!=0){
        for(int i=1; i<=m; i++){
            cout<<i<<" "<<n-i<<endl;
        }
    }
    else{
        int odd = (m+1)/2;
        for (int i = 1; i<=odd; i++)
            cout << i << ' ' << n-i+1 << '\n';
        for (int i = odd+1; i<=m; i++)
            cout << i << ' ' << n-i << '\n';
    }
}

/*input
8 3
*/

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

int main() {
  int n, m;
  cin >> n >> m;
  int l = 1, r = n;
  for (int i = 1; i <= m; ++i) {
    if (n%2 == 0 && i == n/2/2+1) l++;
    cout << l << " " << r << endl;
    l++; r--;
  }
  return 0;
}


///not today

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

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

#define SZ(x) (int)(x).size()
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define xmax(x, y) (x) = max((x), (y))
#define xmin(x, y) (x) = min((x), (y))
#define F first
#define S second
#define int long long
#define accept_sho ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#define lc (v << 1)
#define rc ((v << 1)| 1)
#define mid ((R + L) >> 1)

const int maxn = 3e2 + 2;
int dp[maxn][maxn][maxn];

int32_t main(){
      string s;
      int K, n;
      cin >>s >>K;
      n = SZ(s);
      for(int k = 0; k <= K; k ++){
      for(int j = 0; j < n; j ++){
      for(int i = 0; i + j < n; i ++){
                        dp[k][j][i] = 1;
                        if(j == 0)
                              continue;
                        if(s[i] == s[i + j]){
                              if(j > 1){
                                    dp[k][j][i] = dp[k][j - 2][i + 1] + 2;
                              }
                              else{
                                    dp[k][j][i] = 2;
                              }
                        }
                        else{
                              xmax(dp[k][j][i], dp[k][j - 1][i]);
                              xmax(dp[k][j][i], dp[k][j - 1][i + 1]);
                              if(k){
                                    if(j > 1){
                                          xmax(dp[k][j][i], dp[k - 1][j - 2][i + 1] + 2);
                                    }
                                    else{
                                          dp[k][j][i] = 2;
                                    }
                              }
                        }
      }
      }
      }
      cout <<dp[K][n - 1][0] <<endl;
}

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

int main() {
  int N,K;
  cin >> N >> K;
  int x;
  x = 1;

  for(int i = 0; i < N; i++){
    if(2 * x > x + K)
    x = x + K;
    if(2 * x <= x + K)
    x = 2 * x;
  }
  cout << x << endl;
}


#include <fstream>
#include <iostream>
#include <cmath>
#include <vector>
#include <queue>
#include <utility>
#include <algorithm>
using namespace std;

int main() {
  int year[12] = {31,29,31,30,31,30,31,31,30,31,30,31};
  string day[7] = {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
  int first = 3;

  int m, d;
  while (true) {
    cin >> m >> d;
    if (!(m|d)) break;
    int n = first;
    for (int i = 0; i < m-1; i++) {
      n += year[i];
    }
    n += d-1;

    n %= 7;
    cout << day[n] << endl;
  }
}

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 10005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
    res = 0;T f = 1;char c = getchar();
    while(c < '0' || c > '9') {
	if(c == '-') f = -1;
	c = getchar();
    }
    while(c >= '0' && c <= '9') {
	res = res * 10 +c - '0';
	c = getchar();
    }
    res *= f;
}
template<class T>
void out(T x) {
    if(x < 0) {x = -x;putchar('-');}
    if(x >= 10) {
	out(x / 10);
    }
    putchar('0' + x % 10);
}
const int MOD = 1000000007;
int N,M,s[2],l[2];
int inc(int a,int b) {
    return a + b >= MOD ? a + b - MOD : a + b;
}
int mul(int a,int b) {
    return 1LL * a * b % MOD;
}
void update(int &x,int y) {
    x = inc(x,y);
}
void Solve() {
    read(N);read(M);
    int x;
    for(int i = 1 ; i <= N ; ++i) {
	read(x);
	x = (x + MOD) % MOD;
	update(l[0],inc(mul(i - 1,x),MOD - s[0]));
	update(s[0],x);
    }
    int y;
    for(int i = 1 ; i <= M ; ++i) {
	read(y);
	y = (y + MOD) % MOD;
	update(l[1],inc(mul(i - 1,y),MOD - s[1]));
	update(s[1],y);
    }
    out(mul(l[0],l[1]));
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
}


#include <iostream>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <string>

using namespace std;

int main() {
    int a,b,sa;
    cin>>a>>b;
    sa = max(a+b,a-b);
    sa = max(a*b,sa);
    cout<<sa;
    return 0;
}

//Radhe Radhe
#include<bits/stdc++.h>
#define ll long long
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define mod 1000000007
using namespace std;

int main()
{
    fast_io
    ll i,a,b,c,d,j,k;
    cin>>a>>b>>c;

    cout<<max(a+b,max(a*b,a-b));

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int N, A[1000000], cnt;

void Out(const vector<int> G) {
	cout << G.size() << endl;
	cout << G.back();
	for (int i = G.size() - 2; i >= 0; i--) {
		cout << ' ' << G[i];
	}
	cout << endl;
	cout << cnt << endl;
	for (int i = 0; i < N; i++)
		cout << A[i] << endl;
}

void InsertionSort(int g) {
	for (int i = g; i < N; i++) {
		int v = A[i], j = i - g;
		while (j >= 0 && A[j] > v) {
			A[j + g] = A[j];
			j -= g;
			cnt++;
		}
		A[j + g] = v;
	}
}
void ShellSort(const vector<int> G) {
	cnt = 0;
	for (int i = G.size() - 1; i >= 0; i--) {
		InsertionSort(G[i]);
	}
}

int main()
{
	vector<int> G;
	cin >> N;
	int calc = 1;
	while (calc <= N) {
		G.push_back(calc);
		calc = calc * 3 + 1;
	}
	for (int i = 0; i < N; i++)
		cin >> A[i];
	ShellSort(G);
	Out(G);
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define IL inline
#define rep(i,j,k) for(int i=j;i<=k;++i)
#define repd(i,j,k) for(int i=j;i>=k;--i)
#define pb push_back
#define db double
#define mp make_pair
#define mp3(a,b,c) mp(mp(a,b),c)
#define pii pair<int,int>
#define piii pair<pii,int>
#define fr first
#define se second
#define ll long long
#define ull unsigned long long
#define pbc(x) __builtin_popcount(x)
#define clr(x) memset(x,0,sizeof x)
#define SIZE(x) (int)(x.size())
const int mod=1e9+7;
IL int pls(int x,int y){x+=y;return x>=mod?x-mod:x;}
IL int dec(int x,int y){x-=y;return x<0?x+mod:x;}
IL int mul(int x,int y){return 1ll*x*y%mod;}
IL int mul(int x,int y,int z){return mul(mul(x,y),z);}
IL int mul(int x,int y,int z,int p){return mul(mul(x,y),mul(z,p));}
IL void add(int &x,int y){x+=y;x=(x>=mod)?x-mod:x;}
IL int fpw(int x,int y,int r=1){for(;y;y>>=1,x=mul(x,x))if(y&1)r=mul(r,x);return r;}
IL int inv(int x){return fpw(x,mod-2);}
IL int gi(){int x;int _w=scanf("%d",&x);return x;}
IL void gi(int &x){int _w=scanf("%d",&x);}
IL void write(int x){printf("%d\n",x);}
void chkmax(int &x,int y){x=(x>y)?x:y;}
void chkmin(int &x,int y){x=(x<y)?x:y;}
const int INF=0x3f3f3f3f;
template<typename T>IL void debug(T x){cerr<<x;return;}
/* --------------------------------------------------------------------------------------------------------- */
const int maxn=1e5+10;
int n,m,a[maxn];
namespace sub1{
	IL bool check(){
		rep(i,1,m)if(a[i]&1)return false;
		return true;
	}
	IL void work(){
		rep(i,1,m)printf("%d ",a[i]);
		putchar('\n');
		vector<int>b;
		b.pb(1);
		rep(i,1,m-1)b.pb(a[i]);
		b.pb(a[m]-1);
		printf("%d\n",b.size());
		rep(i,0,b.size()-1)printf("%d ",b[i]);
	}
}
namespace sub2{
	IL bool check(){
		int cnt=0;
		rep(i,1,m)if(a[i]&1)cnt++;
		return cnt==1;
	}
	IL void work(){
		int pos;
		rep(i,1,m)if(a[i]&1)pos=i;
		swap(a[m],a[pos]);
		rep(i,1,m)printf("%d ",a[i]);
		putchar('\n');
		vector<int>b;
		b.pb(1);
		rep(i,1,m-1)b.pb(a[i]);
		if(a[m]!=1)b.pb(a[m]-1);
		printf("%d\n",b.size());
		rep(i,0,b.size()-1)printf("%d ",b[i]);
	}
}
namespace sub3{
	IL bool check(){
		int cnt=0;
		rep(i,1,m)if(a[i]&1)cnt++;
		return cnt==2;
	}
	IL void work(){
		int pos;
		rep(i,1,m)if(a[i]&1)pos=i;
		swap(a[1],a[pos]);
		rep(i,2,m)if(a[i]&1)pos=i;
		swap(a[m],a[pos]);
		rep(i,1,m)printf("%d ",a[i]);
		putchar('\n');
		vector<int>b;
		b.pb(a[1]+1);
		rep(i,2,m-1)b.pb(a[i]);
		if(a[m]!=1)b.pb(a[m]-1);
		printf("%d\n",b.size());
		rep(i,0,b.size()-1)printf("%d ",b[i]);
	}
}
int main(){
#ifdef LOCAL
	freopen("/home/noilinux/Desktop/input.txt","r",stdin);
#endif
	n=gi(),m=gi();
	rep(i,1,n)a[i]=gi();
	if(sub1::check())sub1::work();else if(sub2::check())sub2::work();
	else if(sub3::check())sub3::work();else printf("Impossible\n");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define VIEW(x) do {cerr << #x << ": "; for(auto i : x) cerr << i << " "; cerr << endl;} while(0)
#define ALL(x) (x).begin(),(x).end()
template<class T>bool umax(T &a, const T &b) {if(a<b){a=b;return 1;}return 0;}
template<class T>bool umin(T &a, const T &b) {if(b<a){a=b;return 1;}return 0;}

template<typename A,size_t N,typename T> void FILL(A (&array)[N],const T &val){fill((T*)array,(T*)(array+N),val);}
template<typename T> void FILL(vector<T> &v, const T &x) {fill(v.begin(), v.end(), x);}
template<typename T> void FILL(vector<vector<T>> &v, const T &x) {for(auto &i:v)fill(i.begin(), i.end(), x);}

int main() {
  int n; cin >> n;
  vector<int> a(n);
  rep(i,n) cin >> a[i];

  if(a[0]!=0) {
    cout << -1 << endl;
    return 0;
  }

  ll ans = 0;
  rep(i,1,n) {
    cerr << i-1 << " " << a[i-1] << " " << ans << endl;
    if(a[i]==0) {
      continue;
    }
    if(a[i] > a[i-1]+1) {
      cout << -1 << endl;
      return 0;
    }
    if(a[i] == a[i-1]+1) {
      ans++;
    }
    else /* if(a[i] <= a[i-1]) */ {
      ans += a[i];
    }
  }
  cout << ans << endl;
}

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#define ALL(x) (x).begin(),(x).end()
using namespace std;
const int MAXN = 200005;

int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    vector<int>A(n);
    for(int i=0;i<n;i++){
        cin >> A[i];
    }
    int flag= 1;
    while(k--&&flag){
        flag = 0 ;
        vector<int>B(n);
        for(int i=0;i<n;i++){
            B[max(0,i-A[i])]++;
            if(i+A[i]+1<n)
                B[i+A[i]+1]--;
        }
        for(int i=1;i<n;i++){
            B[i]+=B[i-1];
            //cout << B[i] <<" \n"[i=n-1];
            if(B[i]!=n)
                flag=1;
        }
        if(B[0]!=n)
            flag=1;
        A=B;
    }
    for(int i=0;i<n;i++)
    cout <<A[i]<<" ";
    return 0;
}


#include <iostream>

const int N = 7;

typedef struct {
  int panels[N];
  int upper;
  int east;
  int south;
} Dice;

void rotate_dice(int *x, int *y, int d) {
  *x = *y;
  *y = d;
}

int main(void) {
  Dice dice;

  for (int i = 1; i < N; i++) {
    std::cin >> dice.panels[i];
  }

  dice.upper = 1;
  dice.east = 3;
  dice.south = 2;

  std::string input;
  std::cin >> input;

  for (int i = 0; i < input.size(); i++) {
    char m = input[i];

    if (m == 'E') {
      rotate_dice(&dice.east, &dice.upper, N - dice.east);
    } else if (m == 'W') {
      rotate_dice(&dice.upper, &dice.east, N - dice.upper);
    } else if (m == 'S') {
      rotate_dice(&dice.south, &dice.upper, N - dice.south);
    } else {
      rotate_dice(&dice.upper, &dice.south, N - dice.upper);
    }
  }

  std::cout << dice.panels[dice.upper] << std::endl;

  return 0;
}

/*___________Bismillahir Rahmanir Rahim__________*/
//Think positive, be positive and never use negative words
#include<bits/stdc++.h>
#define ll               long long int
#define uni(a)           a.erase(unique(a.begin(),a.end()),a.end())
#define vsum(a)          accumulate(a.begin(),a.end(),0)
#define vmax(a)          *max_element(a.begin(),a.end())
#define vmin(a)          *min_element(a.begin(),a.end())
#define gcd(a,b)         __gcd(a,b)
#define lcm(a,b)         (a/gcd(a,b))*b
#define yes              cout<<"YES"<<endl
#define no               cout<<"NO"<<endl
using namespace std;
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    string s;
    cin>>s;
    ll x=(s[0]=='R');
    ll y=(s[1]=='R');
    ll z=(s[2]=='R');
    if(x && y && z) cout<<3<<endl;
    else if((x && y) || (y && z)) cout<<2<<endl;
    else if(x || y || z) cout<<1<<endl;
    else cout<<0<<endl;
    return 0;
}
/*_________________Alhamdulillah_________________*/


#include <bits/stdc++.h>

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

using namespace std;

int main() {
  int A, B, M;
  std::cin >> A >> B >> M;
  std::vector<int> a(A), b(B), x(M), y(M), c(M);
  rep(i, A) std::cin >> a[i];
  rep(i, B) std::cin >> b[i];
  rep(i, M) {
    std::cin >> x[i] >> y[i] >> c[i];
  }

  int minv = 1000000;
  rep(i, M) {
    minv = min(minv, a[x[i]-1] + b[y[i]-1] - c[i]);
  }

  sort(a.begin(), a.end());
  sort(b.begin(), b.end());

  minv = min(minv, a[0] + b[0]);

  std::cout << minv << std::endl;

  return 0;
}

#include <bits/stdc++.h>

#define debug(...) fprintf(stderr, __VA_ARGS__)

#ifndef AT_HOME
#define getchar() IO::myGetchar()
#define putchar(x) IO::myPutchar(x)
#endif

namespace IO {
	static const int IN_BUF = 1 << 23, OUT_BUF = 1 << 23;

	inline char myGetchar() {
		static char buf[IN_BUF], *ps = buf, *pt = buf;
		if (ps == pt) {
			ps = buf, pt = buf + fread(buf, 1, IN_BUF, stdin);
		}
		return ps == pt ? EOF : *ps++;
	}

	template<typename T>
	inline bool read(T &x) {
		bool op = 0;
		char ch = getchar();
		x = 0;
		for (; !isdigit(ch) && ch != EOF; ch = getchar()) {
			op ^= (ch == '-');
		}
		if (ch == EOF) {
			return false;
		}
		for (; isdigit(ch); ch = getchar()) {
			x = x * 10 + (ch ^ '0');
		}
		if (op) {
			x = -x;
		}
		return true;
	}

	inline int readStr(char *s) {
		int n = 0;
		char ch = getchar();
		for (; isspace(ch) && ch != EOF; ch = getchar())
			;
		for (; !isspace(ch) && ch != EOF; ch = getchar()) {
			s[n++] = ch;
		}
		s[n] = '\0';
		return n;
	}

	inline void myPutchar(char x) {
		static char pbuf[OUT_BUF], *pp = pbuf;
		struct _flusher {
			~_flusher() {
				fwrite(pbuf, 1, pp - pbuf, stdout);
			}
		};
		static _flusher outputFlusher;
		if (pp == pbuf + OUT_BUF) {
			fwrite(pbuf, 1, OUT_BUF, stdout);
			pp = pbuf;
		}
		*pp++ = x;
	}

	template<typename T>
	inline void print_(T x) {
		if (x == 0) {
			putchar('0');
			return;
		}
		static int num[40];
		if (x < 0) {
			putchar('-');
			x = -x;
		}
		for (*num = 0; x; x /= 10) {
			num[++*num] = x % 10;
		}
		while (*num){
			putchar(num[*num] ^ '0');
			--*num;
		}
	}

	template<typename T>
	inline void print(T x, char ch = '\n') {
		print_(x);
		putchar(ch);
	}

	inline void printStr_(const char *s, int n = -1) {
		if (n == -1) {
			n = strlen(s);
		}
		for (int i = 0; i < n; ++i) {
			putchar(s[i]);
		}
	}

	inline void printStr(const char *s, int n = -1, char ch = '\n') {
		printStr_(s, n);
		putchar(ch);
	}
}
using namespace IO;

const int N = 100005;

int n, m, q, a[N], b[N];

void solve() {
	read(n), read(m), read(q);
	for (int i = 1; i <= n; ++i) {
		read(a[i]);
	}
	for (int i = 1; i <= m; ++i) {
		read(b[i]);
	}
	int ans = *std::min_element(a + 1, a + 1 + n) + *std::min_element(b + 1, b + 1 + m);
	for (int i = 1; i <= q; ++i) {
		int x, y, c;
		read(x), read(y), read(c);
		ans = std::min(ans, a[x] + b[y] - c);
	}
	print(ans);
}

int main() {
	int T = 1;
	// read(T);
	while (T--) {
		solve();
	}
}


#include "bits/stdc++.h"
#include "math.h"

using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vin;
typedef pair<ll,ll> P;
typedef vector<P> vp;
#define rep(i,a,b) for(ll i=(a);i<(b);++i)
#define SIZE(a) int((a).size())
#define out(a) cout<<(a)<<endl;
const int INF=INT_MAX;
const int MAX = 510000;
const ll MOD = 1000000007;

int main(){
  int n;cin>>n;
  vin x(0),y(0),xx(n),yy(n);
  set<int> s1,s2;
  vvb g(8,vb(8,false));
  rep(i,0,n){
    int x1,y1;cin>>x1>>y1;
    s1.insert(x1+y1);s2.insert(x1-y1);
    g[x1][y1]=true;
    xx[i]=x1;yy[i]=y1;
  }
  rep(i,0,8){
      if(find(xx.begin(),xx.end(),i)==xx.end()){x.push_back(i);}
      if(find(yy.begin(),yy.end(),i)==yy.end()){y.push_back(i);}
    }

  do{
      bool ok=true;
      set<int> ss1=s1,ss2=s2;
      rep(i,0,8-n){
        if(find(ss1.begin(),ss1.end(),x[i]+y[i])!=ss1.end()){ok=false;break;}
        else{ss1.insert(x[i]+y[i]);}
        if(find(ss2.begin(),ss2.end(),x[i]-y[i])!=ss2.end()){ok=false;break;}
        else{ss2.insert(x[i]-y[i]);}
      }
      if(ok){
        rep(i,0,8-n){
          g[x[i]][y[i]]=true;
        }
        rep(i,0,8){
          rep(j,0,8){
            if(g[i][j]){cout<<'Q';}
            else{cout<<'.';}
          }
          cout<<endl;
        }
        break;
      }
  }while(next_permutation(x.begin(),x.end()));

}

// Phttps://ei1333.github.io/luzhiled/
// http://beet-aizu.hatenablog.com/entry/2017/01/04/113233
// http://www.prefield.com/algorithm/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vd = vector<ld>;
using vs = vector<string>;
using vb = vector<bool>;
using vvl = vector<vector<ll>>;
using vvd = vector<vector<ld>>;
using vvs = vector<vector<string>>;
using vvb = vector<vector<bool>>;
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
template<class T> using vc = vector<T>;
template<class T> using vvc = vector<vector<T>>;
using Graph = vector<vector<ll>>;
const ll INF = 1LL << 60;
void init() {cin.tie(0);ios::sync_with_stdio(false);cout << fixed << setprecision(15);}
const ll tl= 1'000'000'007;
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REREP(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, a, n) for (ll i = a; i < n; i++)
#define REFOR(i, n, a) for (ll i = n; i >= a; i--)
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() ) //被り削除
#define debug cout << "line : " << __LINE__ << " debug" << endl;
#define ini(...) int __VA_ARGS__; in(__VA_ARGS__)
#define inl(...) long long __VA_ARGS__; in(__VA_ARGS__)
#define ind(...) long double __VA_ARGS__; in(__VA_ARGS__)
#define ins(...) string __VA_ARGS__; in(__VA_ARGS__)
#define inc(...) char __VA_ARGS__; in(__VA_ARGS__)
void in(){} template <typename T,class... U> void in(T &t,U &...u){ cin >> t; in(u...);}
void out(){cout << endl;} template <typename T,class... U> void out(const T &t,const U &...u){ cout << t; if(sizeof...(u)) cout << " "; out(u...);}
template<typename T>void die(T x){out(x); exit(0);}
#define in1(A) REP(i,A.size()) in(A[i]);
#define in2(A,B) REP(i,A.size()) in(A[i],B[i]);
#define in3(s,t,u) REP(i,sz(s)){in(s[i] , t[i] , u[i]);}
#define in4(s,t,u,v) REP(i,sz(s)){in(s[i] , t[i] , u[i] , v[i]);}
#define each(x,v) for(auto& x : v)
#define all(v) (v).begin(),(v).end()
#define sz(v) ((int)(v).size())
struct Point{ ld x,y; };
ld dist(Point a, Point b){return sqrt(abs(a.x-b.x)*abs(a.x-b.x)+abs(a.y-b.y)*abs(a.y-b.y));} // ２点間の距離
ll gcd(ll a, ll b) { return b != 0 ? gcd(b, a % b) : a; }
ll lcm(ll a,ll b){ return a / gcd(a,b) * b;}
ll fact(ll n){ if(n < 2) return 1; return (n * fact(n-1))%tl; } //階乗
inline ll updiv(ll a,ll b){ return (a + b - 1) / b; } //切り上げ
template<typename T,typename U>ll ceil(T a,U b){return (a + b - 1) / b;}
template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); }
template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); }
//-------------------------------------------------------------------------------------------------
ll n=8;
vvl bd(n,vl(n,0));
vvl memo(n); //-1,0,1
vector<pair<ll,ll>>A;

void queen_dfs(int cnt, int p,vvl bd, vvl old_memo){
    bd[cnt][p]=1;
    if(cnt==n-1){
        int f=1;
        each(x,A){
            if(!bd[x.first][x.second]) f=0;
        }
        if(f){
            REP(i,n){
                REP(j,n){
                    cout << ((bd[i][j])?"Q":".");
                }out();
            }
        }
        return;
    }
    //memo 更新
    vvl new_memo(n);
    REP(j,n){
        REP(t,old_memo[j].size()){
            ll x= j+old_memo[j][t];
            if(0<=x && x<n) new_memo[x].pb(old_memo[j][t]);
        }
    }
    // bd
    REP(i,n){
        if(!new_memo[i].size()){
            vvl n_memo= new_memo;
            n_memo[i].pb(-1);
            n_memo[i].pb(0);
            n_memo[i].pb(1);
            queen_dfs(cnt+1, i, bd, n_memo);
        }
    }
}
int main(){
    init();
    inl(k);
    A.resize(k);
    REP(i,k){
        inl(r,c);
        A[i]= {r,c};
    }

    REP(i,n){
        vvl memo(8);
        memo[i].pb(-1);
        memo[i].pb(0);
        memo[i].pb(1);
        queen_dfs(0,i,bd,memo);
    }
}


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

int main (){
  int n, m;
  int b, count, answer;
  int *list, *result;
  list = (int*)malloc(sizeof(int)*n);
  result = (int*)malloc(sizeof(int)*n);

  cin >> n >> m;
  for(int i = 0; i<n; i++){
    cin >>list[i];
    result[i] = 0;
  }

  for(int i= 0; i< m; i++){
    count = 0;
    cin >>b;
    while(1){
      if(list[count] < b+1) break;
      count++;
    }
    //cout <<count <<endl ;
    result[count] ++;
  }
  answer = 0;
  for(int i =0; i<n; i++){
    if(result[answer] < result[i])
      answer = i ;
  }

  cout << answer + 1 <<endl;
  return 0;
}

#include <iostream>
#include <string>

using namespace std;

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

  for(int i = 0; i < n; i++) {
    int score = 0;
    int out = 0;

    int first = 0;
    int second = 0;
    int third = 0;

    while(1) {
      string str;
      cin >> str;
      if(str == "HIT") {
	score += third;
	third = second;
	second = first;
	first = 1;
      } else if(str == "HOMERUN") {
	score += first + second + third + 1;
	first = second = third = 0;
      } else if(str == "OUT") {
	out++;
      }

      if(out == 3) {
	cout << score << endl;
	break;
      }
    }
  }
  return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <algorithm>

#define MAX_N (1000L)
#define NUM_MOD (1000000007L)

using namespace std;

long mod_pow(long n, long k, long mod) {
  long res = 1, num = n;
  while (k > 0) {
    if (k % 2 == 1) {
      res = res * num % mod;
    }
    k /= 2;
    num = num * num % mod;
  }
  return res;
}

long fs[MAX_N + 1], finvs[MAX_N + 1], invs[MAX_N + 1];

void fact_init(long N) {
  fs[0] = fs[1] = 1;
  finvs[0] = finvs[1] = 1;
  invs[1] = 1;
  for (int i = 2; i <= N; i++) {
    fs[i] = fs[i - 1] * i % NUM_MOD;
    invs[i] = NUM_MOD - invs[NUM_MOD % i] * (NUM_MOD / i) % NUM_MOD;
    finvs[i] = finvs[i - 1] * invs[i] % NUM_MOD;
  }
}

long num_comb(long i, long j, long k) {
  long res = 1;
  res = res * fs[i] % NUM_MOD;
  res = res * finvs[i - k * j] % NUM_MOD;
  res = res * finvs[k] % NUM_MOD;
  res = res * mod_pow(finvs[j], k, NUM_MOD) % NUM_MOD;
  return res;
}

int main(int argc, char *argv[]) {
  // read inputs
  long N, A, B, C, D;
  scanf("%ld %ld %ld %ld %ld", &N, &A, &B, &C, &D);

  // init
  fact_init(N);

  // solve dp
  long dpss[MAX_N + 1][MAX_N + 1];
  // initialize
  for (long j = 0; j <= N; j++) {
    dpss[0][j] = 1;
  }
  for (long i = 1; i <= N; i++) {
    // B is the maximum number of people per group
    if (i % B == 0 && i / B >= C && i / B <= D) {
      dpss[i][B] = num_comb(i, B, i / B);
    } else {
      dpss[i][B] = 0;
    }
  }
  // update
  for (long i = 1; i <= N; i++) {
    for (long j = B - 1; j >= A; j--) {
      long dp = 0;
      // dp = max(dp, dpss[i][j + 1]);
      dp = (dp + dpss[i][j + 1]) % NUM_MOD;
      for (long k = C, k_last = min(D, i / j); k <= k_last; k++) {
        dp = (dp + dpss[i - k * j][j + 1] * num_comb(i, j, k) % NUM_MOD) % NUM_MOD;
        // printf("i = %ld, j = %ld, k = %ld, dpss[i - k * j] = %ld, num_comb = %ld\n", i, j, k,
        //        dpss[i - k * j][j + 1], num_comb(i, j, k));
      }
      dpss[i][j] = dp;
    }
  }
  // for (long i = 0; i <= N; i++) {
  //   printf("i = %ld:", i);
  //   for (long j = A; j <= B; j++) {
  //     printf(" %ld", dpss[i][j]);
  //   }
  //   printf("\n");
  // }
  printf("%ld\n", dpss[N][A]);

  return 0;
}

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

template <typename T> inline void read (T &x) {bool b = 0; char c; while (!isdigit (c = getchar()) && c != '-');
if (c == '-') c = getchar(), b = 1; x = c - 48; while (isdigit(c = getchar())) x = (x<<3) + (x<<1) + c - 48; if (b)x=-x;}
template <typename T> inline void wrip(T x) {if (x > 9) wrip(x / 10); putchar(x%10 + 48); }

template <typename T> inline void write(T x) {if (x < 0) putchar('-'), x = -x; wrip(x); putchar(' ');}
template <typename T> inline void writeln(T x) {if (x < 0) putchar('-'), x = -x; wrip(x); putchar('\n');}
inline char readch() {char c; do c = getchar(); while (c == '\n' || c == ' '); return c;}
inline void reads(string &s) {char c; while((c=getchar())==' '||c =='\n'); s=c; while((c=getchar())!=' '&&c!='\n') s+=c;}
inline void getlines(string &s) {char c; while((c=getchar())=='\n'); s=c; while((c=getchar())!='\n') s+=c;}
typedef const int csint;

#define fi first
#define se second
#define mp make_pair
typedef long long ll;
typedef long double ld;
typedef pair <int, int> ii;
typedef vector <int> vi;

const ld pi = acos(-1);
const ll inf = 1e9,
mod = 1e9 + 7,
N = 1011;

int n, a, b, c, d;
ll f[N][N], cb[N][N], g[N][N];
ll fac[N], inv[N];
void Inc(ll &a, ll b) {
    a += b;
    if (a >= mod)
        a -= mod;
}
ll pw(ll a, ll b) {
    if (b == 0)
        return 1;
    ll t = pw(a, b / 2);
    t = t * t % mod;
    if (b % 2)
        return t * a % mod;
    return t;
}
int res, dx[111], pa[111];
void Try(int x) {
    if (x > n) {
        int cnt[21], num[21];
        memset(cnt, 0, sizeof(cnt));
        memset(num, 0, sizeof(num));
        for (int i = 1; i <= n; i++)
            cnt[pa[i]]++;
        for (int i = 1; i <= n; i++) {
            if (!cnt[i])
                continue;
            if (cnt[i] < a || cnt[i] > b)
                return;
            num[cnt[i]]++;
        }
        for (int i = 1; i <= n; i++) {
            if (!num[i])
                continue;
            if (num[i] > d || num[i] < c)
                return;
        }
        res++;
        return;
    }
    for (int i = 1; i <= x; i++) {
        if (dx[i])
            continue;
        dx[x] = i != x;
        pa[x] = i;
        Try(x + 1);
        dx[x] = 0;
    }
}
int main() {
    #ifdef makacha
        freopen("m.inp", "r", stdin);
        freopen("m.out", "w", stdout);
    #endif // makacha
    read(n);
    read(a);
    read(b);
    read(c);
    read(d);
//    Try(1);
//    cout << res << '\n';
    cb[0][0] = 1;
    for (int i = 1; i <= n; i++) {
        cb[i][0] = 1;
        for (int j = 1; j <= i; j++)
            cb[i][j] = (cb[i - 1][j] + cb[i - 1][j - 1]) % mod;
    }
    g[0][0] = 1;
    for (int i = 1; i <= n; i++) {
        g[i][0] = 1;
        for (int j = i; j <= n; j += i)
            g[i][j] = (ll) cb[j][j / i] * g[i - 1][j - j / i] % mod;
    }
    fac[0] = 1;
    for (int i = 1; i <= n; i++)
        fac[i] = (ll)fac[i - 1] * i % mod;
    inv[n] = pw(fac[n], mod - 2);
    for (int i = n; i >= 1; i--)
        inv[i - 1] = (ll)inv[i] * i % mod;
    for (int i = 1; i <= n; i++)
        for (int j = i; j <= n; j += i)
            g[i][j] = (ll)g[i][j] * inv[i] % mod;
    f[a - 1][0] = 1;
    for (int i = a; i <= b; i++) {
        int lim = min(d * i, n);
        int im = i - 1;
        for (int j = i * c, grs = c; j <= lim; j += i, grs++)
            for (int k = j, rem = n, kk = k - j; k <= n; k++, rem--, kk++) {
                ll tmp = f[im][kk];
                tmp = tmp * cb[rem][j] % mod;
                tmp = tmp * g[grs][j] % mod;
                Inc(f[i][k], tmp);
            }
        for (int j = 0; j <= n; j++)
            Inc(f[i][j], f[im][j]);
    }
    cout << f[b][n];
}


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

int main(){
  int n;
  cin>>n;
  int a,b,c,d;
  int pnt[1000]={};

  for(int i=0;i<(n*(n-1)/2);i++){
cin>>a>>b>>c>>d;
 if(c>d) pnt[a]+=3;
 else if(d>c)pnt[b]+=3;
 else if(d==c) pnt[a]++,pnt[b]++;
  }
  int vic[1000];

  for(int i=1;i<=n;i++){
    int cnt=1;
    for(int j=1;j<=n;j++)if(pnt[i]<pnt[j])cnt++;
    vic[i]=cnt;
  }
  for(int i=1;i<=n;i++) cout<<vic[i]<<endl;

  return 0;
}

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <sstream>
#include <functional>
#include <numeric>
#include <cmath>
#include <cstring>
#include <queue>

using namespace std;
int n;

const int INF = 1 << 29;

typedef pair<int,int> P;
vector<P> teams;
bool sortSecond(const P& p1, const P& p2)
{
	if(p1.second != p2.second) return p1.second < p2.second;
	if(p1.first != p2.first) return p1.first < p2.first;
	return false;
}

int main(void)
{
	while(cin >> n && n)
	{
		teams.resize(n);
		for (int i = 0; i < n; i++)
		{
			teams[i] = P(0, i);
		}
		for (int i = 0, leags = n * (n - 1) / 2; i < leags; i++)
		{
			int a, b, c, d;
			cin >> a >> b >> c >> d;
			if(c == d)
			{
				teams[a - 1].first++;
				teams[b - 1].first++;
			}
			else if(c > d) teams[a - 1].first += 3;
			else teams[b - 1].first += 3;
		}
		sort(teams.begin(), teams.end(), greater<P>());
		int hikiwake = 0;
		for (int i = 0; i < n; i++)
		{
			int tmp = teams[i].first;
			teams[i].first = i + 1 - hikiwake;
			if(i + 1 < n && tmp == teams[i + 1].first)
			{
				hikiwake++;
			}
			else hikiwake = 0;
		}
		sort(teams.begin(), teams.end(), sortSecond);
		for (int i = 0; i < n; i++)
		{
			cout << teams[i].first << endl;
		}
	}
	return 0;
}


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

int main(){
    int n;
    cin >> n;
    int a;
    int dp[n];
    int ans=0;
    for(int i=0;i<n;i++)dp[i]=1e9;
    for(int i=0;i<n;i++){
        cin >> a;
        a *= (-1);
        *upper_bound(dp,dp+n,a)=a;
    }
    cout << lower_bound(dp,dp+n,1e9)-dp;
}

/*
https://atcoder.jp/contests/abc134/tasks/abc134_e
N個同時に最小部分列を作る

部分列では常に増加
dp[][]がある
a[i]を見た時, どこを更新するべきか?
30 40 50 60
新たに20が来た
30 40 50 60
20
新たに45が気来た
30 40 50 60
45
vs
30 40 45
50 60
どちらも嬉しさは変わらない 前者の方がいい

最後尾に対して数列を持っておくと,
最後尾のうちxより小さい最大のものをxに更新
条件を満たすものがなければ追加
だから全部-1にしておいて,

20
10
-1
-1

[30が来たら]
30
10
-1
-1

[15が来たら]
20
15
-1
-1
[10が来たら]
20
10
10
-1

dp[j]<A[i]を満たす最小のjを更新

*/
#include<bits/stdc++.h>
using namespace std;
long bs(long x,const vector<long>& v){
  long l=-1,r=v.size();
  while(r-l!=1){
    long z=(l+r)/2;
    if(v[z]<x)r=z;
    else l=z;
  }
  return r;
}
int main(){
  long N;cin>>N;vector<long>A(N),dp(N,-1);
  for(long i=0;i<N;i++)cin>>A[i];
  for(long i=0;i<N;i++){
    long z=bs(A[i],dp);
    dp[z]=A[i];
  }
  long ans=0;
  for(long i=0;i<N;i++)ans+=(dp[i]!=-1);
  cout<<ans<<endl;

}


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

int main(){
    int N, M;
    int tr[21][21];
    while(true){
        scanf("%d", &N);
        if(N == 0) break;
        for(int i = 0; i <= 20; i++){
            fill(tr[i], tr[i] + 21, 0);
        }
        for(int i = 0; i < N; i++){
            int x, y;
            scanf("%d%d", &x, &y);
            tr[x][y] = 1;
        }
        scanf("%d", &M);
        int cnt = 0;
        int rx = 10, ry = 10;
        for(int i = 0; i < M; i++){
            char d; int s;
            scanf(" %c%d", &d, &s);
            int dx, dy;
            if(d == 'N'){
                dx = 0, dy = 1;
            }else if(d == 'E'){
                dx = 1, dy = 0;
            }else if(d == 'S'){
                dx = 0, dy = -1;
            }else{
                dx = -1, dy = 0;
            }
            while(s--){
                rx += dx;
                ry += dy;
                if(tr[rx][ry] == 1){
                    tr[rx][ry] = 0;
                    cnt += 1;
                }
            }
        }
        if(cnt == N){
            puts("Yes");
        }else{
            puts("No");
        }
    }
    return 0;
}

#include <iostream>
using namespace std;

int main()
{
	int n;
	do {
		cin >> n;
		int score_a = 0, score_b = 0;
		for (int i = 0; i < n; i++) {
			int inp_a, inp_b;
			cin >> inp_a >> inp_b;
			if (inp_a == inp_b) {
				score_a += inp_a;
				score_b += inp_b;
			}
			else {
				(inp_a>inp_b ? score_a : score_b) += inp_a + inp_b;
			}
		}
		if (n > 0 && n <= 10000) {
			cout << score_a << ' ' << score_b << '\n';
		}
	} while (n > 0 && n <= 10000);

	return 0;
}


#include<bits/stdc++.h>
#define rep(i,N) for(int i=0;i<(N);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e12;
const int inf = 1e9;
const int mod = 1e9+7;
typedef long long ll;
typedef pair<ll,int> P;
typedef set<int> S;
int main(){
    cout << fixed << setprecision(10);
    int h1,m1,h2,m2,k;
    cin >> h1 >> m1 >> h2 >> m2 >> k;
    int time = 0;
    if(h1==h2){
        time += m2-m1;
        cout << time -k << endl;
    }
    else{
        time += 60 - m1;
        //cout << time << endl;
        time += m2;
        //cout << time << endl;
        time += (h2 - (h1 + 1))*60;
        cout << time - k << endl;
    }
    return 0;
}

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define fo(i,n) for(ll i=0;i<n;i++)
#define fo1(i,n) for(ll i=1;i<=n;i++)
#define loop(i,a,b)for(ll i=a;i<=b;i++)
#define loopr(i,a,b)for(ll i=b;i>=a;i--)
#define vll vector<ll>
#define vvl vector<vll>
#define pii pair<ll,ll>
#define F first
#define S second

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	ll h1,m1,h2,m2,k;
	cin>>h1>>m1>>h2>>m2>>k;
	ll ans=h2*60+m2-h1*60-m1-k;
	if(ans<0)
	cout<<0;
	else
	cout<<ans;
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<string>
#include<map>
#include<vector>

using namespace std;

int main(){

  int n,anum,bnum;
  int as, bs;

  as = bs = 0;

  while(1){
    scanf("%d",&n);

    if(n == 0) break;

    for(int i = 0 ; i < n ; i++){
      scanf("%d%d",&anum,&bnum);

      if(anum == bnum){
	as += anum;
	bs += bnum;
      }

      else if(anum > bnum){
	as += anum;
	as += bnum;
      }

      else if(anum < bnum){
	bs += anum;
	bs += bnum;
      }
    }

    printf("%d %d\n",as,bs);

    as = bs = 0;

  }
  return 0;
}

#include <iostream>
//#include <algorithm>
//#include <vector>
//#include <stack>
//#include <queue>
//#include <map>
//#include <cmath>
//#include <string>
//#include <sstream>
//#include <iomanip>
//#include <complex>
using namespace std;

//#define ll long long
//#define vvi vector< vector<int> >
//#define All(X) X.begin(),X.end()
//#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define REP(i,n) for(int i=0;i<(int)(n);i++)
//#define pb push_back
//ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
//ll lcm(ll a,ll b){return a/gcd(a,b)*b;}

int main(){
  while(1){
    int n, p;
    cin >> n >> p;
    if(n==0&&p==0) break;
    int kago=p;
  	int pc[100]={0};
    int i=0;
    while(1){
      int nowc=i%n;
      if(pc[nowc]==p){
	cout << nowc << endl;
	break;
      }
      if(kago>0){
	kago--;
	pc[nowc]++;
      }
      else{
	kago=pc[nowc];
	pc[nowc]=0;}
      i++;
    }
  }
  return 0;
}


#include <iostream>
#include <vector>
#include <set>

using namespace std;

const int maxN = 500;

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

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

vector<vector<int>> getPrimes(){
    vector<int> p(100000, 1);
    vector<int> primes;
    for(int i=2;;i++){
        if(!p[i]) continue;
        primes.push_back(i);
        if(primes.size() == 4 * maxN) break;
        for(int j=2*i;j<100000;j+=i) p[j] = 0;
    }
    vector<vector<int>> res(2, vector<int>(2*maxN));
    int seek = 0;
    for(int i=0;i<maxN;i++){
        res[0][2*i] = primes[seek];
        res[0][2*i+1] = primes[3*maxN-1-seek];
        ++seek;
    }
    for(int i=0;i<2*maxN;i++){
        if(i < maxN/4 || maxN/4*7 <= i){
            res[1][i] = res[0][i];
        } else {
            if(i%2 == 0){
                res[1][i] = primes[seek];
            } else {
                res[1][i] = primes[3*maxN-1-seek];
                ++seek;
            }
        }
    }
    return res;
}

vector<vector<long long>> makeBoard(){
    vector<vector<long long>> res(maxN, vector<long long>(maxN, 0));
    auto primes = getPrimes();
    for(int i=0;i<maxN;i++){
        for(int j=i%2;j<maxN;j+=2){
            int idxA = (i+j)/2;
            int idxB = (i-j+maxN-2)/2;
            res[i][j] = (long long)primes[0][idxA] * primes[1][idxB];
        }
    }
    for(int i=0;i<maxN;i++){
        for(int j=1-i%2;j<maxN;j+=2){
            res[i][j] = 1;
            int dx[] = {-1, 0, 1, 0};
            int dy[] = {0, -1, 0, 1};
            for(int k=0;k<4;k++){
                int nx = i + dx[k];
                int ny = j + dy[k];
                if(nx < 0 || maxN <= nx || ny < 0 || maxN <= ny) continue;
                res[i][j] = lcm(res[i][j], res[nx][ny]);
            }
            ++res[i][j];
        }
    }
    return res;
}

int main(){
    int N;
    auto board = makeBoard();
    while(cin >> N){
        for(int i=0;i<N;i++){
            cout << board[i][0];
            for(int j=1;j<N;j++) cout << " " << board[i][j];
            cout << endl;
        }
    }
}

#include<stdio.h>
#include<algorithm>
using namespace std;
typedef long long ll;
const int Mod=998244353;
int k,n,ans[4100],C[4100][4100];
int calc(int x,int k,int n){
	int tmp,sum=0;
	for (int i=0;i<=(x-1)/2&&n-i*2>=0;i++){
		if (!k) tmp=0;
		else tmp=(ll)C[(x-1)/2][i]*C[n-i*2+k-1][k-1]%Mod;
		if (i&1) sum=(sum-tmp+Mod)%Mod;
		else sum=(sum+tmp)%Mod;
	}
	return sum;
}
int main(){
	C[0][0]=1;
	for (int i=1;i<=4000;i++){
		C[i][0]=1;
		for (int j=1;j<=i;j++) C[i][j]=(C[i-1][j-1]+C[i-1][j])%Mod;
	}
	scanf("%d%d",&k,&n);
	for (int i=2;i<=k+1;i++)
		if (!(i&1)) ans[i]=(calc(i,k-1,n)+calc(i,k-1,n-1))%Mod;
		else ans[i]=calc(i,k,n);
	for (int i=2;i<=k;i++) printf("%d\n",ans[i]);
	printf("%d\n",ans[k+1]);
	for (int i=k;i>=2;i--) printf("%d\n",ans[i]);
	return 0;
}


#include "bits/stdc++.h"
using namespace std;
using ll = long long;

template <typename T>
void print_vec(const vector<T>& v, bool is_reverse=false, ll num=0){
  if(num == 0) num = (ll)v.size();
  if(is_reverse) for(ll i=num-1; i>=0; i--){ cout<<v[i]; if(i!=0) cout<<" ";}
  else for(ll i=0; i<num; i++){ cout<<v[i]; if(i!=num-1) cout<<" ";}
  cout << endl;
}

template <typename T>
void print_pairvec(const vector<T> &v, ll num=0){
  if(num == 0) num = (ll)v.size();
  cout << endl;
  for(ll i=0; i<num; i++){ cout << v[i].first << " " << v[i].second << endl;}
}

template <typename T>
void print_vec2(const vector<vector<T>>& v){
  cout << endl; cout << "       ";
  for(ll i=0; i<v[0].size(); i++) cout << i << "   ";
  cout << endl;
	for(ll i=0; i<v.size(); i++){
		cout << "i=" << i << ":   ";
		for(ll j=0; j<v[i].size(); j++){
			if(v[i][j] == 0) cout << "\x1B[0m" << v[i][j] << "   ";
      else cout << "\x1B[31m" << v[i][j] << "   ";//https://stackoverrun.com/ja/q/12618775
		}
		cout << "\x1B[0m" << endl;
	}
}

int main(){
  int N, k;
  cin >> N >> k;
  vector<int> x(N);
  for(int i=0; i<N; i++) cin >> x[i];

  // vector<int> anses;
  // const int INF =  2e8+1;
  const int INF = 1e8 + 1e8 + 1e8 + 1;//
  int ans = INF;
  for(int i=0; i<N; i++){
    if(i+k-1 >= N) break;
    int sx = x[i];
    int gx = x[i+k-1];
    if(sx < 0 && gx < 0) ans = min(ans, -sx);
    else if(sx >= 0  &&  gx >= 0) ans = min(ans, gx);
    else if(sx <= 0 && 0 <= gx){
      int tmp = min(2*(-sx) + gx,  -sx + 2*gx);
      ans = min(ans, tmp);
    }
  }

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

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

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

  vector<int>x(N);
  for (int i = 0; i < N; i++) {
    cin >> x.at(i);
  }

  vector<int>A(N-K+1);
  for (int i = K-1; i < N; i++) {
    A.at(i-K+1) = x.at(i) - x.at(i-K+1);
    A.at(i-K+1) += min(abs(x.at(i)),abs(x.at(i-K+1)));
  }
  sort(A.begin(), A.end());
  cout << A.at(0) << endl;
}


//----------------------------------------------------------------------
#include <algorithm>
#include <bits/stdc++.h>
#include <iomanip>
#include <ios>
#include <math.h>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
//----------------------------------------------------------------------

using namespace std;
using ll = long long;
using P = pair<int,int>;
using Graph = vector<vector<int>>;

int main(void) {
    int n;
    cin >> n;
    vector<pair<double, double>> xy(n);
    rep(i, n) {
        cin >> xy[i].first >> xy[i].second;
    }
    vector<int> keiro;
    rep(i, n)
        keiro.push_back(i);

    double ans = 0.0;
    double sum = 0.0;
    do {
        for(int i = 0; i < n - 1; ++i) {
            double dx = xy[keiro[i+1]].first - xy[keiro[i]].first;
            double dy = xy[keiro[i+1]].second- xy[keiro[i]].second;
            ans += sqrt(dx * dx + dy * dy);
        }
        sum++;
    }while(next_permutation(keiro.begin(), keiro.end()));
    ans /= sum;

    cout << fixed << setprecision(16) <<
        ans << endl;
    return 0;
}

#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
#define pb push_back
#define mpr make_pair
#define pii pair<int, int>
#define ll long long
#define ld long double
#define all(arr) arr.begin(), arr.end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define fo(i, l, r) for (int i = l; i <= r; i++)
#define MOD 1000000007
#define mod 998244353
#define pie 3.14159265358979323846264338327950L
#define N 200009
#define M 2000009
#define mid(l, r) l + (r - l) / 2
#define vec vector<int>
#define umap unordered_map<int, int>
#define yes cout << "Yes" << endl;
#define no cout << "No" << endl;
#define endl "\n"
#define int long long
using namespace std;

int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
int ddx[8]={1,1,0,-1,-1,-1,0,1},ddy[8]={0,1,1,1,0,-1,-1,-1};
ll gcd(ll a,ll b){ if(!a)return b;return gcd(b%a,a);}
ll lcm(ll a, ll b) { return (a*b)/ gcd(a,b);}

vector<pair<int,int> > arr(10);

map<pair<int,int>,ld> mp;
ld ans = 0;
int n;
void fun(vector<int> temp, ld curr) {
    if(temp.size() == n) {
        ans+= curr;
        // for(auto it : temp) {
        //     cout<<it<<" ";
        // }
        // cout<<endl;
        return;
    }
    for(int i=0; i<n; i++) {
        if(find(all(temp),i) == temp.end()) {
            int prev = temp.back();
            temp.push_back(i);
            fun(temp,curr + mp[mpr(i,prev)]);
            temp.pop_back();
        }
    }
}
const int MAX = 510000;
long long fac[MAX], finv[MAX], inv[MAX];

void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++){
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

void test_case() {

    cin>>n;

    COMinit();

    rep(i,n) {
        int x,y;
        cin>>x>>y;
        arr[i] = mpr(x,y);
    }

    for(int i=0; i<n; i++) {
        for(int j=0; j<n; j++) {
            ld  d= pow(arr[i].first - arr[j].first,2) + pow(arr[i].second -arr[j].second,2);
            d = sqrt(d);
            // cout<<d<<endl;
            mp[mpr(i,j)] = d;
            mp[mpr(j,i)] = d;
        }
    }

    vec temp;
    for(int i=0; i<n; i++) {
        temp.push_back(i);
        fun(temp,0.0);
        temp.pop_back();

    }

    ld fin = ans/fac[n];

    // cout<<ans<<endl;

    cout<<fin<<endl;

}

signed main ()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cout << fixed << setprecision(20);

    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);

    int t = 1;
    // cin >> t;
    while (t--)
    {
        test_case();
    }
}


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

typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
#define rep(i, n) for(ll i = 0; i < n; i++)
#define REP(i, a, b) for(ll i = a; i < b; i++)
#define out(x) std::cout << x << '\n'
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define sz(x) ((int)(x).size())
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { ll g = gcd(a, b); return a / g * b; }
const int dx[4] = { 1, 0, -1,  0 };
const int dy[4] = { 0, 1,  0, -1 };
const int INF = INT_MAX / 2;
const ll  inf = LLONG_MAX / 2;
const int mod = 1000000007;
const int MAX_N = 101010;
const long double PI = acos(-1);

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

	string s;
	cin >> s;

	cout << s.substr(0, s.length() - 8) << endl;

	return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main(){
  string s;
  cin>>s;
  int ans=0;
  for(int i=0;i<s.size();i++){
    if(s[i]!='J') continue;
    else{
      int now=i+1;
      int J=1,O=0,I=0;
      int flag1=1,flag2=0;
      while(s[now]=='J'){
	//cout<<now<<endl;
	J++;
	now++;
      }
      while(s[now]=='O' && flag1){
	//cout<<now<<endl;
	O++;
	now++;
	flag2=1;
      }
      if(J<O){
	i=now-1;
	continue;
      }
      while(s[now]=='I' && flag2){
	//cout<<now<<endl;
	I++;
	now++;
      }
      if(O>I){
	i=now-1;
	continue;
      }
      ans=max(ans,min(I,min(O,J)));
      // cout<<ans<<endl;
      i=now-1;
      //cout<<i<<endl;
      //cout<<s[i]<<endl;
    }
  }
  cout<<ans<<endl;
  return 0;
}