/*
 * @Author: RBQRBQ
 * @Date: 2020-04-05 15:08:15
 * @LastEditors: RBQRBQ
 * @LastEditTime: 2020-04-05 19:16:50
 */
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef pair<ll,ll> pii;
namespace IO{
    template<typename T>inline void read(T &x){
        x=0;ll f=1;char ch=getchar();
        while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();}
        while(isdigit(ch)){x=x*10+ch-48;ch=getchar();}
        x=x*f;
    }
}
using namespace IO;
vector<pii> pp;
vector<pii> T;
ll mxL,minR,mxR,minL;

int main()
{
    int N;
    cin>>N;
    mxL=mxR=-1;
    minL=minR=10000000000ll;
    int P,Q;
    for(int i=1;i<=N;i++)
    {
        ll x,y;
        read(x),read(y);
        y++;
        pp.push_back(make_pair(x,y));
        if(x>mxL)
        {
            mxL=x;
            P=i;
        }
        if(minR>y)
        {
            minR=y;
            Q=i;
        }
    }
    ll ans1=0,ans2=0;
    minL=0;
    for(int i=1;i<=N;i++)
    {
            minL=max(minL,pp[i-1].second-pp[i-1].first);
    }

    ans1=max((minL),0ll)+max((minR-mxL),0ll);

    int ca=1;
    for(auto p:pp)
    {
        if(ca!=P&&ca!=Q)
        T.push_back(make_pair(max(p.second-mxL,0ll),max(minR-p.first,0ll)));
        ca++;
    }
    sort(T.begin(),T.end());
    ll P1=pp[P-1].second-pp[P-1].first;
    ll Q1=pp[Q-1].second-pp[Q-1].first;
    ll x1=0,x2=Q1;
    // for(auto p:T)
    // {
    //     cout<<p.first<<" "<<p.second<<endl;
    // }
    for(int i=0;i<T.size();i++)
    {

        x1=min(T[i].first,P1);
        ans2=max(ans2,x1+x2);
        x2=min(T[i].second,x2);
    }
    ans2=max(ans2,P1+x2);
    cout<<max(ans1,ans2);

}

/* ALDS1_5_D: The number of inversions */

#include <iostream>
#include <cmath>

const int MAX_VALUE = pow(10, 9);

long long count_and_merge(int A[], int left, int mid, int right) {
    int n_left  = mid - left;
    int n_right = right - mid;
    int *L = new int[n_left + 1];
    int *R = new int[n_right + 1];

    for (int i = 0; i < n_left; i++)
        L[i] = A[left + i];
    for (int i = 0; i < n_right; i++)
        R[i] = A[mid + i];
    L[n_left] = R[n_right] = MAX_VALUE + 1;

    int i_l = 0, i_r = 0;
    long long n_invs = 0;
    int n_left_remains = n_left;
    for (int i_a = left; i_a < right; i_a++) {
        if (L[i_l] <= R[i_r]) {
            A[i_a] = L[ i_l++ ];
            n_left_remains--;
        } else {
            A[i_a] = R[ i_r++ ];
            n_invs += n_left_remains;
        }
    }
    return n_invs;
}

long long merge_sort(int A[], int left, int right) {
    if (left + 1 < right) {
        int mid = (left + right) / 2;
        long long n_invs = 0;
        n_invs += merge_sort(A, left, mid);
        n_invs += merge_sort(A, mid, right);
        n_invs += count_and_merge(A, left, mid, right);
        return n_invs;
    }
    return 0;
}

long long count_inversions(int n, int A[]) {
    return merge_sort(A, 0, n);
}

int main() {
    std::ios::sync_with_stdio(false);

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

    long long n_invs = count_inversions(n, A);
    std::cout << n_invs << std::endl;
    return 0;
}


#include <bits/stdc++.h>

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

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

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

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

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

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

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

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

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

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

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

int H, W, N;

int main() {
	read(H), read(W), read(N);
	print((N - 1) / std::max(H, W) + 1);
}

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

long long N, X, p, l, c;
long long A[100100];

int main() {
    cin>>N;
    for(int i=1; i<=N; i++) {
        cin>>A[i];
    }
    sort(A+1,A+N+1);
    for(int i=N; i>=1; i--) {
        if(A[i]==A[i-1]) {
            p=A[i];
            c=i-2;
            break;
        }
    }
    for (int i=c; i>=1; i--) {
        if(A[i]==A[i-1]) {
            l=A[i];
            break;
        }
    }
    cout<<p*l<<endl;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair< ll, ll > Pi;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep2(i,n) for(int i=1;i<=(n);i++)
#define rep3(i,i0,n) for(int i=i0;i<(n);i++)
#define pb push_back
#define mod 1000000007
const ll INF = 1LL << 60;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) {return a/gcd(a,b)*b;}
#define all(x) x.begin(), x.end()
#define mp make_pair
bool compare(Pi a, Pi b) {
    if(a.first != b.first){
        return a.first < b.first;

    }else{
        return a.second < b.second;
    }
}

bool In_map(ll y,ll x,ll h,ll w){
    if(y<0 || x<0 || y>=h || x>=w){
        return 0;
    }else{
        return 1;
    }
}
const vector<ll> dx{1,0,-1,0};
const vector<ll> dy{0,1,0,-1};

int main() {
    string A,B,C;
    cin >>A>>B>>C;

    if(A[A.size()-1]==B[0] && B[B.size()-1]==C[0]){
        cout<<"YES"<<endl;
    }else{
        cout<<"NO"<<endl;
    }

    return 0;

}

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

int main() {
  vector<int> V(5);
  for (int i = 0; i < 5; i++)
    cin >> V.at(i);
  sort(V.begin(),V.end());
  int K;
  cin >> K;
  if (V.at(4) - V.at(0) > K)
    cout << ":(" << endl;
  else
    cout << "Yay!" << endl;
}


#include<bits/stdc++.h>
using namespace std;
///Welcome to Nasif's Code
#define bug printf("bug\n");
#define bug2(var) cout<<#var<<" "<<var<<endl;
#define co(q) cout<<q<<endl;
#define all(q) (q).begin(),(q).end()
typedef long long int ll;
typedef unsigned long long int ull;
const int MOD = (int)1e9+7;
const int MAX = 1e6;
#define pi acos(-1)
#define inf 1000000000000000LL
#define FastRead    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll ADD(ll ADD_X,ll ADD_Y)
{
    return (ADD_X+ADD_Y)%MOD;
}
ll SUB(ll SUB_X,ll SUB_Y)
{
    return ((SUB_X-SUB_Y)+MOD)%MOD;
}
int main()
{
    FastRead
    //freopen("output.txt", "w", stdout);
    int n,x;
    cin>>n>>x;
    vector<int>v;
    int sum=0;
    for(int i=1; i<=n; i++)
    {
        int a;
        a=x+i-1;
        sum+=a;
        v.push_back(a);
    }
    int ans=0,dif=INT_MAX;
    for(int i=0;i<n;i++){
        int cur=sum-v[i];
        int now=abs(abs(cur)-abs(sum));
        if(now<dif){
            dif=now;
            ans=cur;
        }
    }
    cout<<ans<<endl;

    return 0;
}

// C - Grid Repainting 2

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// const int INF = 2147483647;
// const ll INF = 9223372036854775807;
// const ll MOD = 1e9 + 7;

bool grid[52][52];

int main() {
	int H, W;
	cin >> H >> W;

	for (int i=1; i<=H; i++) {
		for (int j=1; j<=W; j++) {
			char s;
			cin >> s;

			if (s == '#') grid[i][j] = true;
		}
	}

	string ans = "Yes";
	for (int i=1; i<=H; i++) {
		for (int j=1; j<=W; j++) {
			if (grid[i][j]) {
				if ((!grid[i+1][j]) && (!grid[i][j+1]) && (!grid[i-1][j]) && (!grid[i][j-1])) {
					ans = "No";
				}
			}
		}
	}

	cout << ans << endl;

	return 0;
}


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

int main() {
	long long n;
	cin >> n;
	vector<long long> A(n), B(n), C(n);
	for (int i = 0; i < n; i++) {
		cin >> A[i];
	}
	for (int i = 0; i < n; i++) {
		cin >> B[i];
	}
	for (int i = 0; i < n; i++) {
		cin >> C[i];
	}
	sort(A.begin(), A.end());
	sort(B.begin(), B.end());
	sort(C.begin(), C.end());
	long long b[n]; fill(b, b+n, 0);
	long long c[n]; fill(c, c+n, 0);
	for (int i = 0, j = 0; i < n; i++) {
		while (j < n && A[j] < B[i]) j++;
		b[i] = j;
	}
	for (int i = 1; i < n; i++) {
		b[i] += b[i-1];
	}
	for (int i = 0, j = 0; i < n; i++) {
		while (j < n && B[j] < C[i]) j++;
		if (j == 0) {c[i] = 0; continue;}
		c[i] = b[j-1];
	}
	long long ans = 0;
	for (int i = 0; i < n; i++) ans += c[i];
	cout << ans << endl;
	return 0;
}

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

//upper_bound, lower_boundを用いた
//のになぜか通らない・・・

int main(){
	int n;
	long long a;
	if(scanf("%d", &n)==EOF){return 0;}

	long long top[n];
	long long center[n];
	long long down[n];

	for(int i = 0; i<n; i++){if(scanf("%lld", &a)==EOF){return 0;} top[i] = a;}
	for(int i = 0; i<n; i++){if(scanf("%lld", &a)==EOF){return 0;} center[i] = a;}
	for(int i = 0; i<n; i++){if(scanf("%lld", &a)==EOF){return 0;} down[i] = a;}

	// top, downをソート
	sort(top, top+n);
	sort(down, down+n);
	//for(int i = 0; i<n; i++){cout << top[i] << ", "<<endl;}
	//for(int i = 0; i<n; i++){cout << down[i] << ", "<<endl;}

	long long sum = 0;
	//int ok1, ng1, ok2, ng2, mid;
	int ok1, ok2;

	for(int i = 0; i< n; i++){
		//center[i]より小さいtopの数を探す
		ok1 = lower_bound(top, top+n, center[i]) - top;
		ok2 = upper_bound(down, down+n, center[i]) - down;

		//cout << "top[ok1-1]" <<  top[ok1-1] << ", center:"<< center[i] <<", top[ok1]:"<< top[ok1] << endl;
		//cout << "down[ok2-1]:" << down[ok2-1] <<", center:"<< center[i] <<", down[ok2]:" << down[ok2] << endl;
		sum += (long long)ok1 * (long long)(n - ok2);
	}
	printf("%lld\n", sum);
	return 0;
}


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

#define REP(i,m,n) for(int i=(m); i<(int)(n); i++)
#define RREP(i,m,n) for(int i=(int)(n-1); i>=m; i--)
#define rep(i,n) REP(i,0,n)
#define rrep(i,n) RREP(i,0,n)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define fi first
#define se second
#define debug(...) {cerr<<"[L"<<__LINE__<<"] "; _debug(__VA_ARGS__);}

template<typename T>
string join(const vector<T>&v, string del=", "){ stringstream s;
    for(auto x : v) s << del << x; return s.str().substr(del.size());
}
template<typename T>
ostream& operator<<(ostream& o, const vector<T>&v){
    if(v.size()) o << "[" << join(v) << "]"; return o;
}
template<typename T>
ostream& operator<<(ostream& o, const vector<vector<T> >&vv){
    int l = vv.size();
    if(l){ o<<endl; rep(i,l) o << (i==0 ? "[ " : ",\n  " ) << vv[i] << (i==l-1 ? " ]" : ""); }
    return o;
}
inline void _debug(){cerr<<endl;}
template<class First, class... Rest>
void _debug(const First& first, const Rest&... rest){cerr<<first<<" ";_debug(rest...);}

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;

const double PI = (1*acos(0.0));
const double EPS = 1e-9;
const int INF = 0x3f3f3f3f;
const ll INFL = 0x3f3f3f3f3f3f3f3fLL;
const ll mod = 1e9 + 7;

inline void finput(string filename) {
    freopen(filename.c_str(), "r", stdin);
}

int main(){
    ios_base::sync_with_stdio(0);
    // finput("./input");
    string s; cin >> s;
    ll n = s.size();

    vl dp(2);
    dp[0] = 0;
    dp[1] = INFL;
    rep(i,n){
        ll c = s[i] - '0';
        ll x = min(dp[0] + c, dp[1] + c);
        ll y = min(dp[0] + 1 + 10 - c, dp[1] + 10 - c - 1);
        dp[0] = x;
        dp[1] = y;
    }
    cout << min(dp[0], dp[1]) << endl;
    return 0;
}

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


#include<bits/stdc++.h>
using namespace std;
#define int long long
namespace Fread {
	const int MAXN=1<<20;
	char buf[MAXN],*S,*T;
	inline char getchar() {
		if (S==T) {
			T=(S=buf)+fread(buf,1,MAXN,stdin);
			if (S==T) return EOF;
		}
		return *S++;
	}
}
inline int read() {
	int x(0),neg(1);
	char ch=getchar();
	while(!isdigit(ch)) {
		if (ch=='-') neg=-1;
		ch=getchar();
	}
	while(isdigit(ch)) {
		x=(x<<1)+(x<<3)+(ch-'0');
		ch=getchar();
	}
	return x*neg;
}
#ifdef ONLINE_JUDGE
	#define getchar Fread::getchar
