//include
//------------------------------------------
#include <string>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>

#include <numeric>
#include <utility>
#include <complex>

#include <sstream>
#include <iostream>
#include <iomanip>

#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <ctime>

// namespace
using namespace std;

// type alias
using ll = long long;
using ull = unsigned long long;
using comp = complex<double>;

// constant
static const ll MOD = 1000000007LL;
static const double PI = 3.14159265358979323846;

// conversion
inline ll toint(string s) { ll v; istringstream sin(s); sin >> v; return v; }
template<class t> inline string tostring(t x) { ostringstream sout; sout << x; return sout.str(); }

// print
#define RET(x) return cout << x << endl, 0;

// for loop
#define REP(i, a, b) for ((i) = (ll)(a);(i) < (ll)(b);(i)++)
#define REPD(i, a, b) for (ll i = (ll)(a);(i) < (ll)(b);(i)++)
#define REPI(v, vs) for (auto& v : vs)

//debug
#define DUMP(x)  cerr << #x << " = " << (x) << endl
#define DEBUG(x) cerr << #x << " = " << (x) << " (l" << __LINE__ << ")" << " " << __FILE__ << endl

#define MAX_VALUE 9223372036854775807

template<class Integer>
class BinaryIndexedTree
{
private:
	ull maxSize_;
	vector<Integer> values_;
public:
	BinaryIndexedTree(ull maxSize) : maxSize_(maxSize), values_(maxSize_, 0) {}

	void update(ll index, Integer increment)
	{
		if (maxSize_ <= index)
		{
			throw invalid_argument("Index should be less than maxSize.");
		}
		++index;

		while (index <= maxSize_)
		{
			values_[index - 1] += increment;
			index += index & -index;
		}
	}

	Integer query(ll index)
	{
		if (maxSize_ <= index)
		{
			throw invalid_argument("Index should be less than maxSize.");
		}
		++index;

		Integer res = 0;
		while (index > 0)
		{
			res += values_[index - 1];
			index -= index & -index;
		}
		return res;
	}
};
using bit = BinaryIndexedTree<ll>;

int solve() {
	ll n, q;
	cin >> n >> q;

	vector<ll> as(n);
	vector<tuple<ll, ll, ll>> rlis;
	ll l, r;
	REPD(i, 0, n) cin >> as[i];
	REPD(i, 0, q){
		cin >> l >> r;
		--l, --r;
		rlis.emplace_back(r, l, i);
	}
	sort(rlis.begin(), rlis.end());

	bit counters(n);
	map<ll, ll> color_to_max_idx;
	vector<ll> results(q);
	ll idx, curr_r = 0;
	REPD(i, 0, q) {
		tie(r, l, idx) = rlis[i];
		while(curr_r <= r) {
			counters.update(curr_r, 1LL);
			if (color_to_max_idx.count(as[curr_r]) != 0LL) {
				counters.update(color_to_max_idx[as[curr_r]], -1LL);
			}
			color_to_max_idx[as[curr_r]] = curr_r;
			++curr_r;
		}
		results[idx] = counters.query(r) - (l > 0LL ? counters.query(l - 1LL) : 0LL);
	}

	REPD(i, 0, q) cout << results[i] << endl;

	return 0;
}

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

	solve();
	// ll t;
	// cin >> t;
	// REPD(i, 0, t) solve();

	return 0;
}

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define rep(i,N) for(int i=0;i<(int)N;i++)
#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
static inline void PUT(char c)
{
  static char buf[1<<15],*ptr=buf;
  if(ptr==buf+strlen(buf)||c==0){fwrite(buf,1,ptr-buf,stdout),ptr=buf;}*ptr++=c;
}
static inline int IN(void)
{
  int x=0,f=0,c=getchar();while(c<48||c>57){f^=c==45,c=getchar();}
  while(c>47&&c<58){x=x*10+c-48,c=getchar();}return f?-x:x;
}
static inline void OUT(int a)
{
  if(a<0)PUT('-'),a=-a;
  int d[40],i=0;do{d[i++]=a%10;}while(a/=10);
  while(i--){PUT(d[i]+48);}PUT('\n');
}
int N=0;
static inline void Add(int *fwt,int i,int v){while(i<N){fwt[i]+=v;i|=i+1;}}
static inline int Sum(int *fwt,int i){int v=0;while(i>=0){v+=fwt[i];i=(i&(i+1))-1;}return v;}
int main()
{
  N=IN();int Q=IN(),after[500001],dat[500000],fenwick[500000],id[500001],left[500001],pos[500000],result[500000];
  rep(i,N){dat[i]=IN()-1;}
  rep(i,Q){left[i+1]=IN()-1;int r=IN()-1;after[i+1]=id[r];id[r]=i+1;}
  for(int r=0;r<N;r++)
  {
    if(pos[dat[r]]){Add(fenwick,pos[dat[r]]-1,-1);}
    Add(fenwick,r,1);
    for(int i=id[r];i;i=after[i]){result[i-1]=Sum(fenwick,r)-Sum(fenwick,left[i]-1);}
    pos[dat[r]]=r+1;
  }
  rep(i,Q){OUT(result[i]);}
}


#include <bits/stdc++.h>
#define loop2(i, s, n, a) for (int i = int(s); i < int(n); i += a)
#define loop(i, s, n) loop2(i, s, n, 1)
#define rep(i, n) loop(i, 0, n)

#define pb push_back
#define all(in) in.begin(),in.end()

using ll = long long;
using ull = unsigned long long;
using namespace std;

const ll MOD = 1e9+7;
ll dp[101][8];

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

    if(w == 1){ cout << 1 << endl; return 0; }

    dp[0][0] = 1;
    rep(i,h){
        rep(j,1<<(w-1)){
            vector<bool> count(w-1,false);
            rep(k,w-1) if(j & 1<<k) count[k] = true;
            bool check = false;
            rep(k,w-2) if(count[k] && count[k+1]) check = true;
            if(check) continue;

            rep(k,w-1){
                if(count[k]){
                    dp[i+1][k+1] += dp[i][k];
                    dp[i+1][k+1] %= MOD;
                    dp[i+1][k] += dp[i][k+1];
                    dp[i+1][k] %= MOD;
                }
                else if(k == 0){
                    dp[i+1][0] += dp[i][0];
                    dp[i+1][0] %= MOD;
                }
                else if(!count[k-1]){
                    dp[i+1][k] += dp[i][k];
                    dp[i+1][k] %= MOD;
                }
                if(!count[k] && k == w-2){
                    dp[i+1][w-1] += dp[i][w-1];
                    dp[i+1][w-1] %= MOD;
                }
            }
        }
    }

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

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1005,M=1e9+7;
int n,A,B,C,D,fac[N],inv[N],dp[N][N];
int ksm(int x,int y){
	int ans=1;
	for (;y;y>>=1,x=x*x%M)
		if (y&1)(ans*=x)%=M;
	return ans;
}
signed main(){
	scanf("%lld%lld%lld%lld%lld",&n,&A,&B,&C,&D);
	dp[0][A-1]=1;
	fac[0]=inv[0]=1;
	for (int i=1;i<=n;i++)fac[i]=fac[i-1]*i%M,inv[i]=ksm(fac[i],M-2);
	for (int i=0;i<=n;i++)
		for (int j=A;j<=B;j++){
			dp[i][j]=dp[i][j-1];
			for (int k=C,p=ksm(inv[j],C);k<=D;k++,(p*=inv[j])%=M){
				if (i-j*k<0)break;
				(dp[i][j]+=dp[i-j*k][j-1]*p%M*inv[k])%=M;
			}
		}
	printf("%lld\n",dp[n][B]*fac[n]%M);
}


#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
typedef double db;

#define fr first
#define sc second
#define pb push_back
#define rep(i,x) for(ll i=0;i<x;i++)
#define rep1(i,x) for(ll i=1;i<=x;i++)
#define rrep(i,x) for(ll i=x-1;i>=0;i--)
#define rrep1(i,x) for(ll i=x;i>0;i--)

ll ans=0;
ll n;
string s;
ll cnt=0;
P p[1000001];
ll sz;
int main()
{
	cin>>s;
	sz=0;
	n=s.length();
	rep(i,n){
		if(i==0){
			if(s[0]=='J') p[sz]=P(1,0);
			else if(s[0]=='O') p[sz]=P(1,1);
			else p[sz]=P(1,2);
			sz++;
		}
		else{
			if(s[i]!=s[i-1]){
				if(s[i]=='J') p[sz]=P(1,0);
				else if(s[i]=='O') p[sz]=P(1,1);
				else p[sz]=P(1,2);
				sz++;
			}
			else{
				p[sz-1].fr++;
			}
		}
	}
	rep(i,sz-2){
		if(p[i].sc==0&&p[i+1].sc==1&&p[i+2].sc==2&&p[i].fr>=p[i+1].fr&&p[i+1].fr<=p[i+2].fr){
			ans = max(ans , p[i+1].fr );
		}
	}
	cout<<ans<<endl;
}

#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#include<cmath>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int,int> pii;
const int inf = 0x3f3f3f3f,oo = inf;
#define Rep(i,a,b) for(RG int i=(a);i<=int(b);++i)
#define Dep(i,a,b) for(RG int i=(a);i>=int(b);--i)
#define rep(i,a,b) for(RG int i=(a);i<int(b);++i)
#define pi 3.14159265358979323846
#define IL inline
#define pc putchar
#define RG register
#define gc getchar
#define fi first
#define se second
#define debug(x) printf(#x" = %d\n",x);
//char __wzp[1<<15|1],*__S=__wzp+32768;
//#define gc() (__S>=__wzp+32768?(__wzp[fread(__wzp,sizeof(char),1<<15,stdin)]=EOF),*((__S=__wzp)++):*(__S++))
#define rd read
#define mem(x,v) memset(x,v,sizeof(x))
#define sqr(x) ((x)*(x))
#define lowbit(x) ((x)&(-(x)))
inline ll read(){
	RG ll x=0,f=1;RG char c=gc();
	for(;!isdigit(c);c=gc())if(c=='-')f=-1;
	for(;isdigit(c);c=gc())x=(x<<1)+(x<<3)+(c^48);
	return x*f;
}
IL double readdb(){
    RG double x=0,p=0.1;RG char f=0,c=gc();
    for(;!isdigit(c);c=gc())f|=(c=='-');
    for(;isdigit(c);c=gc())x=x*10+(c^48);
    if(c=='.')for(c=gc();isdigit(c);c=gc(),p/=10)x=x+(c^48)*p;
    return f?-x:x;
}
#define rdb() readdb()
void write(ll x){if(x<0)x=-x,pc('-');if(x>=10)write(x/10);putchar(x%10+'0');}
void writeln(ll x){write(x);puts("");}
int color[2][1005][1005];
int db[2<<17],n;
const int dx[] = {1,1,-1,-1};
const int dy[] = {1,-1,1,-1};
void dfs(int x,int y,int d,int col,int c){
	color[c][x][y] = col;
	for(int i=0;i*i<=d;i++){
		if(db[d-i*i]<0)continue;
		int j = db[d-i*i];
		rep(r,0,4){
			int tx = x+dx[r]*i;
			int ty = y+dy[r]*j;
			if(tx<0||ty<0||tx>=2*n||ty>=2*n||color[c][tx][ty]) continue;
			dfs(tx,ty,d,col==1?2:1,c);
		}
	}
}
int main(){
	n = rd();
	rep(i,0,2<<17) db[i] = -1;
	for(int i=0;i*i<(2<<17);++i) db[i*i] = i;
	rep(i,0,2){
		int d = rd();
		rep(x,0,2*n)
			rep(y,0,2*n)
				if(!color[i][x][y])
					dfs(x,y,d,1,i);
	}
	vector<pair<int,int> > a;
	rep(x,0,2*n)
		rep(y,0,2*n)
			if(color[0][x][y]==1&&color[1][x][y]==1)
				a.push_back(make_pair(x,y));
	rep(i,0,n*n)
		printf("%d %d\n",a[i].fi,a[i].se);
}


# include <bits/stdc++.h>
using namespace std ;
bool is_war ( const vector <int >& x , const vector <int >& y ) {
for ( int Z = -100; Z <= 100; ++ Z ) {
bool is_ok = true ;
for ( int i = 0; i < x . size (); ++ i ) {
if ( x [ i ] >= Z ) is_ok = false ;
}
for ( int i = 0; i < y . size (); ++ i ) {
if ( y [ i ] < Z ) is_ok = false ;
}
if ( is_ok ) return false ;
}
return true ;
}
int main () {
int N , M ; cin >> N >> M ;
vector <int > x ( N +1) , y ( M +1);
cin >> x [0] >> y [0];
for ( int i = 0; i < N ; ++ i ) {
cin >> x [ i +1];
}
for ( int i = 0; i < M ; ++ i ) {
cin >> y [ i +1];
}
if ( is_war (x , y )) {
cout << " War " << endl ;
}
else {
cout << "No War " << endl ;
}
}

#include <bits/stdc++.h>

#define ll long long
#define sz(x) (int)x.size()

using namespace std;

