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

#define int long long
#define vvi vector<vector<int>>
#define vec vector
#define pq priority_queue
#define all(v) (v).begin(), (v).end()
#define uniqueV(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end());
#define rep(i, n) for (int (i) = (0); (i) < (n); ++(i))
#define repp(i, m, n) for (int (i) = (m); (i) < (n); ++(i))
#define dbg(x) cerr << #x << ": " << x << endl;
#define dbg2(x, y) cerr<<"("<<#x<<", "<<#y<<") = "<<"("<<x<<", "<<y<<")"<<endl;
#define dbg3(x, y, z) cerr<<"("<<#x<<", "<<#y<<", "<<#z<<") = "<<"("<<x<<", "<<y<<", "<<z<<")"<<endl;
#define dbgB(value, size) cerr<<#value<<": "<<bitset<size>(value) << endl;
#define line() cerr << "---------------" << endl;

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

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; }

template<typename T, typename U>
void print1(T Array, U size) {
  for (int i = 0; i < size; i++) {
    cout << Array[i] << " ";
  }
  cout << endl;
}

template<typename T>
void print(T Array, int height, int width) {
  for (int i = 0; i < height; i++) {
    for (int j = 0; j < width; j++) {
      cout << " " << Array[i][j];
    }
    cout << endl;
  }
}

// ------------------------------------------------------------------------------------------

int n;
int dp[55];

int rec(int stage) {
  if (stage == 0) {
    return dp[stage] = 1;
  }

  if (dp[stage] != -1) {
    return dp[stage];
  }

  int ans = 0;

  for (int i = 1; i <= 3; i++) {
    if (stage - i < 0) continue;
    ans += rec(stage - i);
  }

  return dp[stage] = ans;
}

signed main() {
  fill(dp, dp+33, -1);
  rec(30);
  while (1) {
    cin >> n;
    if (n == 0) break;
    int t = (dp[n] + 3650 - 1) / 3650;
    cout << t << endl;
  }

  return 0;
}

#include <algorithm>
#include <iostream>

using namespace std;

int main(){

        int A[128], N;
        A[0] = 1;
        for(int i=1; i<=32; i++){
                A[i] = A[i-1];
                if(i>=1) A[i] += A[i-2];
                if(i>=2) A[i] += A[i-3];
        }
        while (cin >> N  && N)  cout << (((A[N] + 9)/ 10) + 364) / 365 << endl;

}


#define rep(i, n) for (int i = 0; i < (n); i++)
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int MOD = 1000000007;
// MAX int 2,147,483,647 -> 10桁
// MAX ll 9,223,372,036,854,775,807 -> 19桁
// MAX O(n) 10^8 -> 8回
// double pi = M_PI or acos(-1)
// printf("%.10f\n", ans);

