#include<bits/stdc++.h> //Ithea Myse Valgulious
namespace chtholly{
typedef long long ll;
#define re0 register int
#define rel register ll
#define rec register char
#define gc getchar
//#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<23,stdin),p1==p2)?-1:*p1++)
#define pc putchar
#define p32 pc(' ')
#define pl puts("")
/*By Citrus*/
char buf[1<<23],*p1=buf,*p2=buf;
inline int read(){
  int x=0,f=1;char c=gc();
  for (;!isdigit(c);c=gc()) f^=c=='-';
  for (;isdigit(c);c=gc()) x=(x<<3)+(x<<1)+(c^'0');
  return f?x:-x;
  }
template <typename mitsuha>
inline bool read(mitsuha &x){
  x=0;int f=1;char c=gc();
  for (;!isdigit(c)&&~c;c=gc()) f^=c=='-';
  if (!~c) return 0;
  for (;isdigit(c);c=gc()) x=(x<<3)+(x<<1)+(c^'0');
  return x=f?x:-x,1;
  }
template <typename mitsuha>
inline int write(mitsuha x){
  if (!x) return 0&pc(48);
  if (x<0) pc('-'),x=-x;
  int bit[20],i,p=0;
  for (;x;x/=10) bit[++p]=x%10;
  for (i=p;i;--i) pc(bit[i]+48);
  return 0;
  }
inline char fuhao(){
  char c=gc();
  for (;isspace(c);c=gc());
  return c;
  }
}using namespace chtholly;
using namespace std;
const int yuzu=2e5;
typedef int fuko[yuzu|10];
fuko a,b;
int main() {
  int n,k,i,t;
  read(n),read(k);
  for (i=1;i<=n;++i) a[i]=read();
  for (t=1;t<=min(k,50);++t) {
    memset(b,0,sizeof b);
    for (i=1;i<=n;++i)
      b[max(1,i-a[i])]++,b[min(n,i+a[i])+1]--;
    for (i=1;i<=n;++i) b[i]+=b[i-1];
    memcpy(a,b,sizeof b);
  }
  for (i=1;i<=n;++i) printf("%d ",b[i]);
}

#include <iostream>

#define N 1000

using namespace std;

int main(){
	int data[N];
	int n , sum[N];
	int e[N] , p , q;

	while(1){
		int flag = 0;

		cin >> n;
		if(n == 0) break;

		for(int i = 0; i < n; i++){
			cin >> e[i] >> p >> q;
			sum[i] = p * q;

		}for(int i = 0; i < n; i++){
			for(int j = i + 1; j < n; j++){
				if(e[i] == e[j]){
					sum[i] += sum[j];
				}
			}
		}for(int i =  0; i < n; i++){
			if(sum[i] >= 1000000){
				flag = 1;
				cout << e[i] << endl;
			}
		}if(flag == 0){
			cout << "NA" << endl;
		}
	}

	return 0;
}


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

int main(void) {
    int N;
    cin >> N;
    vector<int> A(N);
    for (int i = 0; i < N; i++)
    {
        cin >> A[i];
    }

    deque<int> d;

    for (int i = 0; i < N; i++)
    {
        int pos = lower_bound(d.begin(), d.end(), A[i]) - d.begin();
        if(pos == 0){
            d.push_front(A[i]);
        } else {
            d[pos-1] = A[i];
        }
    }

    int ans = d.size();

    cout << ans << endl;

    return 0;
}

#include <iostream>
#include <algorithm>
using namespace std;
int a[100005],b[100005],ans[100005];
int low[100005];
int main(){
	int n,i,len,fin;
	int x;
	scanf("%d",&n);
	for(i=1;i<=n;i++)
		scanf("%d",&b[i]);
	for(i=1;i<=n;i++){
        a[i]=b[n-i+1];
	}

	len=0; low[++len]=a[1]; ans[1]=len;
	for(i=2;i<=n;i++){//遍历a[i]
		if(a[i]>=low[len])
			low[++len]=a[i];
		else
			*lower_bound(low+1,low+1+len,a[i]+1)=a[i];//第一个大于a[i]的数，替换掉
		ans[i]=len;
	}
	/*
	for(i=1,x=0;i<=n;i++){
        printf("%d ",ans[i]);
	}
	*/
	printf("%d\n",ans[n]);
	return 0;
}


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

int main() {

	int n; cin >> n;
	string a, b, c; cin >> a >> b >> c;
	int ans = 0;
	for (int i = 0; i < n; i++) {
		if (a[i] != b[i] && b[i] != c[i] && c[i] != a[i]) {
			ans += 2;
		}
		else if ((a[i] == b[i] && b[i] != c[i]) || (b[i] == c[i] && c[i] != a[i]) || (c[i] == a[i] && a[i] != b[i])) {
			ans++;
		}
	}
	cout << ans << endl;
	return 0;
}

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

int main()
{
	int n;
	cin >> n;
	string s[3];
	for(int i  =0;i<3;i++)cin >> s[i];
	int ans = 0;
	for(int i = 0;i<n;i++)
	{
		int cnt = 0;
		for(int j = 0;j<3;j++)
		{
			if(s[j%3][i]!=s[(j+1)%3][i])cnt++;
		}
		if(cnt==2)ans+=1;
		if(cnt==3)ans +=2;
	}
	cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define MOD (int)1e9+7
#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 vi vector<int>
#define vl vector<ll>
#define ld long double
#define vvi vector<vector<int>>
#define vvl vector<vector<long long>>
#define pii pair<int,int>
#define pll pair<long,long>
#define vpii vector<pii>
#define vpll vector<pll>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define PI 3.1415926535897932384626433832795
#define fix(f,n) fixed<<setprecision(n)<<f
#define all(x) x.begin(),x.end()

ll powM(ll a,ll b,ll m )
{
  a%=m;
  ll ans=1;
  while(b>0)
  {
    if(b&1)ans=ans*a%m;
    a=a*a%m;
    b>>=1;
  }

return ans;
}

ll c(ll a,ll b)
{
  if(a==0)return b==0?1:0;

if(b>a)return 0;

  ll val=1;
ll fac=1;
for(int i=1;i<=b;i++)fac*=i;
for(int i=a;i>=a-b+1;i--)val*=i;

  return val/fac;

}

ll f(ll n)
{
  ll ans=1LL;
  while(ans<=n)ans<<=1;

return ans;
}

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

ll n,k;
cin>>n>>k;
ll ans=0;
if(n%k)ans=1;

cout<<ans<<"\n";

return 0;

}

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

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

  n % k == 0 ? cout << 0 << endl : cout << 1 << endl;
}


#include <bits/stdc++.h>
using namespace std;
string str;
int main()
{
    cin >> str;
    int len = str.length();
    len = len - 8;
    for (int i = 0;i <= len-1;i++)
    {
        cout << str[i];
    }
    cout << endl;
    return 0;
}

#include<iostream>
#include<string>
using namespace std;
string s;
int main()
{
	getline(cin,s);
	s.erase(s.length()-8,s.length()-1);
	cout<<s<<endl;
	return 0;
}


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

#define ll long long
#define ld long double
#define ar array

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

template <typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define vt vector
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define sz(x) (int)(x).size()

#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)

template<class A> void read(vt<A>& v);
template<class A, size_t S> void read(ar<A, S>& a);
template<class T> void read(T& x) {
	cin >> x;
}
void read(double& d) {
	string t;
	read(t);
	d=stod(t);
}
void read(long double& d) {
	string t;
	read(t);
	d=stold(t);
}
template<class H, class... T> void read(H& h, T&... t) {
	read(h);
	read(t...);
}
template<class A> void read(vt<A>& x) {
	EACH(a, x)
		read(a);
}
template<class A, size_t S> void read(array<A, S>& x) {
	EACH(a, x)
		read(a);
}

string to_string(char c) {
	return string(1, c);
}
string to_string(bool b) {
	return b?"true":"false";
}
string to_string(const char* s) {
	return string(s);
}
string to_string(string s) {
	return s;
}
string to_string(vt<bool> v) {
	string res;
	FOR(sz(v))
		res+=char('0'+v[i]);
	return res;
}

template<size_t S> string to_string(bitset<S> b) {
	string res;
	FOR(S)
		res+=char('0'+b[i]);
	return res;
}
template<class T> string to_string(T v) {
    bool f=1;
    string res;
    EACH(x, v) {
		if(!f)
			res+=' ';
		f=0;
		res+=to_string(x);
	}
    return res;
}

template<class A> void write(A x) {
	cout << to_string(x);
}
template<class H, class... T> void write(const H& h, const T&... t) {
	write(h);
	write(t...);
}
void print() {
	write("\n");
}
template<class H, class... T> void print(const H& h, const T&... t) {
	write(h);
	if(sizeof...(t))
		write(' ');
	print(t...);
}

void DBG() {
	cerr << "]" << endl;
}
template<class H, class... T> void DBG(H h, T... t) {
	cerr << to_string(h);
	if(sizeof...(t))
		cerr << ", ";
	DBG(t...);
}
#ifdef _DEBUG
#define dbg(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define dbg(...) 0
#endif

template<class T> void offset(ll o, T& x) {
	x+=o;
}
template<class T> void offset(ll o, vt<T>& x) {
	EACH(a, x)
		offset(o, a);
}
template<class T, size_t S> void offset(ll o, ar<T, S>& x) {
	EACH(a, x)
		offset(o, a);
}

mt19937 mt_rng(chrono::steady_clock::now().time_since_epoch().count());
ll randint(ll a, ll b) {
	return uniform_int_distribution<ll>(a, b)(mt_rng);
}
const int d4i[4]={-1, 0, 1, 0}, d4j[4]={0, 1, 0, -1};
const int d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1};
ll mod=1e9+7;
const int N=1e6+5;
const ld PI=2*acos(0.0);

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

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

bool compare(array<int,2> a,array<int,2> b){
return a[0]<b[0];
}
void solve() {
int n,t; read(n,t);
vt<array<int,2> > td(n+1);
FOR(i,1,n+1) read(td[i][0],td[i][1]);
sort(++td.begin(),td.end(),compare);
int siz=(*--td.end())[0]+t+5;
vt<int> dp(siz,0);
vt<int> pre(siz,0);
FOR(i,1,n+1){
FOR(j,1,siz){
if(j-td[i][0]<0||j-td[i][0]>=t) { dp[j]=pre[j];}
else{ dp[j]=max(pre[j],pre[j-td[i][0]]+td[i][1]);}
}
FOR(j,1,siz){ pre[j]=dp[j]; dp[j]=0; }
//dbg(i,pre);
}
print(*max_element(all(pre)));
}

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

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

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

int main() {
  int n,t;
  cin >> n >> t;
  vector<P> data(n);
  rep(i,0,n) {
    int a,b;
    cin >> a >> b;
    data[i] = P(a,b);
  }
  sort(data.begin(), data.end());
  field<int> dp(n+1,vector<int>(t+1,0));
  int ans = 0;
  rep(i,0,n) {
    int a = data[i].first, b = data[i].second;
    rep(j,0,t) {
      chmax(dp[i+1][j],dp[i][j]);
      if (j+a < t) {
        chmax(dp[i+1][j+a],dp[i][j]+b);
      }
    }
    chmax(ans,dp[i][t-1]+b);
  }
  cout << ans << endl;
  return 0;
}


#include<bits/stdc++.h>
#define Rint register int
using namespace std;
const int N = 15;
int n, m, lim, wsum, edge[N][N], sum[1 << N], dp[N][1 << N];
inline int cost(int S, int T){return sum[S | T] - sum[S] - sum[T];}
inline bool chkmax(int &a, int b){return (a < b) ? (a = b,1) : 0;}
int main(){
	scanf("%d%d", &n, &m); lim = 1 << n;
	for(Rint i = 1;i <= m;i ++){
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c); -- a; -- b;
		edge[a][b] = edge[b][a] = c; wsum += c;
	}
	for(Rint S = 1;S < lim;S ++)
		for(Rint i = 0;i < n;i ++) if((S >> i) & 1)
			for(Rint j = i + 1;j < n;j ++) if((S >> j) & 1)
				sum[S] += edge[i][j];
	memset(dp, 0x80, sizeof dp);
	dp[0][1] = 0;
	for(Rint S = 1;S < lim;S += 2)
		for(Rint t = 0;t < n;t ++) if((S >> t) & 1){
			for(Rint u = 0;u < n;u ++) if(!((S >> u) & 1) && edge[t][u])
				chkmax(dp[u][S | (1 << u)], dp[t][S] + edge[t][u]);
			int SS = (lim - 1) ^ S;
			for(Rint T = SS;T;T = (T - 1) & SS)
				chkmax(dp[t][S | T], dp[t][S] + sum[T | (1 << t)]);
		}
	printf("%d\n", wsum - dp[n - 1][lim - 1]);
}

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

struct Edge{
  ll to;
  ll weight;
  Edge(ll t,ll w) : to(t),weight(w){}
};

using Graph = vector<vector<Edge>>;

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

  for(int i=0;i<n-1;++i){
    ll from,to,weight;
    cin >> from >> to >> weight;
    from--;
    to--;
    G[from].push_back(Edge(to,weight));
    G[to].push_back(Edge(from,weight));
  }
  vector<ll> dist(n,-1);
  queue<ll> que;
  int q,k;
  cin >> q >> k;
  k--;
  //cout << q << endl;
  vector<ll> x(q),y(q);
  for(int i=0;i<q;i++){
    cin >> x[i] >> y[i];
    x[i]--;
    y[i]--;
    //cout << "OK" << endl;
  }
  dist[k]=0;
  que.push(k);
  while(!que.empty()){
    ll v=que.front();
    que.pop();
    for(auto nv : G[v]){
      if(dist[nv.to]==-1 || dist[nv.to]>dist[v]+nv.weight){
        dist[nv.to]=dist[v]+nv.weight;
        que.push(nv.to);
      }
    }
  }
  for(int i=0;i<q;++i){
    ll ans=dist[x[i]]+dist[y[i]];
    cout << ans << endl;
  }
  return 0;
}


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

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