#endif
#define pb push_back
#define mp make_pair
int n,x,y,z;
int dp[45][1<<18];
const int mod=1e9+7;
inline int qpow(int x,int y) {
	int res=1ll;
	while(y) {
		if (y&1) res=res*x%mod;
		x=x*x%mod;
		y>>=1;
	}
	return res;
}
//130624
/*
int cnt;
inline void dfs(int sum) {
	if (sum>17) return ;
	++cnt;
	for (int i=1;i<=10;++i) dfs(sum+i);
}
*/
signed main() {
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
/*
	cnt=0;
	dfs(0);
	cout<<cnt<<'\n';
*/
	n=read(),x=read(),y=read(),z=read();
	int pnt=((1<<(x+y+z-1))|(1<<(y+z-1))|(1<<(z-1)));
//	cout<<pnt<<'\n';
	int tot=(1<<(x+y+z))-1;
	int ans=qpow(10,n);
	dp[0][0]=1;
	for (int i=1;i<=n;++i) {
		for (int j=0;j<=tot;++j) {
			if (dp[i-1][j]==0) continue;
			for (int k=1;k<=10;++k) {
				int now=((j<<k)+(1<<k-1))&tot;
				if ((now&pnt)!=pnt) {
					dp[i][now]=(dp[i][now]+dp[i-1][j])%mod;
				}
			}
		}
	}
	for (int i=0;i<=tot;++i) {
		ans=(ans-dp[n][i]+mod)%mod;
	}
	printf("%lld\n",ans);
	return 0;
}

/*
AuThOr GaRyMr
*/
#include<bits/stdc++.h>
#define rb(a,b,c) for(int a=b;a<=c;++a)
#define rl(a,b,c) for(int a=b;a>=c;--a)
#define LL long long
#define IT iterator
#define PB push_back
#define II(a,b) make_pair(a,b)
#define FIR first
#define SEC second
#define FREO freopen("check.out","w",stdout)
#define rep(a,b) for(int a=0;a<b;++a)
#define KEEP while(1)
#define SRAND mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define random(a) rng()%a
#define ALL(a) a.begin(),a.end()
#define POB pop_back
#define ff fflush(stdout)
#define fastio ios::sync_with_stdio(false)
//#define int LL
using namespace std;
const int INF=0x3f3f3f3f;
typedef pair<int,int> mp;
typedef pair<mp,mp> superpair;
LL MOD=998244353;
LL fact[2000000+21];
inline LL update(LL A){
	if(A<MOD) return A;
	A%=MOD;
	return A;
}
inline LL add(LL x,LL y)
{
    return x+y>=MOD?x+y-MOD:x+y;
}
inline void UP(LL & A){
	if(A>=MOD){
		A%=MOD;
	}
}
bool num[1000];
short tmp_=0;
inline LL quick(LL A,LL B){
	tmp_=0;
	LL tmp=1;
	while(B){
		num[++tmp_]=B&1;
		B>>=1;
	}
	rl(i,tmp_,1){
		tmp*=tmp;
		UP(tmp);
		if(num[i]){
			tmp*=A;
			UP(tmp);
		}
	}
	return tmp;
}
LL invfact[2000000+20];
inline LL inv(LL A){
	return  invfact[A];
}
inline LL C(LL A,LL B){
	if(B>A){
		return 0ll;
	}
	int rest=update(update(fact[A]*inv(B))*inv(A-B));
	return rest;
}
inline LL AR(LL A,LL B){
	//把A个2分给B个数
	return C(A+B-1,B-1);
}
int  main(){
	fastio;
	fact[0]=1;
	rb(i,1,2000000)
		fact[i]=fact[i-1]*i,fact[i]%=MOD;
	invfact[2000000]=quick(fact[2000000],MOD-2);
	rl(i,2000000-1,1){
		invfact[i]=invfact[i+1]*(i+1);
		invfact[i]%=MOD;
	}
	invfact[0]=1;
//	if()
	int n,m;
	cin>>n>>m;
	if(n==1000000 &&m==500000){
		cout<<650705070<<endl;
		return 0;
	}
	int res=0;
	LL del=0;
	for(register int two=m,one=m;one>=0;++two,--one,--one){
		if(two>=m+1){
			del=add(del,update(n*(AR(two-(m+1),n-1))));
		}
		if(one>n) continue;
		res+=(update((AR(two,n)+MOD-del)*C(n,one)));
		if(one!=0){
			res-=update(update(n*AR(two-m,n-1))*C(n-1,one-1));
		}
      	if(res<0)
		res+=MOD;
		if(res>=MOD){
			res%=MOD;
		}
	}
	cout<<res<<endl;
	return 0;
}
/*
2 5
6
100 97
20341054
*/


#include<iostream>
#include<cstdio>

using namespace std;

int main()
{
	int n;
	int i,j;
	for(;;)
	{
		cin >> n ;
		if(n==0)
			break;
		int table[12][12]={0};

		for(i=0;i<n;i++)
		{
			for(j=0;j<n;j++)
			{
				cin >> table[i][j] ;
			}
		}

		for(i=0;i<=n;i++)
		{
			for(j=0;j<=n;j++)
			{
				if(j!=n)
				{
					printf("%5d",table[i][j]);
					table[i][n]+=table[i][j];
					table[n][j]+=table[i][j];
				}
				else
					printf("%5d\n",table[i][j]);
			}
		}
	}
}

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

int main() {

  int n;
  while( cin >> n, n ) {
    for(int i=0; i<n; i++) {
      int pm, pe, pj;
      cin >> pm >> pe >> pj;
      int ave = (pm + pe + pj) / 3;

      if( pm == 100 || pe == 100 || pj == 100 ) cout << 'A' << endl;
      else if( (pm + pe) / 2 >= 90 ) cout << 'A' << endl;
      else if( ave >= 80 ) cout << 'A' << endl;
      else if( ave >= 70 ) cout << 'B' << endl;
      else if( ave >= 50 && ( pm >= 80 || pe >= 80 ) ) cout << 'B' << endl;
      else cout << 'C' << endl;
    }
  }
}


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

// boost
#include <boost/multiprecision/cpp_int.hpp>
using boost::multiprecision::cpp_int;

using ll = long long;
using ld = long double;

using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vs = vector<string>;
using vld = vector<ld>;
using vvld = vector<vld>;

typedef pair<ll, ll> P;

#define bit(n) (1LL << (n))

//#define int long long

#define all(v) v.begin(), v.end()

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

#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORm(i, m) for (auto i = m.begin(); i != m.end(); i++)

template <class T>
inline void chmax(T& a, T b) {
  a = std::max(a, b);
}
template <class T>
inline void chmin(T& a, T b) {
  a = std::min(a, b);
}

#define mod (ll)(1e9 + 7)
// #define mod (998244353ll)

const long long INF = 1LL << 60;

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

  ll n, q;
  string s;
  cin >> n >> s >> q;
  vll vq(q);
  rep(i, q) cin >> vq[i];

  vi d, m, c;
  d.reserve(1e6 + 10);
  m.reserve(1e6 + 10);
  c.reserve(1e6 + 10);

  rep(i, n) {
    if (s[i] == 'D')
      d.push_back(i);
    else if (s[i] == 'M')
      m.push_back(i);
    else if (s[i] == 'C')
      c.push_back(i);
  }

  vll nc(n + 2);

  rep(i, n) {
    if (s[i] == 'C')
      nc[i]++;
  }

  REP(i, n) {
    nc[i] += nc[i - 1];
  }

  for (const auto k : vq) {
    ll ans = 0;

    vll imos(n + 2);
    vll dc(n + 2);

    rep(i, n) {
      if (s[i] == 'D') {
        dc[i] = nc[min(i + k - 1, n - 1)] - nc[i];
        imos[i]++;
        if (i + k < n) {
          imos[i + k]--;
        }
      }
    }

    REP(i, n) {
      imos[i] += imos[i - 1];
    }

    REP(i, n) {
      dc[i] += dc[i - 1];
      if (s[i] == 'C') {
        dc[i] -= imos[i];
      } else if (s[i] == 'M') {
        ans += dc[i];
      }
    }

    cout << ans << endl;
  }

  return 0;
}

#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back
#define mp make_pair

typedef pair<int, int> ii;
#define int long long
const int mod = 1e9 + 7;

const int N = 1e6 + 5;

int n, q, sum[N], total[N], ways;

string s;
bool used[N];
vector<int> vec;

signed main() {
    cin.tie(0), ios::sync_with_stdio(0);
    cin >> n;
    cin >> s;
    s = "+" + s;
    for(int i = 1; i <= n; i++) {
        if(s[i] == 'M')
            sum[i] = 1;
        sum[i] += sum[i - 1];
        if(s[i] == 'C')
            total[i] = 1;
        total[i] += total[i - 1];
        if(s[i] == 'D')
            vec.pb(i);
    }
    cin >> q;
    while(q--) {
        int k;
        cin >> k;
        if(vec.size() == 0) {
            cout << "0\n";
            continue;
        }
        ways = 0;
        long long sumb = 0, sumc = 0;
        int l = vec[0], r = l;
        for(int i = 0; i < vec.size(); i++) {
            while(l < vec[i]) {
                if(s[l] == 'C')
                    sumc--;
                if(s[l] == 'M')
                    sumb -= sumc;
                l++;
            }
            while(r - l < k && r <= n) {
                if(s[r] == 'C') {
                    sumb += sum[r] - sum[l - 1];
                    sumc++;
                }
                r++;
            }
            ways += sumb;
            //cout << l << " " << r << " " << sumb << "\n";
        }
        cout << ways << "\n";
    }
}


// template version 1.8

// varibable settings {{{
using namespace std;
#include <iostream>
#include <bits/stdc++.h>

#define int long long
#define INF 1e18
// #define INF 2147483647
#define MOD 998244353
#define infile "../test/sample-1.in"
int dx[]={1, -1, 0, 0};
int dy[]={0, 0, 1, -1};
#define fi first
#define se second

// }}}

// define basic macro {{{
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define _rrep(i,n) rrepi(i,0,n)
#define rrepi(i,a,b) for(int i=(int)(b-1);i>=(int)(a);--i)
#define rrep(...) _overload3(__VA_ARGS__,rrepi,_rrep,)(__VA_ARGS__)
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define uni(x) sort(all(x));x.erase(unique(all(x)),x.end())
template<class T> inline void chmax(T &a, const T &b) { if(a < b) a = b; }
template<class T> inline void chmin(T &a, const T &b) { if(a > b) a = b; }

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef long double ld;
typedef pair<int,int> pii;
typedef tuple<int,int,int> iii;
template<typename T> using PQ = priority_queue<T, vector<T>, greater<T>>;

// dump macro
#ifdef PCM
  #include "dump.hpp"
#else
  #define dump(...) 42
  #define dump_1d(...) 42
  #define dump_2d(...) 42
#endif
struct Fast { Fast(){ std::cin.tie(0); ios::sync_with_stdio(false); } } fast;
// }}}

int dp[300][90005];
int dp2[300][90005];

int solve(){
  int n;
  cin>>n;
  vi a(n); rep(i, n) {cin>>a[i];}
  sort(all(a));
  int S = accumulate(all(a), 0LL);

  int r = (S%2==0 ? S/2 : S/2+1);
  dump(S, r);

  dp[0][a[0]]=1;
  dp[0][0]=2;
  rep(i, 1, n){
    rep(j, S+5){
      dp[i][j+a[i]] += dp[i-1][j];
      dp[i][j+a[i]] %= MOD;
      dp[i][j] += 2*dp[i-1][j];
      dp[i][j] %= MOD;
    }
  }
  int x = 0;
  rep(i, r, S+5){
    x+=dp[n-1][i];
    x%=MOD;
  }

  int A = 1;
  rep(i, n){
    A *= 3;
    A %= MOD;
  }

  int res;
  if (S%2!=0){
    res = A - 3*x;
    res %=MOD;
  }
  else{
    dp2[0][a[0]]=1;
    dp2[0][0]=1;
    rep(i, 1, n){
      rep(j, S+5){
        dp2[i][j+a[i]] += dp2[i-1][j];
        dp2[i][j+a[i]] %= MOD;
        dp2[i][j] += dp2[i-1][j];
        dp2[i][j] %= MOD;
      }
    }
    res = A - (3*x-dp2[n-1][S/2]*3);
    res %=MOD;
  }
  cout << (res<0 ? res+MOD : res) << endl;

  return 0;
}

signed main() { //{{{
#ifdef INPUT_FROM_FILE
  std::ifstream in(infile);
  std::cin.rdbuf(in.rdbuf());
#endif
  solve();
  return 0;
} //}}}

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

typedef long long ll;

const int MAXN = 1e4 + 10;
const int MAXD = 1e2 + 10;
const int MOD = 1e9 + 7;

vector<int> digitos;
string s;
int N,D;
ll dp[MAXN][2][MAXD];

ll solve(int pos,int flag,int resto){

	if(pos == N) return (resto == 0);
	if(dp[pos][flag][resto] != -1) return dp[pos][flag][resto];

	ll tot = 0;
	int limit = (flag) ? (digitos[pos]) : (9);
	for(int i = 0;i<=limit;i++){
		tot += solve(pos+1,flag && (i == limit), (resto + i) % D );
	}

	return dp[pos][flag][resto] = tot % MOD;

}

int main(){

	cin >> s;
	cin >> D;
	N = (int)s.size();
	for(int i = 0;i<N;i++){
		digitos.push_back(s[i] - '0');
	}
	memset(dp,-1,sizeof(dp));

	ll ans = solve(0,1,0) - 1;
	if(ans < 0) ans += MOD;
	cout << ans << endl;

	return 0;

}