int main() {
    int n;
    cin >> n;
    vector<int> x(n), y(n);
    rep(i, n) cin >> x[i] >> y[i];
    auto dist = [&](int i, int j) {
        double dx = x[i] - x[j];
        double dy = y[i] - y[j];
        return sqrt(dx * dx + dy * dy);
    };
    vector<int> p(n);
    rep(i, n) p.at(i) = i;
    double len = 0;
    int cnt = 0;
    do {
        rep(i, n - 1) {
            len += dist(p.at(i), p.at(i + 1));
        }
        cnt++;
    } while (next_permutation(p.begin(), p.end()));
    double ans = len / cnt;
    printf("%.10f\n", ans);

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (long long i = 0; i < (n); ++i)
using ll = long long;
using P = pair<ll,ll>;
using vec = vector<ll>;
using vecp = vector<P>;
using mat = vector<vec>;
using matp = vector<vecp>;
const ll MOD = 1e9+7;
const ll INF = 1e18;
#define all(v) v.begin(), v.end()

int main(){
  ll T;
  cin >> T;
  rep(w,T){
    ll N;
    cin >> N;
    vec A(N);
    rep(i,N){
      cin >> A.at(i);
    }
    string S;
    cin >> S;
    set<ll> B;
    B.insert(0);
    rep(i,N){
      set<ll> C;
      auto b=B.end();
      ll a=A.at(N-1-i);
      rep(j,(ll)B.size()){
        b--;
        a=min(a,a^*b);
      }
      if(S.at(N-1-i)=='0'){
        if(a!=0){
          B.insert(a);
        }
      }else{
        if(a!=0){
          B=C;
          break;
        }
      }
    }
    if(B.size()==0){
      cout << 1 << endl;
    }else{
      cout << 0 << endl;
    }
  }

}


 #include<iostream>
 #include<stdio.h>
 #include<string>
 #include<math.h>
 #include<iomanip>
 #include<algorithm>
 #include<string.h>
 #include<cctype>
 #include<map>
 #include<set>
 #include<vector>
 #include<sstream>
 #include<stack>

 using namespace std;

 long long int arr[5000+5];
 long long int result[5000+5];

 int main()
 {
   int n;
   int count=0;
   while(cin>>n&&(n!=0))
   {
     long long int sum;
     long long int max;
     for(int i=0;i<n;i++)
     {
       cin>>arr[i];
       if(i==0)
       {
         sum=arr[i];
         max=arr[i];
         continue;
       }
       if(sum<0)
       {
         sum=arr[i];
         if(sum>max) max=sum;
       }
       else if(sum+arr[i]>0)
       {
         sum=sum+arr[i];
         if(sum>max) max=sum;
       }
       else sum=arr[i];
     }
     result[count++]=max;
   }
   for(int i=0;i<count;i++) cout<<result[i]<<endl;
   //while(1);
   return 0;
 }

#include<iostream>
using namespace std;
int main(){
  int count,max,i,j,k,l;
   int sum,data_set[5000];
  while(cin>>count){
    if(!count)break;
    cin>>data_set[0];
    max=data_set[0];
    for( i=1;i<count;i++){
      cin>>data_set[i];
    }
    //  for(l=1;l<=count;l++){
      for( j=0;j<count;j++){
	sum=0;
	for( k=j;k<count;k++){
	  sum+=data_set[k];
	if(max<sum)max=sum;
	}
    }
    cout<<max<<endl;
  }
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define INF 120000000

using namespace std;

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

int v, e;
vector<int> g[10010], rg[10010], vs, st;
vector<bool> vis;

void dfs(int x) {
    vis[x] = true;
    for (int i = 0; i < g[x].size(); i++) {
        if (!vis[g[x][i]]) dfs(g[x][i]);
    }
    vs.pb(x);
}

void rdfs(int x, int k) {
    vis[x] = true;
    st[x] = k;
    for (int i = 0; i < rg[x].size(); i++) {
        if (!vis[rg[x][i]]) rdfs(rg[x][i], k);
    }
}

bool isSame(int u, int v) {
    return (st[u] == st[v]);
}

int main() {
    cin >> v >> e;
    for (int i = 0; i < e; i++) {
        int a, b;
        cin >> a >> b;
        g[a].pb(b);
        rg[b].pb(a);
    }
    st.resize(v);

    vis.resize(v, false);
    for (int i = 0; i < v; i++) {
        if (!vis[i]) dfs(i);
    }
    int k = 0;
    fill(vis.begin(), vis.end(), false);
    for (int i = vs.size() - 1; i >= 0; i--) {
        if (!vis[vs[i]]) rdfs(vs[i], k++);
    }

    int q;
    cin >> q;
    int u, v;
    for (int i = 0; i < q; i++) {
        cin >> u >> v;
        cout << isSame(u, v) << endl;
    }

    return 0;
}

#include "bits/stdc++.h"

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

using namespace std;
using ll = long long;

constexpr ll atcoder_mod = 1e9+7;

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

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

class ConnectNodeInfo{
	vector<vector<pair<ll,ll>>> graph;
public:
	ConnectNodeInfo(int node_num){
		graph.resize(node_num);
	}
	void AddNonDirectionalConnection(ll u,ll v,ll w){
		graph[u].emplace_back(v,w);
		graph[v].emplace_back(u,w);
	}
	void AddDirectionalConnection(ll u,ll v,ll w){
		graph[u].emplace_back(v,w);
	}
	vector<pair<ll,ll>>& operator[](ll index){
		return graph[index];
	}
	size_t size(){return graph.size();}
};

class UnionFind{
	vector<ll> rank;
	vector<ll> diff_weight;
	vector<ll> num;
public:
	vector<ll> par;

	UnionFind(ll N):par(N),num(N),rank(N),diff_weight(N){
		for(ll i=0;i<N;i++){
			par[i]=i;
			num[i]=1;
			rank[i]=0;
			diff_weight[i]=0;
		}
	}

	ll root(ll x){
		if(par[x]==x){
			return x;
		}
		ll r = root(par[x]);
		diff_weight[x]+=diff_weight[par[x]];
		return par[x]=r;
	}

	void unite(ll x,ll y){
		ll rx = root(x);
		ll ry = root(y);
		if (rx == ry) return;
		if(rank[rx]<rank[ry]) swap(rx,ry);
		if(rank[rx]==rank[ry]) ++rank[rx];
		par[ry] = rx;
		num[rx] += num[ry];
	}

	void relate(ll x,ll y,ll w){
		w+=weight(x),w-=weight(y);
		ll rx = root(x);
		ll ry = root(y);
		if (rx == ry) return;
		if(rank[rx]<rank[ry]){
			swap(rx,ry);
			w = -w;
		}
		if(rank[rx]==rank[ry]) ++rank[rx];
		par[ry] = rx;
		diff_weight[ry] = w;
		num[rx] += num[ry];
	}

	bool same(ll x,ll y){
		ll rx = root(x);
		ll ry = root(y);
		return rx == ry;
	}

	ll getsize(ll x){
		return num[root(x)];
	}

	ll weight(ll x){
		root(x);
		return diff_weight[x];
	}

	ll diff(ll x,ll y){
		return weight(y)-weight(x);
	}
};

void FirstDFS(int child,int& post_count,vector<int>& post,ConnectNodeInfo& connect,vector<int>& memo){
	memo[child] = 1;
	for(auto& x:connect[child]){
		if(!memo[x.first]){
			FirstDFS(x.first,post_count,post,connect,memo);
		}
	}
	post[child] = post_count++;
}

void SecondDFS(int start,int child,ConnectNodeInfo& connect,UnionFind& uf,vector<int>& memo){
	uf.unite(start,child);
	memo[child] = 1;
	for(auto& x:connect[child]){
		if(!memo[x.first]){
			SecondDFS(start,x.first,connect,uf,memo);
		}
	}
}

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

	int N=in(),M=in();
	ConnectNodeInfo connect(N),inverse_connect(N);
	REP(i,M){
		int u=in(),v=in();
		connect.AddDirectionalConnection(u,v,1);
		inverse_connect.AddDirectionalConnection(v,u,1);
	}

	int post_count=0;
	vector<int> post(N,-1);
	vector<int> memo(N,0);
	REP(i,N){
		if(!memo[i]) FirstDFS(i,post_count,post,connect,memo);
	}
	vector<pair<int,int>> new_order(N);
	REP(i,N){
		new_order[i].first=i;
		new_order[i].second=post[i];
	}
	sort(ALL(new_order),PAIRCOMP(second,>));
	UnionFind uf(N);

	fill(ALL(memo),0);
	REP(i,N){
		if(!memo[new_order[i].first]) SecondDFS(new_order[i].first,new_order[i].first,inverse_connect,uf,memo);
	}

	int Q=in();
	LOOP(Q){
		int u=in(),v=in();
		out(uf.same(u,v)?1:0);
	}
	return 0;
}


#include <bits/stdc++.h>
#define N 1001
using namespace std;
int n,m;
string s1,s2;

int mem[N][N],used[N][N];
int dfs(int I,int J){
  if(I == n) return m-J;
  if(J == m) return n-I;
  if(used[I][J]++) return mem[I][J];
  int res = 1e9;
  res = min(res,1+dfs(I+1,J));//insert
  res = min(res,1+dfs(I,J+1));//erase
  res = min(res,(s1[I]!=s2[J])+dfs(I+1,J+1));//replace or same
  return mem[I][J] = res;
}

int main(){
  cin>>s1>>s2;
  n = s1.size();
  m = s2.size();
  cout<<dfs(0,0)<<endl;;
  return 0;
}

#include <iostream>
#include <algorithm>
#include <vector>
#include <deque>
#include <string>
#include <tuple>
#include <functional>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <map>
//#include "toollib.h"
#pragma warning (disable:4018)
#define INT_MAX 2147483647

using namespace std;

int judge_equivalent(char a, char b) {
	if (a == b) return 0;
	else return 1;
}

int main() {
	string  a, b;
	cin >> a >> b;
	vector<vector<int>> dp(a.length() + 1, vector<int>(b.length() + 1, 0));
	for (int i = 0; i <= a.length(); i++) dp[i][0] = i;
	for (int j = 1; j <= b.length(); j++) dp[0][j] = j;
	for (int i = 1; i <= a.length(); i++) {
		for (int j = 1; j <= b.length(); j++) {
			int p = dp[i - 1][j - 1] + judge_equivalent(a[i - 1], b[j - 1]);
			int q = dp[i - 1][j] + 1;
			int r = dp[i][j - 1] + 1;
			dp[i][j] = min({ p, q, r });
		}
	}

	cout << dp[a.length()][b.length()] << endl;
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define sc(x) scanf("%lld",&x);
int main(){
    ll n, a, b;
    sc(n) sc(a) sc(b)
    ll X[n];
    for (int i = 0; i < n; i++) sc(X[i])
    ll x,ans = 0;
    for (int i = 0; i < n - 1; i++){
        x = (X[i + 1] - X[i]) * a;
        ans += min(x, b);
    }
    cout << ans << endl;
    return 0;
}

#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <iomanip>
#include <numeric>
#include <queue>
#include <cmath>
using namespace std;

int main() {
	string s;
	cin >> s;
	int n = s.size();
	int t = 753;
	int d = 1000;
	string res;
	for (int i = 0; i + 2 < n; i++) {
				string ss = string(1, s[i]) + string(1, s[i + 1]) + string(1, s[i + 2]);
				if (abs(stoi(ss) - t) < d) {
					res = ss;
					d = abs(stoi(ss) - t);
				}

	}
	cout << d << endl;
}


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

int stair(int n){
	if(n==1)
		return 1;
	else if(n==2)
		return 2;
	else if(n==3)
		return 4;
	else
		return stair(n-1)+stair(n-2)+stair(n-3);
}

int main(){
	int n;
	while(scanf("%d",&n) && n!=0){
		int year = stair(n)/3650;
		if(stair(n)%3650!=0)  year++;
		cout<<year<<endl;
	}

	return 0;
}


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

int main(){cout<<fixed<<setprecision(20);
		   ll m;
           cin>>m;
           vll d(m),c(m);
           ll ans=0;
           rep(i,0,m){
             cin>>d[i]>>c[i];
             ans+=c[i];
           }
           ll cnt=0;
           rep(i,0,m){
             cnt+=d[i]*c[i];
             while(cnt>=10){
               ll c=cnt/10;
               ans+=c;
               cnt%=10;
               cnt+=c;
             }
           }
           cout<<ans-1<<endl;
}

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

using namespace std;

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

	int x, y;

	vector<int> y_x(8, -1);

	for (int i = 0; i < N; i++)
	{
		cin >> y >> x;

		y_x[y] = x;
	}

	vector<int> perm(8);

	for (int i = 0; i < 8; i++) { perm[i] = i; }

	bool ok = true;

	do
	{
		ok = true;

		for (int y = 0; y < 8; y++)
		{
			if (y_x[y] != -1 && perm[y] != y_x[y])
			{
				ok = false;
			}
		}

		if (ok == true)
		{
			for (int i = 0; i < 8; i++)
			{
				for (int j = 0; j < 8; j++)
				{
					if (i != j)
					{
						if (i - perm[i] == j - perm[j] || i + perm[i] == j + perm[j])
						{
							ok = false;
						}
					}
				}
			}
		}

		if (ok == true)
		{
			for (int i = 0; i < 8; i++)
			{
				for (int j = 0; j < 8; j++)
				{
					if (perm[i] == j)
					{
						cout << 'Q';
					}
					else
					{
						cout << '.';
					}
				}

				cout << endl;
			}
		}
	} while (next_permutation(perm.begin(), perm.end()));

	return 0;
}


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

using namespace std;

ifstream fin("../data.txt");

int x1, y1, x2, y2, aux, sx, sy, tx, ty, roads, minix, miniy, nx, ny, ax, ay;
char dir;
int a[2005][2005];
string ans, sdir;
vector<pair<int, int>> d = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

int absVal(int x) {
  if (x < 0) return -x;
  return x;
}

bool isValid(int x, int y) {
  if (x > 2004 || x < 0 || y > 2004 || y < 0 || a[x][y] == 1) return 0;
  return 1;
}

int main() {
   ans = "", sdir = "URDL";
   cin >> x1 >> y1 >> x2 >> y2;
   aux = x1;
   x1 -= aux - 1;
   x2 -= aux - 1;
   aux = y1;
   y1 -= aux - 1;
   y2 -= aux - 1;
   sx = x1;
   sy = y1;
   tx = x2;
   ty = y2;
   //cout << sx << " " << sy << " " << tx << " " << ty << "\n";
   //cout << x1 << " " << y1 << " " << x2 << " " << y2;
   a[sx][sy] = 1;
   while (roads < 4) {
    //  cout << sx << " " << sy << " " << tx << " " << ty << "\n\n";
    //  for (int i = 0; i < 5; i++) {
    //   for (int j = 0; j < 5; j++) {
    //     cout << a[i][j] << " ";
    //   }
    //   cout << "\n";
    // }
    //if (sx == 0 && sy == 2) return 0;
    //cout << "\n";
     minix = miniy = 20000;
     for (int i = 0; i < 4; i++) {
      if (isValid(sx + d[i].first, sy + d[i].second)) {
        ax = absVal(tx - sx - d[i].first);
        ay = absVal(ty - sy - d[i].second);
        if (minix > ax || miniy > ay) {
          //cout << ax << " " << ay << "\n";
          minix = ax;
          miniy = ay;
          nx = sx + d[i].first;
          ny = sy + d[i].second;
          dir = sdir[i];
        }
     }
     }

     ans += string(1, dir);
     a[nx][ny] = 1;
     sx = nx;
     sy = ny;
     if (sx == tx && sy == ty) {
       roads++;
       if (roads % 2) {
         sx = x2;
         sy = y2;
         tx = x1;
         ty = y1;
       } else {
         sx = x1;
         sy = y1;
         tx = x2;
         ty = y2;
       }
       a[sx][sy] = 1;
       a[tx][ty] = 0;
     }
    }
  cout << ans;
  return 0;
}

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

int main() {

int sx,sy,tx,ty;
cin >> sx >> sy >> tx >>ty;

for(int i = sy; i < ty; i++){
  cout << "U";
}

for(int i = sx; i < tx; i++){
  cout << "R";
}

for(int i = ty; i > sy; i--){
  cout << "D";
}

for(int i = tx; i > sx; i--){
  cout << "L";
}

cout << "L";

for(int i = sy; i <= ty; i++){
  cout << "U";
}

for(int i = sx; i <= tx; i++){
  cout << "R";
}

cout << "D" << "R";

for(int i = ty; i >= sy; i--){
  cout << "D";
}

for(int i = tx; i >= sx; i--){
  cout << "L";
}

cout << "U";

return 0;
}


#include <array>
#include <cstdlib>
#include <iostream>
#include <string>
#include <queue>
#include <vector>
class SSF {
private:
	void ScanLine(std::vector<std::string> *map, const char lx, const char rx, const char y, const char oy, std::array<std::queue<char>, 4> *buffer) {
		unsigned char i = lx;
		while (i <= rx) {
			while (i < rx) {
				if (map->at(y).at(i) != '0') {
					break;
				}
				++i;
			}
			if (map->at(y).at(i) == '0') {
				break;
			}
			buffer->at(0).push(i);
			while (i <= rx) {
				if (map->at(y).at(i) == '0') {
					break;
				}
				++i;
			}
			buffer->at(1).push(i - 1);
			buffer->at(2).push(y);
			buffer->at(3).push(oy);
		}
		return;
	}
public:
	unsigned char SeedFill(std::vector<std::string> *map, const unsigned char x, const unsigned char y) {
		std::array<std::queue<char>, 4> buffer;
		unsigned char lx, rx, ly, oy;
		if (map->at(y).at(x) == '0' || map->empty() == true) {
			return 0;
		}
		buffer.at(0).push(x);
		buffer.at(1).push(x);
		buffer.at(2).push(y);
		buffer.at(3).push(y);
		while (buffer.front().empty() == false) {
			char lxsav = buffer.at(0).front() - 1, rxsav = buffer.at(1).front() + 1;
			lx = buffer.at(0).front();
			buffer.at(0).pop();
			rx = buffer.at(1).front();
			buffer.at(1).pop();
			ly = buffer.at(2).front();
			buffer.at(2).pop();
			oy = buffer.at(3).front();
			buffer.at(3).pop();
			if (map->at(ly).at(lx) == '0') {
				continue;
			}
			while (rx < map->at(ly).size() - 1) {
				if (map->at(ly).at(rx + 1) == '0') {
					break;
				}
				++rx;
			}
			while (lx > 0) {
				if (map->at(ly).at(lx - 1) == '0') {
					break;
				}
				--lx;
			}
			for (unsigned char i = lx; i <= rx; ++i) {
				map->at(ly).at(i) = '0';
			}
			if (ly - 1 >= 0) {
				if (ly - 1 == oy) {
					ScanLine(map, lx, lxsav, ly - 1, ly, &buffer);
					ScanLine(map, rxsav, rx, ly - 1, ly, &buffer);
				}
				else {
					ScanLine(map, lx, rx, ly - 1, ly, &buffer);
				}
			}
			if (ly + 1 <= map->size() - 1) {
				if (ly + 1 == oy) {
					ScanLine(map, lx, lxsav, ly + 1, ly, &buffer);
					ScanLine(map, rxsav, rx, ly + 1, ly, &buffer);
				}
				else {
					ScanLine(map, lx, rx, ly + 1, ly, &buffer);
				}
			}
		}
		return 1;
	}
};
int main() {
	while (1) {
		SSF ssf;
		std::vector<std::string> map;
		unsigned short count = 0;
		do {
			map.resize(count + 1);
			std::getline(std::cin, map.at(count));
			if (map.at(count).empty() == true) {
				std::getline(std::cin, map.at(count));
				if (map.at(count).empty() == true) {
					return EXIT_SUCCESS;
				}
			}
			map.at(count).resize(map.front().size(), '0');
			map.at(count).shrink_to_fit();
		} while (++count < map.front().size());
		map.shrink_to_fit();
		count = 0;
		for (unsigned char j = 0; j < map.size(); ++j) {
			for (unsigned char i = 0; i < map.at(j).size(); ++i) {
				count += ssf.SeedFill(&map, i, j);
			}
		}
		std::cout << count << std::endl;
	}
	return EXIT_SUCCESS;
}

#include<iostream>
#include<cstdio>
#include<queue>
#include<string>
#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;

int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
int ans;
string island[20];

int dfs(int x,int y){
    island[y][x]='0';
    REP(i,4){
        int nx=x+dx[i];
        int ny=y+dy[i];
        if(nx>=0&&nx<12&&ny>=0&&ny<12&&island[ny][nx]=='1')dfs(nx,ny);
    }
    return 0;
}

int main(){
    while(1){
        REP(y,12){
            if(!(cin>>island[y]))return 0;
        }
        ans=0;
        REP(y,12){
            REP(x,12){
                if(island[y][x]=='1'){
                    dfs(x,y);
                    ans++;
                }
            }
        }
        cout<<ans<<endl;
    }
}


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

typedef long long ll;

using namespace std;

int a[100010] , n;

int partition(int p , int r){
	int x = a[r];
	int i,j;
	i = p-1;
	for(j = p ; j<r ; j++){
		if(a[j] <= x){
			i++;
			swap(a[i],a[j]);
		}
	}
	swap(a[r],a[i+1]);
	return i+1;
}

int main(){
	ios::sync_with_stdio(false);
	cin>>n;
	for(int i = 0 ; i<n ; i++){
		cin>>a[i];
	}

	int q = partition(0,n-1);

	for(int i = 0 ; i<n ; i++){
		if(i)
			printf(" ");
		if(i==q){
			printf("[%d]",a[i]);
		}
		else
			printf("%d",a[i]);
	}
	printf("\n");

	return 0;
}

#include <iostream>
using namespace std;

void swap(int A[], int i, int j)
{
    int t = A[i];
    A[i]  = A[j];
    A[j]  = t;
}

int partition(int A[], int p, int r)
{
    int x = A[r], i = p - 1;
    for (int j = p; j <= r; j++) {
        if (A[j] > x) continue;
        swap(A, ++i, j);
    }
    return i;
}

int main()
{
    int n;
    cin >> n;
    int A[n];
    for (int i = 0; i < n; i++) cin >> A[i];
    int p = partition(A, 0, n - 1);
    for (int i = 0; i < n; i++) {
        if (i == p) cout << "[" << A[i] << "]";
        else cout << A[i];
        if (i == (n - 1)) cout << endl;
        else cout << " ";
    }
    return 0;
}


#include <iostream>

using namespace std;

int main() {

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

#include <iostream>
#include <vector>

using namespace std;

void abc_150a();

void abc_151a();

void abc_152a();

void abc_154a();

void abc_156a();

int main()
{
  //abc_150a();
  //abc_151a();
  //abc_152a();
  //abc_154a();
  abc_156a();

}

void abc_150a()
{
  int K, X;
  cin >> K >> X;

  if (500 * K >= X) cout << "Yes\n";
  else cout << "No\n";
}

void abc_151a()
{
  char C;
  cin >> C;

  cout << (char)((int)C + 1) << "\n";
}

void abc_152a()
{
  int N, M;
  cin >> N >> M;
  if (N == M) cout << "Yes\n";
  else cout << "No\n";
}

void abc_154a()
{
  string S, T;
  cin >> S >> T;
  int a, b;
  cin >> a >> b;
  string U;
  cin >> U;

  if (S == U) cout << a - 1 << " " << b << "\n";
  else cout << a << " " << b - 1 << "\n";

}

void abc_156a()
{
  int N, R;
  cin >> N >> R;

  if (N >= 10) cout << R << "\n";
  else cout << R + 100 * (10 - N) << "\n";
}


#include <bits/stdc++.h>
using namespace std;
int p[1000005];

int getSum(int* BIT,int idx){
	int sum=0;
	while(idx>0){
		sum+=BIT[idx];
		idx-=(idx&-idx);
	}
	return sum;
}

void update(int* BIT,int N,int idx,int val){
	while(idx<=N){
		BIT[idx]+=val;
		idx+=(idx&-idx);
	}
	return;
}

int main() {
	memset(p,0,sizeof(p));

	int N,i;
	scanf("%d",&N);
	int Q;
	scanf("%d",&Q);

	int A[N+1];
	for(i=1;i<=N;i++)
	scanf("%d",&A[i]);

	int *BIT=new int[N+1];
	for(i=0;i<=N;i++)
	BIT[i]=0;

	vector<pair<int,pair<int,int> > > queries(Q);
	for(i=0;i<Q;i++){
		scanf("%d %d",&queries[i].second.first,&queries[i].first);
		queries[i].second.second=i;
	}
	vector<int> ans(Q,0);
	sort(queries.begin(),queries.end());
	int j=0;
	for(i=1;i<=N;i++){
		if(p[A[i]]>0)
		update(BIT,N,p[A[i]],-1);
		p[A[i]]=i;
		update(BIT,N,i,1);
		while(j<Q&&queries[j].first==i){
			ans[queries[j].second.second]=getSum(BIT,i)-getSum(BIT,queries[j].second.first-1);
			++j;
		}
		if(j==Q)
		break;
	}

	for(int i=0;i<Q;i++)
	printf("%d\n",ans[i]);
	// your code goes here
	return 0;
}

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

int main() {
    int n, m;
    cin >> n >> m;
    vector<int> l(m), r(m);
    for (int i = 0; i < m; i++) cin >> l[i] >> r[i];

    sort (l.begin(), l.end());
    sort (r.begin(), r.end());

    int ans = r[0] - l[m-1] + 1;
    ans = max (ans, 0);

    cout << ans << endl;
}


#include <algorithm>
#include <bitset>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <cctype>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <limits>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define ArraySizeOf(array)     (sizeof(array) / sizeof(array[0]))
#define res(i,n) for(int i=n;;i++)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i,n) for(int i=1;i<n;i++)
#define rev(i,n) for(int i=n-1;i>=0;i--)
#define REV(i,n) for(int i=n-1;i>0;i--)
#define req(i,n,m) for(int i=n;i<m;i++)
#define REQ(i,n,m,l) for(int i=n;i<m;i+=l)
#define _GLIBCXX_DEBUG
int INF = 1e9 + 7;
unsigned NthDayOfWeekToDay(unsigned n, unsigned dow, unsigned dow1)
{
    unsigned day;
    if (dow < dow1) dow += 7;
    day = dow - dow1;
    day += 7 * n - 6;
    return day;
}
signed gcd(int x, int y) {
    if (y == 0)return x;
    return gcd(y, x % y);
}
signed lcm(int x, int y) {
    return x / gcd(x, y) * y;
}
unsigned DayToWeekNumber(unsigned day)
{
    return (day - 1) / 7 + 1;
}
unsigned AnotherDayOfWeek(unsigned day, unsigned day0, unsigned dow0)
{
    return (dow0 + 35 + day - day0) % 7;
}
using namespace std;
int main() {
    int N, K; cin >> N >> K;
    vector<int>A(N);
    rep(i, N) {
        cin >> A[i];
        if (A[i] == K) {
            cout << "POSSIBLE" << endl;
            return 0;
        }
    }
    if (N == 1) {
        if (A[0] != K)cout << "IMPOSSIBLE" << endl;
        else cout << "POSSIBLE" << endl;
        return 0;
    }
    sort(A.begin(), A.end());
    int B=gcd(A[0],A[1]);
    REP(i, N - 1)B = gcd(B, A[i + 1]);
    rep(i,N){
        if (A[i] % B == K % B && K <= A[i]) {
            cout << "POSSIBLE" << endl;
            return 0;
        }
    }
    cout << "IMPOSSIBLE" << endl;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i,cc,n) for(int i=cc;i<n;++i)
#define lrep(i,cc,n) for(long long i=cc;i<n;++i)
#define rrep(i,cc,n) for(long i=cc;i>n;--i)
#define pii pair<int, int>
#define pll pair<long long, long long>

using ll = long long;
const ll inf = 1001001001;
ll gcd(ll x, ll y) { return (x % y)? gcd(y, x % y): y; }
int main() {
    int n;
    ll k, p;
    cin >> n >> k >> p;
    ll m = p;
    rep(i, 1, n){
        ll q;
        cin >> q;
        p = gcd(p, q);
        m = max(m, q);
    }
    if(k%p == 0 && k <=m) cout << "POSSIBLE" << endl;
    else cout << "IMPOSSIBLE" << endl;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int  a;
    cin>>a;
    int res=a*(1+a+a*a);
    cout<<res;
}

#include <bits/stdc++.h>
using namespace std;
void h(){
  cout << "Hello World" << endl;
  exit(0);
}
int main() {
  int x,a,b; cin>>x;
  if(x==1) h();
  cin>>a>>b;
  cout << a+b << endl;
}


#include<bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < n; i++)
#define vi vector<int>
#define all(x) (x).begin(),(x).end()
#define INF (1<<30)-1
using ll = long long;
using namespace std;
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; }

int main(){
  int n; cin >> n;
  string s; cin >> s;
  vi txt(n);
  rep(i,n){
    if(s[i] == 'R') txt[i] = 0;
    if(s[i] == 'G') txt[i] = 1;
    if(s[i] == 'B') txt[i] = 2;
  }
  vi rgb(3);
  rep(i,n) rgb[txt[i]]++;
  ll ans = 1;
  rep(i,3) ans *= rgb[i];
  rep(j,n){
    rep(i,j){
      if(2*j-i < n && txt[i] != txt[j] && txt[j] != txt[2*j-i] && txt[2*j-i] != txt[i]) ans--;
    }
  }
  cout << ans;
  cout << "\n";
  return 0;
}

#include <iostream>
using namespace std;
int main()
{
    bool cr = false, cg = false, cb = false;
    int n;
    long long ans = 0;
    string s;
    cin >> n >> s;
    int r[n + 1], g[n + 1], b[n + 1];
    r[0] = 0;
    g[0] = 0;
    b[0] = 0;
    for(int i = 0; i < n; i++)
    {
        r[i + 1] = r[i];
        g[i + 1] = g[i];
        b[i + 1] = b[i];
        if(s[i] == 'R')
        {
            r[i + 1]++;
        }
        else if(s[i] == 'G')
        {
            g[i + 1]++;
        }
        else
        {
            b[i + 1]++;
        }
    }
    for(int i  = 0; i < n - 2; i++)
    {
        if(s[i] == 'R')
        {
            cr = true;
        }
        else if(s[i] == 'G')
        {
            cg = true;
        }
        else
        {
            cb = true;
        }
        for(int j = i + 1; j < n - 1; j++)
        {
            if(s[i] != s[j])
            {
                if(s[j] == 'R')
                {
                    cr = true;
                }
                else if(s[j] == 'G')
                {
                    cg = true;
                }
                else
                {
                    cb = true;
                }
                if(!cr)
                {
                    ans += r[n] - r[j];
                    if(j + j - i < n && s[j + j - i] == 'R')
                    {
                        ans--;
                    }
                }
                else if(!cg)
                {
                    ans += g[n] - g[j];
                    if(j + j - i < n && s[j + j - i] == 'G')
                    {
                        ans--;
                    }
                }
                else
                {
                    ans += b[n] - b[j];
                    if(j + j - i < n && s[j + j -i] == 'B')
                    {
                        ans--;
                    }
                }
                if(s[j] == 'R')
                {
                    cr = false;
                }
                else if(s[j] == 'G')
                {
                    cg = false;
                }
                else
                {
                    cb = false;
                }
            }
        }
        if(s[i] == 'R')
        {
            cr = false;
        }
        else if(s[i] == 'G')
        {
            cg = false;
        }
        else
        {
            cb = false;
        }
    }
    cout << ans << endl;
    return 0;
}


#include <iostream>
#include <utility>
#include <map>
#include <vector>
#include <algorithm>
#include <queue>
#include <math.h>
#include <stack>
#include <set>
#include <deque>
#include <cmath>

using namespace std;
typedef long long ll;
ll mod = 1e9+7;
//ll mod = 998244353;
#define rep(i,n) for(int i = 0; i < (n); ++i)

int main() {
    ll n, m;
    cin >> n >> m;
    if (m % n == 0) {
        cout << m / n << endl;
        return 0;
    }
    for (ll i = m / n; i >= 1; --i) {
        if (m % i != 0) continue;
        ll rest = m - i * n;
        if (rest < 0) continue;
        if (rest % i == 0) {
            cout << i << endl;
            return 0;
        }
    }
    cout << 1 << endl;
    return 0;
}

#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i,s,n) for (int i = (s); i < (n); ++i)
#define rrep(i,n,g) for (int i = (n)-1; i >= (g); --i)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define len(x) (int)(x).size()
#define dup(x,y) (((x)+(y)-1)/(y))
#define append push_back
#define Field(T) vector<vector<T>>
using namespace std;
using ll = long long;
using P = pair<int,int>;

vector<ll> enum_divisors(ll N) {
  vector<ll> res;
  for (ll i = 1; i * i <= N; ++i) {
    if (N % i == 0) {
      res.push_back(i);
      if (N/i != i) res.push_back(N/i);
    }
  }
  sort(res.begin(), res.end());
  return res;
}

int main() {
  ll n,m;
  cin >> n >> m;
  const auto &res = enum_divisors(m);
  ll ans = 0;
  for (ll i : res) {
    if (n * i <= m) ans = i;
  }
  cout << ans << endl;
}


#include<iostream>
#include<iomanip>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<string>
#include<utility>
#include<numeric>
#include<algorithm>
#include<cmath>
#include<climits>
using namespace std;

using ll = long long;

class Stream{
public:
    Stream(){

    }
    //配列以外の入力を受け取る
    void read(){

    }
    template<typename First, typename... Rest>
    void read(First& first, Rest&... rest){
        cin >> first;
        read(rest...);
    }
    //配列を区切って出力する
    template<typename T>
    void write_vec(vector<T>& v, char divider){
        for(size_t i = 0; i < v.size(); i++){
            cout << v[i] << divider;
        }
    }
};

int main(){
    Stream io;

    int A, B, C, D;
    io.read(A, B, C, D);
    string ans;
    if(A + B < C + D){
        ans = "Right";
    }
    else if(A + B == C + D){
        ans = "Balanced";
    }
    else{
        ans = "Left";
    }
    cout << ans;
    return 0;
}

#include<iostream>
int main(){
    int A, B, C, D;
    std::cin >> A >> B >> C >> D;
    if(A+B > C+D) std::cout<<"Left";
    else if(A+B < C+D) std::cout<<"Right";
    else std::cout<<"Balanced";
}


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

typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

#define f first
#define s second
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define pb push_back

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for (int i = (b); i >= (a); i--)
#define R0F(i, a) ROF(i, 0, a-1)
#define trav(x, a) for (auto& x: a)

const int MX  = 2e5+5;
const int MOD = 1e9+7;
const ll  INF = 1061109567; // 4557430888798830399LL
const ld  EPS = 1e-9;
const ld  PI  = acos(-1);

int main() {

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

    ll x; cin >> x;

    if (x%11 == 0) {
        cout << x/11*2 << "\n";
    } else if (x%11 <= 6) {
        cout << x/11*2+1 << "\n";
    } else {
        cout << x/11*2+2 << "\n";
    }

}

#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG //GCC5.4.1で配列の範囲外アクセスをエラーにする

//cmdで g++ test.cpp とするとコンパイルできる
//制限1秒なら10^6は余裕 10^7は多分間に合う 10^8はよほど単純でないと厳しい
// https://github.com/atcoder/live_library
// cppdbg: test.exeのterminalに標準入力を打てる

#define ALL(x) x.begin(),x.end()
#define get_tp(t,i) get<i>(t)
#define len(x) (ll)(x.size())
#define ub(a,x) (upper_bound(ALL(a),x)-a.begin())
#define lb(a,x) (lower_bound(ALL(a),x)-a.begin())
typedef long long ll; //long long
typedef pair<int,int> pii; //pair<int,int>
typedef pair<long long,long long> pllll; //pair<ll,ll>
typedef vector<int> vi; //vector<int>
typedef vector<vector<int>> vvi; //vector<vector<int>>
typedef vector<ll> vll; //vector<long long>
typedef vector<vector<ll>> vvll; //vector<vector<long long>>
typedef priority_queue<ll, vector<ll>, greater<ll> > gpq; //逆順priority_queue
typedef priority_queue<ll> pq;

//const int MOD=998244353;
const int MOD=1000000007; //10^9+7
const long double PI=3.1415926535897932;

int main(){
    ll x;cin>>x;
    ll ans=0;
    ans+=x/11;
    x-=ans*11;
    ans*=2;
    if(x>0){ans+=(x<=6)?1:2;}
    cout<<ans<<endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
using Graph = vector<vector<int>>;
using Graphl = vector<vector<ll>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define Rep(i, k, n) for (int i = k; i < (int)(n); i++)
#define RRep(i, k, n) for (int i = k; i > (int)(n); i--)
#define FOR(itr, v) for (auto itr = (v).begin(); itr != (v).end(); ++itr)
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define ALL(a)  (a).begin(),(a).end()
#define rALL(a)  (a).rbegin(),(a).rend()
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T> void PrintVector(const vector<T> &vec) {for (auto val : vec) cout << val << " "; cout << endl;}
const long long INF = 1LL << 60;
const long long minusINF = -(1LL << 40);
const int MOD = 1000000007;
const double PI = acos(-1); //3.14~
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

vector<pair<long long, long long> > prime_factorize(long long n) {
    vector<pair<long long, long long> > res;
    for (long long p = 2; p * p <= n; ++p) {
        if (n % p != 0) continue;
        int num = 0;
        while (n % p == 0) { ++num; n /= p; }
        res.push_back(make_pair(p, num));
    }
    if (n != 1) res.push_back(make_pair(n, 1));
    return res;
}

const int MAX = 210000;

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;
    }
}
long long COM(int n, int k){
    if(n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n-k] % MOD) % MOD;
}