typedef struct Node_tag Node;

Node *root;
Node *nil = (Node *)malloc(sizeof(Node));

void init(){
	nil->parent = nil->left = nil->right = nil;
	root = nil;
}

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

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

void insert(Node *z){
	Node *x = root, *y = nil;
	while(x != nil){
		y = x;
		if(z->key < x->key)
			x = x->left;
		else
			x = x->right;
	}
	z->parent = y;

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

Node* find(Node *x, int k){
	while(x != nil && k != x->key){
		if(k < x->key)
			x = x->left;
		else
			x = x->right;
	}
	return x;
}

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

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

void deleteNode(Node *z){
	Node *x, *y;
	if(z->left == nil || z->right == nil)
		y = z;
	else
		y = getSuccessor(z);

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

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

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

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

int main(void){
	int m, i, key;
	char S[10];
	init();
	scanf("%d", &m);
	for(i = 0;i < m;i++){
		scanf("%s", S);
		if(S[0] == 'p'){
			inParse(root);
			printf("\n");
			preParse(root);
			printf("\n");
		} else {
			scanf("%d", &key);
			if(S[0] == 'i'){
				Node *z = (Node *)malloc(sizeof(Node));
				z->key = key;
				z->left = z->right = nil;
				insert(z);
			} else if(S[0] == 'f'){
				if(find(root, key) != nil)
					printf("yes\n");
				else
					printf("no\n");
			} else
				deleteNode(find(root, key));	//??¨????????¨?????????key????????????????????´?????¨??????????????????????????????
		}
	}
	return 0;
}

#include <bits/stdc++.h>

typedef long long LL;

//container util
//------------------------------------------
#define PB push_back
#define MP make_pair
#define DECIM(digits)  cout<<fixed<<setprecision(digits)
#define SZ(a) int((a).size())
#define SORT(c) sort((c).begin(),(c).end())

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

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

//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))

using namespace std;
int main(void)
{
  for(;;){
    int n;
    cin >> n;
    if(!n) return 0;
    vector<pair<int,int> >gem;
    gem.resize(n);
    REP(i,n){
      int x,y;
      cin >> x >> y;
      gem[i]=MP(x,y);
    }
    int m;
    cin >> m;
    pair<int,int> pos=MP(10,10);
    REP(i,m){
      string news;
      int amount;
      cin >> news >> amount;
      while(amount--){
	if(news=="N")
	  pos.second++;
	if(news=="E")
	  pos.first++;
	if(news=="W")
	  pos.first--;
	if(news=="S")
	  pos.second--;
	for(auto j=gem.begin();j!=gem.end();)
	  if(pos==*j) j=gem.erase(j);
	  else ++j;
      }
    }
    if(gem.empty()) cout << "Yes" << endl;
    else cout << "No" << endl;
  }
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll          long long
#define pb          push_back
#define INF         9223372036854775807
#define    endl        '\n'
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll int)x.size()
#define hell        1000000007
#define rep(i,a,b)    for(ll int i=a;i<b;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
#define lower(u)    transform(u.begin(), u.end(), u.begin(), ::tolower);//convert string u to lowercase;
#define upper(u)    transform(u.begin(), u.end(), u.begin(), ::toupper);
using namespace std;

using namespace __gnu_pbds;

#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define N  100005

void solve()
{
    ll n;
    cin>>n;
    string s;
    cin>>s;

    ll ans=0;
    ll r=0,g=0,b=0;
    for(auto u:s)
    {
        if(u=='R')
            r++;
        else if(u=='G')
            g++;
        else
            b++;
    }
    ans=r*g*b;
    // cout<<ans<<endl;
    ll res=0;
    for(int l=1;l<=(n-1)/2;l++)
    {
        for(int i=0;i<n-2*l;i++)
        {
            if(s[i]!=s[i+l]&&s[i]!=s[i+2*l]&&s[i+l]!=s[i+2*l])
            {
                res++;
            }
        }
    }
    cout<<ans-res<<endl;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int TESTS=1;
//    cin>>TESTS;
    while(TESTS--)
    {
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>

int main(){
    using namespace std;
    constexpr unsigned long MOD = 1000000007;
    vector<unsigned long> tapi(1048576, 1), tapu(1048576), tape(1048576);
    unsigned long ans{0}, t{500000004}, a{0}, z{0}, A;cin >> A;
    while(cin >> A)if(a ^= A){
        (tapu[a] += (tapi[a] += tapu[a] * (z - tape[a])) %= MOD) %= MOD;
        ans += tapi[a];
        tape[a] = z;
    }else ++z, (t *= 2) %= MOD;
    cout << (a ? tapi[a] : (ans + t) % MOD) << endl;
    return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <vector>
#include <stack>
#include <queue>
#include <tuple>
#include <cassert>
#include <functional>
#include <set>
#include <deque>
#include <list>
using namespace std;
typedef long long ll;
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
#define LNF __LONG_LONG_MAX__
const ll MOD = 1000000007;
const int MAX = 200000;

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;
}
// 法mでのaの逆元を計算
long long modinv(long long a, long long m) {
    long long b = m, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
}
// a^n mod を計算する
long long modpow(long long a, long long n, long long mod) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}
//最大公約数
long long gcd(long long x, long long y){
    if(y==0) return x;
    else return gcd(y,x%y);
}
//最小公倍数
long long lcm(long long x, long long y){
    return x / gcd(x, y) * y;
}
// nが大きく,kが10^7程度の時の二項係数
long long com[200005];
void combination(long long n,long long k){
    com[0] = 1;
    for(int i=1;i<=k;i++){
        com[i] = (com[i-1]*(n-i+1)%MOD)*modinv(i,MOD)%MOD;
    }
}

int main(){
    int N; cin >> N;
    cout << N/3 << endl;
    return 0;
}

#include <iostream>
#include <string>

using namespace std;

string S;
int N;

int main(void)
{
	cin >> S;
	N = S.length();
	int offset = 0;
	int result = 0;
	while(offset < N)
	{
		if(S[offset] == 'O')
		{
			int of = offset;
			int c_O = 0;
			while(S[of] == 'O')
			{
				of++;
				c_O++;
			}
			bool OK = true;
			for(int i = offset - 1;i >= offset - c_O;i--)
			{
				if(i >= 0)
				{
					if(S[i] != 'J')
					{
						OK = false;
						break;
					}
				}
				else
				{
					OK = false;
					break;
				}
			}
			for(int i = of;i < of + c_O;i++)
			{
				if(i < N)
				{
					if(S[i] != 'I')
					{
						OK = false;
						break;
					}
				}
				else
				{
					OK = false;
					break;
				}
			}
			if(OK)
			{
				result = max(result,c_O);
			}
			offset = of;
		}
		else
			offset++;
	}
	cout << result << endl;
	return 0;
}


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

vector<P> G[1001][1001];
int dp[1001][1001];

vector<P> tp;

int dfs(int x,int y,int px=1000,int py=1000)
{
	if(dp[x][y]!=0)return dp[x][y];
	if(G[x][y].empty())return dp[x][y] = 1;
	for(auto i:G[x][y])
	{
		dp[x][y] = max(dp[x][y],dfs(i.first,i.second,x,y)+1);
	}
	return dp[x][y];
}

int main()
{
	int n;
	cin >> n;
	vector<vector<P>> input(n);
	map<P,int> h;//入次数
	for(int i = 0;i<n;i++)
	{
		for(int j = 0;j<n-1;j++)
		{
			int x=i;
			int y;
			cin >> y;
			y--;
			if(x>y)swap(x,y);
			input[i].emplace_back(x,y);

		}
	}
	for(int i = 0;i<n;i++)
	{
		for(int j =0;j<n-2;j++)
		{
			h[input[i][j+1]]++;
			G[input[i][j].first][input[i][j].second].emplace_back(input[i][j+1]);
		}
	}
	stack<P> S; //入次数がゼロの頂点
	for(int i = 0;i<n;i++)
	{
		for(int j = i+1;j<n;j++)
		{
			if(h[P(i,j)]==0)
			{
				S.emplace(i,j);
			}
		}
	}
	while(S.size())
	{
		auto now = S.top();S.pop();
		tp.push_back(now);
		for(auto i:G[now.first][now.second])
		{
			h[i]--;
			if(h[i]==0)S.emplace(i);
		}
	}
	if(tp.size()!=n*(n-1)/2)
	{
		cout << -1 << endl;
		return 0;
	}

	int ans = 0;
	for(int i = 0;i<n;i++)
	{
		for(int j = i+1;j<n;j++)
		{
			ans = max(ans,dfs(i,j));
		}
	}
	cout << ans << endl;
}

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

#define F first
#define S second
#define PB push_back

vector<pair<pair<int,int>,int> > v1,v2;
bool is_reach[100100];
int po_v1[100100]; // id
int po_v2[100100]; // id
priority_queue<pair<int,pair<int,int> > > pq;
int n;

int fi_dif(int a,int b)
{
    return abs(a-b);
}

void add_path(int who)
{
    if(po_v1[who]!=0&&!is_reach[v1[po_v1[who]-1].S])
    {
        pq.push({-fi_dif(v1[po_v1[who]].F.F,v1[po_v1[who]-1].F.F),{who,v1[po_v1[who]-1].S}});
    }
    if(po_v1[who]!=n-1&&!is_reach[v1[po_v1[who]+1].S])
    {
        pq.push({-fi_dif(v1[po_v1[who]].F.F,v1[po_v1[who]+1].F.F),{who,v1[po_v1[who]+1].S}});
    }
    if(po_v2[who]!=0&&!is_reach[v2[po_v2[who]-1].S])
    {
        pq.push({-fi_dif(v2[po_v2[who]].F.F,v2[po_v2[who]-1].F.F),{who,v2[po_v2[who]-1].S}});
    }
    if(po_v2[who]!=n-1&&!is_reach[v2[po_v2[who]+1].S])
    {
        pq.push({-fi_dif(v2[po_v2[who]].F.F,v2[po_v2[who]+1].F.F),{who,v2[po_v2[who]+1].S}});
    }
}

main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        int a,b;
        scanf("%d %d",&a,&b);
        v1.PB({{a,b},i});
        v2.PB({{b,a},i});
    }
    sort(v1.begin(),v1.end());
    sort(v2.begin(),v2.end());
    for(int i=0;i<n;i++)
    {
        po_v1[v1[i].S]=i;
        po_v2[v2[i].S]=i;
    }
    int ans=0;
    is_reach[0]=true;
    add_path(0);
    while(!pq.empty())
    {
        int cost=-pq.top().F;
        int a=pq.top().S.F;
        int b=pq.top().S.S;
        pq.pop();
        if(is_reach[b])
            continue;
        ans+=cost;
        // printf("%d %d %d\n",cost,a,b);
        is_reach[b]=true;
        add_path(b);
    }
    printf("%d\n",ans);
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;
using pll = pair<ll,ll>;

#define INF (1LL << 60)
#define MOD 1000000007
#define PI 3.14159265358979323846

#define REP(i,m,n) for(ll (i)=(m),(i_len)=(n);(i)<(i_len);++(i))
#define FORR(i,v) for(auto (i):v)
#define ALL(x) (x).begin(), (x).end()
#define PR(x) cout << (x) << endl
#define PS(x) cout << (x) << " "
#define SZ(x) ((ll)(x).size())
#define MAX(a,b) (((a)>(b))?(a):(b))
#define MIN(a,b) (((a)<(b))?(a):(b))
#define REV(x) reverse(ALL((x)))
#define ASC(x) sort(ALL((x)))
#define DESC(x) ASC((x)); REV((x))
#define pb push_back
#define eb emplace_back

int main()
{
    ll N;
    cin >> N;
    vec V(N+1);
    REP(i,1,N+1) cin >> V[i];

    vector<pll> od(100001, {0, 0}), ev(100001, {0, 0});
    ll x = V[1];
    bool f = false;
    REP(i,1,N/2+1) {
        ++od[V[i*2-1]].first;
        ++ev[V[i*2]].first;
        od[V[i*2-1]].second = V[i*2-1];
        ev[V[i*2]].second = V[i*2];
        if(x != V[i]) f = true;
    }

    if(f) {
        DESC(od);
        DESC(ev);
        ll mo = od[0].first, me = ev[0].first;
        if(od[0] == ev[0]) {
            if(od[1].first < ev[1].first) me = ev[1].first;
            else mo = od[1].first;
        }
        PR(N-mo-me);
    }
    else PR(N/2);

    return 0;
}

/*

*/

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

const int INF = 1001001001;
int dp[1000005][2];

int main() {
    string s;
    cin >> s;
    reverse(s.begin(), s.end());
    s += "0";
    int n = s.size();
    rep(i, n+1) {
        rep(j, 2) dp[i][j] = INF;
    }
    dp[0][0] = 0;
    rep(i, n) rep(j, 2) {
        int x = s[i] - '0';
        x += j;
        if (x < 10) dp[i+1][0] = min(dp[i+1][0], dp[i][j]+x);
        if (x > 0) dp[i+1][1] = min(dp[i+1][1], dp[i][j]+(10-x));
    }
    cout << dp[n][0] << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e9;
typedef pair<ll, ll> P;

vector<ll> G[1000], Ginv[1000];
vector<P> ps;

int main() {
  ll N, M; cin >> N >> M;
  for (ll i = 0; i < M; i++) {
    ll a, b; cin >> a >> b; a--; b--;
    G[a].push_back(b); Ginv[b].push_back(a);
    ps.push_back(P(a,b));
  }
  ll shortest = INF;
  vector<ll> res;

  for (ll i = 0; i < M; i++) {
    ll s = ps[i].second; ll t = ps[i].first;
    ll d[N];
    for (ll j = 0; j < N; j++) {d[j] = INF;}
    d[s] = 0;

    queue<ll> que; que.push(s);
    while (!que.empty()) {
      ll p = que.front(); que.pop();
      if (p == t) break;
      for (ll j = 0; j < G[p].size(); j++) {
        ll to = G[p][j];
        if (d[to] == INF) {que.push(to); d[to] = d[p]+1;}
      }
    }

    if (d[t]+1 < shortest) {
      ll p = t; res.push_back(p);
      while (d[p] > 0) {
        for (ll j = 0; j < Ginv[p].size(); j++) {
          ll to = Ginv[p][j];
          if (d[to]+1 == d[p]) {
            p = to; res.push_back(p); break;
          }
        }
      }
    }
    shortest = min(d[t]+1, shortest);
  }
  if (shortest == INF) {cout << -1 << endl; return 0;}
  cout << shortest << endl;
  ll n = res.size();
  for (ll i = n-1; i >= n-shortest; i--) {
    cout << res[i]+1 << endl;
  }
}

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

using ll = long long;
using P = pair<ll, ll>;

const ll MOD = 1e9+7;
// const ll MOD = 998244353;
const ll INF = 1ll<<60;

#define FOR(i,a,b) for (ll i=(a);i<(ll)(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define DEBUG(x) std::cerr << #x << " : " << (x) << std::endl;
//#define DEBUG(x) ;

ll N, u, v;
vector<vector<ll>> G;
vector<ll> du, dv;

int main(int argc, char **argv)
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> N >> u >> v;
	--u, --v;

	G.resize(N);
	du.assign(N, -1);
	dv.assign(N, -1);

	REP(i, N-1)
	{
		ll a, b; cin >> a >> b;
		--a, --b;
		G[a].push_back(b);
		G[b].push_back(a);
	}

	queue<ll> que;
	que.push(v);
	dv[v] = 0;

	while (que.size())
	{
		ll now = que.front();
		que.pop();

		for (auto &nv : G[now])
		{
			if (dv[nv] != -1) continue;
			dv[nv] = dv[now] + 1;
			que.push(nv);
		}
	}

	que.push(u);
	du[u] = 0;

	while (que.size())
	{
		ll now = que.front();
		que.pop();

		for (auto &nv : G[now])
		{
			if (du[nv] != -1) continue;
			du[nv] = du[now] + 1;
			que.push(nv);
		}
	}

	ll res{0};
	REP(n, N)
	{
		if (du[n] < dv[n])
		{
			res = max(res, dv[n] - 1);
		}
	}

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


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

int main()
{
  string s;
  cin >> s;
  int ans = 10000;
  for(int i=0; i<s.size()-2; i++)
  {
    string subs = s.substr(i, 3);
    int a = atoi(subs.c_str());
    ans = min(ans, abs(753 - a));
  }

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

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

const int maxn = 20;
LL a[maxn], power[maxn], n, m, ans;

LL dfs1(LL k, LL n, LL pre){
	if (2*k+1 == m){
		LL res; a[k] = n / (power[k] + power[k-1]) - pre;
		if (a[1] <= -10 || 10 <= a[1]) return 0;
		if ((pre + a[k]) * (power[k] + power[k-1]) != n) return 0;
		if (a[1] >= 0) res = 10 - abs(a[1]) - 1;
		else res = 10 - abs(a[1]);
		for (int i=2; i<=k; i++)
			if (a[i] <= -10 || 10 <= a[i]) return 0;
			else res *= 10 - abs(a[i]);
		return res * 10;
	}
	LL res = 0, d = power[k-1] + power[m-1-k], t;
	a[k] = ((n / power[k-1] - pre) % 10 + 10) % 10; t = a[k] + pre;
	res = dfs1(k+1, n-t*d, t);
	if (t){
		a[k] -= 10; t -= 10;
		res += dfs1(k+1, n-t*d, t);
	}
	return res;
}

LL dfs2(LL k, LL n, LL pre){
	if (2*k == m){
		LL res; a[k] = n / power[k-1] - pre;
		if (a[1] <= -10 || 10 <= a[1]) return 0;
		if (a[1] >= 0) res = 10 - abs(a[1]) - 1;
		else res = 10 - abs(a[1]);
		for (int i=2; i<=k; i++)
			if (a[i] <= -10 || 10 <= a[i]) return 0;
			else res *= 10 - abs(a[i]);
		return res;
	}
	LL res = 0, d = power[k-1] + power[m-1-k], t;
	a[k] = ((n / power[k-1] - pre) % 10 + 10) % 10; t = a[k] + pre;
	res = dfs2(k+1, n-t*d, t);
	if (t){
		a[k] -= 10; t -= 10;
		res += dfs2(k+1, n-t*d, t);
	}
	return res;
}

int main(){
	scanf("%lld", &n);
	if (n % 9) { puts("0"); return 0; }
	n /= 9; power[0] = 1;
	for (int i=1; i<=18; i++) power[i] = power[i-1] * 10;
	for (int i=2; i<=18; i++){
		if (i&1) { m = i; ans += dfs1(1, n, 0); }
		else { m = i; ans += dfs2(1, n, 0); }
	}
	printf("%lld\n", ans);
	return 0;
}


#include <bits/stdc++.h>
#define WHOLE(v) (v).begin(), (v).end()
#define REV_WHOLE(v) (v).rbegin(), (v).rend()
using i64 = int64_t;
using namespace std;
template<class F>auto recursive(F f){return[f](auto...a){return f(f,a...);};}
template<class T>using rev_priority_queue=priority_queue<T,vector<T>,greater<T>>;
template<class T>using vector2d=vector<vector<T>>;
struct fixprec{int p;fixprec(int p):p(p){}};
ostream&operator<<(ostream&o,fixprec f){return o<<fixed<<setprecision(f.p);}
/*!* [intutil] *!*/
/*+* 整数演算いろいろ *+*/
i64 divceil(i64 a, i64 b) {
    return a / b + (a > 0 && a % b ? 1 : 0);
}
i64 logceil(i64 a, i64 b) {
    for(i64 i = 0, x = 1; a >= 2; x *= a, i++) if(x <= b) return i; return 0;
}
i64 gcd(i64 a, i64 b) {
    for (i64 c; a;) c = a, a = b % a, b = c; return b;
}
i64 lcm(i64 a, i64 b) {
    return (a / gcd(a, b) * b);
}
int main() {
    int N;
    cin >> N;
    vector<i64> A(N);
    for(i64 &a : A) cin >> a;
    bool ans;
    // i : ターン数 (0 mod 2 で自分のターン)
    for(int i = 0; true; i++) {
        i64 g = A[0];
        for(i64 a : A) g = gcd(g, a);
        for(i64 &a : A) a /= g;

        int even_num = 0;
        for(i64 a : A) even_num += (a % 2 == 0);
        if(even_num % 2) { // 偶数が奇数個
            ans = i % 2 == 0;
            break;
        } else { // 偶数が偶数個
            i64 odd = 0;
            for(i64 a : A) if(a % 2 == 1) odd = a;
            if(N - even_num == 1 && odd > 2) { // 奇数が一つだけで、それは1でない
                for(i64 &a : A) if(a % 2 == 1) a--;
                continue;
            } else {
                ans = i % 2 == 1;
                break;
            }
        }
    }
    cout << (ans ? "First" : "Second") << endl;
    return 0;
}

#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <math.h>
#include <cassert>
#define rep(i,n) for(int i = 0; i < n; ++i )
using namespace std;
using ll = long long;
using P = pair<int,int>;

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

  vector<int> d(n+1), d2(n);
  int v = 0;
  rep(i,n+1) v += d[i] = abs(a[i]-a[i+1]);
  rep(i,n) d2[i] = abs(a[i]-a[i+2]);

  rep(i,n) cout << v-d[i]-d[i+1]+d2[i] << endl;
}


#include<bits/stdc++.h>
using namespace std;
const long long int mod=1e9+7;
int main(){
    int n;
    cin >> n;
    int a;
    long long int ans=1;
    int r=0; int g=0; int b=0;
    for(int i=0;i<n;i++){
        cin >> a;
        ans *= ((a==r)+(a==g)+(a==b));
        ans %=mod;
        if(a==r)r++;
        else if(a==g)g++;
        else b++;
    }
    cout << ans;
}

#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=int(a);i<int(b);++i)
#define SIZE 200005
#define INF 1000000005LL
#define MOD 1000000007
using namespace std;
typedef long long int ll;
typedef pair <int,int> P;

int main(){
    int n;
    cin >> n;
    vector<pair<int,int>> gusu(100000);
    vector<pair<int,int>> kisu(100000);
    rep(i,0,100000){
        gusu[i].second = i;
        kisu[i].second = i;
    }

    rep(i,0,n){
        int a;
        cin >> a;
        if(i%2==0){
            gusu[a].first++;
        }else{
            kisu[a].first++;
        }
    }
    sort(gusu.begin(),gusu.end());
    reverse(gusu.begin(),gusu.end());
    sort(kisu.begin(),kisu.end());
    reverse(kisu.begin(),kisu.end());

    if(gusu[0].second==kisu[0].second){
        int M = max(gusu[1].first,kisu[1].first);
        cout<<n-M-gusu[0].first<<endl;
    }else{
        cout<<n-gusu[0].first-kisu[0].first<<endl;
    }

  return 0;
}


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

int main(){
  int sx, sy, tx, ty;
  cin >> sx >> sy >> tx >> ty;

  int dx = tx - sx, dy = ty - sy;
  string ans = "";
  for (int i = 0; i < dx; i++) ans += "R";
  for (int i = 0; i < dy; i++) ans += "U";
  for (int i = 0; i < dx; i++) ans += "L";
  for (int i = 0; i < dy + 1; i++) ans += "D";
  for (int i = 0; i < dx + 1; i++) ans += "R";
  for (int i = 0; i < dy + 1; i++) ans += "U";
  for (int i = 0; i < 1; i++) ans += "LU";
  for (int i = 0; i < dx + 1; i++) ans += "L";
  for (int i = 0; i < dy + 1; i++) ans += "D";
  for (int i = 0; i < 1; i++) ans += "R";

  cout << ans << endl;
}

#include<iostream>
using namespace std;
int main(void){

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

    }
    for(int i=1;i<=n;i++){
        count = 1;
        for(int j=1;j<=n;j++){
            if(score[i]<score[j])count++;
        }
        cout<<count<<endl;
    }

    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <string.h>
#include <stack>
using namespace std;
#define rep(i, a, b) for(int i = a; i < (b); ++i)
typedef long long ll;
typedef pair<int,int> P;
int main() {
    int n,m;
    cin >> n;
    map<string, int>blue;
    map<string, int>red;
    rep(i, 0, n){
        string in;
        cin >> in;
        blue[in]++;
    }
    cin>>m;
    rep(i, 0, m){
        string in;
        cin >> in;
        red[in]++;
    }
    int ans = 0;
    for(auto i: blue){
        int blucnt =i.second;
        if(red[i.first] != 0){
            blucnt-=red[i.first];
        }
        ans = max(ans, blucnt);
    }
    cout << ans << endl;
    return 0;
}

#include <iostream>
using namespace std;

int main(){
	int sales_sum[10];
	int n;
	int sales;

	for(;;){
		for(int i = 0; i < 10; i++){
			sales_sum[i] = 0;
		}
		cin >> n;
		if(n == 0) break;
		for(int i = 0; i < n; i++){
			cin >> sales;
			sales_sum[sales]++;
		}
		for(int i = 0; i < 10; i++){
			if(sales_sum[i] == 0){
				cout << '-' << endl;
				continue;
			}
			for(int j = 0; j < sales_sum[i]; j++){
				cout << '*';
			}
			cout << endl;
		}
	}

	return 0;
}


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

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

// Author: Amey Bhavsar
// IDE: Geany/Ubuntu
// Platform: *

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

typedef long long ll;

#define rep(i,a,b) for(auto i=a;i<b;i++)
#define repD(i,a,b) for(auto i=a;i>=b;i--)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second

// to be used with algorithms that processes a container Eg: find(all(c),42)
#define all(x) (x).begin(), (x).end() //Forward traversal
#define rall(x) (x).rbegin, (x).rend() //reverse traversal
// traversal function to avoid long template definition
#define tr(c,i) for(__typeof__((c)).begin() i = (c).begin(); i != (c).end(); i++)
//find version works for all containers. This is present in std namespace.
#define cpresent(c,x) (find(all(c),x) != (c).end())
// ll fe (ll x, ll y) {
	// ll ans = 1;
	// while (y>0) {
		// if (y&1) {
			// ans = (x*ans) % MOD;
		// }
		// y /= 2;
		// x = (x*x) % MOD;
	// }
	// return (ans % MOD);
// }

// ll modmul(ll a, ll b) {
    // return ((a%MOD) * (b%MOD)) % MOD;
// }

// ll modadd(ll a , ll b){
    // return((a%MOD)+(b%MOD)+MOD)%MOD;
// }

// ll modsub(ll a , ll b){
    // return((a%MOD) - (b%MOD) + MOD)%MOD;
// }
#define MOD 1000000007
// int n;
// vector<int> a;
// void solution() {
	// cin >> n;
	// a.resize(n);
	// for (auto& e:a) {
		// cin >> e;
	// }
// }

int main() {
	ios_base::sync_with_stdio(false);cin.tie(NULL);
	int a,b;
	cin >> a >> b;
	if (a >= 10) {
		cout << b;
	} else {
		cout << 100*(10-a) + b;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1LL << 60;

int main() {
    int l, r;
    cin >> l >> r;
    int mod_min = 2019;
    for (int i = l; i <= min(l + 2019, r); i++) {
        for (int j = i + 1; j <= min(l + 2019, r); j++) {
            mod_min = min(mod_min, ((i % 2019) * (j % 2019)) % 2019);
        }
    }
    cout << mod_min << endl;
    return 0;
}

#include <bits/stdc++.h>

using namespace std;
#define int long long
const int N = 4e6 + 10, M = 2e3 + 10, MOD = 1e9 + 7;
int fac[N], inv[N], dp[M][M], ps[M][M];

int sum(int a, int b) {return (a + b) % MOD;}
int mul(int a, int b) {return (a * b) % MOD;}
int Pow(int a, int b)
{
    int res = 1;
    for (; b; b>>=1, a = mul(a, a)) if (b & 1) res = mul(res, a);
    return res;
}

int C(int n, int k)
{
    if (k < 0 || n < 0 || k > n) return 0;
    return mul(fac[n], mul(inv[k], inv[n - k]));
}

int32_t main()
{
    fac[0] = 1;
    for (int i = 1; i < N; i++) fac[i] = mul(fac[i - 1], i);
    inv[N - 1] = Pow(fac[N - 1], MOD - 2);
    for (int i = N - 2; i >= 0; i--) inv[i] = mul(inv[i + 1], i + 1);
    int n, k; cin >> n >> k;
    if (k == 1) return cout << "1\n", 0;
    ps[1][1] = dp[1][1] = 1;
    for (int i = 2; i <= n; i++) {
        dp[i][1] = mul(ps[i - 1][1], C(i * k - 2, k - 2));
        for (int j = 2; j <= i; j++) {
            dp[i][j] = mul(ps[i - 1][j - 1], C(i * k - j - 1, k - 2));
        }
        for (int j = i; j >= 1; j--) ps[i][j] = sum(ps[i][j + 1], dp[i][j]);
    }
    cout << mul(ps[n][1], fac[n]) << "\n";
}


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

int main(){
	int d, ans, n;
	while(cin >> d){
		ans = pow(d, 3);
		n = 600 / d - 1;
		ans *= n*(n+1)*(2*n+1) / 6;
		cout << ans << endl;
	}
	return 0;
}

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string.h>
#include <bitset>
#include <queue>
#include <map>
#include <string>
#include <stack>
#include <utility>
#include <queue>
#include <cmath>
#define mp make_pair
#define pii pair<long long int,long long int>
#define ff first
#define pb push_back
#define ss second
#define ll long long
#define ull unsigned long long
#define vi vector<long long int>
#define vii vector<pii>
#define vvi vector <vi>
#define rep(x,a,b,c) for(int x=a;x<=b;x+=c)
#define repp(x,a,b) rep(x,a,b,1)
#define rev(x,a,b,c) for(int x=a;x>=b;x-=c)
#define revv(x,a,b) rev(x,a,b,1)
#define OO (int)2e9
#define INF (ll)1e18

using namespace std;

int n,hasil;

int main()
{
	while(scanf("%d",&n)!=EOF)
	{
		hasil=0;
		rep(x,n,600-n,n)
		{
			hasil+=n*x*x;
		}
		printf("%d\n",hasil);
	}
	return 0;
}


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

double fabs (double n) {
    if (n < 0) return -n;
    else return n;
}

int main() {
    int n;
    cin >> n;
    vector<double> a(n);
    double sum = 0.0;
    for (int i=0; i<n; i++) {
        double num;
        cin >> num;
        a[i] = num;
        sum += num;
    }
    double ave = sum / n;
    double mind = 1000;
    int ans = -1;
    for (int i=0; i<n; i++) {
        if (fabs(a[i]-ave) < mind) {
            mind = fabs(a[i]-ave);
            ans = i;
        }
    }
    cout << ans << endl;
}

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;

template <typename Tp> inline void getint(Tp &num){
	register int ch, neg = 0;
	while(!isdigit(ch = getchar())) if(ch == '-') neg = 1;
	num = ch & 15;
	while(isdigit(ch = getchar())) num = num * 10 + (ch & 15);
	if(neg) num = -num;
}

int N;

int main(){
	getint(N);
	return printf("%d\n", 180 * (N - 2)), 0;
}


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

int main() {
    ll l, r;
    cin >> l >> r;
    r = min(l+2019, r);
    ll ans = INF;
    for (ll i = l; i <= r; i++) {
        for (ll j = i+1; j <= r; j++) {
            //i = i%2019;
            //j = j%2019;
            ans = min(ans, (i*j)%2019);
        }
    }
    cout << ans << endl;
    return 0;
}

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

int main(){
    int l, r;
    cin >> l >> r;
    if(r - l >= 2019) cout << 0 << endl;
    else{
        vector<int> n;
        for(int i = l; i <= r; i++){
            n.push_back(i % 2019);
        }
        vector<int> m;
        rep(i,n.size()){
            for(int j = i+1; j < n.size(); j++) m.push_back((n[i]*n[j])%2019);
        }
        sort(m.begin(), m.end());
        cout << m[0] << endl;
    }
}


#include <iostream>
using namespace std;

class dice{
public:
	int d[7];
	int setd();
	int M(int*,int*,int*,int*);
};
int dice::setd(){
	for(int i=1;i<=6;i++){
		cin>>d[i];
	}
	return 0;
}
int dice::M(int *A,int *B,int *C,int *D){
	int s,t;
	s=*B;
	*B=*A;
	t=*C;
	*C=s;
	s=*D;
	*D=t;
	*A=s;
	return 0;
}
int main(void){
	dice a;
	a.setd();
	char c[110];
	cin>>c;
	for(int j=0;;j++){
		if(c[j]=='E'){
			a.M(&a.d[1],&a.d[3],&a.d[6],&a.d[4]);
			continue;
		}
		if(c[j]=='W'){
			a.M(&a.d[1],&a.d[4],&a.d[6],&a.d[3]);
			continue;
		}
		if(c[j]=='N'){
			a.M(&a.d[1],&a.d[5],&a.d[6],&a.d[2]);
			continue;
		}
		if(c[j]=='S'){
			a.M(&a.d[1],&a.d[2],&a.d[6],&a.d[5]);
			continue;
		}
		break;
	}
	cout<<a.d[1]<<endl;
	return 0;
}

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

int a[11][11];

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

void Comp(int n) {
  for(int i = 0; i < n; ++i) {
	for(int j = 0; j < n; ++j) {
	  a[i][n] += a[i][j];
	  a[n][j] += a[i][j];
	  a[n][n] += a[i][j];
	}
  }
}

void print(int n) {
  for(int i = 0; i <= n; ++i) {
	for(int j = 0; j <= n; ++j) {
	  printf("%5d", a[i][j]);
	}
	printf("\n");
  }
}

int main() {
  int n;

  while(scanf("%d", &n)) {
	if(n == 0) break;

	init(n);
	for(int i = 0; i < n; ++i) {
	  for(int j = 0; j < n; ++j) {
		cin >> a[i][j];
	  }
	}
	Comp(n);
	print(n);
  }
  return 0;
}


#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

void read_separated_string(vector<int> &v, string str) {
  istringstream istr(str);
  string tmp;
  while (getline(istr, tmp, ' ')) {
    v.push_back(stoi(tmp));
  }
}

int main() {
  int N, Q;
  vector<int> n;
  vector<int> q;
  string str;
  cin >> N; cin.ignore();
  getline(cin, str);
  read_separated_string(n, str);
  cin >> Q; cin.ignore();
  getline(cin, str);
  read_separated_string(q, str);

  int count = 0;
  for (int i : q) {
    for (int j : n) {
      if (i == j) {
        count++;
        break;
      }
    }
  }
  cout << count << endl;
}

#include <bits/stdc++.h>
using namespace std;
int main (){
    cin.tie(0);
    ios::sync_with_stdio(false);

    int N;
    cin >> N;

    vector<int> vc1(N);
    for( auto& x : vc1 ){
        cin >> x;
    }

    int Q;
    cin >> Q;

    vector<int> vc2(Q);
    for( auto& x : vc2 ){
        cin >> x;
    }
    set<int> st;
    for( auto&& x : vc2 ){
        for( auto&& y : vc1 ){
            if( x == y ){
                st.insert(x);
            }
        }
    }
    cout << st.size() << endl;
    return 0;
}


#include <bits/stdc++.h>
//#define DEBUG
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
template <class T>
void print_vect(std::vector<T> v){
    for(int i = 0 ; i < v .size(); i++) cout << v[i] << " ";
    cout << endl;
}

void solve(){
    string S;
    cin >> S;
    map<char, vi> mp;
    for(int i  = 0 ; i < S.size() ; i++){
        mp[S[i]].push_back(i);
    }
    for( auto it = mp.begin() ; it != mp.end(); it++){
        char c = (*it).first;
        if((*it).second.size() < 2) continue;

        for(int i = 1 ; i < (*it).second.size(); i++){
            if ( (*it).second[i] <= (*it).second[i-1] + 2){
                //we found
                cout << (*it).second[i-1] + 1 << " " << (*it).second[i] + 1 << "\n";
                return;
            }
        }
    }
    cout << "-1 -1 \n";
}

int main()
{
    //making data IO Fast
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    /****************************/

    #ifdef DEBUG
    int TEST_CASES = 2;
    while(TEST_CASES--){
    #endif // DEBBUG

    solve();

    #ifdef DEBUG
    }
    #endif // DEBUG

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define rep1(i, n) for (int i = 1; i < int(n); ++i)
#define repx(i, x, n) for (int i = int(x); i < int(n); ++i)
#define rrep(i, n) for (int i = int(n) - 1; i >= 0; --i)
#define ALL(n) begin(n), end(n)
#define IN(a, x, b) ((a) <= (x) && (x) < (b))
#define OUT(a, x, b) ((x) < (a) || (x) < (b))
typedef long long ll;
typedef long double ld;
const ll INF = 1e18;
const ll MOD = 1000000007;

int main()
{
    string s;
    cin >> s;

    rep(i, s.length() - 1)
    {
        if (s[i] == s[i + 1])
        {
            cout << i + 1 << " " << i + 2 << endl;
            return 0;
        }
        if (i < s.length() - 1 && s[i] == s[i + 2])
        {
            cout << i + 1 << " " << i + 3 << endl;
            return 0;
        }
    }
    cout << "-1 -1" << 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 vector<int> vint;
typedef vector<double> vd;
typedef vector<ll> vll;
typedef vector<string> vstr;
typedef vector<vector<int>> vvint;
typedef vector<pair<int, int>> vpii;
typedef vector<pair<ll, ll>> vpll;
typedef priority_queue<int, vector<int>, greater<int>> spqint; //小さい順に取り出し
typedef priority_queue<ll, vector<ll>, greater<ll>> spqll;     //小さい順に取り出し
typedef priority_queue<int, vector<int>, less<int>> bpqint;    //大きい順に取り出し
typedef priority_queue<ll, vector<ll>, less<ll>> bpqll;        //大きい順に取り出し

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define FOR(i, a, b) for (ll i = (ll)a; i < (ll)b; i++)
#define IREP(i, v) for (auto i = (v).begin(); i != (v).end(); i++)
#define FI first
#define SE second
#define MP make_pair
#define MT make_tuple
#define PB push_back
#define PF push_front
#define TS to_string
#define BS binary_search
#define LB lower_bound
#define UB upper_bound
#define NP next_permutation
#define ALL(v) (v).begin(), (v).end()
#define SZ(x) (ll) x.size()
#define SP(x) setprecision((ll)x)

int INF = 1e9;
int NIL = -1;
ll MOD = 1000000007;
ll LINF = 1e18;
double EPS = 1e-9;
double PI = M_PI;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } //最大公約数
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }     //最小公倍数

void yes(){ cout << "Yes" << endl;}
void no(){ cout << "No" << endl;}
//-----------------------------------------

//-----------------------------------------
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    std::chrono::system_clock::time_point start,end;
    start = std::chrono::system_clock::now();

    ll n ;
    cin >> n ;
    string ans = "" ;
    vll vecp ;
    vll vecm ;
    map<ll,string> mapp ;
    map<ll,string> mapm ;
    if(n==0){
        cout << 0 <<endl;
        return 0 ;
    }

    ll m = 16 ;

    for (int bitt = 0; bitt < (1 << m); bitt++) {
        bitset<16> bits(bitt);
        ll tmp = 0 ;

        /*
        if(bits.any()==false){
            continue ;
        }
        */

        for(ll j=0;j<m;j++){
            if(bits[j]==1){
                tmp += pow(4,j) ;
            }
        }
        vecp.PB(tmp) ;
        stringstream ss ;
        ss << bits ;
        string ts = ss.str() ;
        mapp[tmp] = ts ;
    }

    for (int bitt = 0; bitt < (1 << m); bitt++) {
        bitset<16> bits(bitt);
        ll tmp = 0 ;

        /*
        if(bits.any()==false){
            continue ;
        }
        */

        for(ll j=0;j<m;j++){
            if(bits[j]==1){
                tmp -= 2*pow(4,j) ;
            }
        }
        vecm.PB(tmp) ;
        stringstream ss ;
        ss << bits ;
        string ts = ss.str() ;
        mapm[tmp] = ts ;
    }

    sort(ALL(vecp)) ;
    sort(ALL(vecm)) ;

    ll ansi, ansj ;

    REP(i,SZ(vecp)){
        ll hog = n - vecp[i] ;
        if(BS(ALL(vecm),hog)){
            ansi = vecp[i] ;
            ansj = hog ;
            break ;
        }
    }
    string ps = mapp[ansi] ;
    string ms = mapm[ansj] ;

    REP(i,SZ(ps)){
        ans += ms[i] ;
        ans += ps[i] ;
    }
    string nans = "" ;
    REP(i,SZ(ans)){
        if(nans=="" && ans[i]=='0'){
            continue ;
        }
        else{
            nans += ans[i] ;
        }
    }

    cout << nans <<endl;

    end = std::chrono::system_clock::now();
    auto elapsed = std::chrono::duration_cast< std::chrono::milliseconds >(end - start).count();
    //std::cout << elapsed <<"ms"<< std::endl;

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define All(a) a.begin(),a.end()
#define INF 1000000007
const int MOD = 1000000007;
//accumulate(vec.begin(), vec.end(), 0)
//std::sort(v.begin(), v.end(), std::greater<Type>());
//set_intersection(x, x+7, y, y+6, back_inserter(v));
//set_union(x, x+7, y, y+6, back_inserter(v));
//set_difference(x, x+7, y, y+6, back_inserter(v));

int gcd(int x,int y){
  if(x%y==0) return y;
  else return gcd(y,x%y);
}

bool isPrime(int n){
  if(n < 2) return false;
  else if(n == 2) return true;
  else if(n%2==0) return false;

  for(int i=3;i<=sqrt(n);i++){
    if(n%i==0) return false;
  }
  return true;
}

int main(){
  int n;
  cin >> n;
  string s = "";
  while(n!=0){
    int r = n%2;
    if(r < 0) r+=2;

    n = (n-r) / (-2);

    s += char('0'+ r);
  }
  reverse(All(s));
  if(s.size()==0) s = "0";
  cout<< s <<endl;
}


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

#define ALL(obj) (obj).begin(),(obj).end()
#define SPEED cin.tie(0);ios::sync_with_stdio(false);

template<class T> using PQ = priority_queue<T>;
template<class T> using PQR = priority_queue<T,vector<T>,greater<T>>;

constexpr long long MOD = (long long)1e9 + 7;
constexpr long long MOD2 = 998244353;
constexpr long long HIGHINF = (long long)1e18;
constexpr long long LOWINF = (long long)1e15;
constexpr long double PI = 3.1415926535897932384626433L;

template <class T> vector<T> multivector(size_t N,T init){return vector<T>(N,init);}
template <class... T> auto multivector(size_t N,T... t){return vector<decltype(multivector(t...))>(N,multivector(t...));}
template <class T> void corner(bool flg, T hoge) {if (flg) {cout << hoge << endl; exit(0);}}
template <class T, class U>ostream &operator<<(ostream &o, const map<T, U>&obj) {o << "{"; for (auto &x : obj) o << " {" << x.first << " : " << x.second << "}" << ","; o << " }"; return o;}
template <class T>ostream &operator<<(ostream &o, const set<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const multiset<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const vector<T>&obj) {o << "{"; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "}"; return o;}
template <class T, class U>ostream &operator<<(ostream &o, const pair<T, U>&obj) {o << "{" << obj.first << ", " << obj.second << "}"; return o;}
template <template <class tmp>  class T, class U> ostream &operator<<(ostream &o, const T<U> &obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr)o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
void print(void) {cout << endl;}
template <class Head> void print(Head&& head) {cout << head;print();}
template <class Head, class... Tail> void print(Head&& head, Tail&&... tail) {cout << head << " ";print(forward<Tail>(tail)...);}
template <class T> void chmax(T& a, const T b){a=max(a,b);}
template <class T> void chmin(T& a, const T b){a=min(a,b);}
void YN(bool flg) {cout << (flg ? "YES" : "NO") << endl;}
void Yn(bool flg) {cout << (flg ? "Yes" : "No") << endl;}
void yn(bool flg) {cout << (flg ? "yes" : "no") << endl;}

ll f(vector<ll> A,int N) {
	A.push_back(0);
	reverse(ALL(A));
	auto dp = multivector(3,N+1,-LOWINF);
	dp[0][0]=0;
	dp[0][1]=A[1];
	for(int i = 1; i <= N; ++i) {
		for(int j = 0; j < 3; ++j) {
			for(int k = 1; k <= 3; ++k) {
				if(0<=i-1-k&&j+k-1<3) chmax(dp[j+k-1][i],dp[j][i-1-k]+A[i]);
			}
		}
	}
	ll ans;
	if(N%2==0) ans=max({dp[0][N],dp[0][N-1],dp[1][N]});
	else ans=max({dp[1][N],dp[1][N-1],dp[2][N]});
	return ans;
}
int main() {
	int N; cin >> N;
	vector<ll> A(N+1,0);
	for(int i = 1; i <= N; ++i) cin >> A[i];
	vector<map<int,ll>> dp1(N+1); //最後にiを使ってない
	vector<map<int,ll>> dp2(N+1); //最後にiを使った
	dp1[0][0]=0;
	for(int i = 1; i <= N; ++i) {
		int M = i/2;
		for(int j = -3+M; j <= M+3; ++j) {
			dp1[i][j]=dp2[i][j]=-LOWINF;
			if(dp1[i-1].count(j)) chmax(dp1[i][j],dp1[i-1][j]);
			if(dp1[i-1].count(j-1)) chmax(dp2[i][j],dp1[i-1][j-1]+A[i]);
			if(dp2[i-1].count(j)) chmax(dp1[i][j],dp2[i-1][j]);
		}
	}
	cout << max(dp1[N][N/2],dp2[N][N/2]) << endl;
    return 0;
}

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

int main(){

    int n;
    cin>>n;
    vector<int> arr(n+1);

    for(int i = 1; i <= n; i++) cin>>arr[i];

    vector<ll> pf(n+1);
    vector<ll> dp(n+1,LLONG_MIN);
    for(int i = 1; i <= n; i++) if(i&1) pf[i]+=arr[i]+pf[i-2 < 0 ? 0 : i-2]; // pref of odd sum

    dp[0] = 0;
    dp[1] = 0;
    for(int i = 2; i <= n; i++){
        if(i&1) dp[i] = max(dp[i-1],arr[i]+dp[i-2]); // for odd
        else dp[i] = max(pf[i-1],dp[i-2]+arr[i]); //for even
    }
    cout<<dp[n];
}


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

int main(){
  int n;
  cin >> n;
  vector<P> ab(n), sum(n);
  for(int i=0; i<n; ++i){
    cin >> ab[i].first >> ab[i].second;
    sum[i].first = ab[i].first + ab[i].second;
    sum[i].second = i;
  }
  sort(sum.begin(),sum.end());
  reverse(sum.begin(),sum.end());
  ll ans = 0;
  for(int i=0; i<n; ++i){
    int x = sum[i].second;
    if(i%2 == 0){
      ans += ab[x].first;
    }else{
      ans -= ab[x].second;
    }
  }
  cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef vector<lint> vlint;
typedef vector<vlint> vvlint;
typedef vector<bool> vbool;
typedef vector<int> vint;

#define inf 1e+9
#define endl "\n"
#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 rep_rev(i, n) for (int i = n-1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()

#define pb push_back
#define eb emplace_back
#define Size(c) (int)(c).size()

#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))

template <class T> using pq = priority_queue<T>;
template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;

template <class T, class S> inline bool chmax(T &a, S b) {
    if(a < b) {a = b;return 1;} return 0;
}
template <class T, class S> inline bool chmin(T &a, S b) {
    if(a > b) {a = b;return 1;} return 0;
}
template <class T>
void line(vector<T> &x){for (auto&& xi : x) cin >> xi;}

inline void IN(void){ return;}

template <typename First, typename... Rest>
void IN(First& first, Rest&... rest){ cin >> first; IN(rest...); return;}

#define INT(...) int __VA_ARGS__; IN (__VA_ARGS__)
#define LINT(...) lint __VA_ARGS__; IN (__VA_ARGS__)
#define STR(...) string __VA_ARGS__; IN(__VA_ARGS__)

template <class T> void UNIQUE(vector<T> &x) {
    sort(all(x)); x.erase(unique(all(x)), x.end());
}
template <class T> void print_vec(T first, T end){
    for (auto i = first; i < end - 1; i++) cout << *i << " ";
    cout << *(end-1) << endl;
}

template<class... T>
void debug_print(T... args){
    vector<lint> tmp = initializer_list<lint>{args...};
    print_vec(all(tmp));
}
template<class T>
void print(T a){ cout << a << endl;}

vector<string> Yes = {"No", "Yes"};
vector<string> YES = {"NO", "YES"};

int ord(char x){ return (int)(x - 'a');}
char chr(lint x){ return (char)(x + (lint)('a'));}
lint mod = 1e9+7;
// lint mod =

lint sum(vlint a){lint ret = 0; for(auto&& v:a) ret += v; return ret;}
lint vmini(vlint a, lint &index){
    lint ret = LLONG_MAX;
    rep(i, Size(a)){ if (chmin(ret, a[i])) index = i;}
    return ret;
}
lint vmaxi(vlint a, lint &index){
    lint ret = -LLONG_MAX;
    rep(i, Size(a)){ if (chmax(ret, a[i])) index = i;}
    return ret;
}

lint vmin(vlint a){
    lint ret = LLONG_MAX; for (auto && v : a) chmin(ret, v); return ret;
}
lint vmax(vlint a){
    lint ret = -LLONG_MAX; for (auto && v : a) chmax(ret, v); return ret;
}

vlint base_3(int k){
    vlint ret; while (k > 0){ ret.pb(k % 3); k /= 3;} reverse(all(ret)); return ret;
}
map<lint, lint> trial_division(lint n){
    map<lint, lint> factor;
    lint tmp = (lint)(sqrt(n)) + 1;
    for (lint i = 2; i < tmp; ++i){
        while (n % i == 0){
            n /= i;
            factor[i] ++;
        }
    }
    if(n != 1) factor[n] ++;
    return factor;
}

lint dist(lint x1, lint y1, lint x2, lint y2){
    return (x1-x2) * (x1-x2) + (y1-y2) * (y1 - y2);
}

int main(){
    INT(h, w);
    char o;
    vector<vint> grid(h+2, vint(w+2, 0));
    int cnt = 0;
    rep1(i, h+1){
        rep1(j, w+1){
            cin >> o;
            if (o == '.'){
                grid[i][j] = 1;
                cnt ++;
            }
        }
    }
    queue<pair<int, int>> q;
    q.push({1, 1});
    vector<vint> dist(h+2, vint(w+2, -1));
    dist[1][1] = 0;
    int y, x, ny, nx;
    vint dx = {-1, 0, 0, 1};
    vint dy = {0, -1, 1, 0};
    while (!q.empty()){
        y = q.front().first;
        x = q.front().second; q.pop();
        rep(i, 4){
            ny = y + dy[i];
            nx = x + dx[i];
            if (dist[ny][nx] != -1 || grid[ny][nx] == 0){
                continue;
            }
            q.push({ny, nx});
            dist[ny][nx] = dist[y][x] + 1;
        }
    }
    if (dist[h][w] == -1){
        print(-1);
    }
    else{
        print(cnt - dist[h][w]-1);
    }

}

#include <iostream>
#include <queue>
#define H 20+1
#define W 20+1
using namespace std;

signed char di[4] = { 1,0,-1,0 }; // i+, j+, i-, j-
signed char dj[4] = { 0,1, 0,-1};

typedef struct {
  char s[H][W]; // ÕÊÌóÔ
  char n;         // Ú®ñ
  char d;         // ±ê©çÚ®·éûü
  char i,j;       // »Ýn
} P;

int main(){
  short h,w, b;
  P p, p2;
  queue<P> q;
  short gi,gj;

  while( cin>>w>>h && (w||h) ){
    while( q.size()>0 ) q.pop();

    for( int i=1;i<=h;i++ )
      for( int j=1;j<=w;j++ ){
	cin >> b;
	p.s[i][j] = b;
	if( b==2 ){ p.i = i; p.j=j; p.s[i][j]=0; }
	else if( b==3 ){ gi=i; gj=j; }
      }
    p.n=1;
    for( int i=0;i<4;i++ ){
      p.d = i;
      q.push( p );
    }

    signed char ans = -1;
    while( q.size()>0 ){
      p = q.front(); q.pop();

      int i=p.i, j=p.j;
      bool f=true;
      while( f ){
	int in=i+di[p.d], jn=j+dj[p.d];

	if( in<1 || in>h || jn<1 || jn>w )
	  break;

	switch( p.s[ in ][ jn ] ){
	case 0: // ½àÈ¢
	  i=in; j=jn;
	  break;
	case 1: // áQ¨
	  if( p.i==i && p.j==j ){
	    f=false; break;
	  }
	  if( p.n==10 ){
	    f=false; break;
	  }else if( p.n==9 ){
	    if( i-gi!=0 && j-gj!=0 ){
	      f=false; break;
	    }
	  }
	  p2 = p;
	  p2.i=i; p2.j=j; p2.n++;
	  p2.s[ in ][ jn ] = 0;
	  for( int k=0;k<4;k++ ){
	    p2.d = k; q.push( p2 );
	  }
	  f=false;
	  break;
	case 3: // S[
	  ans = p.n; f=false;
	  break;
	}
      }
      if( ans>0 ) break;
    }
    cout << (int)ans << endl;
  }

  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fastio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define gcd(a,b) __gcd((a),(b))
#define lcm(a,b) ((a)*(b))/gcd((a),(b))
const int maxn = 1e9 + 5;
#define INF 100000000000
const int MOD = 1e9+7;
const double PI = 3.14159265358979323846264338;
const int MAXN = 201+5;

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	fastio
	ll n;
	cin >> n;
	ll a[n];
	for(ll i=0; i<n; i++){
		cin >> a[i];
	}
	ll ans = 1;
	ll mini = a[0];
	for(ll i=1; i<n; i++){
		if(mini>a[i]){
			ans++;
			mini=a[i];
		}
	}
	cout << ans << "\n";
	return 0;
}

//^-^


#include<bits/stdc++.h>
#define fastio 	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define int long long int
using namespace std;
int mod = 1e9 + 7;

int32_t main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	fastio;

	int n, x, t;
	cin >> n >> x >> t;
	int count = t, orig = x;
	while (x < n) {
		count += t;
		x += orig;
	}
	cout << count;
	return 0;
}

/*input
1000 1 1000
*/

#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <queue>
#include <deque>
#include <bitset>
#include <iterator>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <numeric>
#include <utility>
#include <limits>
#include <climits>
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <stdint.h>
#include <tgmath.h>

using namespace std;

/*******  All Required define Pre-Processors and typedef Constants *******/
#define ar array
#define scd(t) scanf("%d",&t)
#define scld(t) scanf("%ld",&t)
#define sclld(t) scanf("%lld",&t)
#define scc(t) scanf("%c",&t)
#define scs(t) scanf("%s",t)
#define scf(t) scanf("%f",&t)
#define sclf(t) scanf("%lf",&t)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define tests int r; cin >> r; while(r--)
#define MP make_pair
#define PI acos(-1)
typedef pair<long long, long long> PII;
typedef pair<double, double> PDD;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef map<int,int> MPII;
typedef set<int> SETI;
typedef multiset<int> MSETI;
typedef long int int32;
typedef unsigned long int uint32;
typedef long long int int64;
typedef unsigned long long int  uint64;
#define godspeed ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0);

#define M(x) ((x)%(1000000007))

vector<int> adj_list[100010];
bool visited[10010];

void dfs(int node){
	visited[node] = true;
	for(int n: adj_list[node]){
		if(!visited[n]){
			dfs(n);
		}
	}
}

int main() {
 	int n;
 	int x;
 	int t;
 	cin>>n>>x>>t;
 	int ans = ceil(n*1.0/x)*t;

 	cout<<ans<<endl;
}


#include <bits/stdc++.h>
#define LL long long
#define LD long double
#define pb push_back
#define mp make_pair
#define ss(x) (int) x.size()
#define fi first
#define se second
#define cat(x) cerr << #x << " = " << x << endl
#define rep(i, j, n) for (int i = j; i <= n; ++i)
#define per(i, j, n) for (int i = n; j <= i; --i)
#define all(x) x.begin(), x.end()

using namespace std;

const int N = 18;

int n;
char s[100];
LL ans;
LL dp[100][100];

LL solve(vector<int> a, vector <int> b) {
	rep(i, 0, n) rep(j, 0, ss(a)) dp[i][j] = 0;
	dp[0][0] = 1;
	rep(i, 0, n - 1)
		rep(j, 0, min(i, ss(a))) {
			if (!dp[i][j]) continue;
			int k = i - j;
			if (j < ss(a) && s[a[j]] == s[n + n - i - 1]) dp[i + 1][j + 1] += dp[i][j];
			if (k < ss(b) && s[b[k]] == s[n + n - i - 1]) dp[i + 1][j] += dp[i][j];
		}
	return dp[n][ss(a)];
}

int main() {
	scanf ("%d", &n);
	scanf ("%s", s);
	rep(mask, 0, (1 << n) - 1) {
		vector <int> a, b;
		rep(k, 0, n - 1)
			if (((mask >> k) & 1)) a.pb(k);
			else b.pb(k);
		ans += solve(a, b);
		//if (solve(a, b)) printf ("%d\n", mask);
	}
	printf ("%lld\n", ans);

    return 0;
}

#include<iostream>
#include<vector>
#include<algorithm>
#define int long long
using namespace std;
const int N=42;
const int mod=2147483647;
const int mod1=1e9+9;
const int base=43;
int n,mul[N],mul1[N];
string s;
bool used[N];
vector<pair<int,int> > damn[N],damn1[N];
void check(){
	int i,dem=0,val=0,val1=0,val2=0,val3=0;
	for(i=1;i<=n;i++){
		if(!used[i]){
//			cout<<0;
			dem++;
			val=(val*base+(int)(s[i]-'a'))%mod;
			val2=(val2*base+(int)(s[i]-'a'))%mod1;
		}
	}
	for(i=n;i>0;i--){
		if(used[i]){
//			cout<<1;
			val1=(val1*base+(int)(s[i]-'a'))%mod;
			val3=(val3*base+(int)(s[i]-'a'))%mod1;
		}
	}
//	cout<<endl;
//	if(dem==0){
//		cout<<val<<' '<<val1<<endl;
//	}
	damn[dem].push_back({(val*mul[n-dem]-val1+mod)%mod,(val2*mul1[n-dem]-val3+mod1)%mod1});
}
void back(int idx,int lim){
	if(idx==lim+1){
		check();
		return;
	}
	used[idx]=false;
	back(idx+1,lim);
	used[idx]=true;
	back(idx+1,lim);
}
void check1(){
	int dem=0,val=0,val1=0,i,val2=0,val3=0;
	for(i=n+1;i<=2*n;i++){
		if(!used[i]){
//			cout<<0;
			val=(val*base+(int)(s[i]-'a'))%mod;
			val2=(val2*base+(int)(s[i]-'a'))%mod1;
		}
//		else{
//			cout<<1;
//		}
	}
	for(i=2*n;i>n;i--){
		if(used[i]){
			dem++;
			val1=(val1*base+(int)(s[i]-'a'))%mod;
			val3=(val3*base+(int)(s[i]-'a'))%mod1;
		}
	}
//	cout<<endl;
//	if(dem==0){
//		cout<<val<<' '<<val1<<endl;
//	}
	damn1[dem].push_back({(val1*mul[n-dem]-val+mod)%mod,(val3*mul1[n-dem]-val2+mod1)%mod1});
}
void back1(int idx,int lim){
	if(idx==lim+1){
		check1();
		return;
	}
	used[idx]=false;
	back1(idx+1,lim);
	used[idx]=true;
	back1(idx+1,lim);
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int i,j,k,l,ans=0;
	cin>>n>>s;
	mul[0]=1;
	for(i=1;i<=2*n;i++){
		mul[i]=(mul[i-1]*base)%mod;
		mul1[i]=(mul1[i-1]*base)%mod;
	}
	s=" "+s;
	back(1,n);
	back1(n+1,2*n);
	for(i=0;i<=n;i++){
		sort(damn[i].begin(),damn[i].end());
		sort(damn1[i].begin(),damn1[i].end());
		for(j=0;j<damn[i].size();j++){
			//cout<<damn[i][j]<<' ';
			ans+=(int)(upper_bound(damn1[i].begin(),damn1[i].end(),damn[i][j])-lower_bound(damn1[i].begin(),damn1[i].end(),damn[i][j]));
		}
//		cout<<endl;
//		for(j=0;j<damn1[i].size();j++){
//			cout<<damn1[i][j]<<' ';
//		}
//		cout<<"cac "<<i<<endl;
	}
	cout<<ans;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, a, b) for(int i = a; i < b; i++)
#define  all(x) (x).begin(),(x).end()     // 昇順ソート
#define  rall(v) (v).rbegin(), (v).rend() // 降順ソート
#define  FastIO ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
typedef long long ll;
using P = pair<int,int>;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return true; } return false; }

int main(){
    string s;
    cin >> s;
    stack<char> st;
    for(int i = 0; i < s.size(); i++){
        if (st.empty()) st.push(s[i]);
        else if (st.top() == 'S' && s[i] == 'T') st.pop();
        else st.push(s[i]);
    }
    cout << st.size() << endl;
    return 0;
}

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
#define ll long long
#define ull unsigned long long
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
#define FOR(i,start,stop)  for(int i=(start); i < (stop); i++)
#define FORD(i,start,stop) for(int i=(start); i >= (stop); i--)
#define RIP(i,stop) FOR(i,0,stop)
#define PI 3.141592653589793
#define PRECISION(c,f) fixed << setprecision(c) << f
using namespace std;

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

  int X;
  cin >> X;

  int ans=0;
  while(true){
    if(X-500 < 0) break;
    X -= 500;
    ans += 1000;
  }
  //cout << X << endl;
  while(true){
    if(X-5 < 0) break;
    X -= 5;
    ans += 5;
  }

  cout << ans << endl;

  return 0;
}


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

using namespace std;

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

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

    long long cumulativeSum = 0;

    for (int i = 1; i <= n; i++){

        cumulativeSum += abs(a[i]-a[i-1]);

    }
    cumulativeSum += abs(a[n]);

    // i番目を抜く
    for (int i = 1; i <= n; i++){
        long long temp = cumulativeSum;
        if (i == n){
            temp += abs(a[n-1]);
            temp -= abs(a[n-1] - a[n]);
            temp -= abs(a[n]);
            cout << temp << endl;
        }
        else{
            temp += abs(a[i-1] - a[i+1]);
            temp -= abs(a[i-1] - a[i]);
            temp -= abs(a[i] - a[i+1]);
            cout << temp << endl;
        }
    }

}

//#include <bits/stdc++.h>
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
//#include "boost/multiprecision/cpp_int.hpp"
//typedef boost::multiprecision::cpp_int ll;
typedef long double dd;
#define i_7 (ll)(1E9+7)
//#define i_7 998244353
#define i_5 i_7-2
ll mod(ll a){
    ll c=a%i_7;
    if(c>=0)return c;
    return c+i_7;
}
typedef pair<ll,ll> l_l;
ll inf=(ll)1E16;
#define rep(i,l,r) for(ll i=l;i<=r;i++)
#define pb push_back
ll max(ll a,ll b){if(a<b)return b;else return a;}
ll min(ll a,ll b){if(a>b)return b;else return a;}
void Max(ll &pos,ll val){pos=max(pos,val);}//Max(dp[n],dp[n-1]);
void Min(ll &pos,ll val){pos=min(pos,val);}
void Add(ll &pos,ll val){pos=mod(pos+val);}
dd EPS=1E-9;
#define fastio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define fi first
#define se second
////////////////////////////

ll po(ll i,ll p){
    if(p==0)return 1;
    else{
        i=mod(i);
        if(p==1)return i;
        if(p%2==0)return po(mod(i*i),p/2);
        return mod(i*po(i,p-1));
    }
}
ll bunbo(ll n){
    return po(n,i_5);
}
ll p1=1009;

int main(){fastio
    ll n;cin>>n;
    ll a[n+1],b[2*n];
    rep(i,0,n-1)cin>>a[i];
    a[n]=a[0];
    rep(i,0,n-1)cin>>b[i];
    rep(i,n,2*n-1)b[i]=b[i%n];
    ll c[n],d[2*n-1];
    rep(i,0,n-1)c[i]=a[i+1]^a[i];
    rep(i,0,2*n-2)d[i]=b[i+1]^b[i];
    ll sc[n+1],sd[2*n];
    sc[0]=0;sd[0]=0;
    rep(i,0,n-1){
        sc[i+1]=c[i]*po(p1,i);
        Add(sc[i+1],sc[i]);
    }
    vector<l_l>ans;
    rep(i,0,2*n-2){
        sd[i+1]=d[i]*po(p1,i);
        Add(sd[i+1],sd[i]);
    }
  //  rep(i,0,n)cout<<sc[i]<<" ";cout<<endl;
  //  rep(i,0,2*n-1)cout<<sd[i]<<" ";cout<<endl;
    rep(i,n,2*n-1){
        ll sum=mod(sd[i]-sd[i-n])*bunbo(po(p1,i-n));
        ll y=b[i]^a[0];
        if(mod(sum-sc[n])==0){
            if(i==n)ans.pb(l_l(0,y));
            else ans.pb(l_l(2*n-i,y));
        }
    }
    sort(ans.begin(),ans.end());
    for(auto x:ans){
        cout<<x.fi<<" "<<x.se<<endl;
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i,n) for(int i=0; i<(n); i++)
#define REP2(i,x,n) for(int i=x; i<(n); i++)
#define PRINT(x) cout<<(x)<<endl
#define INF 1000000000000 // 10^12
#define MOD 1000000007 // 10^9+7
#define PB push_back
#define MP make_pair

const int MAX_N = 100009;

int N, A[MAX_N];

int same_hat[MAX_N]; // same_hat[i]: 同色の帽子をi人が被っているような色の種類数

int main() {
    cin >> N;
    REP(i, N) cin >> A[i];

    REP(i, N) same_hat[i] = 0;
    same_hat[0] = 3;

    ll num_comb = 1;

    REP(i, N) {
        num_comb = num_comb * same_hat[A[i]] % MOD;
        same_hat[A[i]]--;
        same_hat[A[i] + 1]++;
    }

    PRINT(num_comb);

    return 0;
}

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

int main(){
  int N,a;cin>>N;
  vector<int> A(N+2,0);
  A[0]=3;
  long long c=1;
  REP(i,N){
    scanf("%d",&a);
    c*=(A[a]-A[a+1]++);
    c%=1000000007;
  }
  cout<<c<<endl;
}


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

const int MAXN = 10001;

int main()
{
  bool prime[MAXN];

  memset(prime, true, sizeof(prime));
  prime[0] = prime[1] = false;

  for (int i = 2; i * i < MAXN; i++){
    for (int j = i * i; j < MAXN; j += i){
      prime[j] = false;
    }
  }

  int n;
  while (scanf("%d", &n), n){
    for (int i = n;; i--){
      if (prime[i] && prime[i - 2]){
        printf("%d %d\n", i - 2, i);
        break;
      }
    }
  }

  return (0);
}

#include <iostream>
#include <vector>
#include <algorithm>
std::vector<int> twin_primes(int upper) {
	std::vector<bool> suspects(upper + 1, true);
	suspects[0] = suspects[1] = false;
	std::vector<int> res;
	for (auto i = 2; i <= upper; ++i) {
		if (suspects[i]) {
			if (suspects[i - 2])
				res.push_back(i);
			for (auto j = 2 * i; j <= upper; j += i)
				suspects[j] = false;
		}
	}
	return res;
}
int main() {
	int n;
	std::cin >> n;
	auto primes = twin_primes(10000);
	while (n != 0) {
		auto prime = *(std::upper_bound(primes.begin(), primes.end(), n) - 1);
		std::cout << prime - 2 << ' ' << prime << std::endl;
		std::cin >> n;
	}
}


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

using namespace std;

int main() {
  int n,k,L[100];
  cin >> n >> k;
  for(int i = 0; i < n; i++)
    cin >> L[i];

  sort(L,L+n);
  int sum = 0;
  for(int i = n-1; i >= n-k; i--)
    sum += L[i];

  cout << sum << endl;
}

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

int main()
{
    int N;
    cin >> N;
    vector<int> A(N);
    int count = 0;

    for (size_t i = 0; i < N; i++)
    {
        cin >> A[i];
    }

    while (1)
    {
        bool exist_add = false;
        for (auto &&i : A)
        {
            if (i % 2 == 1)
            {
                exist_add = true;
            }
        }

        if (exist_add)
            break;

        for (auto &&i : A)
        {
            i /= 2;
        }

        count += 1;
    }

    cout << count << endl;
}


#include "bits/stdc++.h"
#define fio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define endl '\n'
#define all(V) (V).begin(), (V).end()
using namespace std;
typedef long long ll;

int main() {fio;
	int n, m;
	cin >> n >> m;
	vector <vector <int>> a(n, vector <int> (m));
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < m; j++) {
			cin >> a[i][j];
			a[i][j]--;
		}
	}
	int g = 1e9;
	vector <bool> done(m, false);
	int iter = 0;
	while(iter < m) {
		vector <int> cnt(m, 0);
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < m; j++) {
				if(!done[a[i][j]]) {
					cnt[a[i][j]]++;
					break;
				}
			}
		}
		int el = max_element(all(cnt)) - cnt.begin();
		g = min(g, cnt[el]);
		done[el] = true;
		iter++;
	}
	cout << g << endl;

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
int read() {
    char cc = getchar(); int cn = 0, flus = 1;
    while(cc < '0' || cc > '9') {  if( cc == '-' ) flus = -flus;  cc = getchar();  }
    while(cc >= '0' && cc <= '9')  cn = cn * 10 + cc - '0', cc = getchar();
    return cn * flus;
}
const int N = 1000 + 5 ;
#define rep( i, s, t ) for( register int i = s; i <= t; ++ i )
int n, m, book[N], w[N][N], vis[N], can[N];
bool check( int x ) {
    memset( book, 0, sizeof(book) ), memset( can, 0, sizeof(can) ) ;
    int flag ; rep( i, 1, n ) vis[i] = 1, ++ book[w[i][vis[i]]] ;
    rep( i, 1, m ) {
        flag = 1 ;
        rep( j, 1, n ) {
            int u = w[j][vis[j]] ;
            if( book[u] > x ) {
                can[u] = 1; flag = 0 ;
                while( vis[j] <= n && can[w[j][vis[j]]] ) ++ vis[j] ;
                if( vis[j] > n ) return 0;
                ++ book[w[j][vis[j]]] ;
            }
        }
        if( flag ) return 1;
    }
    return 0 ;
}
signed main()
{
    n = read(), m = read() ;
    rep( i, 1, n ) rep( j, 1, m ) w[i][j] = read() ;
    int l = 1, r = n, ans = 0, mid ;
    while( l <= r ) {
        mid = ( l + r ) >> 1 ;
        if( check(mid) ) ans = mid, r = mid - 1 ;
        else l = mid + 1 ;
    }
    printf("%d\n", ans ) ;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
  int a,b,c,d;
  cin>>a>>b>>c>>d;
  if(a >= b){
    a = b;
  }
  if(c >= d){
    c = d;
  }
  cout<<a+c<<'\n';
  return 0;
}