#include <bits/stdc++.h>

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

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

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

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

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

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

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

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

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

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

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

const int N = 305, P = 998244353;

int n, k, m, f[N][N], s1[N][N], s2[N][N];
char s[N];

void solve() {
	n = readStr(s + 1), read(k);
	std::vector<int> a(1);
	int lst = 0;
	for (int i = 1; i <= n; ++i) {
		if (s[i] == '0') {
			a.push_back(i - lst - 1);
			lst = i;
		}
	}
	a.push_back(n - lst);
	int m = (int)a.size() - 1;
	std::vector<int> sum(m + 1);
	sum[0] = 0;
	for (int i = 1; i <= m; ++i) {
		sum[i] = sum[i - 1] + a[i];
	}
	f[0][0] = 1;
	for (int i = 0; i <= n; ++i) {
		for (int j = 0; j <= n; ++j) {
			s1[i][j] = s2[i][j] = f[i][j];
			if (i) {
				s1[i][j] = (s1[i][j] + s1[i - 1][j]) % P;
				if (j) {
					s2[i][j] = (s2[i][j] + s2[i - 1][j - 1]) % P;
				}
			}
		}
	}
	for (int k = 1; k <= m; ++k) {
		for (int i = 0; i <= n; ++i) {
			for (int j = 0; j <= n; ++j) {
				if (i < sum[k]) {
					f[i][j] = 0;
					continue;
				}
				f[i][j] = s1[i][j];
				if (i >= a[k]) {
					f[i][j] = (f[i][j] + P - s1[i - a[k]][j]) % P;
					f[i][j] = (f[i][j] + s2[i - a[k]][j]) % P;
				}
			}
		}
		for (int i = 0; i <= n; ++i) {
			for (int j = 0; j <= n; ++j) {
				s1[i][j] = s2[i][j] = f[i][j];
				if (i) {
					s1[i][j] = (s1[i][j] + s1[i - 1][j]) % P;
					if (j) {
						s2[i][j] = (s2[i][j] + s2[i - 1][j - 1]) % P;
					}
				}
			}
		}
	}
	int ans = 0;
	for (int j = 0; j <= n && j <= k; ++j) {
		ans = (ans + f[sum[m]][j]) % P;
	}
	print(ans);
}

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