int main()
{
    int n, m; cin >> n >> m;
    COMinit();
    auto vec = prime_factorize(m);
    ll ans = 1;
    for (auto pa : vec)
    {
        int num = pa.second;
        ll tmp = COM(num + n - 1, num);
        ans = (ans * tmp) % MOD;
    }
    cout << ans << endl;
}

#include <bits/stdc++.h>
#include <iomanip>
#include <math.h>
#include <unistd.h>
#include <stdio.h>
#define all(a) (a).begin(),(a).end()
#define vecin(a) rep(i,a.size())cin >> a[i]
#define vecout(a) rep(i,a.size()){cout << a[i];cout << (i == a.size() - 1 ? "\n":" ");}
#define rep(i,x) for(int i = 0;i<x;i++)
#define REP(i,x) for(int i = 1;i<=x;i++)
#define mp make_pair
using ll = long long;
using ld = long double;
using namespace std;
using dou = double;
const ll inf = 2147483647;
const ll INF = 1LL << 60;
const dou pi = 3.14159265358;
//const ll mod = 1000000007LL;
const ll mod = 998244353LL;
typedef pair<ll,ll> P;
using graph = vector<vector<ll>>;
template<class T> inline bool chmax(T& a, T b){if(a < b){a = b; return true;}return false;}
template<class T> inline bool chmin(T& a, T b){if(a > b){a = b; return true;}return false;}
template<class T> inline bool change(T a,T b){if(a > b){swap(a,b);return true;}return false;}
ll gcd(ll a,ll b){
    if(a < b)swap(a,b);
    if(a % b == 0)return b;
    else return gcd(b,a%b);
}
ll lcm(ll a,ll b){
    return a / gcd(a,b) * b;
}