int main(){
	int n,m,x,y;
	cin>>n>>m>>x>>y;
	vector<int> a(n),b(m);
	for(int i=0;i<n;i++){
		cin>>a[i];
	}
	for(int i=0;i<m;i++){
		cin>>b[i];
	}
	sort(a.begin(), a.end());
	sort(b.begin(), b.end());
	for(int i=0;i<101;i++){
		if(a[n-1]<i && i<=b[0] && i>x && i<y){
			cout<<"No War";
			return 0;
		}
	}
	cout<<"War";
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
using namespace std;
typedef pair<int, int> P;
int k, a[100000], dist[100000];
vector<int> v[100000];
vector<P> w;
bool b[100000];
void cal(int x, int y){
    dist[x] = y;
    b[x] = false;
    w.push_back(P(y, x));
    for(int i=0;i<v[x].size();i++){
        cal(v[x][i], y+1);
    }
}
int hin(int x, int y){
    if(b[x])return 0;
    b[x] = true;
    if(x == 0 || (a[x] == 0 && y == k-1))return 0;
    if(y == k-1)return 1;
    return hin(a[x], y+1);
}
int main() {
    int n, ans = 0;
    cin >> n >> k >> a[0];
    if(--a[0] != 0)ans++;
    for(int i=1;i<n;i++){
        cin >> a[i];
        v[--a[i]].push_back(i);
    }
    cal(0, 0);
    sort(w.begin(), w.end(), greater<P>());
    for(int i=0;i<w.size();i++){
        int p = w[i].second;
        if(!b[p]){
            ans += hin(p, 0);
        }
    }
    cout << ans << endl;
	return 0;
}

#include <iostream>
#include <array>
#include <algorithm>
#include <vector>
#include <bitset>
#include <set>
#include <unordered_set>
#include <cmath>
#include <complex>
#include <deque>
#include <iterator>
#include <numeric>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <limits>
#include <iomanip>
#include <functional>
#include <cassert>
using namespace std;

using ll=long long;
template<class T> using V = vector<T>;
template<class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define ALL(v) v.begin(),v.end()
template < class T > inline bool chmax(T& a, T b) {if (a < b) { a=b; return true; } return false; }
template < class T > inline bool chmin(T& a, T b) {if (a > b) { a=b; return true; } return false; }
#define DEBUG_VLL(vec) for(int sz=0;sz<int(vec.size());sz++) std::cerr<<vec[sz]<<(sz==vec.size()-1?'\n':' ');

const long long MOD = 1000000007;
const long long HIGHINF = (long long)1e18;
const int INF = (int)1e9;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, k; cin >> n >> k;
    V<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        a[i]--;
    }
    V< V<int> > edges(n);
    for (int i = 1; i < n; i++) edges[a[i]].emplace_back(i);

    int ans = 0;
    auto dfs = [&](auto && self, int i) -> int {
        int d = 0;
        for (int e: edges[i]) {
            int chd = self(self, e);
            if (chd == k && i != 0) ans++;
            else chmax(d, chd);
        }
        return d + 1;
    };
    dfs(dfs, 0);
    if (a[0] != 0) ans++;
    cout << ans << '\n';
    return 0;
}


//Name: Space Coconut Crab
//Level: 2
//Category: 数学,探索
//Note:

/**
 * zの範囲は0≦z≦E^(1/3)、yの範囲は0≦y≦E^(1/2)で表される。
 * zとyが決まればxも一意に決まるので、zとyを全探索し、x,y,z≧0を満たすような組み合わせについてx+y+zの最小値を求めればよい。
 *
 * オーダーは O(E^(5/6))。
 */
#include <iostream>
#include <algorithm>

using namespace std;

bool solve(bool first) {
    int E;
    if(!(cin >> E)) return false;
    if(!E) return false;

    int ans = E;
    for(int z = 0; z*z*z <= E; ++z) {
        for(int y = 0; y*y <= E; ++y) {
            const int x = E - z*z*z - y*y;
            if(x >= 0) {
                ans = min(ans, x+y+z);
            }
        }
    }
    cout << ans << endl;
    return true;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout.setf(ios::fixed);
    cout.precision(10);

    bool first = true;
    while(solve(first)) {
        first = false;
    }
    return 0;
}

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

using namespace std;

#define FOR(i,k,n) for(int i=(k); i<(int)n; ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cout<<*i<<" "; cout<<endl; }

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;