#include <bits/stdc++.h>
#define ri register
#define int long long
using namespace std;
inline int read()
{
    int s=0, w=1; ri char ch=getchar();
    while(ch<'0'||ch>'9') { if(ch=='-') w=-1; ch=getchar(); }
    while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+(ch^48), ch=getchar();
    return s*w;
}
signed main()
{
    int a,b,c,d;
    a=read(), b=read(), c=read(), d=read();
    printf("%lld\n",min(a,b)+min(c,d));
    return 0;
}


// Date: 2020-02-29

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

typedef long long LL;
typedef long double LD;
typedef vector<int> VI;
typedef pair<LL, LL> pll;
typedef pair<int, int> pii;
#define FIO ios::sync_with_stdio(false);cin.tie(0)
#define rep(i, a, b) for(int i = int(a); i <= int(b); ++i)
#define per(i, b, a) for(int i = int(b); i >= int(a); --i)
#define mem(x, y) memset(x, y, sizeof(x))
#define all(x) (x).begin(),(x).end()
#define mk make_pair
#define pb push_back
#define fi first
#define se second
const LL INF = 1e18;
const LL mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const int N = 2e5 + 10;
template <typename T> void chkmax(T &x, T y) { x = max(x, y); }
template <typename T> void chkmin(T &x, T y) { x = min(x, y); }
LL qpow(LL x, LL y, LL MOD) {LL a=1; while(y){ if(y&1) a=a*x%MOD; x=x*x%MOD; y>>=1; } return a;}