//素数判定O(sqrt(N))
bool isp(ll n){
    bool res = true;
    if(n == 1)return false;
    else{
        for(ll i = 2;i * i <= n;i++){
            if(n % i == 0){
                res = false;
                break;
            }
        }
        return res;
    }
}
const ll cmax = 1000000;
ll fac[cmax], finv[cmax], inv[cmax];

// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (ll i = 2; i < cmax; 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;
    }
}

// 二項係数計算
ll nCk(ll n, ll k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}
//nのm乗をMODで割ったあまりO(logm)
ll modpow(ll n,ll m,ll MOD){
    if(m == 0)return 1;
    if(m < 0)return -1;
    ll res = 1;
    while(m){
        if(m & 1)res = (res * n) % MOD;
        m >>= 1;
        n *= n;
        n %= MOD;
    }
    return res;
}
ll mypow(ll n,ll m){
    if(m == 0)return 1;
    if(m < 0)return -1;
    ll res = 1;
    while(m){
        if(m & 1)res = (res * n);
        m >>= 1;
        n *= n;
    }
    return res;
}
//printf("%.10f\n", N);
/*  vector<ll> vec = {1,2,5,6,8,9,10};
    cout << binary_search(all(vec),5) << endl; -> true*/
/*
    vector<ll> vec = {1,2,5,6,8,9,10};
    auto n = upper_bound(all(vec),4);
    cout << *n << endl; -> 5*/

//    cout << fixed << setprecision(15);

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll N,M;
    cin >> N >> M;
    swap(N,M);
    vector<ll> vec(N);
    ll up = 0,wa = 0;
    rep(i,N){
        cin >> vec[i];
        wa += vec[i];
        chmax(up,vec[i]);
    }
    if(N == 1){
        cout << vec[0] - 1 << "\n";
        return 0;
    }
    wa -= up;
    if(up + 1 <= wa){
        cout << "0\n";
        return 0;
    }
    else{
        cout << up - wa - 1 << endl;
    }
}


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

int main() {
  int n, m;
  while (cin >> n >> m, n | m) {
    vector<vector<int>> graph(n);
    for (int i = 0; i < m; ++i) {
      int a, b; cin >> a >> b; --a; --b;
      graph[a].push_back(b);
      graph[b].push_back(a);
    }
    vector<int> dist(n, 3);
    queue<int> Q;
    Q.push(0);
    dist[0] = 0;
    while (!Q.empty()) {
      int v = Q.front(); Q.pop();
      for (int w : graph[v]) {
        if (dist[w] == 3) {
          dist[w] = dist[v] + 1;
          Q.push({w});
        }
      }
    }
    int ans = 0;
    for (int d : dist)
      if (d < 3) ++ans;
    cout << ans - 1 << endl;
  }
}


#include <bits/stdc++.h>
#define INF 1e9
#define endl '\n'
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define FORE(i, a, b) for (int i = (a), e = (b); i < e; ++i)
#define RFORE(i, a, b) for (int i = (b)-1, e = (a); i >= e; --i)
#define ALL(x) (x).begin(), (x).end()
#define SORT(c) sort((c).begin(), (c).end())
#define SORTR(c) sort((c).begin(), (c).end(), greater<int>())
using namespace std;
typedef long long ll;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int N;
    cin >> N;
    vector<ll> A(N);
    for (int i = 0; i < N; i++) {
        cin >> A.at(i);
    }
    SORT(A);
    int ans   = 0;
    int tmp   = 0;
    int count = 0;
    for (ll& a : A) {
        if (tmp != a) {
            if (count % 2 == 1) { ++ans; }
            count = 0;
        }
        ++count;
        tmp = a;
    }
    if (count % 2 == 1) { ++ans; }
    cout << ans << endl;
}

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define all(v) v.begin(), v.end()
#define mem(arr) memset(arr, 0, sizeof(arr))
#define ff first
#define ss second
#define int long long int
const int MOD = 1e9 + 7;
const int INF = 1e18;
const int N = 1e6 + 1;
vector<int> g[N];
vector<int> vis(N);
void solve(){
    int n;
    cin >> n;
    int a[n];
    set<int> s;
    for(int &i : a){
        cin >> i;
        if(s.find(i) == s.end()){
            s.insert(i);
        }else{
            s.erase(i);
        }
    }
    cout << (int)s.size() << '\n';
}
int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int tc = 1;
    while(tc--){
        solve();
    }
}


#include <iostream>
#include <algorithm>
using namespace std;
int main(){
	int a[4],b,c;
	cin>>a[0]>>a[1]>>a[2]>>a[3]>>b>>c;
	sort(a,a+4);
	if(b>c)swap(b,c);
	cout<<a[1]+a[2]+a[3]+c<<endl;
	return 0;
	}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll inf = 1e9+7;

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


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define elif else if
#define sp(x) fixed << setprecision(x)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const ll MOD = 1e9+7;
//const ll MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
const ld EPS = 1e-10;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};

int main(){
    int N;
    cin >> N;
    multiset<int> s;
    rep(i, N){
        int A; cin >> A;
        s.insert(-A);
    }
    int ans = 0;
    multiset<int>::iterator it;
    for(it = s.begin(); sz(s);){
        int A = -(*it);
        s.erase(it);
        rep3(j, 29, 0){
            if(A & (1<<j)){
                int B = -((1<<(j+1))-A);
                if(s.count(B)){
                    s.erase(s.find(B));
                    ans++;
                }
                break;
            }
        }
        it = s.begin();
    }
    cout << ans << endl;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i,x) for(long long i=0;i<x;i++)
#define repn(i,x) for(long long i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
vector<pair<string,P> >vec;

void solve(long long n, long long m, vector<long long> x, vector<long long> y){
	ll sumx = 0, sumy = 0;
	rep(i, n) {
		sumx = (sumx + (2*i-n+1) * x[i]) % mod;
	}
	rep(i, m) {
		sumy = (sumy + (2*i-m+1) * y[i]) % mod;
	}
	cout << sumx * sumy % mod << endl;
}

int main(){
	long long m;
	long long n;
	scanf("%lld",&n);
	vector<long long> x(n-1+1);
	scanf("%lld",&m);
	vector<long long> y(m-1+1);
	for(int i = 0 ; i <= n-1 ; i++){
		scanf("%lld",&x[i]);
	}
	for(int i = 0 ; i <= m-1 ; i++){
		scanf("%lld",&y[i]);
	}
	solve(n, m, x, y);
	return 0;
}


#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,b,a) for(int i=b;i>=a;i--)
#define fori(a) for(auto i : a )
#define all(a) begin(a), end(a)
#define set(a,b) memset(a,b,sizeof(a))
#define sz(a) a.size()
#define pi 3.14159
#define ll long long
#define ull unsigned long long
#define pb push_back
#define PF push_front //deque
#define mp make_pair
#define pq priority_queue
#define mod 1000000007
#define f first
#define s second
#define pii pair< int, int >
#define vi vector<int>
#define vpii vector<pii>
#define debug(v) for(auto i:v) cout<<i<<" ";
#define tc int t; cin >> t; while(t--)

using namespace std;
string repeat(string s, int n) {
    string s1 = "";
    for (int i=0; i<n;i++)
        s1+=s;
    return s1;
}
string getString(char x) {
    string s(1, x);
    return s;
}

void optimizeIO(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}
int gcd(int a, int b){
    if (a == 0)  return b;
    return gcd(b % a, a);
}
void  solve(){
  int h,w;
  cin>>h>>w;
  char x[h][w];
  rep(i,0,h){
    rep(j,0,w){
      cin>>x[i][j];
    }
  }
  rep(i,0,h){
    rep(j,0,w){
      int flag=0;
      if(x[i][j]=='#'){
        if(i>0) flag+= x[i-1][j]==x[i][j]?1:0;
        if(i<h-1) flag+= x[i+1][j]==x[i][j]?1:0;
        if(j>0) flag+= x[i][j-1]==x[i][j]?1:0;
        if(j<w-1) flag+= x[i][j+1]==x[i][j]?1:0;
        if(flag==0){
          cout<<"No\n";
          return;
        }
      }

    }
  }
  cout<<"Yes\n";
}
int main(){
    optimizeIO();
    // tc
    { solve();
    }
}

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
#define all(x) x.begin(),x.end()
#define rep(i,j,n) for (long long i = j; i < (long long)(n); i++)
#define _GLIBCXX_DEBUG
#define MOD 1000000007
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; }
//name[i][j]-> vvl name(i,vl(j))
//map<int,int>mp
//余りを切り上げる =>  (a + b - 1) / b
vi vx={-1,0,1,0};
vi vy={0,1,0,-1};
using pi = pair<int,int>;

signed main(){
	int a,b; cin>>a>>b;
	vvi v(a,vi(b));
	queue<pi> q;
	rep(i,0,a){
		rep(j,0,b){
			char c; cin>>c;
			if(c=='#'){
				v[i][j]=1;
				q.push(make_pair(i,j));
			}
			else v[i][j]=0;
		}
	}
	while(!q.empty()){
		pi p=q.front(); q.pop();
		int k,l; tie(k,l)=p;
		bool ok=false;
		rep(i,0,4){
			k+=vx[i]; l+=vy[i];
			if(k>=0 && k<a){
				if(l>=0 && l<b){
					if(v[k][l]==1)ok=true;
				}
			}
			k-=vx[i]; l-=vy[i];
		}
		if(!ok){
			cout<<"No"<<endl;
			return 0;
		}
	}
	cout<<"Yes"<<endl;

	return 0;
}


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

// Morris-Pratt
template<typename T>
struct MP {
  int n;
  T t;
  vector<int> a;
  MP() {}
  MP(const T& t): t(t) {
    n = t.size();
    a = vector<int>(n+1);//便宜上最初に-1を入れるため（1 indexにした方が良い)
    a[0] = -1;
    int j = -1;
    for (int i = 0; i < n; ++i) {
      while (j != -1 && t[j] != t[i]) j = a[j];//次の文字が一致するようなaを見つける
      j++;
      a[i+1] = j;
    }
  }
  int operator[](int i) { return a[i];}//添字アクセスできるように
  vector<int> findAll(const T& s) {
    vector<int> res;
    int j = 0;
    for (int i = 0; i < s.size(); ++i) {
      while (j != -1 && t[j] != s[i]) j = a[j];
      j++;
      if (j == n) {
        res.push_back(i-j+1);
        j = a[j];
      }
    }
    return res;
  }
};

int main(){
  int n;
  cin>>n;
  vector<int> a(n);
  vector<int> b(n);
  vector<int> a1(n);
  vector<int> b1(n);

  rep(i,n)cin>>a[i];
  rep(i,n)cin>>b[i];
  rep(i,n-1){
    a1[i+1]=a[i]^a[i+1];
  }
  rep(i,n-1){
    b1[i+1]=b[i]^b[i+1];
  }
  a1[0]=a[0]^a[n-1];
  b1[0]=b[0]^b[n-1];

  vector<int> A=a1;
  A.insert(A.end(), A.begin(), A.end());
//  rep(i,A.size())cout<<A[i];
  MP <vector<int>> mp(b1);

  vector<int> res=mp.findAll(A);
  rep(i,res.size()){
    if (res[i]>=n)continue;
    int now=res[i];
    int x=b[0]^a[now];
    cout<<res[i]<<" "<<x<<endl;
  }

}

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

template <typename T>
struct Compress {
    vector<T> xs;

    Compress(const vector<T>& vs) : xs(vs) {
        sort(xs.begin(), xs.end());
        xs.erase(unique(xs.begin(), xs.end()), xs.end());
    }

    int compress(const T& x) {
        return lower_bound(xs.begin(), xs.end(), x) - xs.begin();
    }

    T decompress(int i) {
        return xs[i];
    }
};