int main(){
	int n,m;
	while(cin>>n>>m,n){
		vector<int> c(n+1);
		REP(i,n)cin>>c[i+1];
		vector<int> a;
		REP(i,n+1)REP(j,n+1)a.push_back(c[i]+c[j]);
		sort(a.begin(), a.end());
		a.erase(unique(a.begin(),a.end()), a.end());
		int ans = 0;
		REP(i,a.size()){
			if(a[i] <= m){
				int t = m - a[i];
				vector<int>::iterator it = upper_bound(a.begin(), a.end(), t);
				it--;
				ans = max(ans, a[i] + *it);
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <climits>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <bitset>
#include <utility>
#include <numeric>
#include <queue>
#include <stack>
#include <iomanip>

using ll = long long;
using namespace std;

constexpr int MOD = 1e9 + 7;
constexpr ll MOD_LL = ll(1e9 + 7);

int main(void) {
	string a;
	cin >> a;

	int n = (int)a.size();
	map<char, int> mp;

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

	ll ans = 1LL;
	for(int i = 0; i < n; ++i) {
		ans += n - i - 1 - (mp[a[i]] - 1);
		mp[a[i]]--;
	}

	cout << ans << endl;

	return 0;
}

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define FORR(i, a, b) for (int i = b - 1; i >= a; --i)
#define SORT(v) sort(v.begin(), v.end())
#define SORTR(v) sort(v.rbegin(), v.rend())
#define REV(v) reverse(v.begin(), v.end())
#define ITER(itr, v) for (auto itr = v.begin(); itr != v.end(); ++itr)
#define LB(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define UB(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
#define SZ(v) (int)v.size()
using namespace std;
using ll = long long;
using P = pair<int, int>;

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

    string A;
    cin >> A;
    vector<ll> num_char(26);
    for (auto &c : A) {
        num_char[c - 'a']++;
    }
    ll res = 1;
    REP(i, 25) {
        FOR(j, i + 1, 26) {
            res += num_char[i] * num_char[j];
        }
    }
    cout << res << endl;

    return 0;
}


/*{{{*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<limits.h>
#include<assert.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<PII> VPII;
typedef pair<LL,LL> PLL;
typedef vector<PLL> VPLL;
template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(LL &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); }
template<class T> void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const LL &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T,class U> void _W(const pair<T,U> &x) {_W(x.F); putchar(' '); _W(x.S);}
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }
#ifdef HOME
 #define DEBUG(...) {printf("# ");printf(__VA_ARGS__);puts("");}
#else
 #define DEBUG(...)
#endif
int MOD = 1e9+7;
void ADD(LL& x,LL v){x=(x+v)%MOD;if(x<0)x+=MOD;}
/*}}}*/
const int SIZE = 1e6+10;
int N,X;
LL b[SIZE],l[SIZE],u[SIZE];
bool used[SIZE];
int main(){
    R(N,X);
    LL need=0;
    priority_queue<PLL>h0,h1,h2;
    REP(i,N){
        R(b[i],l[i],u[i]);
        need+=b[i]*l[i];
        h0.push({l[i]*b[i]+u[i]*(X-b[i]),i});
    }
    LL an=0;
    while(need>=h0.top().F){
        an+=X;
        used[h0.top().S]=1;
        need-=h0.top().F;
        h0.pop();
    }
    if(need<=0){
        W(an);
        return 0;
    }
    int add_id=h0.top().S;
    LL add_v=l[add_id]*b[add_id]+u[add_id]*(X-b[add_id]);
    LL mi=1e18;
    REP(i,N){
        if(used[i]){
            LL need2=need-add_v+l[i]*b[i]+u[i]*(X-b[i]);

            if(l[i]*b[i]>=need2){
                mi=min(mi,(need2+l[i]-1)/l[i]);
            }
            else{
                mi=min(mi,(need2-l[i]*b[i]+u[i]-1)/u[i]+b[i]);
            }
        }
        else{
            if(l[i]*b[i]+u[i]*(X-b[i])<need)continue;
            if(l[i]*b[i]>=need){
                mi=min(mi,(need+l[i]-1)/l[i]);
            }
            else{
                mi=min(mi,(need-l[i]*b[i]+u[i]-1)/u[i]+b[i]);
            }
        }
    }
    an+=mi;
    W(an);
    return 0;
}

#include<bits/stdc++.h>

#define ll long long
#define fornum(A,B,C) for(A=B;A<C;A++)
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll,ll>

using namespace std;

/////////////////////////////////////////////////////
#define INF 1e15
ll N, X, b[101010];
ll l[101010], u[101010];
vector<pll> mxv;
ll mk[101010];
ll i, j, k,aa,bb,dd,bs,ans;
int main(){
    scanf("%lld%lld", &N,&X);
    bs = 0;
    fornum(i,0,N){
        scanf("%lld%lld%lld", &b[i], &l[i], &u[i]);
        bs += l[i] * b[i];
        mxv.push_back({u[i] * (X - b[i]) + l[i] * b[i], i});
    }
    sort(mxv.begin(), mxv.end(), greater<pll>());
    //printf("%lld\n", bs);
    if(bs==0){
        printf("0");
        return 0;
    }
    while(bb<N&&aa<bs){
        aa += mxv[bb].first;
        ll a = mxv[bb].second;
        mk[a] = 1;
        dd += X;
        //printf("%lld %lld\n", mxv[i].second, bs);
        bb++;
    }
    bb--;
    ans = INF;
    fornum(i,0,bb){
        ll cc = bs-(aa - mxv[i].first);
        ll ii = mxv[i].second;
        ll a = dd-X;
        if(cc<=b[ii]*l[ii]){
            a += (cc - 1) / l[ii] + 1;
        }else{
            a += (cc - b[ii] * l[ii] - 1) / u[ii] + 1 + b[ii];
        }
        if(a<ans){
            ans = a;
        }
        //printf("%llda\n", ans);
    }
    fornum(i,bb,N){
        ll cc = bs-(aa - mxv[bb].first);
        ll ii = mxv[i].second;
        ll a = dd - X;
        if(cc<=b[ii]*l[ii]){
            a += (cc - 1) / l[ii] + 1;
        }else{
            a += (cc - b[ii] * l[ii] - 1) / u[ii] + 1 + b[ii];
        }
        if(a<ans){
            ans = a;
        }
        //printf("%lld\n", ans);
    }
    printf("%lld", ans);
    return 0;
}


#include <bits/stdc++.h>

#define all(x) (x).begin(), (x).end()
#define li long long
#define itn int

using namespace std;

inline int nxt(){
	int n;
	scanf("%d", &n);
	return n;
}

const int N = 111 * 111 / 3;
int dp[N][N];

int main(){

	int n = nxt(), m = nxt();
	vector<string> a(n);
	for (int i = 0; i < n; i++)
		cin >> a[i];

	int posX = 0, posY = 0;
	for (int i = 0; i < n; i++){
		for (int j = 0; j < m; j++){
			if (a[i][j] == 'E'){
				posX = i;
				posY = j;
			}
		}
	}

	vector<int> i1, i2, j1, j2;
	for (int i = 0; i <= posX; i++){
		for (int j = posX; j < n; j++){
			i1.push_back(i);
			i2.push_back(j);
		}
	}
	for (int i = 0; i <= posY; i++){
		for (int j = posY; j < m; j++){
			j1.push_back(i);
			j2.push_back(j);
		}
	}

	vector<vector<int>> prefX(n, vector<int>(m + 1, 0)), prefY(n + 1, vector<int>(m, 0));
	for (int i = 0; i < n; i++){
		for (int j = 0; j < m; j++){
			prefX[i][j + 1] = prefX[i][j] + (a[i][j] == 'o' ? 1 : 0);
		}
	}
	for (int i = 0; i < n; i++){
		for (int j = 0; j < m; j++){
			prefY[i + 1][j] = prefY[i][j] + (a[i][j] == 'o' ? 1 : 0);
		}
	}

	int q = i1.size(), w = j1.size();
	vector<vector<int>> pos1(posX + 1, vector<int>(n - posX));
	for (int i = 0; i < q; i++)
		pos1[i1[i]][i2[i] - posX] = i;
	vector<vector<int>> pos2(posY + 1, vector<int>(m - posY));
	for (int i = 0; i < w; i++)
		pos2[j1[i]][j2[i] - posY] = i;

	dp[pos1[posX][0]][pos2[posY][0]] = 0;
	for (int i = posX; i >= 0; i--){
		for (int ii = 0; ii < n - posX; ii++){
			for (int j = posY; j >= 0; j--){
				for (int jj = 0; jj < m - posY; jj++){
					int l = j, r = jj + posY, u = i, d = ii + posX;
					int actl = jj, actr = m - 1 - (posY - j), actu = ii, actd = n - 1 - (posX - i);
					// if (l == posY && r == posY && u == posX && d == posX)
					// 	continue;
					int& res = dp[pos1[i][ii]][pos2[j][jj]];
					if (r > posY)
						res = max(res, dp[pos1[i][ii]][pos2[j][jj - 1]] + (r > actr ? 0 : prefY[min(d, actd) + 1][r] - prefY[max(u, actu)][r]));
					if (l < posY)
						res = max(res, dp[pos1[i][ii]][pos2[j + 1][jj]] + (l < actl ? 0 : prefY[min(d, actd) + 1][l] - prefY[max(u, actu)][l]));
					if (d > posX)
						res = max(res, dp[pos1[i][ii - 1]][pos2[j][jj]] + (d > actd ? 0 : prefX[d][min(r, actr) + 1] - prefX[d][max(l, actl)]));
					if (u < posX)
						res = max(res, dp[pos1[i + 1][ii]][pos2[j][jj]] + (u < actu ? 0 : prefX[u][min(r, actr) + 1] - prefX[u][max(l, actl)]));
				}
			}
		}
	}

	int ans = 0;
	for (int i = posX; i >= 0; i--){
		for (int ii = 0; ii < n - posX; ii++){
			for (int j = posY; j >= 0; j--){
				for (int jj = 0; jj < m - posY; jj++){
					// cerr << i << " " << ii + posX << " " << j << " " << jj + posY << " " << dp[pos1[i][ii]][pos2[j][jj]] << "\n";
					ans = max(ans, dp[pos1[i][ii]][pos2[j][jj]]);
				}
			}
		}
	}

	cout << ans << "\n";

	return 0;
}

#include <algorithm>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
using namespace std;
using ll = long long;
#define fst first
#define snd second

/* clang-format off */
template <class T, size_t D> struct _vec { using type = vector<typename _vec<T, D - 1>::type>; };
template <class T> struct _vec<T, 0> { using type = T; };
template <class T, size_t D> using vec = typename _vec<T, D>::type;
template <class T> vector<T> make_v(size_t size, const T& init) { return vector<T>(size, init); }
template <class... Ts> auto make_v(size_t size, Ts... rest) { return vector<decltype(make_v(rest...))>(size, make_v(rest...)); }
/* clang-format on */

template <class I>
class SegmentTree {
 public:
  using Op = function<I(const I&, const I&)>;

 private:
  int n;
  vector<I> dat;
  Op op;
  I e;

 public:
  SegmentTree(int n_, const Op& op, const I& e) : op(op), e(e) {
    n = 1;
    while (n < n_) n *= 2;
    dat.resize(2 * n - 1, e);
  }
  SegmentTree(const vector<I>& init, const Op& op, const I& e)
      : SegmentTree(init.size(), op, e) {
    for (int i = 0; i < init.size(); i++) {
      dat[i + n - 1] = init[i];
    }
    for (int i = n - 2; i >= 0; i--) {
      dat[i] = op(dat[i * 2 + 1], dat[i * 2 + 2]);
    }
  }
  const I& operator[](int k) const { return dat[k + n - 1]; }
  void update(int k, const I& a) {
    k += n - 1;
    dat[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = op(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }
  // [a, b)
  I query(int a, int b) const {
    a += n - 1;
    b += n - 1;
    I vl = e;
    I vr = e;
    while (a < b) {
      if ((a & 1) == 0) vl = op(vl, dat[a]);
      if ((b & 1) == 0) vr = op(dat[b - 1], vr);
      a = a / 2;
      b = (b - 1) / 2;
    }
    return op(vl, vr);
  }
  // query(0, i) >= x となる最小のi
  int kth(const I& x) const {
    int k = 0;
    I v = e;
    while (k < n - 1) {
      I t = op(v, dat[k * 2 + 1]);
      if (x <= t) {
        k = k * 2 + 1;
      } else {
        v = t;
        k = k * 2 + 2;
      }
    }
    return k - (n - 1) + 1;
  }
};

int main() {
#ifdef LOCAL
  ifstream cin("in.txt");
#endif
  int N, Q;
  while (cin >> N >> Q) {
    int M = Q + 2;
    vector<ll> x(M);
    for (int i = 0; i < M; i++) cin >> x[i], --x[i];
    vector<ll> dsum(M);
    dsum[0] = 0;
    for (int i = 1; i < M; i++) {
      dsum[i] = dsum[i - 1] + abs(x[i] - x[i - 1]);
    }
    auto op = [](ll a, ll b) { return min(a, b); };
    ll inf = numeric_limits<ll>::max();
    vector<ll> dp(M);
    dp[M - 1] = 0;
    SegmentTree<ll> addSeg(N, op, inf), subSeg(N, op, inf);
    addSeg.update(x[M - 1], dp[M - 1] + dsum[M - 2] + x[M - 1]);
    subSeg.update(x[M - 1], dp[M - 1] + dsum[M - 2] - x[M - 1]);
    for (int i = M - 2; i >= 1; i--) {
      dp[i] = min({dsum[M - 1] - dsum[i],  // 先頭を最後まで持っていく
                   addSeg.query(x[i - 1], N) - dsum[i] - x[i - 1],
                   subSeg.query(0, x[i - 1] + 1) - dsum[i] + x[i - 1]});
      addSeg.update(x[i], min(addSeg[x[i]], dp[i] + dsum[i - 1] + x[i]));
      subSeg.update(x[i], min(subSeg[x[i]], dp[i] + dsum[i - 1] - x[i]));
    }
    cout << dp[1] << endl;
  }
  return 0;
}


#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <math.h>
#include <climits>
#include <assert.h>
#include <cstring>
#include <set>

using namespace std;

typedef long long ll;
int arr[200001];
int main()
{
    int N;
    cin >> N;
    int temp;
    int i;
    int m = 0;
    ll sum = 0;
    for(i = 0; i < N; i++)
    {
        cin >> temp;
        arr[i] = temp;
        m = max(m,arr[i]);
        sum += m-arr[i];
    }

    for(i = N-1; i >= 0; i--)
    {

    }

    cout << sum;
}

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

int main (int argc, char const* argv[]){
  int n, max1, a[5001];

  while(cin >> n && n!=0){
    for(int i=0;i<n;i++) cin >> a[i];

    max1 = a[0];
    for (int i = 0; i < n; i += 1){
      int sum = 0;
      for(int j=i;j<n;j++){
        sum += a[j];
        max1 = max(max1,sum);
      }
    }
    cout << max1 << endl;

  }

  return 0;
}


//52行目から編集
#include <iostream>
#include <limits.h>
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <cassert>

using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define all(x) (x).begin(),(x).end()
#define mp make_pair
#define pb push_back
#define eb emplace_back

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vector<int> > vvi;
typedef vector<vector<ll> > vvl;
typedef pair<int,int> pi;
typedef pair<ll,ll> pl;

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

////////////////////////////////////////////////////////////////////////////////////////////
/*
 * 強連結成分分解
 */
struct StronglyConnectedComponents{
    int V;//頂点数
    vector<vector<int> > G; // グラフの隣接リスト表現
    vector<vector<int> > rG; //辺の向きを逆にしたグラフ
    vector<int> vs; // 帰りがけ順の並び
    vector<bool> used; // すでに調べたか
    vector<int> cmp; // 属する今日連結成分のトポロジカル順序

    StronglyConnectedComponents(int V):V(V){
        G = vector<vector<int> >(V,vector<int>(0)); rG = vector<vector<int> >(V,vector<int>(0));
        vs = vector<int>(V,0); used = vector<bool>(V,0); cmp = vector<int>(V,0);
    }

    void add_edge(int  from,int to){ G[from].push_back(to); rG[to].push_back(from); }
    void dfs(int v){
        used[v] = 1;
        for(int i = 0;i < G[v].size();i++){ if(!used[G[v][i]])dfs(G[v][i]); }
        vs.push_back(v);
    }
    void rdfs(int v,int k){
        used[v] = 1; cmp[v] = k;
        for(int i = 0;i < rG[v].size();i++){ if(!used[rG[v][i]]) rdfs(rG[v][i],k); }
    }
    int calc(){
        for(int i = 0;i < V;i++)used[i] = 0;
        vs.clear();
        for(int v = 0;v < V;v++){ if(!used[v])dfs(v);}
        for(int i = 0;i < V;i++)used[i] = 0;
        int k = 0;
        for(int i = vs.size() - 1;i >= 0;i--){ if(!used[vs[i]]) rdfs(vs[i],k++); }
        return k;
    }
};
////////////////////////////////////////////////////////////////////////////////////////////
int main(void){
    cin.tie(0);
    ios::sync_with_stdio(false);
    //cout << fixed << setprecision(20);
    int n,m;cin >> n >> m;
    StronglyConnectedComponents scc(n);
    rep(i,m){
        int a,b;cin >> a >> b;
        scc.add_edge(a,b);
    }
    int q;cin >> q;
    scc.calc();
    //rep(i,n)cout << i << " : " << scc.cmp[i] << '\n';
    rep(i,q){
        int a,b;cin >> a >> b;
        cout << (scc.cmp[a] == scc.cmp[b]) << '\n';
    }
    return 0;
}

#include "bits/stdc++.h"

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

using namespace std;
using ll = long long;

constexpr ll atcoder_mod = 1e9+7;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

int main() {

  int N, K;
  cin >> N >> K;
  int x = 1;

  for (int i = 0;N > i;i++){

    cin >> x;

    if (x < K){
      x *= 2;
    }
    else{
      x += K;
    }
  }
  cout << x << endl;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
#define fi first
#define se second
const ll mod = 1e9+7;
const int N = 2e5+6;
int k,n;
void solve(){
	cin>>n>>k;
	int st = 1;
	for(int i=1;i<=n;i++){
		if(st*2<st+k)st*=2;
		else st+=k;
	}
	cout<<st<<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;
}


#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define all(x) (x).begin(),(x).end()
using ll = long long;
string char_to_string(char val) {
  return string(1, val);
}
int char_to_int(char val) {
  return val - '0';
}
template<class T> inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template<class T> inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int vector_finder(std::vector<ll> vec, int number) {
  auto itr = std::find(vec.begin(), vec.end(), number);
  size_t index = std::distance( vec.begin(), itr );
  if (index != vec.size()) { // 発見できたとき
    return 1;
  }
  else { // 発見できなかったとき
    return 0;
  }
}

int main() {
  string S; cin >> S;
  string ans = "AC";
  if(S[0] != 'A') ans = "WA";
  if(S[1] != tolower(S[1])) ans = "WA";
  if(S[S.size() - 1] != tolower(S[S.size() - 1])) ans = "WA";
  int cnt = 0;
  for(int i = 2; i < S.size() - 1; ++i) {
    if(S[i] == 'C') cnt++;
    if(cnt > 1) ans = "WA";
    if(i == S.size() - 2 && cnt == 0) ans = "WA";
  }
  cout << ans << endl;
}

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

void b_accepted(void) {
	string s;
	cin >> s;
	bool judge_a = false, judge_b = false, judge_c = false;

	int cnt_c = 0;
	for (int i = 0; i < s.size(); i++) {
		if (i == 0) {
			if (s[i] == 'A') judge_a = true;
			else             break;
		}
		else if (s[i] == 'C'){
			if (i >= 2 && i < s.size() - 1) {
				cnt_c++;
			}
		}
		else {
			if (s[i] < 'a') {
				judge_c = false;
				break;
			}
			else judge_c = true;
		}
	}

	if (cnt_c == 1) judge_b = true;

	if (judge_a & judge_b & judge_c) {
		cout << "AC" << endl;
	}
	else {
		cout << "WA" << endl;
	}
}

int main()
{
	b_accepted();
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//cout << fixed << setprecision(15);
vector<int> forintinput(int n) {
	vector<int> x;
	for(int i = 0;i < n;i++){
		int ia;
		cin >> ia;
		x.push_back(ia);
	}
	return x;
}
int main(){
	int a,b;
	cin >> a >> b;
	cout << max(a+b,max(a*2-1,b*2-1)) << endl;
}

#include <iostream>
#include <iomanip>
//#include <string>
#include <vector>
#include <algorithm>
//#include <utility>
//#include <set>
//#include <map>
//#include <queue>
//#include <deque>
//#include <bitset>
//#include <math.h>
using namespace std ;
using ll = long long ;
//using ld = long double ;
using vll = vector<ll> ;
//using vvll = vector<vll> ;
//using vc = vector<char> ;
//using vvc = vector<vc> ;
//using vb = vector<bool> ;
//using vvb = vector<vb> ;
//using pll = pair<ll,ll> ;
//#define all(v) v.begin(),v.end()
//ll mod = 1e9+7 ;
//long double pie = acos(-1) ;
//ll INF = 1000000000000 ;

//void yorn(bool a){if(a) cout << "Yes" << endl ; else cout << "No" << endl ;}
//void YorN(bool a){if(a) cout << "YES" << endl ; else cout << "NO" << endl ;}
//ll gcd(long long a,long long b){if(b==0) return a ; return gcd(b,a%b) ;}
//ll lcm(long long a,long long b){return a/gcd(a,b)*b ;}
//ll sa(long long a,long long b){if(a>b) return a-b ; return b-a ;}
void fix_cout(){cout << fixed << setprecision(20) ;}

int main(){
	ll n,k ;
	cin >> n >> k ;
	vll a(n) ;
	for(auto &i:a) cin >> i ;
	ll sum = 0 ;
	ll now = 0 ;
	for(int i=0;i<k;i++) now += a.at(i) ;
	sum = now ;
	for(int i=k;i<n;i++){
		now = now-a.at(i-k)+a.at(i) ;
		sum = max(sum,now) ;
	}
	fix_cout() ;
	cout << (sum+k)/2.0 << endl ;
}


#include <iostream>
using namespace std;

int main() {
	long long n,i,mod=1000000007LL;
	cin >> n;
	long long a[n+1],p[n+1],ans=0LL;
	p[0]=0;
	for(i=1;i<=n;i++)
	{
		cin >> a[i];
		p[i]=a[i]+p[i-1];
		p[i]%=mod;
	}
	for(i=1;i<=n;i++)
	{
		ans+=((p[n]-p[i])*a[i]);
		ans+=mod;
		ans%=mod;
	}
	cout << ans%mod;
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

const int N=55;

void maxi(int &a, int b) {
	a = max(a,b);
}

using ll = long long;
ll MOD = 1e9 +7;

ll add(ll x, ll y) {
	return ((x + y) % MOD);
}

int main() {
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	vector<ll> X;
 	vector<ll> Y;
	int n, m; cin >> n >> m;
	for (int i=0; i < n; i++) {
		ll x; cin >> x;
		X.push_back(x);
	}
	for (int i=0; i < m; i++) {
		ll y; cin >> y;
		Y.push_back(y);
	}
	ll x_total = 0;
	for (int k=0; k <n; k++) {
		x_total += add(X[k]*k, -(n-k-1)*X[k]);
		x_total %= MOD;
	}
	//ll x_check =0;
	//for (int i=0; i < n; i++) {
	//	for (int j=i+1; j < n; j++) {
	//		x_check += (X[j] - X[i]);
	//	}
	//}
	//assert(x_check == x_total);
	ll y_total = 0;
	for (int k=0; k < m; k++) {
		y_total += add(Y[k]*k, -(m-k-1)*Y[k]);
		y_total %= MOD;
	}
	//ll y_check = 0;
	//for (int i=0; i < m; i++) {
	//	for (int j=i+1; j < m; j++) {
	//		y_check += (Y[j] - Y[i]);
	//	}
	//}
	//assert(y_check == y_total);
	ll total = (x_total*y_total) % MOD;
	if (total < 0) {
		total += MOD;
	}
	cout << total << endl;
}


/*
ID: anonymo14
TASK: wormhole
LANG: C++
*/
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<pair<int,int>> vpii;

#define F first
#define S second
#define PU push
#define PUF push_front
#define PUB push_back
#define PO pop
#define POF pop_front
#define POB pop_back

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

#define MOD 1000000007

void solve(int test_case) {//227
	int i;
	cin>>i;
	for(;i<=999;i++) {
		if(i%10==(i%100)/10&&i%10==i/100){
			cout<<i;
			return;
		}
	}
}

int main() {

	////// FILE BASED IO////
	//freopen("wormhole.in", "r", stdin);
	//freopen("wormhole.out", "w", stdout);
	///////////////

	ios::sync_with_stdio(0);
	cin.tie(0);
	int t=1;
	//cin>>t;
	REP(i,1,t) {
		solve(i);
	}
	return 0;
}

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

#define rep(i,s,n) for (int i = (int)s; i < (int)n; i++)
#define ll long long
#define ld long double
#define pb push_back
#define eb emplace_back
#define All(x) x.begin(), x.end()
#define Range(x, i, j) x.begin() + i, x.begin() + j
#define lbidx(x, y) lower_bound(x.begin(), x.end(), y) - x.begin()
#define ubidx(x, y) upper_bound(x.begin(), x.end(), y) - x.begin()
#define llbidx(x, y, z) lower_bound(x.begin(), x.end(), z) - lower_bound(x.begin(), x.end(), y) // 二要素間の距離
#define deg2rad(deg) ((((double)deg)/((double)360)*2*M_PI))
#define rad2deg(rad) ((((double)rad)/(double)2/M_PI)*(double)360)
#define Find(set, element) set.find(element) != set.end()
#define Decimal(x) printf("%.10f\n", x) // 小数点を10桁まで表示
// debug用
#define PrintVec(x) for (auto elementPrintVec: x) { cout << elementPrintVec << " "; } cout << "\n";
#define debug(x) cerr << #x << ": " << (x) << "\n";
#define endl "\n"
// gcj print用
#define Case(x) printf("Case #%d: ", x);

typedef pair<int, int> PI;
typedef pair<ll, ll> PLL;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<vector<vector<int>>> vvvi;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<vector<vector<int>>> vvvl;
typedef vector<PI> vpi;
typedef vector<vector<PI>> vvpi;
typedef vector<vector<vector<PI>>> vvvpi;
typedef vector<PLL> vpl;
typedef vector<vector<PLL>> vvpl;
typedef vector<vector<vector<PLL>>> vvvpl;

int POWINT(int x, int n) {
  int ret = 1;
  while (n > 0) {
    if (n & 1) ret *= x;
    x *= x;
    n >>= 1;
  }
  return ret;
};

ll POWLL(ll x, int n) {
  ll ret = 1;
  while (n > 0) {
    if (n & 1) ret *= x;
    x *= x;
    n >>= 1;
  }
  return ret;
};

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

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

void print() {
  cout << endl;
}

template <class T>
void print(vector<T> &vec) {
  for (auto& a : vec) {
    cout << a;
    if (&a != &vec.back()) cout << " ";
  }
  cout << endl;
}

template <class T>
void print(vector<T> &vec, ll k){
   ll n = vec.size();
   k = min(k, n);
   rep(i, 0, k-1) cout << vec[i] << " ";
   cout << vec[k-1] << endl;
}

template <class T>
void print(vector<vector<T>> &df) {
  for (auto& vec : df) {
    print(vec);
  }
}

template<class T, class U>
void print(pair<T,U> &p){
  cout << p.first << " " << p.second << "\n";
}

const ll MAX = 1e6+5;

// エラトステネスの篩
struct Sieve {
  ll n;
  vl f, primes;
  Sieve(ll n = 1): n(n), f(n+1) {
    f[0] = f[1] = -1ll;
    for (ll i = 2; i <= n; i++) {
      if (f[i]) continue;
      primes.pb(i);
      f[i] = i;
      for (ll j = i*i; j <= n; j += i) {
        if (!f[j]) f[j] = i; // jの最小の素因数
      }
    }
  }

  bool isPrime(int x) { return f[x] == x; }

  // 素因数分解
  vl factorList(ll x) {
    vl res;
    while (x != 1ll) {
      res.pb(f[x]);
      x /= f[x];
    }
    return res;
  }

  vpl factor(ll x) {
    vl fl = factorList(x);
    if (fl.size() == 0) return {};

    vpl res(1, PLL(fl[0], 0));

    for (int p: fl) {
      if (res.back().first == p) {
        res.back().second++;
      } else {
        res.emplace_back(p, 1);
      }
    }

    return res;
  }
};

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

  Sieve sieve(MAX);
  vector<int> primes;
  rep(i, 2, 55556) {
    if (sieve.isPrime(i)) {
      primes.pb(i);
    }
  }

  int N; cin >> N;

  vi ans;
  int cnt = 0;
  for (int p: primes) {
    if (p % 10 == 3) {
      ans.pb(p);
      cnt++;
    }
    if (cnt >= N) {
      break;
    }
  }

  print(ans);

  return 0;
};


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

int main(){
    ll n;
    cin >> n;
    ll ans = 1;
    while(pow(ans, 2) <= n){
        ans++;
    }
    cout << (ll)pow(ans-1, 2) << endl;
}

#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
    int N; cin >> N;
    int f = 1.0;
    while(true) {
        double M = sqrt(N), x = 0.0, y = 0.0;
        x = modf(M, &y);
        if (x == 0) {
            printf("%d\n", N);
            return 0;
        }
        else N--;
    }
}


#include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<vector>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<time.h>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double EPS=1e-10;
const int inf=1e8;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
vector<vector<char> >in(14,vector<char>(14));
int x[4]={0,0,1,-1};
int y[4]={1,-1,0,0};
void func(int a,int b){
	in[a][b]=0;
	rep(k,4)if(in[a+x[k]][b+y[k]]!=0)func(a+x[k],b+y[k]);
}

int main(){
	char d;
	while(cin>>d){
		rep(i,12)rep(j,12){
			if(!i&&!j)in[i+1][j+1]=d;
			else cin>>in[i+1][j+1];
			if(in[i+1][j+1]=='0')in[i+1][j+1]=0;else in[i+1][j+1]=1;
		}
		int out=0;
		rep(i,12)rep(j,12)if(in[i+1][j+1]!=0){
			out++;
			func(i+1,j+1);
		}
		cout<<out<<endl;
	}
}

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

int main(){
    int N;
    cin >> N;
    string S;
    cin >> S;
    string T = S.substr(0, N);
    string U = S.substr(N, N);
    reverse(U.begin(), U.end());
    //cout << T << endl << U << endl;

    unordered_map<string, ll> um1(0), um2(0);

    for (int i = 0; i < (1 << N); i++) {
        string a="", b="";
        for (int j = 0; j < N; j++) {
            if (((i >> j) & 1) == 0) a += T[j];
            else b += T[j];
        }
        um1[a + "_" + b]++;
    }

    ll ans = 0;

    for (int i = 0; i < (1 << N); i++) {
        string a="", b="";
        for (int j = 0; j < N; j++) {
            if (((i >> j) & 1) == 0) a += U[j];
            else b += U[j];
        }
        ans += um1[a + "_" + b];
    }
    cout << ans << endl;
    return 0;
}


#include "bits/stdc++.h"

using ll = long long;

using namespace std;
// #include "debug.h"
#define debug(...) 42

const int mod = 998244353;

void solve() {
   int n;
   cin >> n;
   vector <int> d(n), cnt(n);
   for(int i = 0; i < n; i++) {
      cin >> d[i];
      cnt[d[i]]++;
   }
   bool ok = true;
   ok &= d[0] == 0;
   ok &= cnt[0] == 1;

   if(!ok) {
      cout << 0 << endl;
      return;
   }
   int m = *max_element(d.begin(), d.end());

   ll ans = 1;
   for(int i = 1; i <= m; i++) {
      if(cnt[i] == 0) {
         cout << 0 << endl;
         return;
      }
      for(int j = 0; j < cnt[i]; j++) {
         ans = (ans * 1LL * cnt[i - 1]) % mod;
      }
   }

   cout << ans << endl;
}

signed main() {
   cin.tie(0);
   cin.sync_with_stdio(0);
   int testcase = 1;
   // cin >> testcase;

   for(int i = 0; i < testcase; i++) {
      solve();
   }
   return 0;
}

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

ll Ep(int x, int y){
  if(y == 1) return x;
  ll r = Ep(x,y/2);
  if(y%2 == 1) return r *r%MOD *x%MOD;
  else return r *r%MOD;
}

int main(){
  int n, x;
  cin >> n;
  cin >> x;
  if(x != 0){
    cout << 0 << endl;
    return 0;
  }
  map<int, int> mp;
  for(int i=1; i<n; ++i){
    cin >> x;
    if(x == 0){
      cout << 0 << endl;
      return 0;
    }
    ++mp[x];
  }
  ll ans = 1;
  for(auto p: mp){
    if(p.first == 1) continue;
    ans *= Ep(mp[p.first-1], p.second);
    ans %= MOD;
  }
  cout << ans << endl;
}


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

int main() {
  ll N;
  cin >> N;
  if (N == 0) {
    cout << 0 << endl;
    return 0;
  }
  vector<int> ans(0);
  while (N != 0) {
    int tmp = N % 2;
    if (tmp < 0) tmp += 2;
    N = (N-tmp) / (-2);
    ans.push_back(tmp);
  }
  for (int i = ans.size()-1; i >= 0; i--) cout << ans[i];
  cout << endl;
}

#include <bits/stdc++.h>
using namespace std;
long long mypow(long long x, long long y)
{
	long long result = 1;
	while (y != 0) {
		if (y%2 != 0) {
			result *= x;
			y -= 1;
		}
		x *= x;
		y /= 2;
	}
	return result;
}
int main(void)
{
	long long n;
	cin >> n;
	if (n == 0) {
		cout << 0 << endl;
		return 0;
	}

	long long base = 0;
	for (int i = 0; i <= 31; i++) {
		if (i % 2 == 1)
			base -= mypow(2, i);
	}
	long long m = n-base;
	string s;
	for (int i = 0; i <= 31; i++) {
		if (m % mypow(2, i+1) != 0) {
			if (i % 2 == 0) {
				s.push_back('1');
			} else {
				s.push_back('0');
			}
			m -= mypow(2, i);
		} else {
			if (i % 2 == 0)
				s.push_back('0');
			else
				s.push_back('1');
		}
	}
	reverse(s.begin(), s.end());
	int i;
	for (i = 0; i < s.size(); i++) {
		if (s[i] == '1')
			break;
	}
	cout << s.substr(i) << endl;
	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>

using namespace std;

#define NIL -1
#define N 500001

//struct Node{
//	int val;
//	Node *p, *l, *r;
//};

struct Node {
	int val;
	Node *p, *l, *r;
	Node(int v) :val(v), p(NULL), l(NULL), r(NULL){}
	Node() :val(0), p(NULL), l(NULL), r(NULL){}
};

Node T[N];
int D[N], H[N];

Node *root;

void insert(int k){
	Node *y = NULL;
	Node *x = root;
	Node *z = new Node(k);

	while (x != NULL && x->val != k){
		y = x;
		if (k > x->val) x = x->r;
		else x = x->l;
	}
	z->p = y;
	if (y == NULL) root = z;
	else{
		if (k < y->val) y->l = z;
		else y->r = z;
	}
}

Node *find(int val){
	Node *p = root;
	while (p != NULL && p->val != val) {
		if (val < p->val) p = p->l;
		else p = p->r;
	}
	return p;
}

void ino(Node *p){
	if (p == NULL) return;
	ino(p->l);
	printf(" %d", p->val);
	ino(p->r);
}

void preo(Node *p){
	if (p == NULL) return;
	printf(" %d", p->val);
	preo(p->l);
	preo(p->r);
}

Node *mi(Node *z){
	while (z->l != NULL) z = z->l;
	return z;
}

Node *suc(Node *z){
	if (z->r != NULL) return mi(z->r);
	Node *y = z->p;
	while (y != NULL && z == y->r) {
		z = y;
		y = y->p;
	}
	return y;
}

void del(Node *z){
	Node *y = NULL;
	Node *x = NULL;

	if (NULL == z->l || NULL == z->r) y = z;
	else y = suc(z);

	if (NULL != y->l) x = y->l;
	else x = y->r;

	if (NULL != x) x->p = y->p;

	if (NULL == y->p) root = x;
	else {
		if (y == y->p->l) y->p->l = x;
		else y->p->r = x;
	}

	if (y != z) z->val = y->val;
	delete y;
}

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

	int val;
	string com;
	for (int i = 0; i < n; ++i) {
		cin >> com;
		if (com[0] == 'f'){
			cin >> val;
			Node *p = find(val);
			cout << ((p == NULL) ? "no" : "yes") << endl;
		}
		else if (com[0] == 'i'){
			cin >> val;
			insert(val);
		}
		else if (com[0] == 'p'){
			ino(root); cout << "\n";
			preo(root); cout << "\n";
		}
		else if (com[0] == 'd'){
			cin >> val;
			del(find(val));
		}
	}

	return 0;
}

//yes
//yes
//yes
//no
//no
//no
//yes
//yes
//1 2 3 7 8 22
//8 2 1 3 7 22
//1 2 8 22
//8 2 1 22

#include <bits/stdc++.h>
using namespace std;
int main(){
  int m;
  cin >> m;
  vector<int> nodes;
  vector<pair<int, int>> child;
  int n = 0;
  for (int i = 0; i < m; i++){
    string q;
    cin >> q;
    if (q == "insert"){
      int k;
      cin >> k;
      if (n != 0){
        int v = 0;
        while (1){
          if (k < nodes[v]){
            if (child[v].first != -1){
              v = child[v].first;
            } else {
              child[v].first = nodes.size();
              break;
            }
          } else {
            if (child[v].second != -1){
              v = child[v].second;
            } else {
              child[v].second = nodes.size();
              break;
            }
          }
        }
      }
      nodes.push_back(k);
      child.push_back(make_pair(-1, -1));
      n++;
    }
    if (q == "find"){
      int k;
      cin >> k;
      if (n == 0){
        cout << "no" << endl;
      } else {
        int v = 0;
        while (1){
          if (k < nodes[v]){
            if (child[v].first == -1){
              cout << "no" << endl;
              break;
            } else {
              v = child[v].first;
            }
          } else if (k > nodes[v]){
            if (child[v].second == -1){
              cout << "no" << endl;
              break;
            } else {
              v = child[v].second;
            }
          } else {
            cout << "yes" << endl;
            break;
          }
        }
      }
    }
    if (q == "delete"){
      int k;
      cin >> k;
      int v = 0;
      int u;
      while (1){
        if (k < nodes[v]){
          u = v;
          v = child[v].first;
        } else if (k > nodes[v]){
          u = v;
          v = child[v].second;
        } else {
          break;
        }
      }
      if (child[v].first != -1 && child[v].second != -1){
        u = v;
        int w = child[v].second;
        while (child[w].first != -1 || child[w].second != -1){
          u = w;
          if (child[w].first != -1){
            w = child[w].first;
          } else {
            w = child[w].second;
          }
        }
        swap(nodes[v], nodes[w]);
        v = w;
      }
      if (child[v].first != -1 && child[v].second == -1){
        int w = child[v].first;
        nodes[v] = nodes[w];
        child[v].first = child[w].first;
        child[v].second = child[w].second;
      } else if (child[v].second != -1 && child[v].first == -1){
        int w = child[v].second;
        nodes[v] = nodes[w];
        child[v].first = child[w].first;
        child[v].second = child[w].second;
      } else {
        if (child[u].first == v){
          child[u].first = -1;
        } else {
          child[u].second = -1;
        }
      }
      n--;
    }
    if (q == "print"){
      //Inorder
      if (n != 0){
        stack<int> S;
        vector<int> t(nodes.size(), 0);
        S.push(0);
        while (!S.empty()){
          int v = S.top();
          S.pop();
          if (t[v] == 1){
            cout << ' ' << nodes[v];
          }
          if (t[v] == 0){
            S.push(v);
            if (child[v].second != -1){
              S.push(child[v].second);
            }
            S.push(v);
            if (child[v].first != -1){
              S.push(child[v].first);
            }
          }
          t[v]++;
        }
      }
      cout << endl;
      //Preorder
      if (n != 0){
        stack<int> S;
        vector<int> t(nodes.size(), 0);
        S.push(0);
        while (!S.empty()){
          int v = S.top();
          S.pop();
          if (t[v] == 0){
            cout << ' ' << nodes[v];
          }
          if (t[v] == 0){
            S.push(v);
            if (child[v].second != -1){
              S.push(child[v].second);
            }
            S.push(v);
            if (child[v].first != -1){
              S.push(child[v].first);
            }
          }
          t[v]++;
        }
      }
      cout << endl;
    }
  }
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<ctime>
#include<map>
#include<bitset>
#include<set>
#include<assert.h>
#include<chrono>
#include<random>
#define LL long long
#define mp(x,y) make_pair(x,y)
#define pll pair<long long,long long>
#define pii pair<int,int>
#define SZ(x) ((int)x.size())
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
inline LL read()
{
	LL f=1,x=0;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
int stack[20];
template<typename T>inline void write(T x)
{
	if(x<0){putchar('-');x=-x;}
    if(!x){putchar('0');return;}
    int top=0;
    while(x)stack[++top]=x%10,x/=10;
    while(top)putchar(stack[top--]+'0');
}
template<typename T>inline void pr1(T x){write(x);putchar(' ');}
template<typename T>inline void pr2(T x){write(x);putchar('\n');}
template<typename T>inline void chkmin(T &x,T y){x=x<y?x:y;}
template<typename T>inline void chkmax(T &x,T y){x=x>y?x:y;}
const int MAXN=10005;
const int MAXM=505;
int pr[MAXN],is[MAXN],plen;
void getpr()
{
	for(int i=2;i<MAXN;i++)
	{
		if(!is[i])pr[++plen]=i;
		for(int j=1;j<=plen&&i*pr[j]<MAXN;j++)
		{
			is[i*pr[j]]=1;
			if(!(i%pr[j]))break;
		}
	}
}
int n,vis[MAXM][MAXM],now;LL a[MAXM][MAXM],b[MAXM][MAXM];
LL gcd(LL a,LL b){return a==0?b:gcd(b%a,a);}
void get1(int x,int y)
{
	if(x>n||y>n)return ;
	vis[x][y]=1;a[x][y]*=pr[now];
	get1(x+1,y+1);
}
void get2(int x,int y)
{
	if(x<1||x>n||y<1||y>n)return ;
	vis[x][y]=1;a[x][y]*=pr[now];
	get2(x+1,y-1);
}
bool in(int x,int y){return x>=1&&x<=n&&y>=1&&y<=n;}
LL lcm(LL a,LL b){return a/gcd(a,b)*b;}
int main()
{
	getpr();
	n=read();now=0;
	if(n==2)
	{
		printf("4 7\n23 10\n");return 0;
	}
	for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)a[i][j]=1;
	for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)if(((i+j-1)&1)&&!vis[i][j])
		++now,get1(i,j);
	memset(vis,0,sizeof(vis));
	for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)if(((i+j-1)&1)&&!vis[i][j])
		++now,get2(i,j);
	for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)if(!((i+j-1)&1))
	{
		if(in(i-1,j))a[i][j]=lcm(a[i][j],a[i-1][j]);
		if(in(i+1,j))a[i][j]=lcm(a[i][j],a[i+1][j]);
		if(in(i,j-1))a[i][j]=lcm(a[i][j],a[i][j-1]);
		if(in(i,j+1))a[i][j]=lcm(a[i][j],a[i][j+1]);
		++a[i][j];
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)pr1(a[i][j]);
		puts("");
	}
	return 0;
}


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

int main(){
    int N, M, Q;
    scanf("%d %d %d\n", &N, &M, &Q);
    vector<string> grid(N+1, "0");
    grid[0].resize(M);
    for (int i = 1; i <= N; i++) {
        char s[M];
        scanf("%s\n", s);
        grid[i] += s;
    }
    vector<vector<int>> psum(N+1, vector<int>(M+1, 0));
    vector<vector<int>> horsum(N+1, vector<int>(M+1, 0));
    vector<vector<int>> versum(N+1, vector<int>(M+1, 0));
    for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= M; j++) {
            if (grid[i][j] == '1') {
                psum[i][j]++;
                if (grid[i][j-1] == '1') horsum[i][j]++;
                if (grid[i-1][j] == '1') versum[i][j]++;
            }
            psum[i][j] += psum[i][j-1] + psum[i-1][j] - psum[i-1][j-1];
            horsum[i][j] += horsum[i][j-1] + horsum[i-1][j] - horsum[i-1][j-1];
            versum[i][j] += versum[i][j-1] + versum[i-1][j] - versum[i-1][j-1];
        }
    }

    for (int z = 0; z < Q; z++) {
        int x1, x2, y1, y2;
        scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
        int ps = psum[x2][y2] - psum[x1-1][y2] - psum[x2][y1-1] + psum[x1-1][y1-1];
        int hors = horsum[x2][y2] - horsum[x1-1][y2] - horsum[x2][y1] + horsum[x1-1][y1];
        int vers = versum[x2][y2] - versum[x1][y2] - versum[x2][y1-1] + versum[x1][y1-1];
        printf("%d\n", ps - hors - vers);
    }

    return 0;
}


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

ll M,N,P[1001];

int main(){
	while(scanf("%lld%lld",&N,&M),N){
		for(int i=0;i<N;i++) scanf("%lld",&P[i]);
		vector<ll> d;
		for(int i=0;i<=N;i++) for(int j=0;j<=N;j++)
			d.push_back(P[i]+P[j]);
		sort(d.begin(),d.end());
		d.erase(unique(d.begin(),d.end()),d.end());
		vector<ll>::iterator it;
		ll res=0;
		for(int i=0;i<d.size();i++){
			it=upper_bound(d.begin(),d.end(),M-d[i]);
			if(it!=d.begin()){
				it--;
				res=max(res,*it+d[i]);
			}
		}
		printf("%lld\n",res);
	}
}

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

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

int field[101][101];

int xs,ys,xg,yg;

void SetBlock(int c,int d,int x,int y)
{
	if(d==0){//横なが
		for(int ay=0;ay<2;++ay){
			for(int ax=0;ax<4;++ax){
				field[y+ay][x+ax] = c;
			}
		}
	}
	if(d==1){//縦なが
		for(int ay=0;ay<4;++ay){
			for(int ax=0;ax<2;++ax){
				field[y+ay][x+ax] = c;
			}
		}
	}
}

bool dfs(int x,int y)
{
	int c = field[y][x];
	field[y][x] = 0;
	if(x == xg && y == yg) return true;
	for(int i=0;i<4;++i){
		if(c==field[y+dy[i]][x+dx[i]])if(dfs(x+dx[i],y+dy[i]))return true;
	}
	return false;
}

int main(void)
{
	while(1){
		int w,h;
		int n;
		memset(field,0,sizeof(field));
		cin >> w >> h;
		if(w==0&&h==0)
			return 0;
		cin >> xs >> ys;
		cin >> xg >> yg;
		cin >> n;
		for(int i=0;i<n;++i){
			int c,d,x,y;
			cin >> c >> d >> x >> y;
			SetBlock(c,d,x,y);
		}
		if(field[ys][xs] == 0)
			cout << "NG" << endl;
		else
		{
			if(dfs(xs,ys))
				cout << "OK" << endl;
			else
				cout << "NG" << endl;
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define ll long long
#define f first
#define s second
using namespace std;
int n, a[100005];
vector<pair<int,int>> vec, vec2;
unordered_map<int,int> mp, mp2;
int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    #ifdef debug
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    cin >> n;
    for(int i = 1; i <= n; i++)cin >> a[i];
    for(int i = 1; i <= n; i+= 2)mp[a[i]]++;
    for(int i = 2; i <= n; i += 2)mp2[a[i]]++;
    for(auto i : mp)vec.pb({i.s, i.f});
    for(auto i : mp2)vec2.pb({i.s, i.f});
    sort(vec.begin(), vec.end()); sort(vec2.begin(), vec2.end());
    int cnt = 1e9;
    int delta = n/2;
    if(vec.size() == 1 && vec2.size() == 1){
        if(vec[0].s == vec2[0].s){
            cnt = delta;
        }
        else{
            cnt = 0;
        }
    }
    else{
        if(vec[vec.size()-1].s != vec2[vec2.size()-1].s){
            cnt = delta - vec[vec.size()-1].f + delta - vec2[vec2.size()-1].f;
        }
        else{
            if(vec.size() > 1)cnt = min(cnt, delta - vec2[vec2.size()-1].f + delta - vec[vec.size()-2].f);
            else cnt = min(cnt, delta + delta - vec2[vec2.size()-1].f);
            if(vec2.size() > 1)cnt = min(cnt, delta - vec2[vec2.size()-2].f + delta - vec[vec.size()-1].f);
            else cnt = min(cnt, delta + delta - vec[vec.size()-1].f);
        }
    }
    cout << cnt << endl;
    return 0;
}

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

int main() {
    int n;cin >> n;
    n /= 2;
    vector<int> e(100001),o(100001);
    for(int i = 0; i < n*2; i++) {
        int v;cin >> v;
        if(i%2==0){
            o[v]++;
        }
        else e[v]++;
    }

    int a=0,b=0,c=0,d=0;
    for(int i = 0; i < 100001; i++) {
        if(e[i]>a){
            c=i;
            a=e[i];
        }
    }
    for(int i = 0; i < 100001; i++) {
        if(o[i]>b){
            d=i;
            b=o[i];
        }
    }

    int x=a+b;
    if(c==d){
        sort(e.begin(),e.end());
        sort(o.begin(),o.end());
        x=max(a+o[99999],b+e[99999]);
    }
    cout << n*2-x << endl;

    return 0;
}


#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <cstdlib>
#include <list>
#include <numeric>
#include <map>
#include <cmath>
#include <string>
#define rep(i,n) for(int i=0;i<n;i++)
#define all(x) (x).begin(),(x).end()
using namespace std;
//================================================

int a = 0, b = 0, c, d, n, k = 0;
string s, t;

int main()
{
	int n, h, w;
	cin >> n >> h >> w;
	cout << (n - h + 1) * (n - w + 1) << endl;

	return 0;
}

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

int main(){
  int a , b , c , d ;
  cin >> a >> b >> c >> d ;
  int M = 0 ;

  if(a > b ){
    M += b ;
  }
  else{
    M =+ a ;
  }

  if(c>d) M += d ;
  else M += c ;

  cout << M << endl ;

}


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

const ll INF = 1001001001;
const ll mod = 1000000007;

int main(){
    int n, u, v; cin >> n >> u >> v;
    --u; --v;
    if(u == v){
        cout << 0 << endl;
        return 0;
    }
    vector<int> dist_u(n, -1), dist_v(n, -1);
    vector<vector<int>> G(n);
    for(int i = 0; i < n - 1; i++){
        int a, b; cin >> a >> b;
        a--; b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    queue<int> que;
    que.push(u);
    dist_u[u] = 0;
    dist_v[v] = 0;
    while(!que.empty()){
        int v = que.front(); que.pop();
        for(int nv : G[v]){
            if(dist_u[nv] != -1) continue;
            dist_u[nv] = dist_u[v] + 1;
            que.push(nv);
        }
    }
    que.push(v);
    while(!que.empty()){
        int v = que.front(); que.pop();
        for(int nv : G[v]){
            if(dist_v[nv] != -1) continue;
            dist_v[nv] = dist_v[v] + 1;
            que.push(nv);
        }
    }
    set<int> st;
    for(int i = 0; i < n; i++){
        if(dist_u[i] < dist_v[i]) st.insert(dist_v[i]);
    }
    sort(dist_v.begin(), dist_v.end(), greater<int>());
    for(int i = 0; i < n; i++){
        if(st.count(dist_v[i])){
            cout << dist_v[i] - 1 << endl;
            return 0;
        }
    }

}

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=n-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define vtpl(x,y,z) vector<tuple<x,y,z>>
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[9]={1,0,-1,0,1,1,-1,-1,0};
const ll dx[9]={0,-1,0,1,1,-1,1,-1,0};
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
void dfs(ll v,ll par,set<ll> &st,vl &ans, vvl &g){
    for(auto nv:g[v]){
        if(par==nv)continue;
        if(ans[nv]==-1){
            ans[nv]=v;
            st.ins(v);dfs(nv,v,st,ans,g);st.erase(v);
        }
        else{
            if(st.count(ans[nv]))ans[nv]=v;
        }
    }
}
int main(){
    ll n,m;cin >>n >>m;
    vvl g(n);
    vl ans(n,-1);
    set<ll> st;rep(i,n)st.ins(i);
    rep(i,n-1+m){
        ll a,b;cin >> a >> b;a--;b--;
        g[a].pb(b);
        st.erase(b);
    }
    ll s=*st.begin();st.clear();
    dfs(s,-1,st,ans,g);
    for(auto p:ans)cout << p+1 <<endl;
}


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

int main() {

    long N;

    cin >> N;

    // N = A * B
    // B = N / A

    // A , B 桁数大きい方の、最小値
    long ans = 100000000;

    double d = sqrt(N) + 1;
    //cout << "d: " << d << endl;

    for (long i = 1; i < d ; i++) {
        long a = i;

        //整数チェック
        if (N % a != 0){
            continue;
        }
        long b = N / a;

        //cout << "a: " << a << "   b: " << b << endl;
        string s_a = to_string(a);
        int k_a = s_a.size();
        string s_b = to_string(b);
        int k_b = s_b.size();

        int m = max(k_a, k_b);

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

    cout << ans << endl;

    return 0;

}

#include <bits/stdc++.h>
using namespace std;
void chmax(int& a, int b) {
  if (a < b) a = b;
}

void chmin(int& a, int b) {
  if (a > b) a = b;
}

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

  for (int i = 0; i < N; i++) {
    int x;
    cin >> x;
    chmax(X, x);
  }
  for (int i = 0; i < M; i++) {
    int y;
    cin >> y;
    chmin(Y, y);
  }

  if (X < Y) {
    cout << "No War" << endl;
  } else {
    cout << "War" << endl;
  }
}


#include <bits/stdc++.h>
using namespace std;
long long MOD = 1000000007;
long long modpow(long long a, long long b){
  long long ans = 1;
  while (b > 0){
    if (b % 2 == 1){
      ans *= a;
      ans %= MOD;
    }
    a *= a;
    a %= MOD;
    b /= 2;
  }
  return ans;
}
long long modinv(long long a){
  return modpow(a, MOD - 2);
}
vector<long long> mf = {1};
long long modfact(int n){
  if (mf.size() > n){
    return mf[n];
  } else {
    for (int i = mf.size(); i <= n; i++){
      long long next = mf.back() * i % MOD;
      mf.push_back(next);
    }
    return mf[n];
  }
}
long long modbinom(int n, int k){
  return modfact(n) * modinv(modfact(k)) % MOD * modinv(modfact(n - k)) % MOD;
}
int main(){
  long long N, M, K;
  cin >> N >> M >> K;
  long long ans1 = 0;
  for (int i = 1; i <= N; i++){
    ans1 += (N - i) * i;
  }
  ans1 %= MOD;
  ans1 *= M;
  ans1 %= MOD;
  ans1 *= M;
  ans1 %= MOD;
  long long ans2 = 0;
  for (int i = 1; i <= M; i++){
    ans2 += (M - i) * i;
  }
  ans2 %= MOD;
  ans2 *= N;
  ans2 %= MOD;
  ans2 *= N;
  ans2 %= MOD;
  long long ans = ans1 + ans2;
  ans %= MOD;
  ans *= modbinom(N * M - 2, K - 2);
  ans %= MOD;
  cout << ans << endl;
}

#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <string>
#include <queue>
#include <fstream>
#include <string>
#include <math.h>
#include <set>
#include <string>
#include <bitset>
#include <fstream>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
//typedef pair<ll, ll> P;
//typedef pair<ll, P> T;

#define rep(i, n) for (ll i = 0; i < (n); i++)
#define revrep(i, n) for (ll i = (n) - 1; i >= 0; i--)
#define f first
#define s second
#define pb push_back

ll mod = 1000000007;
const ll INF = 100000000000010000;
//const ull B = 998244353;

//ll bit[200010];
//ll kai[3010];
//ll Cnt[10010];

//void pres(double A, ll x = 20) { printf("%.20f\n", x); }

vector<ll> dx = { 0, 1, 0, -1, 1, -1, 1, -1, 0};
vector<ll> dy = { 1, 0, -1, 0, 1, 1, -1, -1, 0};

void chmin(ll& x, ll y) {
	x = min(x, y);
}

void chmax(ll& x, ll y) {
	x = max(x, y);
}

/*ll sum(ll i)
{
	i += 1;
	ll t = 0;
	while (i > 0)
	{
		t += bit[i];
		i -= i & -i;
	}
	return t;
}

void add(ll i, ll x)
{
	i += 1;
	while (i <= 200005)
	{
		bit[i] += x;
		i += i & -i;
	}
}*/

/*`ll SAIKI(ll S) {
	for (ll T = S; ; T = (T - 1) & S) {

	}
}*/

void test() {
	cout << "888888888888888888888888" << endl;
}

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

ll pow_mod(ll x, ll k) {
	x %= mod;
	ll res = 1;
	while (k > 0) {
		if (k % 2) {
			res *= x; res %= mod;
		}
		x *= x; x %= mod;
		k /= 2;
	}
	return res;
}

ll div_mod(ll x) {
	return pow_mod(x, mod - 2);
}

vector <ll> par; // 各元の親を表す配列
vector <ll> siz; // 素集合のサイズを表す配列(1 で初期化)

void init_u(ll sz_) {
	par.resize(sz_);
	siz.assign(sz_, 1LL);  // resize だとなぜか初期化されなかった
	for (ll i = 0; i < sz_; ++i) par[i] = i; // 初期では親は自分自身
}

// Member Function
// Find
ll root(ll x) { // 根の検索
	while (par[x] != x) {
		x = par[x] = par[par[x]]; // x の親の親を x の親とする
	}
	return x;
}

// Union(Unite, Merge)
bool merge(ll x, ll y) {
	x = root(x);
	y = root(y);
	if (x == y) return false;
	// merge technique（データ構造をマージするテク．小を大にくっつける）
	if (siz[x] < siz[y]) swap(x, y);
	siz[x] += siz[y];
	par[y] = x;
	return true;
}

bool issame(ll x, ll y) { // 連結判定
	return root(x) == root(y);
}

ll size(ll x) { // 素集合のサイズ
	return siz[root(x)];
}

//コンビネーション
const int MAXcomb = 1000010;
ll fac[MAXcomb], finv[MAXcomb], inv[MAXcomb];
//facはn!,finvは1/n!
//invは逆元
void COMinit() {
	fac[0] = fac[1] = 1;
	finv[0] = finv[1] = 1;
	inv[1] = 1;
	for (int i = 2; i < MAXcomb; i++) {
		fac[i] = fac[i - 1] * i % mod;
		inv[i] = mod - inv[mod%i] * (mod / i) % mod;
		finv[i] = finv[i - 1] * inv[i] % mod;
	}
}
ll comb(int n, int k) {
	if (n < k) return 0;
	if (n < 0 || k < 0) return 0;
	return fac[n] * finv[k] % mod * finv[n - k] % mod;
}

/*map<ll, ll> pres;
ll ct;

void makePrimeList(ll x) {
	ll X = x;
	for (ll j = 2; j * j <= x; j++) {
		ll cnt = 0;
		while (X % j == 0) {
			cnt++;
			X /= j;
		}
		if (cnt) {
			if (pres[j] == 0) {
				ct++;
				pres[j] = ct;
			}
			Cnt[pres[j]] += cnt;
		}
	}
	if (X != 1) {
		if (pres[X] == 0) {
			ct++;
			pres[X] = ct;
		}
		Cnt[pres[X]] += 1;
	}
}*/

ll N, M, K;
ll ans, num;

void solve() {
	cin >> N >> M >> K;
	COMinit();
	rep(i, N) {
		num += i * (i + 1) / 2 % mod;
		num %= mod;
	}
	ans += comb(N * M - 2, K - 2) * M % mod * M % mod * num % mod;
	ans %= mod;
	num = 0;
	rep(i, M) {
		num += i * (i + 1) / 2 % mod;
		num %= mod;
	}
	ans += comb(N * M - 2, K - 2) * N % mod * N % mod * num % mod;
	ans %= mod;
	cout << ans << endl;
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	ll T = 1;
	//cin >> T;
	rep(i, T) solve();
	ll N;
	cin >> N;
}


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

template<class T>
struct SegmentTree {
	using F = function<T(T, T)>;
	const T init;
	const F f;
	int sz;
	vector<T> seg;
	SegmentTree(int n, const T init, const F f) :
		init(init),
		f(f)
		{
			sz = 1;
			while(sz < n) sz <<= 1;
			seg.resize(sz << 1, init);
		}
	void update(int id) {
		while(id >>= 1) {
			seg[id] = f(seg[2 * id], seg[2 * id + 1]);
		}
	}
	void replace(int id, T x){
		id += sz;
		seg[id] = x;
		update(id);
	}
	// [l,r)
	T get(int l, int r){
		T L = init, R = init;
		for(l += sz, r += sz; l < r; l >>= 1, r>>= 1) {
			if(l & 1) L = f(L, seg[l++]);
			if(r & 1) R = f(seg[--r], R);
		}
		return f(L, R);
	}
	void debug(){
		int i = 1;
		for(int r = 2; r <= 2 * sz; r <<= 1) {
			while(i < r) {
				cout << seg[i++] << " ";
			}
			cout << "\n";
		}
	}
};

int main() {
	int n; cin >> n;
	pair<int, int> p[n];
	for(auto &e : p) {
		cin >> e.first >> e.second;
		e.second++;
	}
	sort(p, p + n);
	SegmentTree<int> segL(n, 0, [](int lhs, int rhs) {
		return max(lhs, rhs);
	});
	SegmentTree<int> segR(n, 1e9 + 10, [](int lhs, int rhs) {
		return min(lhs, rhs);
	});
	for(int i = 0; i < n; ++i) {
		segL.replace(i, p[i].first);
		segR.replace(i, p[i].second);
	}
	int ans = 0;
	for(int i = 0; i < n - 1; ++i) {
		{
			int l1 = p[i].first;
			int r1 = p[i].second;
			int l2 = max(segL.get(0, i), segL.get(i + 1, n));
			int r2 = min(segR.get(0, i), segR.get(i + 1, n));
			ans = max(ans, max(0, (r1 - l1)) + max(0, (r2 - l2)));
		}
		{
			int l1 = segL.get(0, i + 1);
			int r1 = segR.get(0, i + 1);
			int l2 = segL.get(i + 1, n);
			int r2 = segR.get(i + 1, n);
			ans = max(ans, max(0, (r1 - l1)) + max(0, (r2 - l2)));
		}
	}
	cout << ans << '\n';
	return 0;
}

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

typedef long long ll;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef vector<vector<vector<ll>>> vvvll;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<vector<vector<bool>>> vvvb;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;
typedef vector<double> vd;
typedef vector<vd> vdd;

typedef pair<double, double> pd;
typedef vector<pd> vpd;

#define FOR(i,x,y) for(ll i=(ll)x; i<(ll)y; ++i)
#define REP(i,y) FOR(i, 0, y)
#define RFOR(i,x,y) for(ll i=(ll)x; i>=(ll)y; --i)
#define RREP(i,x) RFOR(i, x, 0)
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define debug_print(x...) cerr << "line " << __LINE__ << " : "; debug_print_in(x);

template <typename First>
void debug_print_in(First first){
  cerr << first << endl;
  return;
}

template <typename First, typename... Rest>
void debug_print_in(First first, Rest... rest){
  cerr << first << " ";
  debug_print_in(rest...);
  return;
}

void IN(void){
  return;
}

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

template <typename First>
void OUT(First first){
  cout << first << endl;
  return;
}

template <typename First, typename... Rest>
void OUT(First first, Rest... rest){
  cout << first << " ";
  OUT(rest...);
  return;
}

template<class t, class u> t chmax(t&a,u b){if(a<b)a=b; return a;};
template<class t, class u> t chmin(t&a,u b){if(a>b)a=b; return a;};
int popcount(int t){return __builtin_popcount(t);} //GCC
int popcount(ll t){return __builtin_popcountll(t);} //GCC

template <typename T>
void vec_print(vector<T> VEC){
  REP(i, VEC.size()){
    cerr << VEC[i] << " ";
  }
  cerr << endl;
};

template <typename T>
void mat_print(vector<vector<T> > MAT){
  REP(i,MAT.size()){
    REP(j,MAT[i].size()){
      cerr << MAT[i][j] << " ";
    }
    cerr << endl;
  }
};

constexpr int INF = (1<<30);
constexpr ll INFLL = 1LL<<62;
constexpr long double EPS = 1e-12;
constexpr ll MOD = (ll)((1E+9)+7);

int main(){
  cin.tie(0); // cut the cin and cout (default, std::flush is performed after std::cin)
  ios::sync_with_stdio(false); // cut the iostream and stdio (DON'T endl; BUT "\n";)

  ll N;
  IN(N);
  vpll RL(N);
  REP(i,N){
    IN(RL[i].second, RL[i].first);
  }

  sort(ALL(RL));

  vll Lmax(N, RL[0].second);
  REP(i,N-1){
    Lmax[i+1] = max(Lmax[i], RL[i+1].second);
  }

  /*
  REP(i,N){
    debug_print(RL[i].second, RL[i].first);
  }
  */

  ll ans = max(RL[0].first-Lmax[N-2]+1, (ll)0)+(RL[N-1].first-RL[N-1].second)+1;
  ll Lmax_after = RL[N-1].second;
  for(ll i=N-2; i>0; --i){
    ll Lmax1 = Lmax[i-1];
    ll Rmin1 = RL[0].first;
    ll Lmax2 = RL[i].second;
    ll Rmin2 = RL[i].first;

    ll LR1 = max(Rmin1-Lmax1+1, (ll)0);
    ll LR2 = max(Rmin2-Lmax2+1, (ll)0);
    ll LR1_1 = max(Rmin1-max(Lmax_after, Lmax1)+1, (ll)0);
    ll LR2_1 = max(Rmin2-max(Lmax_after, Lmax2)+1, (ll)0);

    ll ans_tmp = max(LR1+LR2_1, LR1_1+LR2);

    chmax(ans, ans_tmp);
    chmax(Lmax_after, Lmax2);
  }

  OUT(ans);

  return 0;
}


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

const int MAX = 200000, SENTINEL = 2100000000;
int L[MAX / 2 + 2], R[MAX / 2 + 2];

ll merge(int A[], int n, int left, int mid, int right) {
    ll cnt = 0;
    int n1 = mid - left;
    int n2 = right - mid;
    for (int i = 0; i < n1; i++) L[i] = A[left + i];
    for (int i = 0; i < n2; i++) R[i] = A[mid + i];
    L[n1] = R[n2] = SENTINEL;
    int i = 0, j = 0;
    for (int k = left; k < right; k++) {
        if (L[i] <= R[j]) {
            A[k] = L[i++];
        } else {
            A[k] = R[j++];
            cnt += n1 - i;
        }
    }
    return cnt;
}

ll msort(int A[], int n, int left, int right) {
    ll res = 0;
    if (left + 1 < right) {
        int mid = (left + right) / 2;
        res += msort(A, n, left, mid);
        res += msort(A, n, mid, right);
        res += merge(A, n, left, mid, right);
    }
    return res;
}

int main(void) {
    int n, A[MAX];

    cin >> n;
    for (int i = 0; i < n; i++) cin >> A[i];

    cout << msort(A, n, 0, n) << endl;

    return 0;
}

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

int main()
{
  int n,v,p;
  long long m;
  scanf("%d%lld%d%d",&n,&m,&v,&p);
  std::vector<long long> A(n);
  for(int i=0;i<n;i++){
    scanf("%lld",&A[i]);
  }
  std::sort(A.begin(),A.end());
  int l=-1,r=n-p;
  while(l+1<r){
    int d=(l+r)/2;
    bool F=true;
    if(A[d]+m<A[n-p]){
      F=false;
    }
    else{
      long long S=0ll;
      for(int i=d+1;i<=n-p;i++){
	S+=A[d]+m-A[i];
      }
      if(S>=std::max(v-p-d,0)*m){
	F=true;
      }
      else{
	F=false;
      }
    }
    if(F){
      r=d;
    }
    else{
      l=d;
    }
  }
  printf("%d\n",n-r);
  return 0;
}


#ifdef __GNUC__
#include <bits/stdc++.h>
auto & in = std::cin;
#pragma GCC optimize ("O3")
#pragma GCC target ("avx")

#else
std::istringstream in;
#endif
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/dynamic_bitset.hpp>
using namespace std;
#define ALL(v) std::begin(v),std::end(v)
using u6 = std::uint64_t;
using u3 = std::uint32_t;
constexpr u6 moduloNo = 1'000'000'000+7; //'

struct State {
	array<u3, 3> buf;
	//State() :buf{ 1,0,1 } {}
	State() {}
	State(u3 zerocount) :buf{ 1,0,zerocount } { addX(zerocount); }
	void addX(u3 zero) {
		if (zero != buf[2]){
			u6 zerodiff = zero - buf[2];
			buf[0] = (u3)(((u6)buf[0] + zerodiff * buf[1])%moduloNo);
			buf[2] = zero;

		}

		buf[1] = buf[0]+buf[1];
		buf[1] = (buf[1] >= moduloNo) ?buf[1] - moduloNo: buf[1];
	}
};
u6 pow2(u6 n) {
	if (n < 64)return (u6(1) << n)% moduloNo;
	else if (n == 64)return (u6(0) - moduloNo) % moduloNo;
	u6 tmp = pow2(n / 2);
	tmp=tmp * tmp;
	if (n & 1)tmp *= 2;
	return tmp % moduloNo;
}

struct Calc {
	u6 N;

	unordered_map<u3, State> stmap;
	Calc() {
		in >> N;
	}
	u6 calc() {
		u3 st = 0;
		u3 tmp;
		u3 zerocount = 0;
		for (u3 index = 0; index < N; ++index) {
			in >> tmp;
			st ^= tmp;
			if (!st) {
				++zerocount;
				//cout << index << endl;
			}
			else {
				auto ite = stmap.find(st);
				if (ite == stmap.end()) {
					stmap.emplace(st, zerocount);
				}
				else {
					ite->second.addX(zerocount);
				}
			}
		}
		if (st != 0) {
			return stmap[st].buf[0];
		}
		else {
			u6 result = pow2(zerocount-1);
			for (auto & stv: stmap) {
				result = (result + stv.second.buf[1])%moduloNo;
			}

			/*u6 sum0 = 0, sum1 = 0;
			for (auto & stv : stmap) {
				cout << stv.second.buf[0] << " " << stv.second.buf[1]  <<  " " << stv.second.buf[2] << endl;
				sum0 += stv.second.buf[0];
				sum1 += stv.second.buf[1];
			}
			cout << sum0 << " " << sum1 << endl;*/
			return result;
		}
	}
};

int main() {

#ifndef __GNUC__
	in.str("32\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0");
#endif
	Calc calc;

	cout << calc.calc() << endl;
	/*
	u6 i = 0; u6 maxnum = 0;
	for (auto & v : tbl.buf) {
		for (auto n : v.buf) {
			if (n > maxnum)maxnum = n;
		}
	}
	cout << maxnum << endl;
	for (auto & v : tbl.buf) {
		cout << i << "=" << v << endl;
		++i;
	}
	cout << maxnum << endl;*/
}

//khodaya khodet komak kon
#include <bits/stdc++.h>

#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#define int long long

using namespace std;

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

const int N = 600000 + 10;
const ll MOD = 1000000000 + 7;
const ll INF = 1000000010;
const ll LOG = 25;

pii adj[N];
int a[4][N], n, ind[N], rev[N], fen[N], L[N], R[N];

void add(int id, int x){
	for (id; id < N; id += (id) & (-id)) fen[id] += x;
}

int get(int id){
	int res = 0;
	for(; id > 0; id -= id & (-id)) res += fen[id];
	return res;
}

int32_t main(){
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++){
		for (int j = 1; j <= 3; j++){
			for (int k = 1; k <= 3; k++){
				if (k == j) continue;
				if (adj[(i - 1) * 3 + j].F == 0) adj[(i - 1) * 3 + j].F = (i - 1) * 3 + k;
				else adj[(i - 1) * 3 + j].S = (i - 1) * 3 + k;
			}
		}
	}
	for (int i = 1; i <= 3; i++){
		for( int j = 1; j <= n; j++) cin >> a[i][j];
	}
//	for (int i = 1; i <= 3; i++){
//		for (int j = 1; j <= n; j++) cout << a[i][j] << ' ';
//		cout << '\n';
//	}
//	cout << a[3][4] << '\n';
	for (int j = 1; j <= n; j++){
		vi Nw;
//		if (j == 4) cout << a[1][j] << ' ' << a[2][j] << ' ' << a[3][j] << '\n';
//		cout << a[3][4] << '\n';
		for (int i = 1; i <= 3; i++) Nw.pb(a[i][j]);
		vi NN;
		NN.pb(a[1][j]);
		NN.pb(adj[a[1][j]].F);
		NN.pb(adj[a[1][j]].S);
		sort(all(Nw));
		sort(all(NN));
//		cout << a[3][4] << '\n';
//		cout << j << '\n';
//		if (j == 4) for (int j = 0; j < 3; j++) cout << Nw[j] << ' ' << NN[j] << '\n';
		for( int i = 0; i < 3; i++) if (Nw[i] != NN[i]) return cout << "No", 0;
		if (a[2][j] != ((a[1][j] - 1) / 3) * 3 + 2) return cout << "No", 0;
		if (((a[1][j] - 1) / 3 + 1) % 2 != j % 2) return cout << "No", 0;
//		cout << a[3][4] << '\n';
		ind[(a[1][j] - 1) / 3 + 1] = j;
		rev[(a[1][j] - 1) / 3 + 1] = (a[1][j] != ((a[1][j] - 1) / 3 * 3 + 1));
	}
	int inv = 0, cnt = 0;
	for (int i = 1; i <= n; i += 2){
		inv += get(n) - get(ind[i]);
		L[i] = get(n) - get(ind[i]);
		add(ind[i], 1);
	}
	memset(fen, 0, sizeof fen);
	for (int i = n - ((n - 1) % 2); i >= 1; i -= 2){
		R[i] = get(ind[i]);
		add(ind[i], 1);
	}
	for (int i = 1; i <= n; i += 2){
		int sm = L[i] + R[i];
		cnt += (sm + rev[i]) % 2;
	}
	memset(fen, 0, sizeof fen);
	int inv2 = 0, cnt2 = 0;
	for (int i = 2; i <= n; i += 2){
		inv2 += get(n) - get(ind[i]);
		L[i] = get(n) - get(ind[i]);
		add(ind[i], 1);
	}
	memset(fen, 0, sizeof fen);
	for (int i = n - (n % 2); i >= 2; i -= 2){
		R[i] = get(ind[i]);
		add(ind[i], 1);
	}
	for (int i = 2; i <= n; i += 2){
		int sm = L[i] + R[i];
		cnt2 += (sm + rev[i]) % 2;
	}
	if ((inv % 2 == cnt2 % 2) && (inv2 % 2 == cnt % 2)) return cout << "Yes", 0;
	cout << "No";

	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 rep1(i, n) for (int i = 1; i <= (int)(n); i++)
ll factorial(int i) {       //階乗
    if (i == 0) return 1;
    return (factorial(i - 1)) * i;
}

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

}

#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <map>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <string.h>
#include <cstdio>
#include <tuple>
using namespace std; using ll = long long; using pll = pair<ll, ll>;
using vl = vector<ll>; using vll = vector<vl>; using vpll = vector<pll>;
//**デバッグ準備**//
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
void debug_out() { cerr << endl; }
template<typename Head, typename... Tail>
void debug_out(Head h, Tail... t) {
	cerr << " " << h;
	if (sizeof...(t) > 0) cerr << " :";
	debug_out(t...);
}
template <typename T>
ostream& operator<<(ostream& os, vector<T> vec) {
	for (size_t i = 0; i < vec.size(); i++)os << vec[i] << (i + 1 == vec.size() ? "" : " ");
	return os;
}
ll ctoi(char c) {
	switch (c) {
	case '0': return 0; case '1': return 1; case '2': return 2;
	case '3': return 3; case '4': return 4; case '5': return 5;
	case '6': return 6; case '7': return 7; case '8': return 8;
	case '9': return 9;  default: return 0;
	}
}
bool pairCompare(const pll firstElof, pll secondElof)
{
	return firstElof.first > secondElof.first;
}
//**Snippetリスト**//
//rep, vin, all, iteLoop//
//gcdlcm, isPrime, eratos, Npow, divisor, modinv, bitSearch, bitList, dfs, bfs//
//dijkstra, WarshallFloyd, BellmanFord, UnionFind, COM, digitDP ,RMQ//
ll i, j, k, l; ll N, M, K, H, W, L, X, Y, Z;
ll MOD = 1000000007; ll INF = 1LL << 60; ll ans = 0; ll z = 0, o = 1;
vl flag, color, D; vll path;
//***********//
int main() {
	cin >> H >> W;
	vll A(H, vl(W, 0));
	vll B(H, vl(W, 0));
	vll D(H+1, vl(W+1, 0));
	for (i = 0; i < H; i++) {
		for (j = 0; j < W; j++) {
			cin >> A[i][j];
		}
	}
	for (i = 0; i < H; i++) {
		for (j = 0; j < W; j++) {
			cin >> B[i][j];
			D[i+1][j+1] = abs(A[i][j] - B[i][j]);
		}
	}
	ll M = (H + W) * 80;
	vector<vll> dp(H+1, vll(W+1, vl(M, 0)));
	dp[1][1][D[1][1]] = 1;
	for (i = 1; i <= H; i++) {
		for (j = 1; j <= W; j++) {
			for (k = 0; k < M; k++) {
				if (i == H && j == W) continue;
				if (i != H) {
					if (k + D[i + 1][j] < M) dp[i + 1][j][k + D[i + 1][j]] |= dp[i][j][k];
					if (abs(k - D[i + 1][j]) < M) dp[i + 1][j][abs(k - D[i + 1][j])] |= dp[i][j][k];
				}
				if (j != W) {
					if (k + D[i][j + 1] < M) dp[i][j + 1][k + D[i][j + 1]] |= dp[i][j][k];
					if (abs(k - D[i][j + 1]) < M) dp[i][j + 1][abs(k - D[i][j + 1])] |= dp[i][j][k];
				}
			}
		}
	}
	for (k = 0; k < M; k++) {
		if (dp[H][W][k]) {
			cout << k;
			exit(0);
		}
	}
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
int main()
{

	int n;
	long long sn, x, y;
	vector<long long>p;
	map<long, long>m;
	while (1){
		m.clear();
		p.clear();
		cin >> n;

		if (n == 0)break;
		for (int i = 0; i < n; i++){
		cin >> sn >> x >> y;
			if (m.find(sn) == m.end()){
				p.push_back(sn);
			}
				m[sn] += x*y;
		}
		int flag = 0;
		for (int i = 0; i < p.size(); i++){
			if (m[p[i]] >= 1000000){
				cout << p[i] << endl;
				flag++;
			}
		}
		if (flag==0)cout << "NA" << endl;
	}
}

#include<bits/stdc++.h>
using namespace std;
long long pd,n,s,p[200005],loc,num,l,r,ss;
long long ans,val[200005];
int main(){
//	freopen("go home.in","r",stdin);
//	freopen("go home.out","w",stdout);
	scanf("%lld%lld",&n,&s);
	pd=0;l=1;r=n+1;
	for (long long i=1;(i<=n);i++){
		scanf("%lld%lld",&loc,&num);
		if(pd==0)
			if (loc>=s){
				p[i]=s;val[i]=0;pd=1;ss=i;
			}
		p[i+pd]=loc;val[i+pd]=num;
	}
	if (pd==0) p[n+1]=s;
	while (true){
		if (p[l]>=p[ss]){

			ans+=p[r]-p[l];
			printf("%lld",ans);
			return 0;
		}
		if (p[r]<=p[ss]){

			ans+=p[r]-p[l];
			printf("%lld",ans);
			return 0;
		}
		ans+=p[r]-p[l];
		if (val[l]>=val[r]){
			while (val[l]>=val[r]){
				val[l]+=val[r];
				r--;
				if (p[r]==p[ss]) break;
			}

		} else{
			while (val[r]>val[l]){
				val[r]+=val[l];
				l++;
				if (p[l]==p[ss]) break;
			}
		}

	}
	//fclose(stdin);fclose(stdout);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
const int mxn=2e5+3,du=1e9+7;
int m,n;
signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>m>>n;
	map<int,int> mp;
	multiset<int> s;
	for (int i=1;i<=n;i++) mp[i]=i,s.insert(0);
	for (int i=1;i<=m;i++)
    {
        int l,r;
        cin>>l>>r;
        auto it=mp.lower_bound(l);
        int mx=0;
        while(it!=mp.end()&&it->first<=r)
        {
            mx=max(mx,it->second);
            int dis=(it->first)-(it->second);
            s.erase(s.find(dis));
            mp.erase(it++);
        }
        if (mx!=0&&r<n&&mp.find(r+1)==mp.end())
        {
            mp[r+1]=mx;
            s.insert(r+1-mx);
        }
       // cout<<"DM";
        if (s.size()) cout<<(*s.begin())+i<<"\n";
        else cout<<-1<<"\n";
    }
	return 0;
}


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

void solve(){
    ll n;
    std::cin >> n;
    vector<ll> a(n);
    for (int i = 0; i < n; i++) {
        std::cin >> a[i];
    }

    string s;
    std::cin >> s;

    vector<vector<ll>> dp(n+1);
    dp[n] = {0};

    for (int i = n-1; i >= 0; i--) {
        ll v = a[i];
        for (auto e : dp[i+1]) {
            v = min(v,v^e);
        }
        if(v==0){
            dp[i] = dp[i+1];
        }else{
            if(s[i]=='0'){
                dp[i+1].push_back(v);
                sort(dp[i+1].rbegin(),dp[i+1].rend());
                for (auto e : dp[i+1]) {
                    dp[i].push_back(e);
                }
            }else{
                std::cout << 1 << std::endl;
                return;
            }
        }
    }
    std::cout << 0 << std::endl;
}

int main() {
    int t;
    std::cin >> t;
    for (int iii = 0; iii < t; iii++) {
        solve();
    }
}

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

int main() {
    int a[5], k;
    rep(i, 5) cin >> a[i];
    cin >> k;

    bool ok = true;

    rep(i, 5) {
        for (int j = i+1; j < 5; j++) {
            if (abs(a[i]-a[j]) > k) ok = false;
        }
    }

    if (ok) cout << "Yay!" << endl;
    else cout << ":(" << endl;
}


#include<stdio.h>
int main(void)
{
	int m[12]={31,29,31,30,31,30,31,31,30,31,30,31};
	int man,day;
	int s;
	int i;

	scanf("%d %d",&man,&day);
	while(man!=0){
		s=0;
		if(man!=1){
			for(i=0;i<man-1;i++){
				s+=m[i];
			}
			s+=day;
		}
		else {
			s+=day;
		}
	//	printf("%d\n",s);
		s=s%7;
		if(s==0){
			printf("Wednesday\n");
		}
		else if(s==1){
			printf("Thursday\n");
		}
		else if(s==2){
			printf("Friday\n");
		}
		else if(s==3){
			printf("Saturday\n");
		}
		else if(s==4){
			printf("Sunday\n");
		}
		else if(s==5){
			printf("Monday\n");
		}
		else if(s==6){
			printf("Tuesday\n");
		}
		scanf("%d %d",&man,&day);
	}
	return 0;
}

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

int main() {
  vector<string> s(3);
  rep(i,3) cin >> s[i];

  int cur = 0;
  while (true) {
    string t = s[cur];
    if (t.empty()) {
      cout << char(cur + 'A') << endl;
      return 0;
    }
    s[cur] = t.substr(1,t.size()-1);
    cur = t[0] - 'a';
  }
  return 0;
}


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

int main(){
    long N, A, B;
    cin >> N >> A >> B;
    vector<long> X(N);
    for (int i=0; i<N; i++){
        cin >> X[i];
    }
    long ans = 0;
    for (int i=0; i<N-1; i++){
        if ((X[i+1]-X[i]) * A < B){
            ans += (X[i+1]-X[i]) * A;
        }
        else ans += B;
    }
    cout << ans << endl;
    return 0;
}

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

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

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;

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

  vector<int> t(N);
  rep(i, N) {
    cin >> t[i];
  }

  long long sum = 0;
  rep(i, N-1) {
    sum += std::min(T, t[i+1] - t[i]);
  }
  sum += T;

  cout << sum << endl;

  return 0;
}


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

int main()
{
	long long n, x; cin >> n;
	x = int(sqrt(n));
	while (n%x) x--;
	cout << max(int(log10(x) + 1), int(log10(n/x) + 1)) << endl;
}

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define printVec(v) printf("{"); for (const auto& i : v) { std::cout << i << ", "; } printf("}\n");
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
using namespace std;
using P = pair<int,int>;
using ll = long long;
const ll INF = 1LL<<60;
const double PI = 3.1415926535897932;
const int MOD = 1e9 + 7;

vector<pair<ll, ll>> prime_factorize(ll n) {
  vector<pair<ll, ll>> res;
  for (ll i = 2; i * i <= n; i++) {
    if (n % i != 0) continue;
    ll ex = 0;
    while (n % i == 0) {
      ++ex;
      n /= i;
    }
    res.push_back({i, ex});
  }
  if (n != 1) res.push_back({n, 1});
  return res;
}

//cin.tie(0);ios::sync_with_stdio(false);
int main() {
  ll n;
  cin >> n;
  if (n == 1) {
    cout << 1 << endl;
    return 0;
  }
  vector<ll> primes;
  for (auto v : prime_factorize(n)) {
    for (ll i = 0; i < v.second; i++) {
      primes.push_back(v.first);
    }
  }
  vector<ll> ans;
  ll tmp1, tmp2;
  for (int bit = 0; bit < 1 << primes.size(); bit++) {
    tmp1 = tmp2 = 1;
    for (int j = 0; j < primes.size(); j++) {
      if (bit & 1 << j) {
	tmp1 *= primes[j];
      } else {
	tmp2 *= primes[j];
      }
    }
    ans.push_back(max(to_string(tmp1).size(), to_string(tmp2).size()));
  }

  cout << *min_element(ans.begin(), ans.end()) << endl;
  return 0;
}


#include <iostream>
#include <map>

using namespace std;

void solve()
{
	int a[5];
	while(~scanf("%d,%d,%d,%d,%d", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]))
	{
		map<int, int> Map;
		for(int i = 0; i < 5; ++i)
		{
			++Map[a[i]];
		}
		if(Map.size() == 2)
		{
			int Max = 0;
			for(map<int, int>::iterator Ite = Map.begin(); Ite != Map.end(); ++Ite)
			{
				if(Max < Ite->second)
				{
					Max = Ite->second;
				}
			}
			if(Max == 3)
			{
				cout << "full house" << endl;
			}
			else
			{
				cout << "four card" << endl;
			}
		}
		else if(Map.size() == 3)
		{
			int Max = 0;
			for(map<int, int>::iterator Ite = Map.begin(); Ite != Map.end(); ++Ite)
			{
				if(Max < Ite->second)
				{
					Max = Ite->second;
				}
			}
			if(Max == 3)
			{
				cout << "three card" << endl;
			}
			else
			{
				cout << "two pair" << endl;
			}
		}
		else if(Map.size() == 4)
		{
			cout << "one pair" << endl;
		}
		else
		{
			map<int, int>::iterator Ite = Map.begin();
			map<int, int>::iterator IteNext = Map.begin();
			++IteNext;
			map<int, int>::iterator IteEnd = Map.end();
			IteEnd--;
			if(IteEnd->first - Ite->first == 4 || IteNext->first - Ite->first == 9)
			{
				cout << "straight" << endl;
			}
			else
			{
				cout << "null" << endl;
			}
		}
	}
}

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

#define	_USE_MATH_DEFINES
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cfloat>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <string>
using namespace std;
char shima[13][13];
void tansaku(int x,int y){
	if(x<0||x>11||y<0||y>11||shima[y][x]=='0')
		return ;
	if(shima[y][x]=='1'){
		shima[y][x]='0';
	}
	tansaku(x+1,y);
	tansaku(x-1,y);
	tansaku(x,y+1);
	tansaku(x,y-1);

}
int main(){

	while(cin>>shima[0][0]){
	for(int i=0;i<12;i++){
		for(int j=0;j<12;j++){
			if(i==0&&j==0)
			continue;
			else
			cin>>shima[i][j];
		}
	}
		int a=0;
		for(int i=0;i<12;i++){
			for(int j=0;j<12;j++){
				if(shima[i][j]=='1'){
					tansaku(j,i);
					a++;
				}
			}
		}
		cout<<a<<endl;
	}
}