int n, m, a, b;
LL fac[N], inv[N];

void add(LL& x, LL y) { x = (x + y) % mod; }

void init() {
    fac[0] = inv[0] = 1;
    rep(i, 1, N-1) fac[i] = fac[i-1] * i % mod, inv[i] = qpow(fac[i], mod-2, mod);
}

LL C(int x, int y) {
    return fac[x] * inv[y] % mod * inv[x-y] % mod;
}

LL cal(int x1, int y1, int x2, int y2) {
    //cout<<x1<<" "<<y1<<" "<<x2<<" "<<y2<<" "<< C(x2-x1+y2-y1, x2-x1) <<endl;
    return C(x2-x1+y2-y1, x2-x1);
}

int main() {
    init();
    scanf("%d%d%d%d", &n, &m, &a, &b);
    LL ans = 0;
    rep(i, 1, n-a) add(ans, cal(1, 1, i, b) * cal(i, b+1, n, m) % mod);
    printf("%lld\n", ans);

    return 0;
}

#include <bits/stdc++.h>
#define ll long long
#define INF LLONG_MAX
#define dd double
#define fi first
#define se second
#define rep(i,a,b) for(ll i=a;i<=b;i++)
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key
#define dll deque<long long>
#define qll queue<long long>
#define vll vector<long long>
#define vc vector<char>
#define vs vector<string>
#define vvll vector<vector<ll>>
#define vpll vector<pair<long long,long long>>
#define vpcl vector<pair<char,long long>>
#define vpsl vector<pair<string,long long>>
#define stll stack<long long>
#define stc stack<char>
#define mll map<long long,long long>
#define pll pair<long long,long long>
#define psl pair<string,long long>
#define mcl map<char,long long>
#define msl map<string,long long>
#define pcl pair<char,long long>
#define mmll multimap<long long,long long>
#define mmcl multimap<char,long long>
#define mmsl multimap<string,long long>
#define sll set<long long>
#define sc  set<char>
#define ss  set<string>
#define msll multiset<long long>
#define msc  multiset<char>
#define mss  multiset<string>
#define lb lower_bound
#define up upper_bound
#define lt length
#define clr clear
#define ap append
#define sz size
#define sub substr
#define ull unsigned long long int
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//#include <boost/multiprecision/cpp_int.hpp>
//using boost::multiprecision::cpp_int;
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>