vector<int> suffix_array(vector<int>& s) {
    int n = s.size();
    Compress<int> comp(s);
    vector<int> t(n);
    for (int i = 0; i < n; i++) t[i] = comp.compress(s[i]);
    vector<int> p(n), c(n), cnt(n);
    for (int i = 0; i < n; i++) cnt[t[i]]++;
    for (int i = 1; i < n; i++) cnt[i] += cnt[i-1];
    for (int i = 0; i < n; i++) p[--cnt[t[i]]] = i;
    int cl = 0;
    for (int i = 1; i < n; i++) {
        if (s[p[i]] != s[p[i-1]]) cl++;
        c[p[i]] = cl;
    }
    vector<int> pn(n), cn(n);
    for (int h = 0; (1 << h) < n; h++) {
        for (int i = 0; i < n; i++) {
            pn[i] = p[i] - (1 << h);
            if (pn[i] < 0) pn[i] += n;
        }
        fill(cnt.begin(), cnt.end(), 0);
        for (int i = 0; i < n; i++) cnt[c[pn[i]]]++;
        for (int i = 1; i < n; i++) cnt[i] += cnt[i-1];
        for (int i = n - 1; i >= 0; i--) {
            cnt[c[pn[i]]]--;
            p[cnt[c[pn[i]]]] = pn[i];
        }
        cn[p[0]] = 0;
        cl = 0;
        for (int i = 1; i < n; i++) {
            pair<int, int> cur(c[p[i]], c[(p[i] + (1 << h)) % n]);
            pair<int, int> prev(c[p[i-1]], c[(p[i-1] + (1 << h)) % n]);
            if (cur != prev) cl++;
            cn[p[i]] = cl;
        }
        swap(c, cn);
    }
    return p;
}

int bin_search(vector<int>& a, vector<int>& b, vector<int>& sa, bool lower) {
    int n = a.size();
    int lb = -1, ub = n;
    while (ub - lb > 1) {
        int m = (lb + ub) / 2;
        bool flag = false;
        for (int i = 0; i < n; i++) {
            int j = (sa[m] + i) % n;
            if (a[i] < b[j]) {
                ub = m;
                flag = true;
                break;
            }
            if (a[i] > b[j]) {
                lb = m;
                flag = true;
                break;
            }
        }
        if (!flag) {
            if (lower) ub = m;
            else lb = m;
        }
    }
    return ub;
}

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

    int N;
    cin >> N;
    vector<int> a(N), b(N);
    for (int i = 0; i < N; i++) cin >> a[i];
    for (int i = 0; i < N; i++) cin >> b[i];
    vector<int> c(N), d(N);
    for (int i = 0; i < N; i++) {
        c[i] = a[i] ^ a[(i+1) % N];
        d[i] = b[i] ^ b[(i+1) % N];
    }
    auto sa = suffix_array(d);
    int lb = bin_search(c, d, sa, true);
    int ub = bin_search(c, d, sa, false);
    vector<pair<int, int>> ans;
    for (int i = lb; i < ub; i++) {
        int k = (N - sa[i]) % N;
        int x = a[k] ^ b[0];
        ans.push_back({k, x});
    }
    sort(ans.begin(), ans.end());
    for (auto& p : ans) cout << p.first << " " << p.second << "\n";
}


#include <bits/stdc++.h>
using namespace std;
void print() {
    cout << endl;
}
template <class Head, class... Tail>
void print(Head &&head, Tail &&... tail) {
    cout << head;
    if (sizeof...(tail) != 0)
        cout << " ";
    print(forward<Tail>(tail)...);
}
template <class T>
void print(vector<T> &vec) {
    for (auto &a : vec) {
        cout << a;
        if (&a != &vec.back())
            cout << " ";
    }
    cout << endl;
}
template <class T>
void print(vector<vector<T>> &df) {
    for (auto &vec : df) {
        print(vec);
    }
}
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define xrep(i, a, b) for (int i = int(a); i < int(b); ++i)
using ll = long long;
const int INF = 1001001001;

int bills[3] = {10000, 5000, 1000};

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

    rep(i, N+1) {
        rep(j, N+1) {
            int k = N-(j+i);
            if(k<0) continue;
            if(bills[0]*i+bills[1]*j+bills[2]*k==Y) {
                print(i, j, k);
                return 0;
            }
        }
    }

    print(-1, -1, -1);

    return 0;
}

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<time.h>
#include<vector>
using namespace std;
int main()
{
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    //cout.tie(0);
    int n,x,t;
    int ans;
    scanf("%d%d%d",&n,&x,&t);
    if(n%x==0)
        ans = n/x*t;
    else
        ans = n/x*t + t;
    printf("%d\n",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int n,p;
	cin>>n;
	vector<int>v;
	for(int i=0;i<2*n;i++){
		cin>>p;
		v.push_back(p);
	}
	sort(v.begin(),v.end());
	long res=0;
	for(int i=0;i<2*n;i+=2){
		res+=min(v[i],v[i+1]);
	}
	cout<<res;
	return 0;
}

#include<iostream>
#include<unordered_map>
using namespace std;
int main(){
    int n;
    cin>>n;
    unordered_map<string,int>bmp,rmp;
    for(int i=0;i<n;i++)
    {
        string element;
        cin>>element;
        if(bmp.find(element)==bmp.end()){
            bmp[element]=1;
        }
        else
        {
            bmp[element]++;
        }
    }
    int m;
    cin>>m;
    for(int i=0;i<m;i++)
    {
        string element;
        cin>>element;
        if(rmp.find(element)==rmp.end()){
            rmp[element]=1;
        }
        else
        {
            rmp[element]++;
        }
    }
    int finalresult=0;
    for(auto it=bmp.begin();it!=bmp.end();it++)
    {
        int result=0;
        if(rmp.find(it->first)==rmp.end()){
            result+=it->second;
        }
        else
        {
            int red=rmp.find(it->first)->second;
            result+=max(0,it->second-red);
        }
        if(finalresult<result)
        {
            finalresult=result;
        }
    }
    cout<<finalresult;
}


#include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
template<class T>
using Table = vector<vector<T>>;
const ld eps=1e-9;

vector<long long int> divisor(long long int n) {
    vector<long long int> res;
    for (long long int i = 1; i * i <= n; ++i) {
        if (n % i == 0) {
            res.push_back(i);
            if (i * i != n) res.push_back(n / i);
        }
    }
    sort(begin(res), end(res));
    return res;
}
long long int gcd(long long int l, long long int r) {
    if (l > r)return gcd(r, l);
    else {
        if (r%l) {
            return gcd(l, r%l);

        }
        else {
            return l;
        }
    }
}
int main() {
    int N; cin >> N;
    vector<long long int>ns(N);
    for (int i = 0; i < N; ++i) {
        cin >> ns[i];
    }
    long long int a =  gcd(ns[0], ns[1]);
    if (N == 3) {
        a =  gcd(a, ns[2]);
    }
    auto v(divisor(a));
    for (auto n : v) {
        cout << n << endl;
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
typedef long long ll;
struct edge { ll s, t, x; };
typedef pair<ll, ll> P;
typedef pair<ll,P> event;
const ll MAX = 510000;
const ll MOD = 1000000007;

int main() {
  ll N;
  cin>>N;
  ll A[N];
  rep(i,N){cin>>A[i];A[i]=-1ll*A[i];}
  multiset<ll>ma;
  ma.insert(2ll);
  ma.insert(-100000000000ll);
  ll ans=0ll;
  rep(i,N){
    auto t=ma.upper_bound(A[i]);
    if(*t==2){ma.insert(A[i]);ans++;continue;}
    else{
      ma.erase(t);
      ma.insert(A[i]);
    }
  }
  cout<<ans;
}


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

const int MAX = 510000;
const int MOD = 1000000007;

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;
    }
}

int main(){
    int N,M;
    cin >> N >> M;
    int r= 2;
    int count=0;//素因数の数
    map<long long  ,int  > insuu;
    vector<int > prime;
    COMinit();
    //因数分解部
    int pM=M;
    while(M>1){
        if(M%r!=0){//割り切れないなら次の因数を検索
            while(true){
                if(r==2){
                    r++;
                }else{
                    r+=2;
                }
                if(M%r==0){
                    break;
                }
                if(r>sqrt(pM)){
                    prime.push_back(M);
                    insuu[M]++;
                    M=1;
                    break;
                }
            }
        }else{
            M=M/r;
            if(insuu[r]<1){
                prime.push_back(r);
            }
            count++;
            insuu[r]++;
        }
    }
    //出力計算部
    long long ans=1;
    for(auto v :prime){
        ans*=fac[N+insuu[v]-1]*((finv[insuu[v]])*finv[N-1]%MOD)%MOD;
        ans=ans%MOD;
    }
    cout << ans << endl;
    return 0;
}

#include <cstdio>
#include <algorithm>
#include <vector>
#include <set>
#include <cstdlib>
#include <utility>
#include <cmath>
#include <queue>
#include <stack>
#include <cstring>

using namespace std;

#define ll long long

#ifndef ONLINE_JUDGE
#define debug(format, ...) fprintf(stderr, \
    "%s:%d: " format "\n", __func__, __LINE__,##__VA_ARGS__)
#else
#define debug(format, ...)
#define NDEBUG
#endif

int n, m;

bool vis[1005][1005];
char s[1005];
int dist[1005][1005];

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

int main()
{
    scanf("%d%d", &n, &m);
    queue<pair<int, int> > bfs;
    for (int i = 1; i <= n; i++) {
        scanf("%s", s + 1);
        for (int j = 1; j <= m; j++) {
            if (s[j] == '#') {
                bfs.push(make_pair(i, j));
                vis[i][j] = true;
            }
        }
    }
    while (!bfs.empty()) {
        pair<int, int> cur = bfs.front();
        bfs.pop();
        for (int i = 0; i < 4; i++) {
            pair<int, int> newpos = make_pair(cur.first
                + dx[i], cur.second + dy[i]);
            if (newpos.first < 1 || newpos.first > n ||
                newpos.second < 1 || newpos.second > m) continue;
            if (vis[newpos.first][newpos.second]) continue;
            bfs.push(newpos);
            dist[newpos.first][newpos.second] =
                dist[cur.first][cur.second] + 1;
            vis[newpos.first][newpos.second] = true;
        }
    }
    int ans = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            ans = max(ans, dist[i][j]);
        }
    }
    printf("%d", ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=1e6+7;
#define ll long long
char a[N];
int main(){
    int n,q,k;
    scanf("%d\n",&n);
    scanf("%s",a);
    scanf("%d",&q);
    while(q--){
        scanf("%d",&k);
        ll nowd=0,nowm=0,cnt=0;
        ll sum=0;
        for(int i=0;i<n;i++){
            if(i-k>=0&&a[i-k]=='D'){
                nowd--;
                cnt-=nowm;
            }
            else if(i-k>=0&&a[i-k]=='M'){
                nowm--;
            }
            if(a[i]=='D')nowd++;
            if(a[i]=='M'){
                nowm++;
                cnt+=nowd;
            }
            if(a[i]=='C')sum+=cnt;
        }
        printf("%lld\n",sum);
    }
    return 0;
}

#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<cctype>
#include<string.h>
#include <stdlib.h>

#define PI 3.1415926
void swap(int* x, int* y) {
	int a = *x;
	int b = *y;
	*x = b;
	*y = a;
}
int factorial(int n);
int sum(int n, int*);
int ave(int n, int*);
int min(int n, int*), min(int n, int m);
int max(int n, int*), max(int n, int m);

int main() {
	int N, X[200] = { 0 }, a, b, c, d, Y[200];
	scanf("%d", &N);
	for (int i = 1; i <= N*(N - 1) / 2; i++) {
		scanf("%d %d %d %d", &a, &b, &c, &d);
		if (c == d) {
			X[a]++;
			X[b]++;
		}
		else if (c < d) {
			X[b] += 3;
		}
		else {
			X[a] += 3;
		}
	}
	int p = 1;
	for (int i = 1; i <= N; i++) {
		p = 1;
		//printf("%d\n", X[i]);
		for (int j = 1; j <= N; j++) {
			if (X[i] < X[j]) {
				p++;
			}
		}
		printf("%d\n", p);
	}
	return 0;
}

int factorial(int n) {
	if (n > 0) {
		return n * factorial(n - 1);
	}
	else {
		return 1;
	}
}
int sum(int n, int *all) {
	int total = 0;
	for (int i = 0; i < n; ++i) {
		total += all[i];
	}
	return total;
}
int ave(int n, int *all) {
	int total = 0;
	for (int i = 0; i<n; ++i) {
		total += all[i];
	}
	return total / n;
}
int min(int n, int *all) {

	int min = all[0];
	for (int i = 1; i < n; ++i) {
		if (all[0] > all[i]) {
			min = all[i];
		}
	}
	return min;
}
int max(int n, int *all) {
	int max = all[0];
	for (int i = 1; i<n; ++i) {
		if (all[0]<all[i]) {
			max = all[i];
		}
	}
	return max;
}
int max(int n, int m) {
	if (n >= m) {
		return n;
	}
	else {
		return m;
	}
}
int min(int n, int m) {
	if (n >= m) {
		return m;
	}
	else {
		return n;
	}
}


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

int main(){
  int N,Q;
  cin>>N>>Q;
  int I=1100100100;
  vector<pair<int,int>> p;
  for(int i=0;i<N;i++){
    int a,b,c;
    cin>>a>>b>>c;
    p.push_back(make_pair(a-c,c));
    p.push_back(make_pair(b-c,-1*c));
  }
  for(int i=0;i<Q;i++){
    int a;
    cin>>a;
    p.push_back(make_pair(a,I));
  }
  sort(p.begin(),p.end());
  priority_queue<int, vector<int>, greater<int>> pq;
  priority_queue<int, vector<int>, greater<int>> end;
  for(int i=0;i<2*N+Q;i++){
    int a,b;
    tie(a,b)=p[i];
    if(b<0){
      end.push(b*-1);
    }
    else if(b<I){
      pq.push(b);
    }
    else{
      while(!pq.empty()&&!end.empty()){
        if(pq.top()!=end.top()){
          break;
        }
        pq.pop();
        end.pop();
      }
      if(pq.empty()){
        cout<<"-1"<<endl;
      }
      else{
        cout<<pq.top()<<endl;
      }
    }
  }
}

#include<bits/stdc++.h>
using namespace std;
int psum[505][505];
int main(){
	int n,m,q;
	cin>>n>>m>>q;
	while(m--){
		int l,r;
		cin>>l>>r;
		psum[r][l]++;
	}
	for(int i=1; i<=n; i++){
		for(int j=1; j<=n; j++) psum[i][j]+=psum[i][j-1];
	}
	for(int i=1; i<=n; i++){
		for(int j=1; j<=n; j++) psum[i][j]+=psum[i-1][j];
	}
	while(q--){
		int l,r;
		cin>>l>>r;
		cout<<psum[r][n]-psum[r][l-1]<<'\n';
	}
}


#define _USE_MATH_DEFINES

#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef tuple<ll, ll, ll> tl3;

//typedef modint998244353 mint;

const int BIG_NUM = 1e9;
const ll INF = 1000000000000000000;
const ll MOD = 1e9 + 7;
//const ll MOD = 998244353;

const ll MAX = 1e9 + 5;

int main() {
	int m;
	cin >> m;
	vector<int> d(m);
	vector<ll> c(m);

	for (int i = 0; i < m; i++) {
		cin >> d[i] >> c[i];
	}

	vector<vector<pll>> dl(10);

	for (int i = 0; i < 10; i++) {
		dl[i].emplace_back(make_pair(i, 0));
	}

	for (int i = 0; i < 10; i++) {
		for (int j = 0; j < 60; j++) {

			//cout << j << endl;
			ll num = dl[i][j].first * 2;
			ll cnt = dl[i][j].second * 2 + 1;

			if (num >= 10) {
				cnt++;
				num = num % 10 + 1;
			}
			dl[i].emplace_back(make_pair(num, cnt));
		}
		//cout << i << endl;
	}

	/*
	for (int i = 0; i < 10; i++) {
		for (int j = 0; j < 60; j++) {
			cout << dl[i][j].first << " ";// << dl[i][j].second << endl;
		}
		cout << endl;
	}
	*/

	ll cnt = 0;
	vector<int> a(m);
	for (int i = 0; i < m; i++) {
		int num = 0;
		cnt--;
		for (int b = 0; b < 60; b++) {

			if (((c[i] >> b) & 1) == 1) {
				num += dl[d[i]][b].first;
				cnt += dl[d[i]][b].second;
				cnt++;
				//cout << i << " " << dl[d[i]][b].second << " " << dl[d[i]][b].first << endl;
			}
			if (num >= 10) {
				num -= 9;
				cnt++;

			}
		}

		a[i] = num;

	}
	//cout << cnt << endl;
	int num = a[0];
	for (int i = 1; i < m; i++) {
		//cout << a[i] << endl;
		num += a[i];
		cnt++;
		if (num >= 10) {
			num -= 9;
			cnt++;

		}
	}

	cout << cnt << endl;
}

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
typedef long long ll;
typedef pair<int,int> pp;
const int INF = 1e9;
const int MOD = 1000000007;

int main() {
  int m;
  cin >> m;
  ll now = 0;
  ll nine_count = 0;
  ll d = 0;
  rep(i,m){
    ll a,b;
    cin >> a >> b;
    d += b;
    nine_count += (a * b)/9;
    now += (a * b) % 9;
  }
  nine_count += now/9;

  if(now%9 == 0) cout << nine_count + d - 2 << endl;
  else cout << nine_count + d - 1 << endl;

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
const ll mo = 1000000007;
const int MAX = 510000;
const int MOD = 1000000007;
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;
  }
}
long long COM(int n, int k){
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
ll mypow(ll a, ll b){
  ll res = 1;
  a %= mo;
  while (b)
  {
    if (b & 1)
      res = res * a % mo;
    a = a * a % mo;
    b >>= 1;
  }
  return res;
}
int main()
{
ll n ;
cin >> n ;
ll a , b , c , d ;
cin >> a >> b >> c >> d ;
COMinit();
vector<vector<ll>> dp(b-a+2, vector<ll>(n+1,0) );

dp[0][0]=1;
rep(i,b-a+1)rep(j,n+1){
  ll te=dp[i][j];
  for (ll k = 0; k <= d && j+k*(i+a)<=n ; k++)
  {
    if(te==0)break;
    if(k){
      te*=COM(n-j-(k-1)*(i+a),i+a);
      te%=mo;
      te*=mypow(k,mo-2);
      te%=mo;
    }
    te%=mo;
    if(k==0 || k>=c)dp[i+1][j+k*(i+a)]=(dp[i+1][j+k*(i+a)]+te)%mo;
  }

}

cout << dp[b-a+1][n]%mo << endl;
return 0;
}

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

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pdd;
typedef vector<ll> vll;
typedef vector<ld> vld;
typedef vector<pll> vpl;
typedef vector<vll> vvll;

#define ALL(a) a.begin(),a.end()
#define SZ(a) ((int)a.size())
#define FI first
#define SE second
#define REP(i,n) for(int i=0;i<((int)n);i++)
#define REP1(i,n) for(int i=1;i<((int)n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define PB push_back
#define EB emplace_back
#define MP(a,b) make_pair(a,b)
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define yes cout<<"Yes"<<endl
#define YES cout<<"YES"<<endl
#define no cout<<"No"<<endl
#define NO cout<<"NO"<<endl
#define Decimal fixed<<setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL

const int inf = 1e9;
const ll linf = 1LL << 50;
const double eps = 1e-10;
const int MOD = 1e9 + 7;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};