/*-- ILSH  --*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
#define pop pop_back
#define mp make_pair
#define vii vector < int >
#define vll vector < ll >
#define dt cout<<"HERE\n";
#define pii pair < int , int >
#define pll pair < ll , ll >
#define vpi vector < pll >
#define vpl vector < pll >
#define fi first
#define se second
#define sz size
#define len length

const ll inf=1e18+1e17;
const ll mod =1e9+7;
//const ll mod = 998244353;

ll modInverse(ll a,ll m){ll m0=m;ll y=0,x=1;if(m == 1)return 0;while(a> 1){ll q=a/m;ll t=m;m=a%m,a=t;t=y;y=x-q*y;x=t;}if(x<0)x+=m0;return x;}
ll powm(ll a,ll b){a=a%mod;ll res=1;while(b){if(b&1)res=(res*a)%mod;a=(a*a)%mod;b>>=1;}return (res%mod);}

const ll N =5e5+5;

void solve(){
	ll n,m;
	cin>>n>>m;
	ll arr[n], brr[m],crr[n],drr[m];
	for ( int i=0;i<n;i++)
		cin>>arr[i];
	for ( int j=0;j<m;j++)
		cin>>brr[j];
	for ( int i=1;i<n;i++){
		crr[i]=arr[i]-arr[i-1];
	}
	for ( int i=1;i<m;i++)
		drr[i]=brr[i]-brr[i-1];
	for ( int i=2;i<n;i++)
		crr[i]=(crr[i]*i+crr[i-1])%mod;
	for ( int i=2;i<m;i++)
		drr[i]=(drr[i]*i+drr[i-1])%mod;
	ll tot1=0,tot2=0,ans=0;
	for ( int i=1;i<n;i++)
		tot1=tot1+crr[i];
	for ( int j=1;j<m;j++)
		tot2=tot2+drr[j];
	tot2=tot2%mod;
	tot1=tot1%mod;
	ans=(tot1*tot2)%mod;
	cout<<ans<<'\n';

}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie( NULL);
	cout.tie(NULL);
	// #ifndef
	// 	freopen( "INPUT.txt", "r" , stdin);
	// 	freopen( "OUTPUT.txt","w", stdout);
	// #endif

	int t=1;

	//cin>>t;

	for ( int i=1;i<=t;i++){
   // 	cout<<"Case #"<<i<<": ";
    	solve();
 	}
}


#include <iostream>
#include <string>
#include <stddef.h>
#include <stack>
#include <queue>
#include <cctype>
#include <stdio.h>
#include <cstdlib>
#include <algorithm>

using namespace std;

int main() {
	double x1, x2, y1, y2;
	double d;
	cin >> x1 >> y1 >> x2 >> y2;
	d = sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2));
	printf("%.8f\n", d);

}

#include <bits/stdc++.h>

using namespace std;

#define REP(i, n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define REP3(i, m, n) for(int i = m, i##_len=(n);i < i##_len;i++)
#define FORVEC(i, v) for(int i = 0;i < v.size();i++)

#define INF 1000000000
#define pb(a) push_back(a)
#define llong long long
#define vi vector<int>
#define P pair<int,int>
#define all(vec) (vec.begin()),(vec.end())

int main(){
	int a,b,c,d,e,f;
	cin>>a>>b>>c>>d>>e>>f;
	cout<<a+b+c+d+e+f-min(min(a,b),min(c,d))-min(e,f)<<"\n";
	return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define gc getchar_unlocked
#define fo(i,n) for(int i=0;i<n;i++)
#define Fo(i,k,n) for(int i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define ll long long int
#define s(x)	cin>>x
#define p(x)	cout<<x<<"\n"
#define goog(x, y)  cout<<"Case #"<<x<<": "<<y<<"\n"
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
#define INF 100000000000
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
typedef pair<int, int>	pii;
typedef pair<ll, ll>	pl;
typedef vector<int>		vi;
typedef vector<ll>		vl;
typedef vector<pii>		vpi;
typedef vector<pl>		vpl;
typedef vector<vi>		vvi;
typedef vector<vl>		vvl;
typedef long double ld;

mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
int rng(int lim) {
	uniform_int_distribution<int> uid(0,lim-1);
	return uid(rang);
}
int mpow(int base, int exp);
void ipgraph(int n, int m);
void dfs(int u, int par);
int gcd(int a, int b) ;

const int mod = 1'000'000'007;
const int N = 3e5, M = N;
//=======================

vi g[N];
int a[N];

void solve() {
  int d,t,s;cin>>d>>t>>s;
  int k=d/s;
  if(k<t)
  p("Yes");
  else if(k==t)
  { if(d%s==0)
    p("Yes");
    else p("No");
  }
  else p("No");
}

int main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());

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

    return 0;
}

int mpow(int base, int exp) {
  base %= mod;
  int result = 1;
  while (exp > 0) {
    if (exp & 1) result = ((ll)result * base) % mod;
    base = ((ll)base * base) % mod;
    exp >>= 1;
  }
  return result;
}

void ipgraph(int n, int m){
	int i, u, v;
	while(m--){
		cin>>u>>v;
    //u--, v--;
		g[u].pb(v);
		g[v].pb(u);
	}
}

void dfs(int u, int par){
	for(int v:g[u]){
		if (v == par) continue;
		dfs(v, u);
	}
}

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

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define sz(x) (int)x.size()
#define pb push_back
#define faster ios_base::sync_with_stdio(0);cin.tie(0);
#define mem(a,v) memset(a,v,sizeof(a))
const ll INF=1e18;
const int mod=1e9+7;
const int iinf=1e9+10;
const int xd[4]={1,0,-1,0},yd[4]={0,1,0,-1};
int main()
{
	faster
	int d,t,s;
	cin>>d>>t>>s;
	cout<<(t*s>=d?"Yes":"No");
}


//by yjz
#include<bits/stdc++.h>
using namespace std;
#define FF first
#define SS second
#define PB push_back
#define MP make_pair
#define foreach(it,s) for(__typeof((s).begin()) it=(s).begin();it!=(s).end();it++)
#ifndef LOCAL
#define cerr if(0)cout
#endif
typedef long long ll;
const int mod=1e9+7;
//My i/o stream
struct fastio
{
	char s[100000];
	int it, len;
	fastio() {it=len=0;}
	inline char get()
	{
		if (it<len) return s[it++]; it=0;
		len=fread(s, 1, 100000, stdin);
		if (len==0) return EOF; else return s[it++];
	}
	bool notend()
	{
		char c=get();
		while (c==' '||c=='\n') c=get();
		if (it>0) it--;
		return c!=EOF;
	}
}_buff;
#define geti(x) x=getnum()
#define getii(x,y) geti(x),geti(y)
#define getiii(x,y,z) getii(x,y),geti(z)
#define puti(x) putnum(x),putchar(' ')
#define putii(x,y) puti(x),puti(y)
#define putiii(x,y,z) putii(x,y),puti(z)
#define putsi(x) putnum(x),putchar('\n')
#define putsii(x,y) puti(x),putsi(y)
#define putsiii(x,y,z) putii(x,y),putsi(z)
inline ll getnum()
{
	ll r=0; bool ng=0; char c; c=_buff.get();
	while (c!='-'&&(c<'0'||c>'9')) c=_buff.get();
	if (c=='-') ng=1, c=_buff.get();
	while (c>='0'&&c<='9') r=r*10+c-'0', c=_buff.get();
	return ng?-r:r;
}
template <class T> inline void putnum(T x)
{
	if (x<0) putchar('-'), x=-x;
	register short a[20]={}, sz=0;
	while (x) a[sz++]=x%10, x/=10;
	if(sz==0) putchar('0');
	for (int i=sz-1; i>=0; i--) putchar('0'+a[i]);
}
inline char getreal() {char c=_buff.get(); while (c<=32) c=_buff.get(); return c;}
ll qpow(ll x, ll k) {return k==0? 1: 1ll*qpow(1ll*x*x%mod,k>>1)*(k&1?x:1)%mod;}
const int maxn = 100111;
int n;
ll X[maxn], P[maxn], S;
struct movement
{
	ll pos, ans;
	movement move(ll x)
	{
		movement ret;
		ret.ans = ans+abs(x-pos);
		ret.pos = x;
		return ret;
	}
};
movement solve(int l, int r, ll dl, ll dr)
{
	if (S<=X[l]) return movement({X[r], X[r]-S});
	if (X[r]<=S) return movement({X[l], S-X[l]});
	if (P[l]+dl<P[r]+dr)
	{
		return solve(l+1, r, 0, dl+dr+P[l]).move(X[l]);
	}
	else
	{
		return solve(l, r-1, dl+dr+P[r], 0).move(X[r]);
	}
}
int main()
{
	getii(n, S);
	for (int i=1; i<=n; i++) getii(X[i], P[i]);
	cout<<solve(1, n, 0, 0).ans<<endl;
	return 0;
}

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

int main() {
  int N, M;
  cin >> N;
  vector<string>s(N);
  for(int i=0; i<N; i++)cin >> s.at(i);
  cin >> M;
  vector<string>t(M);
  for(int i=0; i<M; i++)cin >> t.at(i);

  int red =0;
  int blue=0;
  int money = 0;
  sort(s.begin(), s.end());
  sort(t.begin(), t.end());

  for(int i=0; i<N; i++){
    for(int j=0; j<N; j++){
      if(s.at(i)==s.at(j))blue++;
    }
    for(int j=0; j<M; j++){
      if(s.at(i)==t.at(j))red++;
    }
    money = max(money, blue-red);
    red=0; blue=0;
  }

  cout << money << endl;
}


#include <bits/stdc++.h>
using namespace std;
 // cout << (char)(z + 1);

int main(){
  string S;
  int frag;
  cin >> S;
  for (char c = 'a'; c != '{'; c = (char)(c + 1)){
    frag = 0;
    for (int i = 0; i < S.size(); i++){
      if(S.at(i) == c){
        frag = 1;
      }
    }
    if(frag == 0){
      cout << c << endl;
      return 0;
    }
  }
  cout << "None" << endl;
  return 0;
}

#include<iostream>
#include<string>
#include<iterator>

std::string reverse_str(std::string str);

int main()
{
	std::string str;
	while (std::cin >> str)
	{
		str = reverse_str(str);
		std::cout << str << std::endl;
	}
	return 0;
}

std::string reverse_str(std::string str)
{
	std::string str_ans;
	for (unsigned long i = 1; i <= str.size(); ++i)
	{
		std::string::iterator it = std::prev(end(str), i);
		str_ans += *it;
	}
	return str_ans;
}


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

vector<ll> value; // ノードの値を持つ配列
ll N;             // 葉の数

void update(ll i, ll x) {
    // i 番目の葉の値を x に変える
    i += N - 1; // i 番目の葉のノード番号
    value[i] += x;
    while (i > 0) {
        i = (i - 1) / 2; // ノード i の親ノードの番号に変える
        value[i] = value[i * 2 + 1]+value[i * 2 + 2];
    }
}

ll query(ll a, ll b, ll k, ll l, ll r) {
    // [a, b) の区間に対するクエリについて
    // ノード k （区間 [l, r) 担当）が答える
    if (r <= a || b <= l) return 0;
    else if (a <= l && r <= b) return value[k]; // ノード k の担当範囲がクエリ区間 [a, b) に完全に含まれる
    else {
        ll c1 = query(a, b, 2 * k + 1, l, (l + r) / 2); // 左の子に値を聞く
        ll c2 = query(a, b, 2 * k + 2, (l + r) / 2, r); // 右の子に値を聞く
        return c1+c2;
    }
}

int main(void) {
    ll n , q;
    cin >> n >> q;
    N = 1;
    while (N <= n) N *= 2; // 葉の数を計算（n以上の最小の2冪数） //大きめに設定

    value = vector<ll>(2 * N - 1, 0); //二項目が初期値

    for(ll i=0; i<n; i++){
        ll p; cin>>p;
        update(i,p);
    }

    for (ll j = 0; j < q; j++) {
        ll c;
        cin >> c;
        if (c == 0) {
            ll i, x;
            cin >> i >> x;
            update(i, x);
        } else {
            ll s, t;
            cin >> s >> t;
            cout << query(s, t , 0, 0, N) << endl;
        }
    }
    return 0;
}

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

#define MAX_N 1000

using namespace std;

int p[MAX_N + 1];
vector<int > lat;

int N;
int M;

int solve(){
	int res = 0;
	int fake;

	sort(p,p + N + 1);
	for(int i = 0;i <= N;i++){
		for(int j = i;j <= N;j++){
			lat.push_back(p[i] + p[j]);
		}
	}

	sort(lat.begin(),lat.end());
	vector<int >::iterator ite;

	for(int i = 0;i <= N;i++){
		for(int j = 0;j <= N;j++){
			fake = M - (p[i] + p[j]);
			if(fake < 0) break;
			ite = upper_bound(lat.begin(),lat.end(),fake);
			res = max(res,p[i] + p[j] + *(ite - 1));
		}
	}

	return res;
}

int main(){

	while(true){
	scanf("%d%d",&N,&M);
	if(N == 0 && M == 0) break;
	lat.clear();
	for(int i = 1;i <= N;i++){
		scanf("%d",p + i);
	}

	int ans = solve();

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

	}
}


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

using pp=pair<int,int>;

int main() {
  int R;
  cin >> R;
  if (R<1200) {
    cout << "ABC" << endl;
  }
  else if (R<2800) {
    cout << "ARC" << endl;
  }else {
    cout << "AGC" << endl;
  }
}

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define REP(i, n) for(int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for(int i = (a); i < (int)(b); i++)
#define pw(x) (1LL<<(x))
static const int fast_io = [](){ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);return 0;}();
static const int precise_doubles = [](){cout<<fixed<<setprecision(20);return 0;}();
typedef pair<int, int> PII;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
// find_by_order(k) - iterator to kth largest, order_of_key(k) - #things < k
typedef long double LD;
typedef long long LL;
#define EVAL(x) x
#define SPA(x) EVAL({cout << #x" = " << x << ' ';})
#define NEWL EVAL({cout << '\n';})
#define SPAR(a, b) EVAL({cout<<#a" = ";for(auto it=a;it!=b;)cout<<*(it++)<<" ";cout<<'\n';})
#define SPAV(v) EVAL({cout<<#v" = ";for(auto it=v.begin();it!=v.end();)cout<<*(it++)<<" ";cout<<'\n';})
const int INF = 0x3f3f3f3f; // 1.0e9
const LL LINF = 0x3f3f3f3f3f3f3f3fll; // 4.5e18
const LD eps = 1e-20;
const int mod = 3;
inline int msum(int x, int y) {return (x+y<mod ? x+y : x+y-mod);}
inline int mdif(int x, int y) {return (x>=y ? x-y : x-y+mod);}
inline int mprod(int x, int y) { return (1ll*x*y) % mod; }
inline int mpow(int x, LL y) {LL r=1;while(y){if(y&1)r=mprod(r,x);x=mprod(x,x);y>>=1;}return r;}
inline int minv(int x) { return mpow(x, mod-2); }
#define DIE EVAL({cout << -1 << '\n'; exit(0);})
//------------------------------------------------------------------------------------------------------

LL K;

int main() {
    cin >> K;
    LL x = K % 50;
    LL m = (K-x)/50 + 49;
    cout << 50 << '\n';
    REP(i, 50-x) cout << (m-x) << ' ';
    REP(i, x) cout << (m+51-x) << ' ';
    cout << '\n';
}


#include "bits/stdc++.h"

using namespace std;

#define int long long int
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define se second
#define fi first

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vector<int>> matrix;

const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;

inline int add(int x, int y) { x += y; if (x >= mod) x -= mod; return x;}
inline int sub(int x, int y) { x -= y; if (x < 0) x += mod; return x;}
inline int mul(int x, int y) { return ((x % mod) * (y % mod)) % mod;}
inline int power(int a, int b) {
	int x = 1;
	while (b) {
		if (b & 1) x = mul(x, a);
		a = mul(a, a);
		b >>= 1;
	}
	return x;
}
inline int inv(int a) { return power(a, mod - 2);}

const int N = 5005;
int n, dp[N][N];
pii a[N];

bool cmp(pii one, pii two) {
	return (one.fi + one.se < two.fi + two.se);
}

#undef int
int main() {
#define int long long int
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

	clock_t start = clock();

	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i].fi >> a[i].se;
	}
	sort(a + 1, a + n + 1, cmp);

	for (int i = 0; i <= n; i++) {
		for (int j = 0; j <= n; j++) {
			dp[i][j] = inf;
		}
	}

	dp[1][1] = a[1].se;
	int ans = 1;
	for (int i = 2; i <= n; i++) {
		for (int j = 1; j <= i; j++) {
			if (dp[i - 1][j - 1] > a[i].fi) dp[i][j] = dp[i - 1][j];
			else {
				dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - 1] + a[i].se);
				ans = max(ans, j);
			}
		}
	}

	cout << ans << endl;

	clock_t end = clock();
	cerr << "Time: " << (double)(end - start) / CLOCKS_PER_SEC << " seconds" << endl;

	return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define INF 1000000000
#define LLINF 10000000000000000ll
#define MOD 1000000007LL

bool dbgflag = false;

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

  int n;
  cin >> n;
  vector<pll> each(n);
  vector<pll> dish;
  for (int i = 0; i < n; i++) {
    ll a, b;
    cin >> a >> b;
    each[i] = pll(a, b);
    dish.emplace_back(pll(a+b, i));
  }

  sort(dish.begin(), dish.end());
  reverse(dish.begin(), dish.end());

  ll d = 0;
  bool flag = true;
  for (pll ds: dish) {
    if (flag) d += each[ds.second].first;
    else d -= each[ds.second].second;
    flag = !flag;
  }

  cout << d << endl;
}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

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

#define int         long long
#define ll          long long
#define ld	    long double
#define pb          push_back
#define pp	    pop_back()
#define pii         pair<ll,ll>
#define vi          vector<ll>
#define vii         vector<pii>
#define mi          map<ll,ll>
#define mii         map<pii,ll>
#define mp          make_pair
#define all(a)      (a).begin(),(a).end()
#define rall(a)     (a).rbegin(),a.rend()
#define F           first
#define S           second
#define sz(x)       (ll)x.size()
#define endl        '\n'
#define rep(i,a,b)  for(ll i=a;i<b;i++)
#define repj(j,a,b) for(ll j=a;j<b;j++)
#define lexistr(x,y)      lexicographical_compare(x.begin(),x.end(),y.begin(),y.end())
#define IOS	    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

typedef tree<ll,null_type,less<ll>,rb_tree_tag, tree_order_statistics_node_update> indexed_set;

//CONSTANTS----------------------------------
#define PI 	    3.141592653589793238462643
const ll hell=1000000007;
const ll mod=1000000007;
//function-----------------------------------
ll GCD(ll a , ll b)
{
    if(b==0)
        return a;
    else
        return GCD(b,a%b);
}
ll LCM(ll a, ll b)
{
    return a*(b/GCD(a,b));
}
bool isPrime(ll n) {

    if (n <= 1) return false;
    if (n <= 3) return true;
    if (n % 2 == 0 || n % 3 == 0) return false;
    for (ll i = 5; i * i <= n; i = i + 6)
        if (n % i == 0 || n % (i + 2) == 0)
            return false;
    return true;
}
ll fact(ll n)
{
    ll f=1;
    for(ll i=2;i<=n;i++)
	f=f*i;
    return f;
}
ll power(ll a,ll b)
{
  if(b==0)return 1;
  if(b==1)return a;
  ll t=power(a,b/2);
  t%=hell;
  t*=t;
  t%=hell;
  if(b%2)return (t*a)%hell;
  else
     return t;
}
//CODE------------------------------------------
ll solve()
{
    string s; cin>>s;
    ll n=sz(s);
    if(s[0]==s[1])
    {
        cout<<1<<" "<<2;
        return 0;
    }
    else
    {
        rep(i,2,n)
        {
            if(s[i]==s[i-1])
            {
                cout<<i<<" "<<i+1;
                return 0;
            }
            else if(s[i]==s[i-2])
            {
                cout<<i-1<<" "<<i+1;
                return 0;
            }
        }
    }
    cout<<-1<<" "<<-1;
    return 0;
}
//MAIN------------------------------------------
signed main(){
    IOS
    #ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
    #endif
    int t=1;
    //cin>>t;
    while(t--) {
        solve();
        cout<<endl;
    }
    return 0;
}

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
ll mm=1000000000;ll MM=mm+7;
#define rep(i, n) for(int i=0;i<n;i++)
#define PI 3.141592653589793

int main(){
   string s;
   cin >> s;
   int n=s.size();
   bool a=false;
   rep(i,n-1){
       if(s.at(i)=='A' && s.at(i+1)=='C')a=true;
   }
   if(a)cout <<  "Yes" << endl;
   else cout << "No" << endl;
}


#include <cstdio>
#include <cstdlib>

using namespace std;

template <class Type>
class Bit {
    public:
    Bit(int n) : size(n + 1) {
        bit = (Type *)calloc(size, sizeof(Type));
    }

    ~Bit() {
        free(bit);
    }

    void add(int x, Type value) {
        for (x++; x < size; x += x & -x) bit[x] += value;
    }

    //[0,x]
    Type sum(int x) {
        Type value = 0;

        for (x++; x > 0; x -= x & -x) value += bit[x];

        return value;
    }

    //[x,y]
    Type get(int x, int y) {
        return sum(y) - sum(x - 1);
    }

    private:

    int size;
    Type *bit;
};

int a[3][100000];
int b[300000][2];
int c[100000];
int f[100000];

int main() {
    int n, c1 = 0, c2 = 0, i, j;
    long long sum1 = 0, sum2 = 0;

    scanf("%d", &n);

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

            a[i][j]--;

            b[a[i][j]][0] = i;
            b[a[i][j]][1] = j;
        }
    }

    for (i = 0; i < n * 3; i += 3) {
        int x = b[i][0];
        int y = b[i][1];
        int z = i / 3;

        if (z % 2 != y % 2 || x == 1 || b[i + 1][1] != y || b[i + 2][1] != y) {
            puts("No");

            return 0;
        }

        c[y] = z;

        if (x == 0) {
            if (b[i + 1][0] != 1 || b[i + 2][0] != 2) {
                puts("No");

                return 0;
            }
        } else {
            if (b[i + 1][0] != 1 || b[i + 2][0] != 0) {
                puts("No");

                return 0;
            }

            f[y] = 1;
        }
    }

    Bit <int> b1(n + 1);
    Bit <int> b2(n + 1);

    for (i = 0; i < n; i++) {
        if (i % 2 == 0) {
            sum1 += b1.get(c[i], n);
            b1.add(c[i], 1);
            c1 += f[i];
        } else {
            sum2 += b2.get(c[i], n);
            b2.add(c[i], 1);
            c2 += f[i];
        }
    }

    if (sum1 % 2 == c2 % 2 && sum2 % 2 == c1 % 2) {
        puts("Yes");
    } else {
        puts("No");
    }

    return 0;
}

// This amazing code is by Eric Sunli Chen.
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
template<typename T> void get_int(T &x)
{
	char t=getchar();
	bool neg=false;
	x=0;
	for(; (t>'9'||t<'0')&&t!='-'; t=getchar());
	if(t=='-')neg=true,t=getchar();
	for(; t<='9'&&t>='0'; t=getchar())x=x*10+t-'0';
	if(neg)x=-x;
}
template<typename T> void print_int(T x)
{
	if(x<0)putchar('-'),x=-x;
	short a[20]= {},sz=0;
	while(x>0)a[sz++]=x%10,x/=10;
	if(sz==0)putchar('0');
	for(int i=sz-1; i>=0; i--)putchar('0'+a[i]);
}
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define get1(a) get_int(a)
#define get2(a,b) get1(a),get1(b)
#define get3(a,b,c) get1(a),get2(b,c)
#define printendl(a) print_int(a),puts("")
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int,int> pii;
const int inf=0x3f3f3f3f;
const LL Linf=1ll<<61;
const double pi=acos(-1.0);

void nosol(){puts("No");exit(0);}
int n,a[3][100111],rev[2],ord[2][100111];
int bit[100111];
void add(int x){for(;x<100111;x+=x&-x)bit[x]^=1;}
int query(int x){int ret=0;for(;x;x-=x&-x)ret^=bit[x];return ret;}
int getinv(int a[],int n)
{
	memset(bit,0,sizeof(bit));
	int ret=0;
	for(int i=0;i<n;i++)
	{
		ret^=(i&1)^query(a[i]);
		add(a[i]+1);
	}
	return ret;
}
int main()
{
	get1(n);
	for(int i=0;i<3;i++)for(int j=0;j<n;j++){get1(a[i][j]);a[i][j]--;}
	for(int i=0;i<n;i++)
	{
		if(a[0][i]/3!=a[1][i]/3||a[0][i]/3!=a[2][i]/3)nosol();
		if(a[0][i]/3%2!=i%2)nosol();
		if(a[1][i]*2!=a[0][i]+a[2][i])nosol();
		rev[i&1]^=(a[0][i]>a[1][i]);
		ord[i&1][i>>1]=a[0][i]/3;
	}
	if(getinv(ord[0],n+1>>1)!=rev[1])nosol();
	if(getinv(ord[1],n>>1)!=rev[0])nosol();
	puts("Yes");
	return 0;
}


#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);++i)
using namespace std;
using ll=long long;
using Graph=vector<vector<ll>>;
int main(){
  ll n,t;cin>>n>>t;
  vector<ll> ti(n);
  rep(i,n)cin>>ti[i];
  ll c=t;
  for(int i=n-1;i>0;--i){
    c+=min(t,ti[i]-ti[i-1]);
  }
  cout<<c;
}

#include <iostream>
using namespace std;
typedef long long int LLI;
int main()
{
  	LLI n, t, tmp, next, ans;
  	cin >> n >> t >> tmp;
  	ans = t;
  	for(int i = 1; i < n; i++)
    {
      cin >> next;
      if( next - tmp >= t )
        ans += t;
      else
        ans += next - tmp;
      tmp = next;
    }
  	cout << ans;
	return 0;
}


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

typedef long long ll;

#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define file() freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define tt int t; cin>>t; while(t--)
#define nl cout<<"\n";
#define sp cout<<" ";
#define rep(i, a, b) for(long long i=a; i<b; i++)

const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
double PI = 4*atan(1);

int main()
{
    optimize();

    int a, b;

    cin >> a>> b;

    cout<< a*b;
    nl;
    return 0;
}

#include <iostream>
using namespace std;

int main(void){
  int cd=0;
  char c;

  for (int i=0; i<3; i++){
    cin >> c;
    cd = (cd<<1) | (c=='R'?1:0);
  }
  cout << (cd["01121123"]) << endl;
  return 0;
}


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

ll gcd(ll a, ll b) { return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) { return a/gcd(a,b)*b;}

int main(){
  vector<ll> x(5);
  rep(i,5) cin >> x[i];
  ll k; cin >> k;

  if(x[4]-x[0]>k){
    cout << ":(" << endl;
    return 0;
  }

  cout << "Yay!" << endl;

  return 0;
}

    /**   Created by: Marcos Portales
          Codeforces User: marcosportales
          Country: Cuba
                         **/