ll modexp(ll x,ll n,ll p){    //modular exponential function
  if(n==0){return 1;}
  ll u = modexp(x,n/2,p);
  u = u*u%p;
  if(n%2==1){u=(u*x)%p;}
  return u;
}

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

ll arr[10001];
ll dp[10001][101][2];
ll m = 1e9+7;
ll f(ll idx,ll sum,ll flag,ll n,ll d){
    if(idx == n){
        if(sum==0){return 1;}
        else{return 0;}
    }
    if(dp[idx][sum][flag]!=-1){return dp[idx][sum][flag];}
    ll ans=0;
    if(flag==1){
        rep(i,0,arr[idx]){
            if(i<arr[idx]){
              ans = (ans + f(idx+1,(sum+i%d)%d,0,n,d)%m)%m;
            }else{
              ans = (ans + f(idx+1,(sum+i%d)%d,1,n,d)%m)%m;
            }
        }
    }
    else{
        rep(i,0,9){
             ans = (ans + f(idx+1,(sum+i%d)%d,0,n,d)%m)%m;
        }
    }
    dp[idx][sum][flag] = ans;
    return ans;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    memset(dp,-1,sizeof(dp));
    string k; cin>>k;
    ll d; cin>>d;
    ll n = k.length();
    rep(i,0,n-1){
     arr[i] = (ll)k[i]-48;
    }
    cout<<(f(0,0,1,n,d)-1+m)%m ;
}