ll mpow(ll m, ll p){
  ll power = m;
  ll res = 1;
  while(p>0){
    if(p&1)
      res = res*power%MOD;
    power = (power*power)%MOD;
    p /= 2;
  }

  return res;
}

ll mod_inv(ll m){
  return mpow(m, MOD-2);
}

struct COM{
  vll fact, fact_inv;
  COM(ll n): fact(n+1,1), fact_inv(n+1,1){
    for(ll i=1; i<=n; i++)
      fact[i] = fact[i-1]*i%MOD;

    fact_inv[n] = mod_inv(fact[n]);
    for(ll i=n; i>=1; i--)
      fact_inv[i-1] = fact_inv[i]*i%MOD;
  }

  ll calc(ll n, ll k){
    if(k<0 || n<k)
      return 0;
    ll res = fact[n]*fact_inv[n-k]%MOD*fact_inv[k]%MOD;
    return res;
  }
};

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

  ll n,m,k;
  cin>>n>>m>>k;

  COM com(n*m);
  ll ans2=0;
  for(ll d=1; d<=m-1; d++){
    ll cost=d*n*n*(m-d)%MOD;
    ans2=(ans2+com.calc(n*m-2,k-2)*cost%MOD)%MOD;
  }
  for(ll d=1; d<=n-1; d++){
    ll cost=d*m*m*(n-d)%MOD;
    ans2=(ans2+com.calc(n*m-2,k-2)*cost%MOD)%MOD;
  }

  cout<<ans2<<endl;
}


#include <bits/stdc++.h>

#define endl '\n'
#define fi first
#define se second
#define MOD(n,k) ( ( ((n) % (k)) + (k) ) % (k))
#define forn(i,n) for (ll i = 0; i < n; i++)
#define forr(i,a,b) for (ll i = a; i < b; i++)
#define all(v) v.begin(), v.end()
#define pb push_back

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef vector<ll> vi;
typedef vector<vector<ll>> vvi;
typedef vector<ll> vl;
typedef vector<ii> vii;

const ll MX = 300005;
const ll inf = 2e18;

#define ptr nodo*
#define tam 1234
char resp[tam][tam];
int main ()
{
    vector<string> v4;
    v4.pb("bbio");
    v4.pb("ppio");
    v4.pb("omqq");
    v4.pb("omcc");
    vector<string> v5;
    v5.pb("..cnk");
    v5.pb("..cnk");
    v5.pb("bbuuq");
    v5.pb("mmi.q");
    v5.pb("qqibb");
    vector<string> v6;
    v6.pb("..cctr");
    v6.pb("...ktr");
    v6.pb("..ck.h");
    v6.pb("bbc..h");
    v6.pb("mmf.gg");
    v6.pb("qqfee.");

    vector<string> v7;
    v7.pb("....gca");
    v7.pb("....gca");
    v7.pb("....vks");
    v7.pb("....vks");
    v7.pb("bbaa.jj");
    v7.pb("mmzzf..");
    v7.pb("qqoof..");
    vector<vector<string> > vvs;
    vvs.pb(v4);
    vvs.pb(v5);
    vvs.pb(v6);
    vvs.pb(v7);
    ios_base::sync_with_stdio(0); cin.tie(0);
    int n;
    cin>>n;
    int N=n;
    if (n==2)
        cout<<"-1";
    else
    {
        if (n==3)
        cout<<"a..\na..\n.bb\n";
        else
        {
            forr(i,0,n)
            forr(j,0,n)
            resp[i][j]='.';
            int x,y;
            x=y=0;
            while(n>7)
            {
                forr(i,0,4)
                forr(j,0,4)
                resp[y+i][x+j]=v4[i][j];
                x+=4,y+=4;
                n-=4;
            }
            n-=4;
            int m=vvs[n].size();
                forr(i,0,m)
                forr(j,0,m)
                resp[y+i][x+j]=vvs[n][i][j];
            forr(i,0,N)
            {
                forr(j,0,N)
                cout<<resp[i][j];
                cout<<endl;
            }
        }
    }

}


#include <iostream>
using namespace std;
int main(){
	int n;
	cin >> n;
	int a[n];
	for(int i=0;i<n;i++) cin >> a[i];
	for(int i=n-1;i>0;i--) cout << a[i] << " ";
	cout << a[0] << endl;
}

#include<iostream>
using namespace std;

int a[10];
int b[10];

int score(int x[10]){
  int a=0,b=0,c=0,tmp;

  for(int i=0;i<10;i++){
    if(a < x[i]){
      tmp = a;
      a = x[i];
      x[i] = tmp;
    }
    if(b < x[i]){
      tmp = b;
      b = x[i];
      x[i] = tmp;
    }
    if(c < x[i]){
      tmp = c;
      c = x[i];
      x[i] = tmp;
    }
  }
  return a + b + c;
}
int main(){
  for(int i=0;i<10;i++)cin >> a[i];
  for(int i=0;i<10;i++)cin >> b[i];

  int sa = score(a);
  int sb = score(b);

  cout << sa << " " << sb << endl;

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#include <math.h>
#include <iomanip>
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const int INF=1e9;
const int mod = 1000000007;

int main(){
  int N,L;
  cin>>N>>L;
  int MIN=INF,sum=0;
  for(int i=1;i<=N;i++){
    sum+=L+i-1;
  }
  int ans=0;
  for(int i=1;i<=N;i++){
    int x=sum-(L+i-1);
    if(abs(sum-x)<MIN){MIN=abs(sum-x);ans=x;}
  }
  cout<<ans<<endl;
  return 0;
}

#include<bits/stdc++.h>
using namespace std;
const long long INF = 1LL << 60;
template<class T> inline bool chmax(T& a, T b){if(a<b){a=b;return 1;}return 0;}
template<class T> inline bool chmin(T& a, T b){if(a>b){a=b;return 1;}return 0;}
#define rep(i,n) for(int i=0;i<n;i++)
#define all(a)  (a).begin(),(a).end()
#define P pair<int, int>
#define ll long long
const int MOD = 1000000007;
//bitset<64>(x).count()
//fixed << setprecision(15) <<

int main(){
  int n, l;
  cin >> n >> l;
  ll ans = (n*(n+2*l-1))/2;
  if(l>=0)
    ans -= l;
  else
    if(n+l-1<0)
      ans -=n+l-1;
  cout << ans << endl;
}


#define _USE_MATH_DEFINES
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <map>
#include <iomanip>
#include <deque>
#include <set>
#define rep(i,a,b) for(int i=a;i<b;++i)
#define rrep(i,a,b) for(int i=a;i>=b;--i)
#define fore(i,a) for(auto &i:a)
#define INF INT_MAX/2;
typedef long long ll;
using namespace std;
using vi = vector<int>;
using vll = vector<ll>;
ll mod = 1e9 + 7;
//ll llmax = 10000000000000000000ll;
using namespace std;
using Graph = vector<vector<int>>;

int cnt_digit(ll N)
{
	int digit = 0;

	while (N > 0)
	{
		N /= 10;
		digit++;
	}
	return digit;
}

 // 最大公約数計算

ll gcd(ll a, ll b)
{
	if (b == 0) {
		return a;
	}
	else {
		return gcd(b, a % b);
	}
}
// 最小公倍数の計算

ll lcm(ll a, ll b)
{
	ll g = gcd(a, b);

	return a / g * b;
}

struct union_find
{
	vector<int> par, r;
	union_find(int n)
	{
		par.resize(n);
		r.resize(n);
		init(n);
	}

	void init(int n)
	{
		for (int i = 0; i < n; i++) par[i] = i;
		for (int i = 0; i < n; i++) r[i] = 0;
	}

	int find(int x)
	{
		if (par[x] == x)return x;
		else return find(par[x]);
	}

	void unite(int x, int y)
	{
		x = find(x);
		y = find(y);

		if (r[x] < r[y])
		{
			par[x] = y;
		}
		else
		{
			par[y] = x;
			if (r[x] == r[y])
			{
				r[x]++;
			}
		}
	}
};

int main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n;
	cin >> n;
	vector<int> data(n + 2);
	vector<int> ans(n);
	data[0] = data[n + 1] = 0;
	rep(i, 1, n + 1) {
		cin >> data[i];
	}

	int orians = 0;
	rep(i, 0, n + 1) {
		orians += abs(data[i + 1] - data[i]);
	}

	rep(i, 0, n) {
		ans[i] = orians - abs(data[i + 1] - data[i]) - abs(data[i + 2] - data[i + 1]) + abs(data[i + 2] - data[i]);
	}

	rep(i, 0, n) {

		cout << ans[i] << endl;
	}

}