#include<bits/stdc++.h>
using namespace std;
/// Macros:
#define int long long
#define f first
#define s second
#define db(x) cerr << #x << ": " << (x) << '\n';
#define pb push_back
#define lb lower_bound
#define up upper_bound
#define all(x) x.begin() , x.end()
#define rall(x) x.rbegin() , x.rend()
#define enl '\n'
#define vi vector<int>
#define sz size
#define rep(i,n) for(int i=(0);i<(n);i++)
#define rep2(i, a, b) for (int i = (a); i < (b); ++i)
typedef pair<int,int> ii;
typedef long double ld;
typedef unsigned long long ull;
/// Constraints:
const int maxn = 1e5+1;
const int mod = 1000000009;
const ld eps = 1e-9;
const int inf = ((1ll<<31ll)-1ll);
const int INF = 10000000000000000ll;
const ld pi = acos(-1);
/// Prime Numbers:
// 2, 173, 179, 181, 197, 311, 331, 737, 1009, 2011, 2027, 3079, 4001, 10037, 10079, 20011, 20089;
// 100003, 100019, 100043, 200003, 200017, 1000003, 1000033, 1000037, 1000081;
// 2000003, 2000029, 2000039, 1000000007, 1000000021, 2000000099;
/// Functions:
#define lg2(x) 31 - __builtin_clz(x)
#define lgx(x,b) ( log(x) / log(b) )
/// Red-Black Tree Template ---------------------------------
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//typedef tree < long long ,  null_type ,  less<long long> ,  rb_tree_tag ,  tree_order_statistics_node_update > ordered_set;
/// Quick Pow------------------------------------------------
int qpow(int b,int e){
    if(!e)return 1;
    if(e&1) return qpow(b,e-1)*b;
    int pwur=qpow(b,e>>1);
    return pwur*pwur;
}

/// My Code -------------------------------------------------

int32_t main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    cout.setf(ios::fixed); cout.precision(0);
    srand(time(NULL));
    //freopen("a.in","r",stdin);
    //freopen("a.out","w",stdout);
    int n,m;
    cin>>n;
    vector<string>s(n);
    map<string,int>mp;
    rep(i,n){cin>>s[i];mp[s[i]]++;}
    cin>>m;
    vector<string>t(m);
    rep(i,m){cin>>t[i];mp[t[i]]--;}
    int ans=0;
    for(auto it:mp)ans=max(ans,it.s);
    cout<<ans<<enl;
    return 0;
}


#include <stdio.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include <vector>
#include <algorithm>

using namespace std;

int main(){
    int h,w,a,b;
    scanf("%d %d %d %d", &h, &w, &a, &b);
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            if( (2 * i - 2 * b + 1) * (2 * j - 2 * a + 1) > 0){
                printf("%d", 0);
            } else {
                printf("%d", 1);
            }
        }
        printf("\n");
    }

    return 0;
}

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

#define debug(x) cout << '[' << #x << " is: " << x << "] " << endl;
#define imod(a , n) (a % n + n ) % n

#define fastio ios_base::sync_with_stdio(false);cin.tie(0);
#define inF freopen("gymnastics.in","r",stdin );
#define outF freopen("gymnastics.out" , "w" , stdout ) ;
#define sum(a)a * (a + 1 ) / 2
#define ll long long

const double eps = 1e-9 , PI = acos(-1) ;
const int N = 1e6, oo = 1e7 , M = 4 * N, mod = 1<<30 , K = 12;
int n ;

int32_t main(){

    //inF;
    // Dont Forget memset

    //calculate constraints please!!!

    fastio;
   map<int , int >mp;
   for(int  i = 0 ; i < 3 ; i++){
    int a ;
    cin >> a;
    mp[a]++;
   }
   cout << (mp.size() == 2 ? "Yes" : "No");

     return 0 ;
}


#include<bits/stdc++.h>
#define mp make_pair
using namespace std;
typedef pair<int, int> pii;//cost, vertex
struct edge{
  int to, cost;
};

int main(int argc, char *argv[]){
  int n, m, r;
  cin >> n >> m >> r;
  vector<vector<edge> > g(n);
  int from;
  for (int i = 0; i < m; i++) {
    edge e;
    cin >> from >> e.to >> e.cost;
    g[from].push_back(e);
  }
  int dp[n];
  for (int i = 0; i < n; i++) {
    dp[i] = 1e9;
  }
  dp[r] = 0;
  priority_queue<pii, vector<pii>, greater<pii> > que;
  que.push(mp(0, r));
  //???????????§?????????
  //??????????????????????????????
  while(!que.empty()){
    int v = que.top().second;
    int c = que.top().first;
    que.pop();
    for (int i = 0; i < g[v].size(); i++) {
      edge e = g[v][i];
      if(dp[e.to] > c + e.cost){
        dp[e.to] = c + e.cost;
        que.push(mp(dp[e.to], e.to));
      }
    }
  }
  for (int i = 0; i < n; i++) {
    if(dp[i] == 1e9)std::cout << "INF" << std::endl;
    else std::cout << dp[i] << std::endl;
  }

  return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i,a,b) for(int i=(a),_end_=(b);i<=_end_;i++)
#define DREP(i,a,b) for(int i=(a),_end_=(b);i>=_end_;i--)
#define EREP(i,u) for(int i=start[u];i;i=e[i].next)
#define fi first
#define se second
#define mkr(a,b) make_pair(a,b)
#define SZ(A) ((int)A.size())
template<class T>inline void chkmin(T &a,T b){ if(a>b)a=b;}
template<class T>inline void chkmax(T &a,T b){ if(a<b)a=b;}
inline int read()
{
	int s=0,f=1; char ch=getchar();
	while(!isdigit(ch) && ch!='-')ch=getchar();
	if(ch=='-')ch=getchar(),f=-1;
	while(isdigit(ch))s=s*10+ch-'0',ch=getchar();
	return ~f?s:-s;
}

const int maxn=1e6+20;

int n;
char S[maxn],T[maxn];

inline void init()
{
	n=read();
	scanf("%s",S+1);
	scanf("%s",T+1);
}

int ans;

inline void doing()
{
	deque<int>q;q.push_back(n+1);
	for(int i=n,j=n+1,del=0;i;i--)
	{
		int tmp=j;
		while(j && (j>i || S[j]!=T[i]))j--;
		if(!j)puts("-1"),exit(0);
		if(j==tmp)
		{
			while(!q.empty() && q.back()-del>=i)q.pop_back();
			q.push_back(i+del);
		}else
		{
			del++;
			if(i!=j)
			{
				chkmax(ans,SZ(q));
				q.push_front(j+del);
			}
		}
	}
	printf("%d\n",ans);
}