#include<bits/stdc++.h>

using namespace std;

using ll   = long long;
using ii   = pair   < int , int >;
using i3   = pair   < int , ii  >;
using li   = pair   < ll  , int >;
using lii  = pair   < ll  , ii  >;
using pll  = pair   < ll  , ll  >;
using vi   = vector < int       >;
using vl   = vector < ll        >;
using vii  = vector < ii        >;
using vli  = vector < li        >;
using vpll = vector < pll       >;
using vi3  = vector < i3        >;
using vlii = vector < lii       >;

const int N = 2e5 + 5;
const ll INF = 1e17 + 7 , mod = 1e9 + 7;
const double eps = 1e-9 , PI = acos(-1);

int n;

ll mul(ll a , ll b) {
    return a%mod * (b%mod) % mod;
}

ll sum(ll a , ll b) {
    return (a%mod + b%mod) % mod;
}

ll a[N];
ll suff[N];

void solve(int testCase){

    scanf("%d" , &n);

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

    suff[n] = 0;

    for(int i = n - 1 ; i >= 0 ; i --) {
            suff[i] = sum(suff[i + 1] , a[i]);
    }

    ll ans = 0ll;
    for(int i = 0 ; i < n ; i ++) {
            ans = sum(ans , mul(a[i] , suff[i + 1]));
    }

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

main(){

    int t = 1;
    // scanf("%d" , &t);

    for(int testCase = 1 ; testCase <= t ; testCase ++){
            solve(testCase);
    }

    return 0;
}

/*input
4
141421356 17320508 22360679 244949
*/
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
const int M=1e9+7;
int n, sq=0, s=0;
int a[200005];

signed main()
{
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin>>n;
	for(int i=1; i<=n; ++i){
		cin>>a[i];
	}
	for(int i=2; i<=n; ++i){
		s += a[i]*(a[i-1])%M;
		s %= M;
		(a[i] += a[i-1]) %= M;;
	}
	cout<<s;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define fr first
#define sc second
#define pb push_back
const ll mod = 1e9+7;
const int N = 2e5+205;
void solve(){
	int n;
	cin>>n;
	vector<ll>v;
	for(int i=0;i<n;i++){
		ll x;
		cin>>x;
		v.pb(x);
	}
	sort(v.begin(),v.end());
	ll ans = 0;
	for(int i=0;i+1<n;i+=2){
		if(v[i]+v[i+1]>-v[i]-v[i+1]){
			ans+=v[i]+v[i+1];
		}
		else{
			ans+=(-v[i]-v[i+1]);
		}
	}
	if(n&1)ans+=v[n-1];
	cout<<ans<<endl;
}
int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int t=1;
//	cin>>t;
	while(t--){
		solve();
	}
	return 0;
}
/// 2:3 /// 2,3
/// 1:1 /// 3,3
/// aa/bb =x/y
/// bb*x/y = aa
/// bb = aa*y/x

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

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

long long MOD = 10000000000 + 7;

int main(){
  cout << setprecision(10);

  int N; cin >> N;
  vector<ll> v(N);
  ll sum = 0;
  int num = 0;

  for(int i = 0; i < N; i++){
    cin >> v[i];

    if(v[i] < 0){
      num++;
      v[i] = abs(v[i]);
    }
  }

  if(num % 2 == 0){
    for(int i = 0; i < N; i++){
      sum += v[i];
    }
  } else {
    sort(v.begin(), v.end());
    for(int i = 0; i < N; i++){
      if(i==0){
        sum -= v[i];
      } else {
        sum += v[i];
      }
    }
  }

  cout << sum << endl;
}


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

static const long long INFTY = (1LL << 32);

void WarshallFloyd(long long weight[][100], int verNum) {

	//k????¢??????????????????????????????°????¢??????????????????????????±???????
	//???????????????????¨???????
	for (int k = 0; k < verNum; k++)
		for (int i = 0; i < verNum; i++) {
			//?????????????????£?????????????????§??´??°???????????????????????????
			if (weight[i][k] == INFTY) continue;
			for (int j = 0; j < verNum; j++) {
				if (weight[k][j] == INFTY) continue;
				weight[i][j] = min(weight[i][j], weight[i][k] + weight[k][j]);
			}
		}
}

int main() {

	long long weight[100][100];
	int verNum;
	int edgeNum;
	cin >> verNum >> edgeNum;

	//Initialize
	for (int i = 0; i < verNum; i++)
		for (int j = 0; j < verNum; j++)
			weight[i][j] = (i == j) ? 0 : INFTY;

	for (int i = 0; i < edgeNum; i++) {
		int first_ver, second_ver, wei;
		cin >> first_ver >> second_ver >> wei;
		weight[first_ver][second_ver] = wei;
	}

	WarshallFloyd(weight, verNum);

	//?????¨???
	bool negative = false;
	for (int i = 0; i < verNum; i++)
		if (weight[i][i] < 0) negative = true;

	if (negative)
		cout << "NEGATIVE CYCLE" << endl;
	else {
		for (int i = 0; i < verNum; i++) {
			for (int j = 0; j < verNum; j++) {
				if (j) cout << " ";

				string verVal;
				if (weight[i][j] == INFTY) verVal = "INF";
				else                                  verVal = to_string(weight[i][j]);

				cout << verVal;
			}
			cout << endl;
		}
	}

	return 0;
}

#include <bits/stdc++.h>

#ifndef LOCAL_
#define fprintf if( false ) fprintf
#endif // LOCAL_
// #define dump() fprintf(stderr, "#%s.%d\n", __func__, __LINE__);
#define dumpl(x1) fprintf(stderr, "#%s.%d (%s) = (%ld)\n", __func__, __LINE__, #x1, x1);
#define dumpll(x1, x2) fprintf(stderr, "#%s.%d (%s, %s) = (%ld, %ld)\n", __func__, __LINE__, #x1, #x2, x1, x2);
#define dumplll(x1, x2, x3) fprintf(stderr, "#%s.%d (%s, %s, %s) = (%ld, %ld, %ld)\n", __func__, __LINE__, #x1, #x2, #x3, x1, x2, x3);
#define dumpd(x1) fprintf(stderr, "#%s.%d (%s) = (%lf)\n", __func__, __LINE__, #x1, x1);
#define dumpdd(x1, x2) fprintf(stderr, "#%s.%d (%s, %s) = (%lf, %lf)\n", __func__, __LINE__, #x1, #x2, x1, x2);
#define loop for(;;)

template<typename T> void scan1(T& x) { fprintf(stderr, "unknown type\n"); }
template<> void scan1(long& x) { if( scanf("%ld", &x) < 0 ) exit(0); }
void scan() {}
template<typename Head, typename... Tail>
void scan(Head& x, Tail&... xs) {
  scan1(x); scan(xs...);
}

template<typename W>
struct N003 {
   typedef std::vector<long> LI;
   typedef std::vector<W>    LW;
   long n, e;
   LI ss, ds;
   LW ws;
   std::vector<LI> iss;
   N003(long n_, const LI& ss_, const LI& ds_, const LW& ws_)
      : n(n_), e(ss_.size()), ss(ss_), ds(ds_), ws(ws_) {
      iss.resize(n+1);
      for(long i = 0; i < e; ++i) {
         iss[ss[i]].push_back(i);
      }
   }
};

template<typename W>
struct N006 {
   std::vector<std::vector<W>> xss;
   bool negativeCycle;
   N006(const N003<W>& g, W inf) : negativeCycle(false) {
      xss.resize(g.n+1);
      for(auto& xs : xss) xs.resize(g.n+1, inf);
      for(long i = 0; i < g.n+1; ++i) xss[i][i] = 0;
      for(long i = 0; i < g.e; ++i) {
         xss[g.ss[i]][g.ds[i]] = g.ws[i];
      }
      for(long z = 0; z < g.n+1; ++z) {
         for(long x = 0; x < g.n+1; ++x) {
            for(long y = 0; y < g.n+1; ++y) {
               W d = xss[x][y];
               W nd = xss[x][z] + xss[z][y];
               xss[x][y] = std::min(d, nd);
            }
         }
      }
      for(long x = 0; x < g.n+1; ++x) {
         for(long y = 0; y < g.n+1; ++y) {
            if( xss[x][y] >= inf / 2 ) {
               xss[x][y] = inf;
            }
         }
         if( xss[x][x] < 0 ) {
            negativeCycle = true;
         }
      }
   }
};

struct Solver {
   Solver() { fprintf(stderr, "--------Solver begin--------\n"); }
   ~Solver() { fprintf(stderr, "--------Solver end--------\n"); }
   void solve() {
      typedef std::vector<long> LI;
      long v, e;
      scan(v, e);
      LI ss(e+1), ds(e+1);
      LI ws(e+1);
      for(long i = 0; i < e; ++i) {
         scan(ss[i], ds[i], ws[i]);
      }
      N003<long> g(v, ss, ds, ws);
      N006<long> warshallfloyd(g, (1LL << 60));
      if( warshallfloyd.negativeCycle ) {
         puts("NEGATIVE CYCLE");
      }
      else {
         for(long i = 0; i < v; ++i) {
            for(long k = 0; k < v; ++k) {
               if( warshallfloyd.xss[i][k] == (1LL << 60) ) {
                  printf("INF");
               }
               else {
                  printf("%ld", warshallfloyd.xss[i][k]);
               }
               putchar( k == v - 1 ? '\n' : ' ');
            }
         }
      }
   }
};

int main() {
  loop std::unique_ptr<Solver>(new Solver())->solve();
}


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

#define dump(...) cout<<"# "<<#__VA_ARGS__<<'='<<(__VA_ARGS__)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define peri(i,a,b) for(int i=int(b);i-->int(a);)
#define rep(i,n) repi(i,0,n)
#define per(i,n) peri(i,0,n)
#define all(c) begin(c),end(c)
#define mp make_pair
#define mt make_tuple

using uint=unsigned;
using ll=long long;
using ull=unsigned long long;
using vi=vector<int>;
using vvi=vector<vi>;
using vl=vector<ll>;
using vvl=vector<vl>;
using vd=vector<double>;
using vvd=vector<vd>;
using vs=vector<string>;

template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& p){
	return os<<'('<<p.first<<','<<p.second<<')';
}