#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
#define initdp(a, b)  for (int i = 0; i <= a; i++)   for (int j = 0; j <= b; j++) dp[i][j] = -1;
#define pb push_back
#define mpr make_pair
#define pii pair<int, int>
#define ll long long
#define pll pair<ll, ll>
#define all(arr) arr.begin(), arr.end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repd(i, n) for (int i = n - 1; i >= 0; i--)
#define fo(i, l, r) for (int i = l; i <= r; i++)
#define INF 1000000001
#define inf1 1000000000000000001
#define mod 1000000007
#define pie 3.14159265358979323846264338327950L
#define N 1000005
#define mid(l, r) l + (r - l) / 2
#define vec vector<int>
#define vecl vector<ll>
#define umap unordered_map<ll,ll>
#define yes cout << "YES" << endl;
#define no cout << "NO" << endl;
#define endl "\n"
using namespace std;

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

void solve(){

  int n,M;
    cin>>n>>M;
    int a,b;
    if(M%2==0){
        a=M/2;
        b=M/2;
    }else{
        a=M/2;
        b=M-a;
    }
    for(int i=1;i<=a;++i){
        cout<<i<<" "<<2*a+2-i<<endl;
    }
    for(int i=1;i<=b;++i){
        cout<<i+2*a+1<<" "<<M*2+2-i<<endl;
    }

}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cout<<fixed<<setprecision(20);
    int t = 1;
   //  cin >> t;
    while (t--)
    {
        solve();
    }
}


#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <math.h>

//#define PI 3.141592653589

using namespace std;

int main()
{
	int n;
	long seisuu[3];

	cin >> n;
	for (int i=0; i<n; i++) cin >> seisuu[i];
	sort(seisuu, seisuu+n);

	for (int i=1; i<=seisuu[0]; i++)
	{
		if (n == 2)
		{
			if (seisuu[0]%i==0 && seisuu[1]%i==0)
			{
				cout << i << endl;
			}
		} else {
			if (seisuu[0]%i==0 && seisuu[1]%i==0 && seisuu[2]%i==0)
			{
				cout << i << endl;
			}
		}
	}
	//for (int i=0; i<n; i++) cout << seisuu[i] << endl;

	return 0;
}

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

const string YES = "POSSIBLE";
const string NO = "IMPOSSIBLE";

int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
void solve(long long N, long long K, std::vector<long long> A)
{
    long long gc = 0, ma = 0;
    for (int i = 0; i < N; i++)
        gc = gcd(A[i], gc), ma = max(ma, A[i]);
    if (K % gc)
    {
        cout << NO << endl;
        return;
    }
    if (K > ma)
    {
        cout << NO << endl;
        return;
    }
    cout << YES << endl;
}

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
int main()
{
    long long N;
    scanf("%lld", &N);
    long long K;
    scanf("%lld", &K);
    std::vector<long long> A(N);
    for (int i = 0; i < N; i++)
    {
        scanf("%lld", &A[i]);
    }
    solve(N, K, std::move(A));
    return 0;
}


#include <iostream>
#include <complex>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <numeric>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <cmath>
#include <bitset>
#include <cassert>
#include <queue>
#include <stack>
#include <deque>
#include <random>
#include <iomanip>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> P;
typedef pair<P,P> PP;
const ll MOD=998244353;
const ll MAX_N=500010;
const ll INF=100100100100100;

int main(){
    int n,k;
    cin>>n>>k;
    vector<ll> v(n);
    vector<ll> sum(n+1);
    ll s=0;
    for(int i=0;i<n;i++){
        cin>>v[i];
        sum[i+1]=sum[i]+v[i];
        s+=v[i];
    }
    ll ans=-INF;
    for(int l=0;l<=n;l++){
        for(int r=0;r<=n;r++){
            if(l>r) continue;
            ll buf=0;
            int life=k;
            vector<ll> q(0);
            for(int i=0;i<l;i++){
                buf+=v[i];
                q.push_back(v[i]);
                life--;
            }
            for(int i=r;i<n;i++){
                buf+=v[i];
                q.push_back(v[i]);
                life--;
            }
            if(life<0) continue;
            sort(q.begin(), q.end());
            int m=q.size();
            for(int i=0;i<min(life,m);i++){
                if(q[i]>=0) break;
                buf-=q[i];
            }
            ans=max(ans,buf);
        }
    }
    cout<<ans<<endl;
}

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int main()
{
	long long mod=(1e+9)+7;
	long long n,m;
	cin>>n>>m;
	static long long sum_x,sum_y;
	vector<long long> x(n+10),y(m+10);
	for(ll i=1;i<=n;i++) cin>>x[i];
	for(ll i=1;i<=m;i++) cin>>y[i];
	for(ll xx=1;xx<=n;xx++){
		sum_x+=(2*xx-n-1)*x[xx];
		sum_x%=mod;
	}
	for(ll yy=1;yy<=m;yy++){
		sum_y+=(2*yy-m-1)*y[yy];
		sum_y%=mod;
	}
	cout<<(sum_x*sum_y)%mod<<endl;
}


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

using namespace std;

#define rep(i,N) for(int i=0;i<N;i++) //0から
#define REP(i,N) for(int j=i;j<N;j++) //iから

typedef long long ll;

ll binary_search(vector<ll> a, ll n, ll key){
    ll right = n, left = -1;
    ll md = (right + left) / 2;

    while(right - left > 1){
        if(a[md] <= key){
            right = md;
        }else{
            left = md;
        }
        md = (right + left) / 2;
    }
    if(left == -1) return -1; //無い場合
    return right;
}

vector<ll> prime;
void Prime(ll n){ //線形篩,素数列挙
    vector<ll> p(n,0);
    p[0]=1;
    p[1]=1;
    for(ll i=2;i<n;i++){
        if(p[i]==0){
            prime.push_back(i);
            p[i]=i;
        }
        ll k=prime.size();
        for(ll j=0;j<k && i*prime[j]<n && prime[j]<=p[i];j++){
            p[i*prime[j]]=prime[j];
        }
    }
}

#define MOD ((ll)1e+9 + 7)

//cout<<fixed<<setprecision(10);
int main(){
    int n,m;
    cin>>n>>m;
    vector<int> L(m),R(m+1);
    rep(i,m){
        cin>>L[i]>>R[i];
        L[i]--;
    }
    vector<int> cnt(n+1,0);
    rep(i,m){
        cnt[L[i]]++;
        cnt[R[i]]--;
    }
    rep(i,n){
        cnt[i+1]+=cnt[i];
    }
    int ans=0;
    rep(i,n){
        if(cnt[i]==m){
            ans++;
        }
    }
    cout<<ans<<endl;

    return 0;
}

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

#define ll long long
#define inf 0x3f3f3f3f
#define infll 0x3f3f3f3f3f3f3f3f
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define ii pair<int,int>
#define vii vector<ii>
#define vi vector<int>

#define F_OR(i, a, b, s) for (int i=(a); (s)>0?i<(b):i>(b); i+=(s))
#define F_OR1(e) F_OR(i, 0, e, 1)
#define F_OR2(i, e) F_OR(i, 0, e, 1)
#define F_OR3(i, b, e) F_OR(i, b, e, 1)
#define F_OR4(i, b, e, s) F_OR(i, b, e, s)
#define GET5(a, b, c, d, e, ...) e
#define F_ORC(...) GET5(__VA_ARGS__, F_OR4, F_OR3, F_OR2, F_OR1)
#define FOR(...) F_ORC(__VA_ARGS__)(__VA_ARGS__)
#define EACH(x, a) for (auto& x: a)

// debug templates
#define debug(x)       cerr<< #x <<" : "<< x << endl;
#define debuga(A,N)    cerr<< #A <<" : [";for(int i = 0; i<N;i++) cerr<<A[i]<<" "; cerr<<"]\n";
#define debuga2(A,N,M) cerr<< #A << " : \n"; for(int i=0;i<N;i++){cerr<<"[";for(int j=0;j<M;++j) cerr<<A[i][j]<<" ";cerr<<"]\n";}
#define debugp(p)      cerr<< #p <<" : "<<"("<<(p).first<<","<<(p).second<<")\n";
#define debugv(v)      cerr<< #v <<" : "<<"[";for(int i = 0; i< (v).size(); i++) cerr<<v[i]<<" "; cerr<<"]\n";
#define debugv2(v)     cerr<< #v << " : \n"; for(int i=0;i<v.size();i++){cerr<<"[";for(int j=0;j<(v[0].size());++j) cerr<<v[i][j]<<" ";cerr<<"]\n";}
#define debugs(m)      cerr<< #m <<" : [ "; for(auto itr = m.begin(); itr!=m.end();itr++) cerr<<*itr<<" "; cerr<<"]\n";
#define debugm(m)      cerr<< #m <<" : [ "; for(auto itr = m.begin();itr!=m.end(); itr++) cerr<<"("<<itr->first<<","<<itr->second<<") ";cerr<<"]\n";

void solve(){
	int n,m; cin >> n >> m;
	int big_l = 0, small_r = inf;
	FOR(m){
		int l,r; cin >> l >> r;
		big_l = max(l,big_l);
		small_r = min(small_r, r);
	}
	cout << max(small_r - big_l + 1,0);
}

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

	solve();
	return 0;
}


#include <bits/stdc++.h>
#define int long long
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define F first
#define S second

using namespace std;
typedef pair<int,int> P;
typedef vector<int> ivec;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }

const int MOD=1000000007;
int INF=100100100100100;
int sa[100010];
signed main(){
  int n;cin>>n;
  int ans=0;
  rep(i,n){
    int a,b;cin>>a>>b;
    sa[i]=a+b;
    ans-=b;
  }
  sort(sa,sa+n,greater<int>());
  for(int i=0;i<n;i+=2){
    ans+=sa[i];
  }
  cout<<ans<<endl;
  return 0;
}

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

int main() {
  int N;
  cin>>N;
  vector<long> a(N);
  vector<long> b(N);
  for(int i=0;i<N;i++) cin>>a[i]>>b[i];
  vector<vector<long>> c(N,vector<long>(2));
  for(int i=0;i<N;i++) c[i][0]=a[i]+b[i],c[i][1]=i;
  sort(c.begin(),c.end());
  reverse(c.begin(),c.end());
  long A=0,B=0;
  for(int i=0;i<N;i++){
    if(i%2) B+=b[c[i][1]];
    else A+=a[c[i][1]];
  }
  cout<<A-B<<endl;
}


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

using namespace std;

int cnt = 0;
int a[1000500];

void insertsort(int a[], int n, int g){
	for (int i = g; i < n; i++){
		int v = a[i];
		int j = i - g;
		while (j >= 0 && a[j] > v){
			a[j + g] = a[j];
			j -= g;
			cnt++;
		}
		a[j + g] = v;
	}
}

int main(void)
{
	int n;

	cin >> n;

	for (int i = 0; i < n; i++){
		scanf("%d", &a[i]);
	}

	// 3g + 1
	vector <int> kotori;
	for (int g = 1; g <= n; g = 3 * g + 1){
		kotori.push_back(g);
	}

	for (int i = kotori.size() - 1; i >= 0; i--){
		insertsort(a, n, kotori[i]);
	}

	reverse(kotori.begin(), kotori.end());
	cout << kotori.size() << endl;
	for (int i = 0; i < kotori.size(); i++){
		cout << kotori[i] << (i + 1 == kotori.size() ? '\n' : ' ');
	}
	cout << cnt << endl;

	for (int i = 0; i < n; i++){
		cout << a[i] << endl;
	}

	return 0;
}

#include<stdio.h>
#include<algorithm>
using namespace std;
int a[200000];
int main()
{
	int i,n,flag=0;
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
	  scanf("%d",&a[i]);
	}
	sort(a,a+n);
	for(i=0;i<n-1;i++)
	{
	   if(a[i]==a[i+1])
	   {
	      flag=1;
	      break;
	   }
	}
	if(flag==1)
	{
		printf("NO\n");
	}
	else
	{
		printf("YES\n");
	}
	return 0;
 }


#include <iostream>
#include <vector>
#include <map>
using namespace std;
const int MAX = 410000;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];
//invの要素にはiの逆元が格納されている
//前処理
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;//i!(mod MOD)を計算
        inv[i] = MOD - inv[MOD%i]* (MOD / i) % MOD;//iの逆元を計算
        finv[i] = finv[i - 1] * inv[i] % MOD;//i!(mod MOD)の逆元を計算
    }
}