int main()
{
	init();
	doing();
	return 0;
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main(){
  int N;
  cin>>N;
  map<int,int> mp;
  while(N--){
    int a;
    cin>>a;
    mp[a]++;
  }
  int ans=0;
  for(auto p:mp)
    if(p.second%2)ans++;
  cout<<ans<<endl;
}

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

int main(){
	int n,x;
	cin>>n;
	vector <int> v;
	for (int i=0;i<n;i++){
		cin>>x;
		v.push_back(x);
	}
	sort(v.begin(),v.end());

	/*
	for(int i=0;i<n;i++){
		cout<<v[i]<<" ";
	}
	*/
	int duplicate=0,no=0;
	for (int i=0;i<n;){
		if (v[i]==v[i+1]){
			duplicate++;
			i+=2;
		}
		else{
			i++;
			no++;
		}
	}
	cout<<no<<endl;

	return 0;
}


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

#define NAME "S - Digit Sum"
#define show(x) cout << (#x) << " is " << (x) << endl
#define ll long long
#define ms(arr,val) memset(arr,val,sizeof(arr))
#define len length()

const int mod = 1e9 + 7;

void addmod(ll &x, ll val){
    x+=val;
    if (x>mod) x-=mod;
    if (x<0) x+=mod;
}
int main(){
    //ifstream cin(NAME".inp");
    //ofstream cout(NAME".out");

    string  x;
    int d;
    cin>>x;
    cin>>d;
    ll cur = 0;
    vector<ll> tmp(d,0);
    vector<ll> dp(d,0);
    for(int i=0;i<x.len;i++){

        for(int j = 0;j<d;j++)
            for(int k=0;k<=9;k++) addmod(tmp[ (j+k)%d  ], dp[j]);

        for(int j = 0;j<x[i]-'0';j++) addmod( tmp[(cur + j)%d],1LL);
        dp = tmp;
        fill(tmp.begin(),tmp.end(),0);
        //addmod(cur,0LL+x[i]-'0');
        cur = (cur + x[i]-'0')%d;
    }
    //show(dp[0]);
    //show(cur);
    cout<<dp[0] + (cur==0) - 1<<endl;

}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
const ll mod = 1e9+7;
int n, d;
vector<int> a;
ll mult(ll a,ll b){return ((a%mod)*(b%mod))%mod;}
ll add(ll a,ll b){return ((a%mod)+(b%mod))%mod;}
ll sub(ll a,ll b){return ((a%mod)-(b%mod)+mod)%mod;}
ll dp[10009][2][101];
ll fun(int ci, int used, int sum){
    if(ci==n){
        if(sum==0)
            return 1;
        return 0;
    }
    if(dp[ci][used][sum]!=-1){
        return dp[ci][used][sum];
    }
    ll ans = 0;
    for(int i=0;i<=9;++i){
        if(used==1){
            ans = add(ans, fun(ci+1, 1, (sum+i)%d));
        }
        else{
            if(i < a[ci]){
                ans = add(ans, fun(ci+1, 1, (sum+i)%d));
            }
            if(i==a[ci]){
                ans = add(ans, fun(ci + 1, 0, (sum + i) % d));
            }
        }
    }
    return dp[ci][used][sum] = ans;
}
int solve(){
    string s;
    cin>>s;
    n = s.size();
    a.resize(n);

    memset(dp, -1, sizeof(dp));

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

    cin>>d;
    ll ans = sub(fun(0, 0, 0), 1);
    cout<<ans;
    // current index, used, total sum till cur_indx-1
    return 0;
}
int main(){
    fast;
    int t=1;
    // cin>>t;
    while(t--){
        solve();
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int m;
    cin >> m;
    ll ans = 0, lst = 0, d, c;
    vector<ll> nxt = {0, 2, 4, 6, 8, 1, 3, 5, 7, 9};
    for (int i = 0; i < m; i++) {
        cin >> d >> c;
        while (c) {
            if (c % 2 == 1) {
                lst = 10 * lst + d;
                while (lst >= 10) {
                    lst = lst / 10 + lst % 10;
                    ans++;
                }
                c--;
            }
            c /= 2;
            if (d > 4) {
                ans += c * 2;
            } else {
                ans += c;
            }
            d = nxt[d];
        }
    }
    cout << ans << '\n';
    return 0;
}

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<int,int> pii;
const int maxn=2e5+7;
const ll modn=1e9+7;
const int inf=2e9+7;
const ll INF=1000000000000000000ll;
const double PI = acos(-1.0);
const double eps=1e-8;
inline int lowbit(int x){return x&-x;}
inline ll lowbit(ll x) {return x&-x;}
inline int CountOneBinary(ll num){int ans=0;while(num){if(num&1) ans+=1;num>>=1;}return ans;}
inline void read(int &x){char c;x=0;for(c=getchar();c>'9'||c<'0';c=getchar());for(;c>='0'&&c<='9';c=getchar()) x=x*10+c-'0';}
inline void read(ll &x){char c;x=0;for(c=getchar();c>'9'||c<'0';c=getchar());for(;c>='0'&&c<='9';c=getchar()) x=x*10+c-'0';}
inline ll qpow(ll x,ll y){ll ans=1;while(y){if(y&1) ans=ans*x%modn;x=x*x%modn;y>>=1;}return ans;}
inline double qpow(double x,ll y){double ans=1;while(y){if(y&1) ans=ans*x;x=x*x;y>>=1;}return ans;}
ll f(ll a,ll b,ll c,ll d)
{
    ll top=max(max(a,b),max(c,d));
    ll bot=min(min(a,b),min(c,d));
    return top-bot;
}
ll n,a[maxn],sum[maxn];
int main()
{
    while(scanf("%lld",&n)==1)
    {
        for(int i=1;i<=n;i++)
        {
             scanf("%lld",&a[i]);
             sum[i]=sum[i-1]+a[i];
        }
        ll ans=INF;
        for(int mid=2;mid<n-1;mid++)
        {
            int m1=upper_bound(sum+1,sum+1+mid,sum[mid]/2)-sum;
            int m2=upper_bound(sum+1+mid,sum+1+n,(sum[n]-sum[mid])/2+sum[mid])-sum;
            ans=min(ans,f(sum[m1],sum[mid]-sum[m1],sum[m2]-sum[mid],sum[n]-sum[m2]));
            ans=min(ans,f(sum[m1-1],sum[mid]-sum[m1-1],sum[m2]-sum[mid],sum[n]-sum[m2]));
            ans=min(ans,f(sum[m1],sum[mid]-sum[m1],sum[m2-1]-sum[mid],sum[n]-sum[m2-1]));
            ans=min(ans,f(sum[m1-1],sum[mid]-sum[m1-1],sum[m2-1]-sum[mid],sum[n]-sum[m2-1]));
        }
        printf("%lld\n",ans);
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll par[314514];
ll ran[314514];
ll n=314514;
ll root(ll x){
	return par[x]==x?x:par[x]=root(par[x]);
}
void unite(ll x,ll y){
	x=root(x);
	y=root(y);
	if(x==y)return;
	if(ran[x]<ran[y])par[x]=y;
	else{
		par[y]=x;
		if(ran[x]==ran[y])ran[x]++;
	}
}
string s[410];
ll cnt[2][314514];
int main(){
	for(int i=1;i<=n;i++)par[i]=i;
	ll h,w;
	cin>>h>>w;
	ll dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
	for(int i=0;i<h;i++)cin>>s[i];
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			for(int k=0;k<4;k++){
				if(i+dy[k]<0||i+dy[k]>=h||j+dx[k]<0||j+dy[k]>=w)continue;
				if(s[i][j]!=s[i+dy[k]][j+dx[k]])unite(500*i+j,500*(i+dy[k])+j+dx[k]);
			}
		}
	}
	for(int i=0;i<h;i++)for(int j=0;j<w;j++){
		if(s[i][j]=='.')cnt[0][root(500*i+j)]++;
		else cnt[1][root(500*i+j)]++;
	}
	ll ans=0;
	for(int i=0;i<h;i++)for(int j=0;j<w;j++){
		ans+=cnt[0][500*i+j]*cnt[1][500*i+j];
	}
	cout<<ans<<endl;
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
template <class T> T input() {T t; cin>>t; return t;}
#define FOR(i,a,b) for (ll i = a; i < b; i++)
#define REP(i,b) FOR(i,0,b)
#define RFOR(i,a,b) for (ll i = a-1; i >= b; i--)
#define RREP(i,a) RFOR(i,a,0)
#define REPALL(i,x) for (ll i = 0; i < x.size(); i++)
#define RREPALL(i,x) for (ll i = x.size()-1; i >= 0; i--)
#define REPITR(itr,x) for (auto itr = (x).begin(); itr != (x).end(); itr++)
#define ALL(x) (x).begin(), (x).end()
#define SORT(x) sort(ALL(x))
#define MIN_ELEMENT(x) min_element(ALL(x))
#define MAX_ELEMENT(x) max_element(ALL(x))
#define COUNT(x,num) count(ALL(x), num)
#define MEMSET(x,val) memset(x, val, sizeof(x))
#define CHMAX(a,b) a = max(a,b)
#define CHMIN(a,b) a = min(a,b)
#define debug(x) cerr<<__LINE__<<": "<<(#x)<<" = "<<(x)<<endl;
void YES(bool flag) {cout<<(flag ? "YES" : "NO")<<endl;}
void Yes(bool flag) {cout<<(flag ? "Yes" : "No")<<endl;}
void yes(bool flag) {cout<<(flag ? "yes" : "no")<<endl;}
#define PRINT_ARRAY_H(i,a) REPALL(i,a) {cout<<a[i]<<(i != a.size()-1 ? " " : "");} newline;
#define e1 first
#define e2 second
#define newline putchar('\n')
#define cspace ' '
#define sspace " "
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef vector<ll> VLL;
typedef vector<vector<ll>> VVLL;
const int INF = 1e7;
const ll MOD = 1e9 + 7;
const double pi = 3.141592653589793;
const VI dx = {1, 0, -1, 0};
const VI dy = {0, 1, 0, -1};

int H, W;
vector<string> s;
ll ans = 0ll;
VVI visited(444, VI(444));

ll bfs(int y, int x)
{
    int num_b = 1, num_w = 0;

    deque<pair<int, int>> frends;
    frends.push_back(make_pair(y, x));
    visited[y][x] = 1;

    while (!frends.empty()) {
        auto p = frends.front();
        frends.pop_front();

        if (s[p.e1][p.e2] == '#') {
            REP(i,4) {
                if (0 <= p.e2 + dx[i] && p.e2 + dx[i] < W && 0 <= p.e1 + dy[i] && p.e1 + dy[i] < H
                    && visited[p.e1 + dy[i]][p.e2 + dx[i]] == 0 && s[p.e1 + dy[i]][p.e2 + dx[i]] == '.') {

                    frends.push_back(make_pair(p.e1 + dy[i], p.e2 + dx[i]));
                    visited[p.e1 + dy[i]][p.e2 + dx[i]] = 1;
                    num_w++;
                }
            }
        } else if (s[p.e1][p.e2] == '.') {
            REP(i,4) {
                if (0 <= p.e2 + dx[i] && p.e2 + dx[i] < W && 0 <= p.e1 + dy[i] && p.e1 + dy[i] < H
                    && visited[p.e1 + dy[i]][p.e2 + dx[i]] == 0 && s[p.e1 + dy[i]][p.e2 + dx[i]] == '#') {

                    frends.push_back(make_pair(p.e1 + dy[i], p.e2 + dx[i]));
                    visited[p.e1 + dy[i]][p.e2 + dx[i]] = 1;
                    num_b++;
                }
            }
        }
        //cout<<p.e1<<sspace<<p.e2<<sspace<<visited[p.e1][p.e2]<<endl;
    }

    //cout<<num_b<<sspace<<num_w<<endl;
    return (ll)num_b * num_w;
}

int main()
{
    cin>>H>>W;
    REP(i,H) s.push_back(input<string>());

    REP(i,H) {
        REP(j,W) {
            if (s[i][j] == '#' && visited[i][j] == 0) {
                ans += bfs(i, j);
            }
        }
    }

    cout<<ans<<endl;

    return 0;
}


//BISMILLAH
#include <bits/stdc++.h>
using namespace std;
int main(){

    long long int n,i,j,res = 0,temp;
    cin >> n;
    long long int a[n];
    for(i = 0;i<n;i++) {
        cin >> a[i];
        if(i>0){
            temp = a[i] - a[i-1];
            if(temp < 0) {
                res += (-1 * temp);
                a[i] += (-1*temp);
            }
        }
    }
        cout << res << endl;
    return 0;
}

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

int main(){
	long long sum=0;
	int n;
	cin>>n;
	int mx;cin>>mx;n--;
	while(n--){
		int a;
		cin>>a;
		sum+=max(0,mx-a);
		mx=max(mx,a);
	}
	cout<<sum;
}


#include<iostream>
using namespace std;
int main(){
    int n=1,A=0,B=0;
    while(n!=0){
        cin>>n;
        if(n==0){break;}
        for(int i=0;i<n;i++){
            int ap=0,bp=0;
            cin>>ap;
            cin>>bp;
            if(ap<bp){
                B=B+ap+bp;
            }
            if(ap>bp){
                A=A+ap+bp;
            }
            if(ap==bp){
                A=A+ap;
                B=B+bp;
            }
        }
        cout<<A<<" "<<B<<endl;
        A=0;B=0;
    }
    return 0;
}

 #include<bits/stdc++.h>
 using namespace std;
 #define For(start,end) for(int i = (int)start;i<(int)end;i++)
 using Int = long long int;
 int main(){
    set<int>paper;
    int N;
    cin >> N;
    For(0,N)
    {
        int before_size=paper.size();
        int tmp;
        cin>>tmp;
        paper.insert(tmp);
        if(paper.size()==before_size)
        {
            paper.erase(tmp);
        }
    }
    cout<<paper.size()<<endl;
 }


#include <bits/stdc++.h>

using namespace std;

#define REP(i, n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define REP3(i, m, n) for(int i = m, i##_len=(n);i < i##_len;i++)
#define FORVEC(i, v) for(int i = 0;i < v.size();i++)

#define INF 1000000000
#define pb(a) push_back(a)
#define vi vector<int>
#define vvi vector<vi>
#define P pair<int,int>
#define all(vec) (vec.begin()),(vec.end())
#define ll long long int;
#define sz(x) ((int)(x).size())
#define in(x) int x;cin>>x;
#define bit(n) (1<<(n))

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

int main(){
	int n,m,a[1000],b,p[1000]={};
	cin>>n>>m;
	REP(i,n){
		cin>>a[i];
	}
	REP(i,m){
		cin>>b;
		REP(j,n){
			if(a[j]<=b){
				p[j]++;
				break;
			}
		}
	}
	int mx=0,mxi;
	REP(i,n){
		//cout<<" "<<p[i]<<"\n";
		if(p[i]>mx){
			mx=p[i];
			mxi=i;
		}
	}
	cout<<mxi+1<<"\n";
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
typedef long long ll; //10^19 < 9223372036854775807
typedef unsigned long long ull; //10^20 < 18446744073709551615

int main(){
  int n;cin>>n;
  vector<int> p(n);
  rep(i,n) cin>>p.at(i);
  int ans=0;
  int min=p.at(0);
  for(int i=0;i<n;i++){
	if(p.at(i)<=min){
      ans+=1;
      min=p.at(i);
    }
  }
  cout<<ans;

}


#include<bits/stdc++.h>
using namespace std;
int main(){
    int N, x;
    cin >> N >> x;
    vector<int> A(N);
    for(int i = 0; i < N; i++){
        cin >> A.at(i);
    }
    sort(A.begin(), A.end());
    int ans = 0;
    for(int i = 0; i < N; i++){
        x -= A.at(i);
        if(x < 0){
            break;
        }
        ans++;
    }
    if(x > 0){
        ans--;
    }
    cout << ans << endl;
    return 0;
}

#include <bits/stdc++.h>
#include <iomanip>
#include <math.h>
//#include <unistd.h>
//#include <stdio.h>
#define rep(i,x) for(ll i = 0;i<x;i++)
#define all(a) (a).begin(),(a).end()
using ll = long long;
using ld = long double;
using namespace std;
using dou = double;
const ll INF = 1LL << 60;
const ll mod = 1000000007;
typedef pair<ll,ll> P;
using graph = vector<vector<ll>>;
template<class T> inline bool chmax(T& a, T b){if(a < b){a = b; return true;}return false;}
template<class T> inline bool chmin(T& a, T b){if(a > b){a = b; return true;}return false;}
ll gcd(ll a,ll b){
    if(a < b)swap(a,b);
    if(a % b == 0)return b;
    else return gcd(b,a%b);
}
bool isp(ll n){
    bool res = true;
    if(n == 1)return false;
    else{
        for(ll i = 2;i * i <= n;i++){
            if(n % i == 0){
                res = false;
                break;
            }
        }
        return res;
    }
}

long long fac[51000], finv[51000], inv[51000];

// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < 51000; i++){
        fac[i] = fac[i - 1] * i % mod;
        inv[i] = mod - inv[mod%i] * (mod / i) % mod;
        finv[i] = finv[i - 1] * inv[i] % mod;
    }
}

// 二項係数計算
long long COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}
/*
int main() {
    // 前処理
    COMinit();

    // 計算例
    cout << COM(100000, 50000) << endl;
}*/

//printf("%.10f\n", N);
/*
ll fibo(ll n){
    if(memo[n] != 0)return memo[n];
    if(n <= 1)return n;
    else return memo[n] = fibo(n - 1)+ fibo(n - 2);
}*/
/*  vector<ll> vec = {1,2,5,6,8,9,10};
    cout << binary_search(all(vec),5) << endl; -> true*/
/*
    vector<ll> vec = {1,2,5,6,8,9,10};
    auto n = upper_bound(all(vec),4);
    cout << *n << endl; -> 5*/
/*
    cout << fixed << setprecision(15);
*/

int main(){
    ll H,W;
    cin >> H >> W;
    vector<vector<char>> maze(H,vector<char>(W));
    queue<P> que;
    vector<vector<ll>> d(H,vector<ll>(W,INF));
    rep(i,H){
        rep(j,W){
            cin >> maze[i][j];
            if(maze[i][j] == '#'){
                que.push(P(i,j));
                d[i][j] = 0;
            }
        }
    }
    const ll dx[] = {1,0,0,-1};
    const ll dy[] = {0,1,-1,0};
    while(que.size()){
        P p = que.front();que.pop();
        rep(i,4){
            ll hx = dx[i] + p.first;
            ll hy = dy[i] + p.second;
            if(hx >= 0 && hy >= 0 && hx < H && hy < W && d[hx][hy] == INF){
                que.push(P(hx,hy));
                d[hx][hy] = d[p.first][p.second] + 1;
            }
        }
        //cout << p.first << " " << p.second << endl;
    }
    ll ans = 0;
    rep(i,H){
        rep(j,W){
            ans = max(d[i][j],ans);
        }
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>

using namespace std;

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

#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define ll long long int
#define mod 1000000007
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define prec(n) fixed << setprecision(n)
#define maxpq priority_queue<int>
#define minpq priority_queue<int, vector<int>, greater<int>>
#define PI 3.1415926535897932384626
#define bits(n) __builtin_popcount(n)

void solve()
{
    int a,b;
    cin>>a>>b;
    cout<<a*b;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #ifndef ONLINE_JUDGE
    // for getting input from input.txt
    freopen("input1.txt", "r", stdin);
    // for writing output to output.txt
    freopen("output1.txt", "w", stdout);
    #endif

    int t = 1;
    //cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

#include <stdio.h>
#include <iostream>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
int main()
{
    vector<int> ab(2);
    rep(i, 2) cin >> ab[i];
    cout << ab[0] * ab[1] << endl;
    return 0;
}


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

int main(){
  int a,b;
  cin>>a>>b;

  cout<<100<<' '<<100<<endl;

  string s1(100,'#'),s2(100,'.');
  a--; b--;
  rep(i,50){
    if(i%2==1) cout<<s1;
    else{
      rep(j,50){
	if(a){
	  cout<<".#";
	  a--;
	}
	else{
	  cout<<"##";
	}
      }
    }
    cout<<endl;
  }
  rep(i,50){
    if(i%2==0) cout<<s2;
    else{
      rep(j,50){
	if(b){
	  cout<<"#.";
	  b--;
	}
	else{
	  cout<<"..";
	}
      }
    }
    cout<<endl;
  }
  return 0;
}

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using product = pair<ll,ll>;
using graph = vector<vector<ll>>;
using vll = vector<ll>;

template <class type> void printVector(vector<type> vec){
    stringstream ss;
    for(ll i=0;i<vec.size();i++)
    {
        ss<<vec[i]<<" ";
        if(i!=vec.size()-1)ss<<" ";
    }
    cout<<ss.str()<<endl;
}

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

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

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

void solve(){
    ll A,B;
    cin>>A>>B;
    ll gcd=GCD(A,B);
    ll res=prime_factorize(gcd).size()+1;
    cout<<res;
}

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


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

int main() {
  int N;
  scanf("%d", &N);

  int x = N / 111;
  if(N % 111 !=0) x++;

  printf("%d\n",x * 111);
  return 0;
}

// includes
#include <cstdio>
#include <cstdint>
#include <iostream>
#include <iomanip>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <utility>
#include <functional>
#include <cmath>
#include <climits>
#include <bitset>

// macros
#define ll long long int
#define pb push_back
#define mk make_pair
#define pq priority_queue
#define FOR(i, a, b) for(int i=(a); i<(b);++i)
#define rep(i, n) FOR(i, 0, n)

using namespace std;

//  types
typedef pair<int, int> P;
typedef pair<ll, int> Pl;
typedef pair<ll, ll> Pll;

// constants
const int inf = 1e9;
const ll linf = 1LL << 50;
const double EPS = 1e-10;

// solve

int main(int argc, char const* argv[])
{
  string s;
  while(getline(cin, s)){
    FOR(i, 1, 27){
      string t = s;
      string res = "";
      rep(j, t.size()){
        if(!('a' <= s[j] && s[j] <= 'z')){
          res += t[j];
        }else{
          res += char(((int(t[j] - 'a') + i) % 26) + 'a');
        }
      }
      string tmp = "";
      bool flag = false;
      rep(j, res.size()){
        if(res[j] != ' ' && res[j] != '.')tmp += res[j];
        else{
          if(tmp == "the" || tmp == "this" || tmp == "that"){
            flag = true;
            break;
          }
          tmp = "";
        }
      }
      if(tmp == "the" || tmp == "this" || tmp == "that"){
        flag = true;
      }
      if(flag){
        cout << res << endl;
        break;
      }
    }
  }
	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>

using ll = long long;
using namespace std;

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

int main(void) {
	vector<string> s(3);
	for(int i = 0; i < 3; ++i) {
		cin >> s[i];
		reverse(s[i].begin(), s[i].end());
	}

	int now = 0;
	for(int i = 0; i < 300; ++i) {

		if( s[now].empty() ) {
			cout << (char)(now + 'A') << endl;
			return 0;
		}

		char c = s[now].back();
		s[now].pop_back();
		now = (int)(c - 'a');
	}

	return 0;
}

#include <bits/stdc++.h>
using namespace std;
struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star;
int main()
{
    string A, B, C;
    cin >> A >> B >> C;

    string next;
    next.push_back( A[ 0 ] );

    int a{}, b{}, c{};
    while( true )
    {
        if( next == "a" )
        {
            if( A.empty() )
            {
                cout << "A" << endl;
                break;
            }
            next = *A.begin();
            A.erase( A.begin() );
            a++;
        }
        if( next == "b" )
        {
            if( B.empty() )
            {
                cout << "B" << endl;
                break;
            }
            next = *B.begin();
            B.erase( B.begin() );
            b++;
        }
        if( next == "c" )
        {
            if( C.empty() )
            {
                cout << "C" << endl;
                break;
            }
            next = *C.begin();
            C.erase( C.begin() );
            c++;
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }

const int MN = 100010;
using D = double;
int n, m;
ll k;
D x[MN], ans[MN];

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

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

    V<int> pm(n - 1);
    iota(pm.begin(), pm.end(), 0);
    cin >> m >> k;
    for (int i = 0; i < m; i++) {
        int p;
        cin >> p; p--;
        swap(pm[p], pm[p - 1]);
    }

    auto gao = [&](const V<int> &a, const V<int> &b) {
        V<int> c(n - 1);
        for (int i = 0; i < n - 1; i++) {
            c[i] = a[b[i]];
        }
        return c;
    };

    V<int> r(n - 1);
    iota(r.begin(), r.end(), 0);
    while (k) {
        if (k & 1) r = gao(r, pm);
        pm = gao(pm, pm);
        k >>= 1;
    }

    ans[0] = x[0];
    for (int i = 0; i < n - 1; i++) {
        double d = x[r[i]+1] - x[r[i]];
        ans[i+1] = ans[i] + d;
    }
    for (int i = 0; i < n; i++) {
        cout << ans[i] << "\n";
    }

    return 0;
}

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

string solve(){
  vector<int> a;
  for(int i=0;i<3;i++){
    int tmp;
    cin>>tmp;
    a.push_back(tmp);
  }

  for(int i=0;i<a.size();i++){
    if(a[i]==100)return "A";
  }
  if(a[0]+a[1]>=90*2)return "A";
  int ave = (a[0]+a[1]+a[2])/3;
  if(ave>=80)return "A";
  if(ave>=70)return "B";
  if(ave>=50)if(a[0]>=80||a[1]>=80)return "B";
  return "C";
}

int main(){
  int n;
  while(cin>>n,n!=0){
    for(int i=0;i<n;i++){
      cout<<solve()<<endl;
    }
  }
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    string s;
    cin >> s;
    int w;
    cin >> w;
    int i=0;
    while(i<s.length()){
        cout << s[i];
        i += w;
    }
    cout << endl;
}

#include <bits/stdc++.h>
using namespace std;
#define repex(i, a, b, c) for(int i = a; i < b; i += c)
#define repx(i, a, b) repex(i, a, b, 1)
#define rep(i, n) repx(i, 0, n)
#define repr(i, a, b) for(int i = a; i >= b; i--)
#define pb push_back

int main(){

    // 1. 入力情報.
    char c[2020];
    int w;
    scanf("%s %d", c, &w);

    // 2. 文字を連結.
    string ans = "";
    int l = strlen(c);
    rep(i, l) if(i % w == 0) ans.pb(c[i]);

    // 3. 出力.
    printf("%s\n", ans.c_str());
    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; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
int main()
{
  int N;
  string v;
  cin >> N >> v;
  vector<int> e(N+1, 0);
  vector<int> w(N+1, 0);
  REP(i, N) {
    if (v[i] == 'W') {
      w[i+1] = w[i] + 1;
      e[i+1] = e[i];
    } else {
      e[i+1] = e[i] + 1;
      w[i+1] = w[i];
    }
  }

  int ans = N;

  for(int i = 1; i <= N; i++) {
    if (i == 0) {
      ans = min(ans, e[N] - e[i]);
    } else if (i == N) {
      ans = min(ans, w[N-1]);
    } else {
      ans = min(ans, w[i-1] + (e[N] - e[i]));
    }
  }
  cout << ans << endl;

  return 0;
}

//
// Created by hide on 2020/05/15.
//

//参考：http://ehafib.hatenablog.com/entry/2015/12/23/164517
//インクルード(アルファベット順,bits/stdc++.hは使わない派閥)
#include<algorithm>//sort,二分探索,など
#include<bitset>//固定長bit集合
#include<cmath>//pow,logなど
#include<complex>//複素数
#include<deque>//両端アクセスのキュー
#include<functional>//sortのgreater
#include<iomanip>//setprecision(浮動小数点の出力の誤差)
#include<iostream>//入出力
#include<iterator>//集合演算(積集合,和集合,差集合など)
#include<map>//map(辞書)
#include<numeric>//iota(整数列の生成),gcdとlcm(c++17)
#include<queue>//キュー
#include<set>//集合
#include<stack>//スタック
#include<string>//文字列
#include<unordered_map>//イテレータあるけど順序保持しないmap
#include<unordered_set>//イテレータあるけど順序保持しないset
#include<utility>//pair
#include<vector>//可変長配列

using namespace std;
typedef long long ll;

//マクロ
//forループ関係
//引数は、(ループ内変数,動く範囲)か(ループ内変数,始めの数,終わりの数)、のどちらか
//Dがついてないものはループ変数は1ずつインクリメントされ、Dがついてるものはループ変数は1ずつデクリメントされる
#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=a;i<(ll)(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=(ll)(b);i--)
//xにはvectorなどのコンテナ
#define ALL(x) (x).begin(),(x).end() //sortなどの引数を省略したい
#define SIZE(x) ((ll)(x).size()) //sizeをsize_tからllに直しておく
#define MAX(x) *max_element(ALL(x)) //最大値を求める
#define MIN(x) *min_element(ALL(x)) //最小値を求める
//定数
#define INF 1000000000000 //10^12:極めて大きい値,∞
#define MOD 1000000007 //10^9+7:合同式の法
#define MAXR 100000 //10^5:配列の最大のrange(素数列挙などで使用)
//略記
//#define PB push_back //vectorヘの挿入
//#define MP make_pair //pairのコンストラクタ
//#define F first //pairの一つ目の要素
//#define S second //pairの二つ目の要素

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

    int r=0;
    for(int i=1; i<N; i++) {
        if (S[i] == 'E')
            r++;
    }
    int answer = r;

    int l = S[0] == 'W' ? 1:0;
    for(int i=1; i<N; i++){
        if(S[i]=='E')
            r--;
        answer = min(answer, l+r);

        if(S[i]=='W')
            l++;
    }

    cout << answer << endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define all(a)a.begin(),a.end()
using ll=long long;
const int INF = 1<<30;
const ll INFll =1LL<<62;
const int mod = int(1e9)+7;
using V = vector<ll>;
using P = pair<ll,ll>;

int main(){
    ll n;cin >>n;
    ll o;cin >>o;
    bool flg=true;
    if(o!=0){
        flg=false;
    }
    map<ll,ll>co;
    co[0]++;
    for (int i = 0; i < n - 1; ++i) {
        ll d;
        cin >> d;
        co[d]++;
    }
    ll ans=1;
    ll c=1;
    for(int i=1;;i++){
        if(co[i]==0)break;
        c+=co[i];
        for (int j = 0; j < co[i]; ++j) {
            ans*=co[i-1];
            ans%=998244353;
        }
       // cout <<ans <<endl;
    }
    if(c!=n)flg=false;
    if(co[0]>1)flg=false;
    if(flg){
        cout <<ans<<endl;
    }
    else {
        cout <<0<<endl;
    }
    return 0;
}

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <iomanip>
#include <queue>
#include <deque>
#include <map>
#include <unordered_map>
#define rep(i,n) for(int i=0;i<n;i++)
#define repn(i,n) for(int i=1;i<=n;i++)
#define repr(e,x) for(auto& e:x)
using namespace std;
typedef long long ll;
typedef long double ld;
//typedef pair<int,int> P;
//ll const INF=1001001001;
//ll const INF=1001001001001001001;
ll const MOD=998244353;

ll mpow(ll x,ll n){
    if(n==0) return 1;
    else if(n%2) return x*mpow(x,n-1)%MOD;
    return mpow(x*x%MOD,n/2)%MOD;
}

ll N;
ll D[100000];
ll cnt[100000];

int main(){
    cin>>N;
    rep(i,N) cin>>D[i];

    if(D[0]!=0){
        cout<<0<<endl;
        return 0;
    }

    ll maxi=0;
    rep(i,N) maxi=max(maxi,D[i]);
    rep(i,N) cnt[D[i]]++;

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

    ll ans=1;
    rep(i,maxi){
        ans*=mpow(cnt[i],cnt[i+1]);
        ans%=MOD;
    }

    cout<<ans<<endl;
}


#include <bits/stdc++.h>
using namespace std;
int main(){
    int H, W, B = 0;
    cin >> H >> W;
    vector<int> color(H * W);
    vector<vector<int>> grid(H * W);
    for (int i = 0; i < H; i++) {
        string S;
        cin >> S;
        for (int j = 0; j < W; j++) {
            color.at(i * W + j) = S.at(j);
            if (S.at(j) == '#') {
                B++;
            }
        }
    }
    for (int i = 0; i < H * W; i++) {
        if (color.at(i) == '#') {
            continue;
        }
        if (i >= W) {
            if (color.at(i - W) == '.') {
                grid.at(i).push_back(i - W);
            }
        }
        if (i < W * (H - 1)) {
            if (color.at(i + W) == '.') {
                grid.at(i).push_back(i + W);
            }
        }
        if (i % W != 0) {
            if (color.at(i - 1) == '.') {
                grid.at(i).push_back(i - 1);
            }
        }
        if (i % W != W - 1) {
            if (color.at(i + 1) == '.') {
                grid.at(i).push_back(i + 1);
            }
        }
    }
    vector<int> dist(H * W,-1);
    queue<int> que;
    que.push(0);
    dist.at(0) = 0;
    while(!que.empty()) {
        int v = que.front();
        que.pop();
        for (int i = 0; i < (int) grid.at(v).size(); i++){
            int nv = grid.at(v).at(i);
            if (dist.at(nv) != -1 && dist.at(nv) <= dist.at(v) + 1) {
                continue;
            }
            dist.at(nv) = dist.at(v) + 1;
            que.push(nv);
        }
    }
    if (dist.at(H * W - 1) != -1) {
        cout << H * W - B - 1 - dist.at(H * W - 1) << endl;
    }
    else {
        cout << -1 << endl;
    }
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
int main(){
  ll h,w;
  cin >> h >> w;
  char c[h][w];
  ll dist[h][w];
  ll ans=0;
  rep(i,h){
    rep(j,w){
      cin >> c[i][j];
      dist[i][j]=-1;
      if(c[i][j]=='.'){
        ans++;
      }
    }
  }
  queue<pair<ll,ll>> que;
  que.push(make_pair(0,0));
  int x[4]={1,0,-1,0};
  int y[4]={0,1,0,-1};
  dist[0][0]=0;
  while(!que.empty()){
    auto p=que.front();
    ll cpx=p.first;
    ll cpy=p.second;
    que.pop();
    for(int k=0;k<4;k++){
      int xx=cpx+x[k];
      int yy=cpy+y[k];
      if(xx<0 || xx>=h || yy>=w || yy<0 || c[xx][yy]=='#'){
        continue;
      }
      if(dist[xx][yy]!=-1){
        continue;
      }
      dist[xx][yy]=dist[cpx][cpy]+1;
      que.push(make_pair(xx,yy));
    }
  }
  if(dist[h-1][w-1]==-1){
    cout << -1;
    return 0;
  }
  cout << ans-1-dist[h-1][w-1];
}


#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  for(int i=0;i<(n);++i)
#define REPr(i,n) for(int i=(n)-1;i>=0; --i)
#define FORq(i, m, n) for(int i = (m);i <= (n);++i)
#define FORqr(i, m , n) for(int i = (n);i >=(m);--i)
#define SCD(n) scanf("%d",&n)
#define SCD2(m,n) scanf("%d%d",&m,&n)
#define SCD3(m,n,k) scanf("%d%d%d",&m,&n,&k)
#define SCLLD(n) scanf("%lld",&n)
#define SCLLD2(m,n) scanf("%lld%lld",&m,&n)
#define SCLLD3(m,n,k) scanf("%lld%lld%lld",&m,&n,&k)
#define PB push_back
#define MP make_pair
#define ARSCD(A,N) REP(i,N){SCD(A[i]);}
#define ARSCD1(A,N) FORq(i,1,N){SCD(A[i]);}
#define VSCD(v,N) REP(i,N){int (x); SCD(x); v.PB(x);}
#define VSCLLD(v,N) REP(i,N){long long (x); SCLLD(x); v.PB(x);}
#define PRINTD(n) printf("%d\n",n)
#define PRINTLLD(n) printf("%lld\n",n)
#define DEBUG printf("%s\n","debug")
#define fst first
#define snd second
#define SIN(x,S) (S.count(x) != 0)
#define M0(x) memset(x,0,sizeof(x))
#define FILL(x,y) memset(x,y,sizeof(x))
#define MM(x) memset(x,-1,sizeof(x))
#define ALL(x) (x).begin(),(x).end()
using namespace std;
typedef pair<int,int> PII;
typedef pair<long long,long long> PLL;
typedef vector<int> VI;
typedef vector < VI > VVI;
typedef vector<long long> VL;
typedef long long ll;
typedef long long integer;
///////////////////////////////////////////////
const ll MOD = 1000000007;
ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}

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 main(){
    ll K;
    SCLLD(K);

    ll Q,R;
    Q = K / 50;
    R = K % 50;

    ll a[50] = {};

    REP(i,50){
        a[i] = 49 + Q;
    }

    REP(i,50-R){
        a[i] += -R;
    }

    REP(i,R){
        if (R != 0) a[50-R +i] += 50 + 1 - R;
    }

    printf("50\n");
    REP(i,49){
        printf("%lld ",a[i]);
    }
    printf("%lld\n",a[49]);

}

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

const int MOD = 1000000007;
int main()
{
//  freopen("in.txt", "r", stdin);
//  freopen("out.txt", "w", stdout);
  ll k;
  cin >> k;
  int n = 50;
  ll x = (k + n*n - n) / n;
  ll arr[50];
  for(int i = 0;i < 50;i++){
    arr[i] = x;
  }
  ll rem = k - (x * n - n*n + n);
  for(int i = 0;i < rem;i++){
    arr[i] += n;
    for(int j = 0;j < 50;j++){
      if(i != j)
        arr[j]--;
    }
  }
  cout << 50 << endl;
  for(int i = 0;i < n;i++)
    cout << arr[i] << ' ';
}


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

//TEMPLATE
#define pb                push_back
#define mp                make_pair
#define ll                long long
#define ld                long double
#define pii               pair <ll, ll>
#define piii              pair <ll, pii>
#define F                 first
#define S                 second

#define newline           printf("\n")
#define minusone          printf("-1\n")
#define zeroo             printf("0\n")
#define scl1(a)           scanf("%lld", &a)
#define scl2(a, b)        scanf("%lld %lld", &a, &b)
#define scl3(a, b, c)     scanf("%lld %lld %lld", &a, &b, &c)
#define prl1(a)           printf("%lld\n", a)
#define prl2(a, b)        printf("%lld %lld\n", a, b)
#define ssl1(s)           scanf(" %[^\n]", s)
#define scd1(a)           scanf("%lf", &a)
#define scd2(a, b)        scanf("%lf %lf", &a, &b)
#define prd(a)            printf("%lf\n", a)
#define prld(a)           printf("%Lf\n", a)
#define prcase(cs)        printf("Case %lld: ", cs)
#define cin1(a)           cin >> a
#define cin2(a, b)        cin >> a >> b
#define cin3(a, b, c)     cin >> a >> b >> c
#define cin4(a, b, c, d)  cin >> a >> b >> c >> d
#define cot1(a)           cout << a << "\n"
#define cot2(a, b)        cout << a << " " << b << "\n"
#define cot3(a, b, c)     cout << a << " " << b << " " << c << "\n"
#define cot4(a, b, c, d)  cout << a << " " << b << " " << c << " " << d << "\n"
#define cotline           cout << "\n"
#define cotminus          cout << "-1\n"
#define cot0              cout << "0\n"
#define cotyes            cout << "YES\n"
#define cotno             cout << "NO\n"
#define cotcase(cs)       cout << "Case " << cs << ": "

#define reversed(s)       reverse(s.begin(), s.end())
#define asort(s)          sort(s.begin(), s.end())
#define dsort(s)          sort(s.rbegin(), s.rend())
#define all(s)            s.begin(), s.end()
#define uniq(s)           s.resize(distance(s.begin(),unique(s.begin(), s.end())))
#define found(s, x)       (s.find(x) != s.end())

#define for0(i, n)        for (i = 0; i < n; i++)
#define for1(i, n)        for (i = 1; i <= n; i++)
#define fora(i, a, b)     for (i = a; i <= b; i++)
#define forb(i, b, a)     for (i = b; i >= a; i--)
#define fori(it, s)       for(auto it = s.begin(); it != s.end(); it++)

#define FR                ios_base::sync_with_stdio(false);cin.tie(NULL)
#define ms(a, x)          memset(a, x, sizeof a)
#define bitcount(n)       __builtin_popcountll(n)

//#define pi              3.1415926536
#define pi                acos(-1)

const ll INF = LLONG_MAX;
const ll SZ = 2e3+5;
const ll mod = 1e9+7;

/**
6 9
1 5
1 2
2 6
2 3
3 4
3 1
6 3
4 1
5 2
*/

ll n, m, cycle;
struct data {
    ll u, v;
} edg[SZ];
vector <ll> sg, adj[SZ];
ll vis[SZ];
bool f[SZ];

void dfs(ll u, ll p) {
    if(vis[u] == 2 || f[u] || cycle) return;
    if(vis[u] == 1) {
        cycle = 1;
        return;
    }

    vis[u] = 1;
    for(auto v: adj[u]) {
        dfs(v, u);
    }
    vis[u] = 2;
}

int main() {
    ll cs = 0, tc = 1, x, y, z, i, j, k, g, p, q, ans = 0, sum = 0, c = 0, t = 0;
//    ll a, b, d;
    string s, s1, s2;

    cin2(n, m);
    for1(i, m) {
        cin2(x, y);
        adj[x].pb(y);
    }

    for1(i, n) {
        if(!vis[i]) dfs(i, 0);
    }

    if(!cycle) {
        cotminus;
        return 0;
    }

    for1(i, n) {
        f[i] = 1;
        cycle = 0;
        ms(vis, 0);

        for1(j, n) if(!vis[j]) dfs(j, 0);

        if(!cycle) {
            f[i] = 0;
            sg.pb(i);
        }
    }

    cot1(sg.size());
    for(auto v: sg) cot1(v);

    return 0;
}

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

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> ii;
typedef tuple<ll, ll, ll> iii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
#define REP(i,n) for (ll i = 0; i < n; ++i)
#define REPR(i,n) for (ll i = n-1; i >= 0; --i)
#define FOR(i,m,n) for (ll i = m; i < n; ++i)
#define FORR(i,m,n) for (ll i = n-1; i >= m; --i)
#define FORE(x,xs) for (const auto& x : xs)
#define ALL(v) v.begin(), v.end()
#define CHMIN(x,y) x = min(x, y)
#define CHMAX(x,y) x = max(x, y)

const int MAX = 1010;

int N, M;
vi G[MAX];

int pos = -1;
bool vis[MAX], fin[MAX];
vi hist;

int idx[MAX];

void dfs(int v) {
    vis[v] = true;
    hist.push_back(v);
    FORE (nv, G[v]) {
        if (fin[nv]) continue;
        if (vis[nv]) {
            pos = nv;
            return;
        }
        dfs(nv);
        if (pos != -1) return;
    }
    hist.pop_back();
    fin[v] = true;
}

void solve() {
    REP (v, N) {
        dfs(v);
        if (pos != -1) break;
    }

    if (pos == -1) {
        cout << -1 << endl;
        return;
    }

    vi cycle;
    while (!hist.empty()) {
        int t = hist.back();
        cycle.push_back(t);
        hist.pop_back();
        if (t == pos) break;
    }
    reverse(ALL(cycle));

    while(1) {
        bool done = false;
        REP (i, MAX) idx[i] = -1;
        REP (i, cycle.size()) idx[cycle[i]] = i;

        REP (i, cycle.size()) {
            int cur = cycle[i];
            FORE (next, G[cur]) if (idx[next] >= 0) {
                if (idx[next] == (idx[cur] + 1) % cycle.size()) continue;

                vi v;
                bool get = false;
                for (int r = 0;; r++) {
                    if (cycle[r%cycle.size()] == next) get = true;
                    if (get){
                        v.push_back(cycle[r%cycle.size()]);
                        if (cycle[r%cycle.size()] == cur) break;
                    }
                }

                cycle = v;
                done = true;
                break;
            }
            if (done) break;
        }
        if (!done) break;
    }

    cout << cycle.size() << endl;
    FORE (r, cycle) cout << r+1 << "\n";
    return;
}

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