template<typename Tuple>
void print_tuple(ostream&,const Tuple&){}
template<typename Car,typename... Cdr,typename Tuple>
void print_tuple(ostream& os,const Tuple& t){
	print_tuple<Cdr...>(os,t);
	os<<(sizeof...(Cdr)?",":"")<<get<sizeof...(Cdr)>(t);
}
template<typename... Args>
ostream& operator<<(ostream& os,const tuple<Args...>& t){
	print_tuple<Args...>(os<<'(',t);
	return os<<')';
}

template<typename Ch,typename Tr,typename C>
basic_ostream<Ch,Tr>& operator<<(basic_ostream<Ch,Tr>& os,const C& c){
	os<<'[';
	for(auto i=begin(c);i!=end(c);++i)
		os<<(i==begin(c)?"":" ")<<*i;
	return os<<']';
}

constexpr int INF=1e9;
constexpr int MOD=1e9+7;
constexpr double EPS=1e-9;

struct FenwickTree{
	vector<ll> data;
	FenwickTree(int n):data(n+1){}
	void Add(int i,int x){
		for(i++;i<data.size();i+=i&-i)
			data[i]+=x;
	}
	ll Sum(int i){
		ll res=0;
		for(;i;i-=i&-i)
			res+=data[i];
		return res;
	}
	ll Sum(int i,int j){
		return Sum(j)-Sum(i);
	}
};