// 二項係数計算
long long COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;//nCk = n!/(k!*(n-k)!)を計算
}
//例のアレ
long long nPm(int n, int m){
    if(n < m) return 0;
    if(n < 0 || m < 0) return 0;
    return fac[n] * finv[n - m] % MOD;
}
map<long long, long long> divide(long long n){//素因数分解
    map<long long, long long> res;
    for(long long i = 2; i * i <= n; i++){
        while(n % i == 0){
            res[i]++;
            n /= i;
        }
    }
    if(n != 1) res[n] = 1;
    return res;
}
int main(){
    int N;
    long long M;
    cin >> N >> M;
    COMinit();
    map<long long, long long> m = divide(M);
    long long ans = 1;
    for(auto v: m){
        ans = ans * COM(v.second + N - 1, v.second) % MOD;
    }
    cout << ans << endl;
}

#include<bits/stdc++.h>
using namespace std;
int n,lst[1000100][26],gg[1000100];
char s[1000100],t[1000100];

struct treaps
{
	int son[1201000][2],size[1201000],num[1201000],tag[1201000],rd[1200100],tt;
	void update(int s){size[s]=size[son[s][0]]+size[son[s][1]]+1;}
	int build(int n)
	{
		if (n==0) return 0;
		int s=++tt;size[s]=1;
		n--;
		son[s][0]=build(n/2);son[s][1]=build(n-n/2);
		update(s);
		return s;
	}
	inline void pushdown(int s)
	{
		if (son[s][0]) {tag[son[s][0]]+=tag[s];num[son[s][0]]+=tag[s];}
		if (son[s][1]) {tag[son[s][1]]+=tag[s];num[son[s][1]]+=tag[s];}
		tag[s]=0;
	}

	void addtag(int s) {tag[s]++;num[s]++;}
	int merge(int s,int t)
	{
		if (!s) return t;
		if (!t) return s;
		pushdown(s);pushdown(t);
		if ((rand()*32768+rand())%(size[s]+size[t])<size[s]) {son[s][1]=merge(son[s][1],t);update(s);return s;}
		else {son[t][0]=merge(s,son[t][0]);update(t);return t;}
	}
	pair<int,int> split(int s,int k)
	{
		if (!s) return pair<int,int>(0,0);
		pushdown(s);
		if (size[son[s][0]]>=k)
		{
			pair<int,int> ls=split(son[s][0],k);
			son[s][0]=ls.second;update(s);
			return pair<int,int>(ls.first,s);
		}
		pair<int,int> ls=split(son[s][1],k-size[son[s][0]]-1);
		son[s][1]=ls.first;update(s);
		return pair<int,int>(s,ls.second);
	}
	int findkth(int s,int k)
	{
		while (1)
		{
			pushdown(s);
			if (size[son[s][0]]>=k) s=son[s][0];
			else if (size[son[s][0]]+1==k) return num[s];
			else {k-=(size[son[s][0]]+1);s=son[s][1];}
		}
	}
}treap;
int main()
{
	//freopen("in.txt","r",stdin);
	scanf("%d\n%s\n%s",&n,s+1,t+1);
	bool err=false;
	for (int i=1;i<=n;i++)
	{
		for (int j=0;j<26;j++) lst[i][j]=lst[i-1][j];
		lst[i][s[i]-'a']=i;
		if (s[i]!=t[i]) err=true;
	}
	if (err==false) {printf("0\n");return 0;}
	int root=treap.build(n),ans=1,rem=n;
	for (int i=n;i>=1;i--) if (t[i]!=t[i-1])
	{//if (i%10000==0) cerr<<i<<' '<<treap.size[root]<<endl;
		//for (int j=1;j<=i;j++) cout<<treap.findkth(root,j)<<' ';cout<<endl;
		int w=treap.findkth(root,i);
		if ((w!=0)||(s[i]!=t[i])) ans=max(ans,w+2);
		int d=lst[min(i-(w!=0),rem)][t[i]-'a'];rem=d-1;
			if (d==0) {printf("-1\n");return 0;}
			pair<int,int> p=treap.split(root,d-1);
			pair<int,int> q=treap.split(p.second,1);
			treap.addtag(q.second);
			root=treap.merge(p.first,q.second);

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


// ░░░░░░░░( •̪●)░░░░░░░░░░░░░░░░░░░░░░░░
// ░░░░░░███████ ]▄▄▄▄▄▄▄▄▃░░░▃░░░░ ▃░
// ▂▄▅█████████▅▄▃▂░░░░░░░░░░░░░░░░░
// [███████████████████].░░░░░░░░░░░░░░
// ◥⊙▲⊙▲⊙▲⊙▲⊙▲⊙▲⊙◤...░░░░░░░░░░░░

//PointBlank's code (⌐■_■)

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

ll power(ll x, ll y);
#define repp(I, A, B) for(int I = A; I <= B; I++)
#define rep(I, A, B) for(int I = A; I < B; I++)
#define rrep(I, B, A) for(int I = B; I >= A; I--)
#define pb emplace_back
#define ff first
#define ss second
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define LB lower_bound
#define UB upper_bound
#define MP make_pair
#define mem(A, B) memset(A, B, sizeof(A));
#define mem0(A) memset(A, 0, sizeof(A));
#define quickio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define debug(this) cerr<<"> "<<#this<<" : "<<this<<"\n"
#define bitcount(n) __builtin_popcountll(n)
#define in_edges(M) repp(I, 1, M){int A, B; cin >> A >> B; v[A].pb(B), v[B].pb(A);}
#define in_edges2(M) repp(I, 1, M){int A, B; cin >> A >> B; v[A].pb(B);}
#define endl "\n"

const ll MOD = 1000000007;

vector<int> en[505];
int n, m, q;

int main() //PointBlank's code ¯\_(ツ)_/¯
{
    quickio
    cin >> n >> m >> q;
    repp(i, 1, m){
        int l, r;
        cin >> l >> r;
        en[l].pb(r);
    }

    repp(i, 0, 500) sort(all(en[i]));

    while(q--){
        int l, r;
        cin >> l >> r;
        ll res = 0;
        repp(i, l, r) res += UB(all(en[i]), r) - en[i].begin();
        cout << res << endl;
    }
}

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

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

int main()
{
    int n, m, q;
    cin >> n >> m >> q;

    vector<vector<int>> table(n + 1, vector<int>(n + 1, 0));
    while(m--)
    {
        int l,r;
        cin >> l >> r;
        table[l][r]++;
    }
    rep(i, n) rep(j, n) table[i + 1][j + 1] += table[i + 1][j] + table[i][j + 1] - table[i][j];

    while (q--)
    {
        int s, t;
        cin >> s >> t;

        cout << table[t][t] - table[s - 1][t] - table[t][s - 1] + table[s - 1][s - 1] << endl;
    }
}


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

template <typename _Tp> inline _Tp read(_Tp&x){
	char c11=getchar();x=0;while(!isdigit(c11))c11=getchar();
	while(isdigit(c11))x=x*10+c11-'0',c11=getchar();return x;
}

int n,m,tot,a[105];

int main(){
	read(n),read(m);
	for(rg int i=1;i<=m;i++)read(a[i]),tot+=(a[i]&1);
	if(tot>2){puts("Impossible");return 0;}
	if(tot){
		bool flag=0;
		for(rg int i=1;i<m;i++)
			if(a[i]&1)
				if(flag)swap(a[i],a[m]);
				else flag=1,swap(a[i],a[1]);
	}
	if(m==1){
		if(a[1]==1){puts("1\n1\n1");return 0;}
		printf("%d\n",a[1]);puts("2");
		printf("1 %d\n",a[1]-1);
		return 0;
	}
	for(rg int i=1;i<=m;i++)printf("%d ",a[i]);
	if(a[m]==1){
		printf("\n%d\n",m-1);
		printf("%d\n",a[1]+1);
		for(rg int i=2;i<m;i++)printf("%d ",a[i]);
	}
	else {
		printf("\n%d\n",m);
		printf("%d\n",a[1]+1);
		for(rg int i=2;i<m;i++)printf("%d ",a[i]);
		printf("%d\n",a[m]-1);
	}return 0;
}

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

class Dice{
public:
  int value[6];
  void roll(char _order) {
    switch(_order) {
    case 'N':
      swap(value[4], value[0]);
      swap(value[0], value[1]);
      swap(value[1], value[5]);
      break;
    case 'E':
      swap(value[2], value[0]);
      swap(value[0], value[3]);
      swap(value[3], value[5]);
      break;
    case 'S':
      swap(value[1], value[0]);
      swap(value[0], value[4]);
      swap(value[4], value[5]);
      break;
    case 'W':
      swap(value[0], value[2]);
      swap(value[2], value[5]);
      swap(value[5], value[3]);
      break;
    }
  }
  void rotate(void) {
    this->roll('N');
    this->roll('E');
    this->roll('S');
  }
  bool operator==(const Dice other) {
    bool res = true;
    for (int knock=1;knock<=30;knock++) {
      res = true;
      for (int i=0;i<6;i++)
        if (other.value[i] != this->value[i])
          res = false;
      if (res)
        break;
      else {
        if (knock % 5 == 0) {
          if (knock / 5 <= 3) {
            this->roll('N');
          } else if (knock / 5 == 4) {
            this->roll('E');
          } else {
            this->roll('E');
            this->roll('E');
          }
        } else {
          this->rotate();
        }
      }
    }
    return res;
  }
};

int main() {
  int n;
  Dice dice[100];
  cin >> n;
  for (int i=0;i<n;i++)
    for (int j=0;j<6;j++)
      cin >> dice[i].value[j];

  bool res = true;
  for (int i=0;i<n;i++)
    for (int j=i+1;j<n;j++)
      if (dice[i] == dice[j])
        res = false;

  if (res)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}


#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
#define M 1000020
using namespace std;
int read(){
	int nm=0,fh=1; char cw=getchar();
	for(;!isdigit(cw);cw=getchar()) if(cw=='-') fh=-fh;
	for(;isdigit(cw);cw=getchar()) nm=nm*10+(cw-'0');
	return nm*fh;
}
int n,m,ans,s[M],dt;
char S[M],ch[M];
bool fg=false;
int main(){
	n=read(),scanf("%s%s",S+1,ch+1);
	for(int last=n,i=n;i>0;i--,fg=false){
		m+=s[i+dt];
		while(last>i||(S[last]!=ch[i]&&last)) last--,fg=true;
		if(last<=0){puts("-1");return 0;}
		if(fg&&last<i) m++,s[i+dt]=0,s[last+dt]--,dt++;
		ans=max(ans,m);
	}
	printf("%d\n",ans); return 0;
}

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <iomanip>
#include <utility>
#include <tuple>
#include <functional>
#include <bitset>
#include <cassert>
#include <complex>
#include <stdio.h>
#include <time.h>
#include <numeric>
#define all(a) a.begin(),a.end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef long double ld;
typedef complex<ld> com;
constexpr ll INF = 1000000000000000000;
constexpr ld EPS = 1e-12;
constexpr ld PI = 3.141592653589793238;
template<class T, class U> inline bool chmax(T &a, const U &b) { if (a < b) { a = b; return true; } return false; }
template<class T, class U> inline bool chmin(T &a, const U &b) { if (a > b) { a = b; return true; } return false; }

constexpr ll mod = 1000000007;
constexpr ll modsize = 2000;
vector<ll> fac(modsize);
vector<ll> inv(modsize);
vector<ll> facinv(modsize);

void modcalc() {
	if (modsize == 0) abort();
	fac[0] = 1; fac[1] = 1; inv[1] = 1;
	facinv[0] = 1; facinv[1] = 1;
	for (ll i = 2; i < modsize; i++) {
		fac[i] = fac[i - 1] * i % mod;
		inv[i] = mod - inv[mod % i] * (mod / i) % mod;
		facinv[i] = facinv[i - 1] * inv[i] % mod;
	}
}

ll modinv(ll a) {
	if (a == 0) abort();
	ll b = mod, u = 1, v = 0;
	while (b) {
		ll t = a / b;
		a -= t * b; swap(a, b);
		u -= t * v; swap(u, v);
	}
	u %= mod;
	if (u < 0) u += mod;
	return u;
}

ll modpow(ll a, ll b) {
	ll ans = 1;
	a %= mod;
	while (b) {
		if (b & 1) ans = ans * a % mod;
		a = a * a % mod;
		b >>= 1;
	}
	return ans;
}

ll modcomb(ll n, ll k) {
	if (n < 0 || k < 0 || n < k) return 0;
	return fac[n] * facinv[k] % mod * facinv[n - k] % mod;
}

ll modperm(ll n, ll k) {
	if (n < 0 || k < 0 || n < k) return 0;
	return fac[n] * facinv[n - k] % mod;
}

ll modhom(ll n, ll k) {
	if (n < 0 || k < 0 || n == 0 && k > 0) return 0;
	if (n == 0 && k == 0) return 1;
	return fac[n + k - 1] * facinv[k] % mod * facinv[n - 1] % mod;
}

ll dp[1010][1010];

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

	modcalc();
	ll n, a, b, c, d;
	cin >> n >> a >> b >> c >> d;
	dp[0][a] = 1;
	for (int j = a; j <= b; j++) {
		rep(i, n + 1) {
			dp[i][j + 1] += dp[i][j];
			if (dp[i][j + 1] > mod) dp[i][j + 1] -= mod;
			int st = i;
			for (int k = c; k <= d; k++) {
				if (i + j * k > n) break;
				dp[i + j * k][j + 1] += dp[i][j] * fac[n - i] % mod * modpow(facinv[j], k) % mod * facinv[n - i - j * k] % mod * facinv[k] % mod;
				if (dp[i + j * k][j + 1] > mod) dp[i + j * k][j + 1] -= mod;
			}
		}
	}
	cout << dp[n][b + 1] << endl;
}