ll InversionNumber(const vi& a)
{
	FenwickTree ft(a.size());
	ll res=0;
	rep(i,a.size()){
		res+=ft.Sum(a[i],a.size());
		ft.Add(a[i],1);
	}
	return res;
}

bool test(vector<tuple<int,int,int>> src,vector<tuple<int,int,int>> dst)
{
	int n=src.size();
	rep(i,n){
		int x,y,z;
		tie(x,y,z)=src[i];
		src[i]=x<z?mt(x,y,z):mt(z,y,x);
		tie(x,y,z)=dst[i];
		dst[i]=x<z?mt(x,y,z):mt(z,y,x);
	}
	sort(all(src)); sort(all(dst));
	return src==dst;
}

tuple<bool,bool> calc(vector<tuple<int,int,int>> src,vector<tuple<int,int,int>> dst)
{
	int m=src.size(),flip=0;
	rep(i,m){
		int x,y,z;
		tie(x,y,z)=src[i];
		src[i]=x<z?mt(x,y,z):(flip++,mt(z,y,x));
		tie(x,y,z)=dst[i];
		dst[i]=x<z?mt(x,y,z):(flip++,mt(z,y,x));
	}
	map<tuple<int,int,int>,int> f;
	rep(i,m) f[dst[i]]=i;
	vi a(m);
	rep(i,m) a[i]=f[src[i]];
	return mt(InversionNumber(a)&1,flip&1);
}

bool solve(int n,vvi grid)
{
	array<bool,2> inv,flip;
	rep(i,2){
		vector<tuple<int,int,int>> src,dst;
		rep(j,n) if(j%2==i){
			if(j/2%2==0){
				src.emplace_back(mt(3*j+1,3*j+2,3*j+3));
				dst.emplace_back(mt(grid[0][j],grid[1][j],grid[2][j]));
			}
			else{
				src.emplace_back(mt(3*j+3,3*j+2,3*j+1));
				dst.emplace_back(mt(grid[2][j],grid[1][j],grid[0][j]));
			}
		}
		if(!test(src,dst)) return false;
		tie(inv[i],flip[i])=calc(src,dst);
	}
	return inv[0]==flip[1]&&inv[1]==flip[0];
}

int main()
{
	for(int n;cin>>n&&n;){
		vvi grid(3,vi(n));
		rep(i,3) rep(j,n) cin>>grid[i][j];
		cout<<(solve(n,grid)?"Yes":"No")<<endl;
	}
}

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cassert>
#include <iostream>
#include <cctype>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <utility>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <bitset>
#include <complex>
#include <fstream>
#include <iomanip>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
template<class T> T RoundOff(T a){ return int(a+.5-(a<0)); }
template<class T, class C> void chmax(T& a, C b){ if(a < b) a = b; }
template<class T, class C> void chmin(T& a, C b){ if(b < a) a = b; }
template<class T, class C> pair<T, C> mp(T a, C b){ return make_pair(a, b); }

int main()
{
	int n;
	while(cin >> n && n)
	{
		vint v(10, 0);

		rep(i, n)
		{
			int d;
			cin >> d;

			v[d]++;
		}

		rep(i, 10)
		{
			if(v[i]) rep(j, v[i]) cout << "*";
			else cout << "-";

			cout << endl;
		}
	}
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair <int, int>
#define pll pair <ll, ll>
#define pci pair <char, int>
#define pld pair <ld, ld>
#define ppld pair <pld, pld>
#define ppll pair <pll, pll>
#define pldl pair <ld, ll>
#define vll vector <ll>
#define vvll vector <vll>
#define vpll vector <pll>
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define mll map <ll, ll>
#define fastmap gp_hash_table
#define cd complex <double>
#define vcd vector <cd>
#define PI 3.14159265358979
#define ordered_set tree <ll, null_type, less <ll>, rb_tree_tag, tree_order_statistics_node_update>
#pragma 03
using namespace std;
using namespace __gnu_pbds;
ll a[200005], b[200005], c[400005], d[200005], lps[200005];
ll n;
void clps(){
    ll l = 0; lps[0] = 0; ll i = 1;
    while (i < n){
        if (d[i] == d[l]) l++, lps[i] = l, i++;
        else if (l != 0) l = lps[l - 1];
        else lps[i] = 0, i++;
    }
}
vll fp;
void kmp(){
  	clps();
    ll i = 0, j = 0;
    while (i < 2 * n - 1){
        if (d[j] == c[i]){
            j++; i++;
        }
        if (j == n){
			fp.pb(i - j);
            j = lps[j - 1];
        }
        else if ((i < 2 * n - 1) && (d[j] != c[i])){
            if (j != 0) j = lps[j - 1];
            else i++;
        }
    }
}
int main(){
	cin >> n;
	for (ll i = 0; i < n; i++) cin >> a[i];
	for (ll i = 0; i < n; i++) cin >> b[i];
	for (ll i = 0; i < n; i++) c[i] = a[i] ^ a[(i + 1) % n];
	for (ll i = 0; i < n; i++) d[i] = b[i] ^ b[(i + 1) % n];
	for (ll i = n; i < 2 * n - 1; i++) c[i] = c[i - n];
	kmp();
	for (ll i = 0; i < fp.size(); i++) cout << fp[i] << " " << (b[0] ^ a[fp[i]]) << endl;
}


#include<cstdio>
#include<algorithm>
typedef long long lint;
lint doz[23];
lint D;

lint dfs(int l,int r,lint d)
{
	if(l>=r)
	{
		if(d) return 0;
		return l==r?10ll:1ll;
	}
	if(d%10==0)
	{
		if(l==1) return dfs(l+1,r-1,d/10)*9;
		return dfs(l+1,r-1,d/10)*10;
	}
	lint di=d%10-10;
	lint de=d+di*(doz[r-l]-1ll);
	if(l==1) return dfs(l+1,r-1,(de>0?de:-de)/10)*(di+9);
	return dfs(l+1,r-1,(de>0?de:-de)/10)*(di+10);
}

int main()
{
	scanf("%lld",&D);
	doz[0]=1;for(int i=1;i<=18;i++) doz[i]=doz[i-1]*10ll;
	lint ans=0;
	for(int i=2;i<=18;i++) ans+=dfs(1,i,D);
	printf("%lld\n",ans);
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

template<typename F>
struct FixPoint : F{
  FixPoint(F&& f):F(forward<F>(f)){}
  template<typename... Args>
  decltype(auto) operator()(Args&&... args) const{
    return F::operator()(*this,forward<Args>(args)...);
  }
};
template<typename F>
inline decltype(auto) MFP(F&& f){
  return FixPoint<F>{forward<F>(f)};
}

__int128_t abs128(__int128_t val){return val<0?-val:val;}

ostream &operator<<(ostream &os,__int128_t val){
  if(ostream::sentry(os)){
    __uint128_t tmp=abs128(val);
    char buf[64];
    char *d=end(buf);
    do{
      --d;
      *d=char(tmp%10+'0');
      tmp/=10;
    }while(tmp);
    if(val<0) *--d='-';
    int len=end(buf)-d;
    if(os.rdbuf()->sputn(d,len)!=len){
      os.setstate(ios_base::badbit);
    }
  }
  return os;
}

istream &operator>>(istream &is,__int128_t &val){
  string s;
  is>>s;
  val=0;
  for(int i=0;i<(int)s.size();i++)
    if(isdigit(s[i])) val=val*10+s[i]-'0';
  if(s[0]=='-') val*=-1;
  return is;
}

//INSERT ABOVE HERE
signed main(){
  using ll = __int128_t;

  ll d;
  cin>>d;
  const ll MAX = 37;
  ll ans=0;

  vector<ll> po(MAX,1);
  for(int i=0;i+1<MAX;i++)
    po[i+1]=po[i]*10;

  vector<ll> c1(12,0),c2(12,0);
  for(int i=1;i<10;i++)
    for(int j=1;j<=i;j++)
      c1[abs(i-j)]++;

  for(int i=0;i<10;i++)
    for(int j=0;j<=i;j++)
      c2[abs(i-j)]++;

  for(int l=2;l<=MAX;l++){
    // [s, t]
    ans+=
      MFP([&](auto dfs,ll s,ll t,ll w)->ll{
            if(s>=t){
              if(w!=0) return 0;
              return s==t?10:1;
            }
            ll dif=po[t]-po[s];
            ll x=w/dif;
            if(x>=10) return 0;

            ll res=0;
            res+=(s==0?c1:c2)[x+0]*dfs(s+1,t-1,abs128(w-(x+0)*dif));
            res+=(s==0?c1:c2)[x+1]*dfs(s+1,t-1,abs128(w-(x+1)*dif));
            return res;
          })(0,l-1,d);
  }
  cout<<ans<<endl;
  return 0;
}