#include<stdio.h>

#define MIN(x,y) (((x)<(y))?(x):(y))

struct{
  short com;
  short flg[3];
  short num;
  short bef[199];
}game[200]={{0}};

int chk_clear(int k,int f);

int main(void){
  int i,j,k,in,n,room;
  int count[3]={0},cleard[3]={0};

  scanf("%d%*c",&n);
  for(i=0;i<n;i++) scanf("%hd%*c",&game[i].com);
  for(i=0;i<n;i++){
    scanf("%hd",&game[i].num);
    for(j=0;j<game[i].num;j++){
      scanf("%hd",&game[i].bef[j]);
    }
  }

  //puts("end set-data");

  for(i=0;i<3;i++){
    room=i;
    do{
      room%=3;
      room++;
      for(j=0;j<n;j++){
	if(game[j].com!=room) continue;
	if(game[j].flg[i]==1) continue;
	in=chk_clear(j,i);
	if(in==1){
	  game[j].flg[i]=1;
	  cleard[i]++;
	  j=-1;
	}
      }
      //     printf("now cleard[%d]=%d@room=%d\n",i,cleard[i],room);
    }while(cleard[i]<n && ++count[i]);
    count[i]+=n;
  }

  printf("%d\n",MIN(count[0],MIN(count[1],count[2])));
  return 0;
}

int chk_clear(int k,int f){
  int i;
  for(i=0;i<game[k].num;i++){
    if(game[game[k].bef[i]-1].flg[f]==0) return 0;
  }
  return 1;
}


#include<stdio.h>
#include<string.h>
#define maxn 210

int p[maxn], num[maxn], bef[maxn][maxn], vis[maxn], an[4];

int min(int a, int b, int c){
	a = a < b ? a : b;
	return a < c ? a : c;
}

int gao(int start, int n){
	int finish = 0, ans = 0, f = 0, now, flag = 0, i, j;
	memset(vis, 0, sizeof(vis));
	while(finish != n){
		for(now = 1; now <= 3; now++){
			if(now == start){
				flag = 1;
			}
			if(flag == 1){
				for(i = 1; i <= n; i++){
					if(p[i] == now){
						if(!num[i] && !vis[i]){
							ans++;
							//printf("+");//
							vis[i] = 1;
							finish++;
							//printf("%d ", i);//
							i = 0;
						}else{
							f = 0;
							for(j = 1; j <= num[i]; j++){
								if(!vis[bef[i][j]]){
									f = 1;
								}
							}
							if(f == 0 && !vis[i]){
								ans++;
								//printf("+");//
								vis[i] = 1;
								finish++;
								//printf("%d ", i);//
								i = 0;
							}
						}
					}
				}
			}
			if(flag == 1 && finish != n){
				ans++;
				//printf("(+)");//
			}
		}
	}
	//printf("\n");//
	return ans;
}

int main(void){
	int n, i, j, ans;
	scanf("%d", &n);
	for(i = 1; i <= n; i++){
		scanf("%d", &p[i]);
	}
	for(i = 1; i <= n; i++){
		scanf("%d", &num[i]);
		for(j = 1; j <= num[i]; j++){
			scanf("%d", &bef[i][j]);
		}
	}

	for(i = 1; i <= 3; i++){
		an[i] = gao(i, n);
		//printf("%d\n", an[i]);//
	}
	ans = min(an[1], an[2], an[3]);
	printf("%d", ans);
	return 0;
}


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

int main()
{
	long long int t,n,l,k,d[200],a[200],flag,i,tmr,temp;
	scanf("%lld",&t);
	while(t--)
	{
		scanf("%lld %lld %lld",&n,&k,&l);
		for(i=0;i<n;i++)
		{
			scanf("%lld",&d[i]);
		}
		flag=0,tmr=1000;
		for(i=0;i<n;i++)
		{
			if(d[i]+k<=l)
			{
				a[i]=d[i];
				tmr=1000;
			}
			else if(d[i]>l)
			{
				flag=1;
				// printf("%d-%d\n",d[i],i);
				break;
			}
			else if(d[i]+k>l)
			{
				if(i==0 || tmr==1000)
				{
					a[i]=l;
					tmr=l-d[i];
				}
				else
				{
					if(tmr>0)
					{
						temp=a[i-1]-d[i-1];
						a[i]=d[i]+tmr-1;
						tmr--;
						if(a[i]>l)
						{
							tmr=tmr-(a[i]-l);
							a[i]=l;
						}
					}
					else
					{
						tmr=tmr-1;
						if(d[i]-tmr>l)
						{
							flag=1;
							// printf("%d\n",i);
							break;
						}
						else
						{
							a[i]=d[i]-tmr;
						}
						// if(tmr==-k)
						// {
						// 	tmr=k;
						// }
					}
				}
			}
		}
		if(flag==1)
		{
			printf("No\n");
		}
		else
		{
			printf("Yes\n");
		}
	}
}


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

int main()
{
	int t,n,l,k,d[200],a[200],flag,i,tmr,temp;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d %d %d",&n,&k,&l);
		for(i=0;i<n;i++)
		{
			scanf("%d",&d[i]);
		}
		flag=0,tmr=1000;
		for(i=0;i<n;i++)
		{
			if(d[i]+k<=l)
			{
				a[i]=d[i];
				tmr=1000;
			}
			else if(d[i]>l)
			{
				flag=1;
				// printf("%d-%d\n",d[i],i);
				break;
			}
			else if(d[i]+k>l)
			{
				if(i==0 || tmr==1000)
				{
					a[i]=l;
					tmr=l-d[i];
				}
				else
				{
					if(tmr>0)
					{
						temp=a[i-1]-d[i-1];
						a[i]=d[i]+tmr-1;
						tmr--;
						while(a[i]>l)
						{
							a[i]--;
							tmr--;
						}
					}
					else
					{
						tmr=tmr-1;
						if(d[i]-tmr>l)
						{
							flag=1;
							// printf("%d\n",i);
							break;
						}
						else
						{
							a[i]=d[i]-tmr;
						}
						// if(tmr==-k)
						// {
						// 	tmr=k;
						// }
					}
				}
			}
		}
		if(flag==1)
		{
			printf("No\n");
		}
		else
		{
			printf("Yes\n");
		}
	}
}


#include <stdio.h>

long long xy[2];

int gcdxy(int a, int b) {
	if (b == 0) {
		xy[0] = 1, xy[1] = 0;
		return a;
	} else {
		int g = gcdxy(b, a % b);
		int tmp;

		tmp = xy[0] - a / b * xy[1];
		xy[0] = xy[1];
		xy[1] = tmp;
		return g;
	}
}

int main() {
	int a1, b1, a2, b2, l, r, g;
	long long x, b, n, k_, l_, cnt, cnt1, cnt2;

	scanf("%d%d%d%d%d%d", &a1, &b1, &a2, &b2, &l, &r);
	/* (a1k + b1 = a2l + b2) = (a1k - a2l = b2 - b1) */
	g = gcdxy(a1, -a2);
	b = (long long) b2 - b1;
	if (b % g != 0)
		printf("0\n");
	else {
		k_ = xy[0] * b / g, l_ = xy[1] * b / g;
		if (g < 0)
			g = -g;
		cnt2 = a2 / g;
		cnt1 = a1 / g;
		cnt = (long long) a1 / g * a2;
		k_ = (k_ % cnt2 + cnt2) % cnt2;
		l_ = (l_ % cnt1 + cnt1) % cnt1;
		x = a1 * k_ + b1 > a2 * l_ + b2 ? a1 * k_ + b1 : a2 * l_ + b2;
		n = 0;
		if (x >= l && x <= r)
			n = (r - x + 1 + cnt - 1) / cnt;
		else if (x < l)
			n = (r - x + 1 + cnt - 1) / cnt - (l - 1 - x + 1 + cnt - 1) / cnt;
		printf("%lld\n", n);
	}
	return 0;
}


#include<stdio.h>
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
int main()
{
    long long int a,b,c,d,move,move_p,move_v;
    scanf("%lld %lld %lld %lld",&a,&b,&c,&d);
    if(a<c&&b<d)printf("Polycarp");
    else if((a<c&&b>d)||(a>c&&b<=d)||(a>=c&&b>d)){
        move_p=a+b;
        move_v=MAX(c,d);
        if(move_p<=move_v)printf("Polycarp");
        else printf("Vasiliy");
    }
    else if(a==c&&b<d){
        move=d-b;
        a-=move;
        c-=move;
        d-=move;
        if(a<=c&&b<=d)printf("Polycarp");
        else printf("Vasiliy");
    }
     else if(a<c&&b==d){
        move=c-a;
        b-=move;
        c-=move;
        d-=move;
        if(a<=c&&b<=d)printf("Polycarp");
        else printf("Vasiliy");
    }
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdbool.h>

#define min(a,b) ((a)<(b) ? (a):(b))
#define max(a,b) ((a)>(b) ? (a):(b))
#define clr(ar) memset(ar, 0, sizeof(ar))
#define read() freopen("lol.txt", "r", stdin)

int main(){
    int i, j, k, l, x, y, z, x1, y1, x2, y2;

    while (scanf("%d %d %d %d", &x1, &y1, &x2, &y2) != EOF){
        if ((x1 + y1) <= max(x2, y2)) puts("Polycarp");
        else if ((x1 + y1) == 1) puts("Polycarp");
        else if (( (x1 - 1 + 1) == x2 && (y1 + 1) == y2)) puts("Polycarp");
        else if (( (x1 + 1) == x2 && (y1 - 1 + 1) == y2)) puts("Polycarp");
        else if (x1 <= x2 && y1 <= y2) puts("Polycarp");
        else if (max(x2, y2) < (x1 + y1)) puts("Vasiliy");
        else{
            puts("Polycarp");
        }
    }
    return 0;
}


#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#define prime 1000000007
int main()
{
	long long num,flag=0;
	scanf("%lli",&num);
	if(num<0)
	{
		flag=1;
		num=-1*num;
	}
	num=num%360;
	if(flag==0)
	{
		if(num<=45)
		{
			printf("0\n");
		}
		else if(num<=135)
		{
			printf("1\n");
		}
		else if(num<=225)
		{
			printf("2\n");
		}
		else if(num<315)
		{
			printf("3\n");
		}
		else printf("0\n");
	}
	else if(flag==1)
	{
		if(num==135)
			printf("2\n");
		else if(num==225)
			printf("1\n");
		else if(num==315)
			printf("0\n");
		else if(num<=45)
		{
			printf("0\n");
		}
		else if(num<=135)
		{
			printf("3\n");
		}
		else if(num<=225)
		{
			printf("2\n");
		}
		else if(num<315)
		{
			printf("1\n");
		}
		else printf("0\n");
	}
	return 0;
}


#include <limits.h>
#include <stdio.h>

#define N 31

int main() {
	static long long cc[N];
	long long min, sum;
	int n, k, l, i, h;

	scanf("%d%d", &n, &l);
	for (i = 0; i < n; i++)
		scanf("%lld", &cc[i]);
	for (i = 1; i < N; i++)
		if (i >= n || cc[i] > cc[i - 1] * 2LL)
			cc[i] = cc[i - 1] * 2LL;
	min = LLONG_MAX;
	sum = 0;
	k = 0;
	for (h = N - 1; h >= 0; h--)
		if ((l & 1 << h) > 0) {
			k |= 1 << h;
			sum += cc[h];
			if (k == l && min > sum)
				min = sum;
		} else
			if (min > sum + cc[h])
				min = sum + cc[h];
	printf("%lld\n", min);
	return 0;
}


#include <stdio.h>
#include <limits.h>
int c[30];
long long cost[32],dp[32];
long long min(long long a,long long b){return (a<b)?a:b;}
int main()
{
    int n,L,i,j;
    scanf("%d%d",&n,&L);
    for(i=0;i<n;i++) scanf("%d",&c[i]);
    for(i=0;i<32;i++)
    {
        cost[i]=LLONG_MAX;
        for(j=0;j<n && j<=i;j++) cost[i]=min(cost[i],c[j]*(1LL<<(i-j)));
    }
    for(i=0;i<32;i++)
    {
        int bit=L&(1<<i);
        if(i==0)
        {
            dp[i]=0;
            if(bit) dp[i]=cost[i];
        }
        else
        {
            if(bit) dp[i]=cost[i]+dp[i-1];
            else dp[i]=min(cost[i],dp[i-1]);
        }
    }
    printf("%lld\n",dp[31]);
    return 0;
}


#include <stdio.h>
#include <stdbool.h>
typedef long long i64;
i64 gcd(i64 a, i64 b) {
	return b == 0 ? a : gcd(b, a % b);
}
bool is_prime(i64 n) {
	if(n <= 1) return false;
	else if(n <= 3) return true;
	else if(n % 2 == 0 || n % 3 == 0) return false;
	for(i64 i = 5; i * i <= n; i += 6) if(n % i == 0 || n % (i + 2) == 0) return false;
	return true;
}
i64 f(i64 x, i64 y) {
	i64 ans = 0, d;
	if(x == 1) return y;
	while(y != 0) {
		if(is_prime(x)) {
			ans += x < y ? y / x + y % x : y;
			break;
		}
		while((d = gcd(x, y)) == 1LL && y != 0) ans++, y--;
		if(y == 0) break;
		x /= d, y /= d;
	}
	return ans;
}
int main(void) {
	i64 x, y;
	scanf("%lld%lld", &x, &y);
	printf("%lld\n", f(x, y));
	return 0;
}


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

#define D	6720

int compare(const void *a, const void *b) {
	long long ia = *(long long *) a;
	long long ib = *(long long *) b;

	return ia == ib ? 0 : (ia < ib ? -1 : 1);
}

int main() {
	static long long aa[D];
	long long x, y, a, tmp, cnt;
	int k, i;

	scanf("%lld%lld\n", &x, &y);
	k = 0;
	for (a = 1; a * a <= x; a++)
		if (x % a == 0) {
			aa[k++] = a;
			if (x / a != a)
				aa[k++] = x / a;
		}
	srand(time(NULL));
	for (i = k - 1; i > 0; i--) {
		int i_ = rand() % (i + 1);

		tmp = aa[i], aa[i] = aa[i_], aa[i_] = tmp;
	}
	qsort(aa, k, sizeof *aa, compare);
	for (i = k - 1; i >= 0; i--)
		if (y % aa[i] == 0)
			break;
	cnt = 0;
	while (y > 0) {
		int j, jmin;
		long long min;

		min = 1e12 + 1;
		jmin = -1;
		for (j = i + 1; j < k; j++)
			if (aa[j] % aa[i] == 0 && min > (y % aa[j]) / aa[i]) {
				min = (y % aa[j]) / aa[i];
				jmin = j;
			}
		if (aa[i] == x)
			min = y / x;
		cnt += min;
		y -= min * aa[i];
		i = jmin;
	}
	printf("%lld\n", cnt);
	return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdbool.h>

#define SQR 1097
#define MAX 1200000
#define clr(ar) memset(ar, 0, sizeof(ar))
#define read() freopen("lol.txt", "r", stdin)
#define chkbit(ar, i) (((ar[(i) >> 6]) & (1 << (((i) >> 1) & 31))))
#define setbit(ar, i) (((ar[(i) >> 6]) |= (1 << (((i) >> 1) & 31))))
#define isprime(x) (( (x) && ((x)&1) && (!chkbit(ar, (x)))) || ((x) == 2))

char str[12];
long long p, q;
unsigned int ar[(MAX >> 6) + 5] = {0};
int m = 0, counter[MAX] = {0}, P[12010];

void Sieve(){
    int i, j, k;
    setbit(ar, 0), setbit(ar, 1);

    for (i = 3; i < SQR; i++, i++){
        if (!chkbit(ar, i)){
            k = i << 1;
            for (j = (i * i); j < MAX; j += k) setbit(ar, j);
        }
    }

    k = 0, counter[2] = ++k;
    for (i = 3; (i + 1) < MAX; i++, i++){
        if (isprime(i)) k++;
        counter[i] = counter[i + 1] = k;
    }
}

void backtrack(int i, int j){
    if (i > j){
        int x = atoi(str);
        if (x < MAX) P[m++] = x;
        return;
    }

    int k;
    for (k = 0; k < 10; k++){
        if (!i && !k) continue;
        str[i] = str[j] = k + 48;
        backtrack(i + 1, j - 1);
    }
}

void Generate(){
    m = 0;
    int i, j;
    for (i = 1; i <= 7; i++){
        str[i] = 0;
        backtrack(0, i - 1);
    }
}

bool Check(int n, int i){
    long long x = q * counter[n];
    long long y = p * (i + 1);
    return (x <= y);
}

int main(){
    Sieve();
    Generate();
    int n, i, j, low, high, mid, res;

    while (scanf("%I64d %I64d", &p, &q) != EOF){

        res = -1;
        for (i = m - 2; i >= 0; i--){
            if (Check(P[i], i)){
                low = P[i], high = P[i + 1] - 1;

                while ((low + 1) < high){
                    mid = (low + high) >> 1;
                    if (Check(mid, i)) low = mid;
                    else high = mid;
                }

                if (Check(high, i)) res = high;
                else if (Check(low, i)) res = low;
                if (res != -1) break;
            }
        }

        if (res == -1) puts("Palindromic tree is better than splay tree");
        else printf("%d\n", res);
    }
    return 0;
}


#include <stdio.h>
#define max 2000001
int prime[max],pal[max],digits[10];

int isPalindrome(int a){
    int temp=a,i=0;
    while(temp!=0){
        digits[i++]=temp%10;
        temp/=10;
    }
    int k=i-1;
    i=0;
    while(i<k && digits[i]==digits[k]){
        i++;
        k--;
    }
    if(i<k)
        return 0;
    else{
        return 1;
    }
}

void set(){
int i=2;
while(i<max){
    int j=2;
    int p=j*i;
    while(p<max){
        prime[p]=1;
        j++;
        p=i*j;
    }
    i++;
    while(prime[i]==1)
        i++;
}
int count=0;
for(i=2;i<max;i++){
    if(prime[i]==0)
        prime[i]=(++count);
    else
        prime[i]=count;
}

i=1;
//printf("test\n");
while(i<max){
        pal[i]=pal[i-1];
    if(isPalindrome(i))
        pal[i]+=1;
 i++;
}
}

int main(){
int p,q,i=1;
set();
int res=-1;
scanf("%d%d",&p,&q);
while(i<max){
    if(q*prime[i]<=p*pal[i]){
        res=i;
    }
    i++;
}
if(res==-1)
printf("Palindromic tree is better than splay tree");
else
    printf("%d\n",res);
    return 0;
}


/* practice with Dukkha */
#include <stdio.h>

#define MD	998244353
#define N_	(1 << 20)	/* N_ = pow2(ceil(log2(N * 9 + 1))) */

long long ww[21], ww_[21];

long long power(int a, int k) {
	long long p;

	if (k == 0)
		return 1;
	p = power(a, k / 2);
	p = p * p % MD;
	if (k % 2)
		p = p * a % MD;
	return p;
}

void init() {
	int h;

	for (h = 0; h <= 20; h++) {
		ww[h] = power(3, (MD - 1) >> h);
		ww_[h] = power(3, MD - 1 - ((MD - 1) >> h));
	}
}

void ntt(int *aa, int n, int inverse) {
	int h, i, j, b, l, r, u, v, tmp;
	long long w, w_;

	for (i = 1, j = 0; i < n; i++) {
		b = n;
		do
			j ^= b >>= 1;
		while ((j & b) == 0);
		if (i < j)
			tmp = aa[i], aa[i] = aa[j], aa[j] = tmp;
	}
	for (h = 0; (b = 1 << h) < n; h++) {
		w_ = inverse ? ww_[h + 1] : ww[h + 1];
		for (i = 0; i < n; i += b * 2) {
			w = 1;
			for (j = 0; j < b; j++) {
				l = i + j, r = l + b;
				u = aa[l], v = aa[r] * w % MD;
				if ((aa[l] = u + v) >= MD)
					aa[l] -= MD;
				if ((aa[r] = u - v) < 0)
					aa[r] += MD;
				w = w * w_ % MD;
			}
		}
	}
}

int main() {
	static int aa[N_];
	int n, n_, k, i;
	long long v, a, ans;

	init();
	scanf("%d%d", &n, &k), n /= 2;
	n_ = 1;
	while (n_ < n * 9 + 1)
		n_ *= 2;
	while (k--) {
		int d;

		scanf("%d", &d);
		aa[d] = 1;
	}
	ntt(aa, n_, 0);
	for (i = 0; i < n_; i++)
		aa[i] = power(aa[i], n);
	ntt(aa, n_, 1);
	v = power(n_, MD - 2);
	for (i = 0; i < n_; i++)
		aa[i] = aa[i] * v % MD;
	ans = 0;
	for (i = 0; i <= n * 9; i++) {
		a = aa[i];
		ans = (ans + a * a) % MD;
	}
	printf("%lld\n", ans);
	return 0;
}


#include <string.h>
#include <stdio.h>
#include <ctype.h>
//#include <DEBUG.h>
#define LL long long
#define uLL long long
#define uL unsigned int
#define iL unsigned int
#define max(a,b) (((a)>(b))?(a):(b))
#define min(a,b) (((a)<(b))?(a):(b))

LL CP=(LL)998244353;
LL inv[0x100000];
LL num1[0x100000],num2[0x100000];
uL exgcd(uL a,uL c)
{
    static uL gcd;
    uL k;
    uLL x;
    if (a==0)
    {
        gcd=c;
        return 1;
    }
    else
    {
        k=c%a;
        if (k==0)
        {
            gcd=a;
            return 1;
        }
        else
        {
            x=exgcd(a%k,k);
        }
    }
    return (x*c-c/a*gcd)/k;
}
void ini1(long n,LL P)
{
    long i;
    inv[1]=1;
    for (i=2;i<=n;i++)
    {
        inv[i]=(uLL)inv[P%i]*(P-P/i)%P;
    }
}
static inline LL madd(LL a,LL b)
{
    a+=b;
    a-=(a>CP)?CP:0;
    return a;
}
static inline LL msub(LL a,LL b)
{
    a-=b;
    a+=(a<0)?CP:0;
    return a;
}
static inline LL MR(LL a,LL b)
{
    return (a*b)%CP;
}
uLL mexp(uLL a,uLL b)
{
    register uLL t;
    for (t=1;b;b=b>>1)
    {
        if (b&1) {t=MR(a,t);}
        a=MR(a,a);
    }
    return t;
}
static inline uLL PolynomialPower(uLL *pol,uLL psize,uLL ex,uLL N,uLL *ans)
{
    uLL i,j,a,b,c,d;
    psize=(psize>N)?N:psize;
    uLL temp[psize],ans2[N+1];
    ans[1]=mexp(pol[1],ex);
    for (i=1;i<psize;i++) {temp[i-1]=pol[i+1]*i%CP;}
    c=ex%CP;d=exgcd(pol[1],CP);
    for (i=2;i<=N;i++)
    {
        a=0;b=min(psize-1,i-1);
        for (j=0;j<b;j++)
        {
            a=madd(a,MR(ans[i-j-1],temp[j]));
        }
        a=MR(a,c);
        b=min(psize,i-1);
        for (j=2;j<=b;j++)
        {
            a=msub(a,MR(ans2[i-j],pol[j]));
        }
        ans[i]=MR(a,MR(d,inv[i-1]));
        ans2[i-1]=MR(i-1,ans[i]);
    }
}
int main()
{
    //freopen("in.in","r",stdin);
    LL i,j,k,a,b,c,n,ans;
    ini1(1000013,CP);
    scanf("%lld%lld",&n,&k);
    *num1=10;
    memset(num1+1,0,sizeof(*num1)*(*num1+1));
    for (i=1;i<=k;i++)
    {
        scanf("%lld",&a);
        num1[a+1]=1;
    }
    //printf("%lld\n",n/2);
    //print(num1,20);
    for (i=1;i<=10;i++) {if (num1[i]>0) {break;}}
    PolynomialPower(num1+i-1,10-i+1,n/2,1000001,num2);
    //print(num2,20);
    ans=0;
    for (i=1;i<=1000000;i++)
    {
        a=num2[i]*num2[i]%CP;
        ans=madd(a,ans);
    }
    printf("%lld\n",ans);
    return 0;
}


#include <stdio.h>

long int pow2[220];
const long int mod = 1000000007L;
char s[110];

int main() {
	long int ans;
	int i, n;
	pow2[0] = 1L;
	for(i = 1; i <= 220; i++) pow2[i] = (pow2[i-1]<<1)%mod;
	scanf("%s", s);
	n = strlen(s);
	if(s[n-1] == '0') ans = 0;
	else ans = 1;
	for(i = n-2; i >= 0; i--) {
		ans = (ans<<1)%mod;
		//printf("... %ld\n", ans);
		if(s[i] == '1') ans = (ans+pow2[(n-i-1)*2])%mod;
	}
	printf("%ld\n", ans);
	return 0;
}


#include <stdio.h>
#define int long long
int n,k,a,x,y,T,M=1e9+7;
int power(int p,int z)
{
	if(z==0) return 1%M;
	if(z==1) return p%M;
	int tt=power(p,z/2);
	if(z&1) return ((tt*tt)%M)*p%M;
	else return (tt*tt)%M;
}
main()
{
	scanf("%lld",&T);
	while(T--)
	{
		scanf("%lld%lld",&n,&k);
		if(n&1)
		{
			y=(power(2,n-1)+1)%M;
			y=power(y,k);
		}
		else
		{
			y=power(2,n-1);
			x=(power(2,n-1)-1+M)%M;
			a=((y*power(x,k))%M+power((2*y)%M,k)+M)%M;
			a=a*power((y+1)%M,M-2)%M;
			y=a;
		}
		printf("%lld\n",y);
	}
}


#include <stdio.h>
#include <ctype.h>

// Program de Mircea Rebengiuc
// inceput pe 2021.08.09

#define MOD 1000000007
#define MAXN 200000
#define MAXK 200000

int p2[MAXK + 1];

static inline int multmod( int a, int b ){
  return (((long long)a) * b) % MOD;
}

static inline int addmod( int a, int b ){
  return (a + b) % MOD;
}

// chestii invmod
static inline int myExp( int a, int n ){
  int acc = 1;

  while( n > 0 ){
    if( n & 1 )
      acc = multmod(acc, a);
    a = multmod(a, a);
    n >>= 1;
  }

  return acc;
}

static inline int inv( int x ){
  return myExp(x, MOD - 2);
}

int main(){
  int t, n, k, i, big_num, comb, numeq, answer;

  p2[0] = 1;
  for( i = 1 ; i <= MAXK ; i++ )
    p2[i] = multmod(2, p2[i - 1]);

  for( scanf("%d", &t) ; t-- ; ){
    scanf("%d%d", &n, &k);

    // calculam big_num = numarul de moduri de a face o pozitie sa fie egala (and = xor) modificand doar bitii de pe acea pozitie
    // cazul and = xor = 1
    big_num = (n & 1);

    // cazul and = xor = 0
    comb = 1;
    for( i = 0 ; i < n ; i += 2 ){
      big_num = addmod(big_num, comb);
      comb = multmod(comb, multmod(n - i, n - i - 1));
      comb = multmod(comb, multmod(inv(i + 1), inv(i + 2)));
    }

    // simulam
    numeq = 1;// numeq = numarul de moduri de a face primele i cifre egale modificand doar primele i cifre
    answer = 0;
    for( i = 0 ; i < k ; i++ ){
      if( (n & 1) == 0 )// cazul in care pentru bitul i and = 1, xor = 0
        answer = addmod(answer, multmod(numeq, myExp(p2[k - i - 1], n)));// adaugam numarul de posibilitati

      numeq = multmod(numeq, big_num);// actualizam x
    }
    answer = addmod(answer, numeq);// adaugam numarul de cazuri in care e egal ca semnul este >=

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

  return 0;
}


#include <stdio.h>
#define lli long long int
lli n,i,j,u,k,l,x,y,d,s,t,h[200000];
lli gg(lli x){int u=0;  while(x){u++;x/=10;} return u;}
lli ggg(lli n)
{
    while(n/10)n/=10;
    return n;
}
lli g(lli u)
{
    lli a=1;
    while(--u)a=10*a+1;
    return a;
}
lli f(lli n);
lli fff(lli n)
{
    lli u=gg(n);
    if(u==1) return 2+(11-n);
    if(ggg(n)<6) return 10000;
    return u+1+f(g(u+1)-n);
}
lli ff(lli n)
{
    lli u=gg(n);
    if(u==1)return n;
    return u+(f(n-g(u)));
}
lli f(lli n)
{

    if(!n)return 0;
    if(n<0)n*=-1;
    lli x,y,u=gg(n),i;
    x=ff(n);
    y=fff(n);
    return (x>y ? y : x);
}
int main()
{
    scanf("%lld",&n);
    x=n;
    while(x){u++;x/=10;}
    printf("%lld",f(n));
    return 0;
}


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

typedef long long int int64;

static void printInt(int64 n){
  if(n<0){putchar('-');n=-n;}
  if(n==0){putchar('0');return;}
  int s[20];int len=0;
  while(n>0){s[len++]=n%10+'0';n/=10;}
  while(len>0){putchar(s[--len]);}
}
static int64 readInt(void){
  int prev='\0';int c=getchar();
  while(!('0'<=c && c<='9')){prev=c;c=getchar();}
  int64 res=0;
  while('0'<=c && c<='9'){res=10*res+c-'0';c=getchar();}
  return prev=='-'?-res:res;
}

#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#define ABS(a) ((a)>(0)?(a):-(a))

int64 func(int64 n,int64 b){
  int64 res=0;
  while(n>0){
    res+=n/b;
    n/=b;
  }
  return res;
}

void run(void){
  int64 n=readInt();
  int64 b=readInt();
  int64 ans=n;
  for(int64 k=2;k*k<=b;k++){
    if(b%k!=0) continue;
    int cnt=0;
    do{
      b/=k;
      cnt++;
    } while(b%k==0);
    int64 f=func(n,k)/cnt;
    ans=MIN(ans,f);
  }
  if(b>1){
    ans=MIN(ans,func(n,b));
  }
  printInt(ans);
  putchar('\n');
}

int main(void){
  run();
  return 0;
}


#include <stdio.h>

long long zeros(long long n, long long p) {
	return n == 0 ? 0 : n / p + zeros(n / p, p);
}

int main() {
	long long n, b, a, ans, z;
	int k;

	scanf("%lld%lld", &n, &b);
	ans = n;
	for (a = 2; a <= b / a; a++)
		if (b % a == 0) {
			k = 0;
			while (b % a == 0) {
				b /= a;
				k++;
			}
			if (ans > (z = zeros(n, a) / k))
				ans = z;
		}
	if (b > 1) {
		a = b;
		k = 1;
		if (ans > (z = zeros(n, a) / k))
			ans = z;
	}
	printf("%lld\n", ans);
	return 0;
}


#include <stdio.h>

#define MOD	((long long) 1e9 + 7)

long long totient(long long n) {
	long long p, x;

	x = n;
	for (p = 2; p * p <= n; p++)
		if (n % p == 0) {
			x = x / p * (p - 1);
			while (n % p == 0)
				n /= p;
		}
	if (n > 1)
		x = x / n * (n - 1);
	return x;
}

int main() {
	long long n, k;

	scanf("%lld%lld", &n, &k);
	k = (k + 1) / 2;
	while (n > 1 && k > 0) {
		n = totient(n);
		k--;
	}
	printf("%lld\n", n % MOD);
	return 0;
}


#define M 2000002
#define MOD 1000000007
long long inv[M],fact[M],factr[M];
int main(){
	int i,N;
	for(i=0;i<M;i++)inv[i]=fact[i]=factr[i]=1;
	for(i=2;i<M;i++){
		inv[i]=(MOD-MOD/i)*inv[MOD%i]%MOD;
		fact[i]=i*fact[i-1]%MOD;
		factr[i]=inv[i]*factr[i-1]%MOD;
	}
	scanf("%d",&N);N++;
	int r=(2*fact[2*N-1])%MOD;
	r=(r*factr[N])%MOD;
	r=(r*factr[N-1])%MOD;
	printf("%d\n",r-1);
}


#include <stdio.h>
#include <string.h>
#include <stdbool.h>

#define MAX 2000010
#define MOD 1000000007
#define clr(ar) memset(ar, 0, sizeof(ar))
#define read() freopen("lol.txt", "r", stdin)

long long factorial[MAX];

long long expo(long long x, long long n){
    long long res = 1;

    while (n){
        if (n & 1) res = (res * x) % MOD;
        x = (x * x) % MOD;
        n >>= 1;
    }

    return (res % MOD);
}

long long binomial(int n, int k){
    long long y = (factorial[k] * factorial[n - k]) % MOD;
    long long res = (factorial[n] * expo(y, MOD - 2)) % MOD;
    return res;
}

int main(){
    int i, n, m, k, res;

    factorial[0] = 1LL;
    for (i = 1; i < MAX; i++) factorial[i] = (factorial[i - 1] * i) % MOD;

    while (scanf("%d", &n) != EOF){
        res = binomial((n << 1) + 2, n + 1);
        res = (res - 1 + MOD) % MOD;
        printf("%d\n", res);
    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>

bool reversed(char c, char t)
{
    return (c == 'N' && t == 'S') || (c == 'S' && t == 'N') || (c == 'W' && t == 'E') || (c == 'E' && t == 'W');
}

char reverse(char c)
{
    if (c == 'S')
        return 'N';
    if (c == 'W')
        return 'E';
    if (c == 'N')
        return 'S';
    if (c == 'E')
        return 'W';
    return '\0';
}

int main()
{
    int n;
    assert(scanf("%d", &n) == 1);
    n--;
    char* s = malloc(sizeof(char) * (n + 1));
    char* t = malloc(sizeof(char) * (n + 1));
    assert(scanf("%s", s));
    assert(scanf("%s", t));
    if (reversed(s[n - 1], t[n - 1]))
        printf("NO\n");
        else
    {
        int i = 0;
        int j = 0;
        int ilast = 0;
        int op = 0;
        while (j < n || i < n)
        {
            op += n - i;
            for (; i < n; i++)
            {
                if (j < n && t[j] == s[i])
                    j++;
                else if (j > 0 && reverse(t[j - 1]) == s[i])
                    j--;
            }
            if (j == n && i == n)
            {
                printf("YES\n");
                return 0;
            }
            op += n - j;
            for (; j < n; j++)
            {
                if (i < n && t[j] == s[i])
                    i++;
                else if (i > 0 && reverse(s[i - 1]) == t[j])
                    i--;
            }
            if (op > 4e8)
            {
                printf("YES\n");
                return 0;
            }
            if (ilast == i)
                break;
            ilast = i;
        }
        if (j == n && i == n)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}


/*  AUTHOR:AKASH JAIN
*   USERNAME:akash19jain
*   DATE:31/03/2019
*/
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<stdbool.h>
int cmp(const void * a,const void * b)
{
    return (*(int*)a - *(int*)b);
}
int main()
{
    long long t=1;
    //scanf("%lld",&t);
    while(t--)
    {
        double t=clock();
        long long n;
        scanf("%lld",&n);
        long long arr[n];
        for(long long i=0;i<n;i++)
            scanf("%lld",&arr[i]);
        qsort(arr,n,sizeof(arr[0]),cmp);
        while(clock()-t<=1500);
        for(long long i=0;i<1500;i++);
        for(long long i=0;i<n;i++)
            printf("%lld ",arr[i]);
    }

    return 0;
}


#include<stdio.h>
#include<string.h>

long long foo(const char* str, int base, int len, long long mul, long long bar){
	if(!*str) return bar;
	long long c = 0;
	long long p = 1;
	int i = 0;
	do{
		c += p * (str[i++] - '0');
		p *= 10;
	}while(str[i] && c + p * (str[i] - '0') < base && i < len);
	if(!str[i]) return bar + c * mul;
	if(c != 0){
		str += i - 1;
		while(*str == '0') --str;
	}
	return foo(++str, base, len, mul * base, bar + c * mul);
}

long long calc(const char* str, int base){
	int len = 0;
	do{
		int temp = base;
		while(temp){
			++len;
			temp /= 10;
		}
	}while(0);
	return foo(str, base, len, 1, 0);
}

int main(void){
	char str[100], d[100];
	int b;
	scanf("%d %s", &b, str);
	int l = strlen(str) - 1;
	for(int i = 0; i <= l; ++i) d[i] = str[l - i];
	d[l + 1] = '\0';
	printf("%lld\n", calc(d, b));
	return 0;
}


#include <stdio.h>
#define m 1000000007u
#define lim 5555555
typedef long long unsigned llu;
typedef unsigned u;
u F[lim],G[lim];
int main()
{
	u i,j,k,r=0;
	for(i=1;++i<lim;)
	{
		if(!F[i])for(j=0;(j+=i)<lim;)if(!F[j])F[j]=i;
		j=i/F[i];
		G[i]=(G[j]+j*(llu)((F[i]*(llu)(F[i]-1)>>1)%m))%m;
	}
	for(scanf("%u%u%u",&k,&i,&j);j>=i;--j)r=(G[j]+r*(llu)k)%m;
	printf("%u\n",r);
	return 0;
}


#include <stdio.h>

#if defined( _WIN32 )
typedef __int64 az_int64_t;
typedef unsigned __int64 az_uint64_t;
#define I64(x) x ## I64
#define F64 "I64"
#else
typedef long long az_int64_t;
typedef unsigned long long az_uint64_t;
#define I64(x) x ## ll
#define F64 "ll"
#endif

#define MODN 1000000007

int minp[5000004];
az_int64_t f[5000004];

void gen( int limit )
{
  int i, j, finish = 0;

  for( i = 2; i <= limit; ++i)
    if( minp[i] == 0 )
    {
      minp[i] = i;
      if( !finish )
      {
        int start = i * i;
        if( start > limit )
          finish = 1;
        else for( j = start; j <= limit; j += i)
          if( minp[j] == 0 || minp[j] > i ) minp[j] = i;
      }
    }

  for( i = 2; i <= limit; ++i)
    f[i] = f[i/minp[i]] + (((az_int64_t) i * (minp[i]-1)) >> 1);
}

int main( void )
{
  int t, l, r, i, res = 0;
  az_int64_t pt = 1;

  scanf( "%d %d %d", &t, &l, &r);
  gen( r );

  for( i = l; i <= r; ++i)
  {
    f[i] %= MODN;
    res += (int) ((pt * f[i]) % MODN);
    if( res >= MODN ) res -= MODN;
    pt *= t;
    pt %= MODN;
  }

  printf( "%d\n", res);
  return 0;
}


/* https://codeforces.com/contest/819/submission/28105538 (Dukkha) */
#include <stdio.h>
#include <string.h>

#define AB	24
#define N	(AB * 4)

int count(int *cc, int l, int r) {
	static char used[AB];
	int i, cnt;

	memset(used, 0, sizeof used);
	cnt = 0;
	for (i = l; i <= r; i++)
		if (!used[cc[i]]) {
			used[cc[i]] = 1;
			cnt++;
		}
	return cnt;
}

int main() {
	static int cc[N];
	int a, b, ab, d, l, r, m, k0, k1, k2, k3, k, h, min;

	scanf("%d%d%d%d", &a, &b, &l, &r);
	l--, r--;
	ab = a + b;
	m = 4;
	h = l / ab;
	r -= h * ab;
	l -= h * ab;
	h = (r - l + 1) / ab;
	if (h > 2)
		r -= (h - 2) * ab;
	d = a > b ? a - b : 1;
	for (h = 0; h < m; h++) {
		for (k = 0; k < a - d; k++)
			cc[h * ab + k] = k;
		for (k = 0; k < d; k++)
			cc[h * ab + a - d + k] = h % 2 == 0 ? a - d + k : a + k;
	}
	min = a + a;
	for (k0 = 1; k0 <= d; k0++) {
		for (k = 0; k < b; k++)
			cc[ab * 0 + a + k] = cc[ab * 0 + a - k0];
		for (k1 = 1; k1 <= d; k1++) {
			for (k = 0; k < b; k++)
				cc[ab * 1 + a + k] = cc[ab * 1 + a - k1];
			for (k2 = 1; k2 <= d; k2++) {
				for (k = 0; k < b; k++)
					cc[ab * 2 + a + k] = cc[ab * 2 + a - k2];
				for (k3 = 1; k3 <= d; k3++) {
					int cnt;

					for (k = 0; k < b; k++)
						cc[ab * 3 + a + k] = cc[ab * 3 + a - k3];
					if (min > (cnt = count(cc, l, r)))
						min = cnt;
				}
			}
		}
	}
	printf("%d\n", min);
	return 0;
}


#include <stdio.h>
#define MOD 1000000007

int K;
long long PA,PB,ans;

long long INV1,INV2;
long long f[1010][2010];

long long power(long long a,int b)
{
    long long y = 1;
    for(;b;b>>=1)
    {
        if(b&1) y = y * a % MOD;
        a = a * a % MOD;
    }
    return y;
}

int main()
{
    int i,j;
    long long x,y;

    scanf("%d%I64d%I64d",&K,&PA,&PB);
    INV1 = power(PA+PB,MOD-2);
    INV2 = power(PB,MOD-2);

    f[1][0] = 1;
    for(i=1;i<=K;i++) for(j=(i==1);j<=2*K;j++)
    {
        if(j < K) f[i][j] = f[i-1][j] * PA % MOD * INV1 % MOD;
        if(j >= i && j-i < K) f[i][j] = (f[i][j] + f[i][j-i] * PB % MOD * INV1) % MOD;
    }

    for(i=1;i<K;i++) for(j=K;j<=2*K;j++) ans = (ans + j*f[i][j]) % MOD;
    for(i=0;i<K;i++) ans = (ans + (PA*INV2 + K+i) % MOD * f[K][i]) % MOD;

    printf("%I64d\n",ans);
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <assert.h>

#define MOD 1000000007
#define clr(ar) memset(ar, 0, sizeof(ar))
#define read() freopen("lol.txt", "r", stdin)

long long k, pa, pb, inv, dp[1010][1010];

long long expo(long long x, long long n){
    x %= MOD;
    long long res = 1;

    while (n){
        if (n & 1) res = (res * x) % MOD;
        x = (x * x) % MOD;
        n >>= 1;
    }

    return (res % MOD);
}

long long F(int a, int c){
    if (c >= k) return c;
    if (dp[a][c] != -1) return dp[a][c];

    long long res = 0, loop = 0;

    if (a == 0){
        res = pa * F(a + 1, c) % MOD;
        loop += pb;
        res = res * expo(MOD - loop, MOD - 2) % MOD;
    }
    else if ((a + c) >= k){
        loop += pa;
        res = pb * F(a, c + a) % MOD;
        res = (res + 1) * expo(MOD - loop, MOD - 2) % MOD;
    }
    else{
        res += pa * F(a + 1, c);
        res += pb * F(a, c + a);
        res %= MOD;
    }

    return (dp[a][c] = res);
}

int main(){
    while (scanf("%lld %lld %lld", &k, &pa, &pb) != EOF){
        memset(dp, -1, sizeof(dp));

        inv = expo(pa + pb, MOD - 2);
        pb = pb * inv % MOD;
        pa = pa * inv % MOD;
        printf("%lld\n", (F(0, 0) - 1 + MOD) % MOD );
    }
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdbool.h>

#define MAX 1000000
#define MOD 1000003
#define clr(ar) memset(ar, 0, sizeof(ar))
#define read() freopen("lol.txt", "r", stdin)

long long fact[MAX], inv[MAX];

void Generate(){
    int i, x;
    fact[0] = 1, inv[MAX - 1] = 6;
    for (i = 1; i < MAX; i++) fact[i] = ((long long)i * fact[i - 1]) % MOD;
    for (i = MAX - 2; i >= 0; i--) inv[i] = ((long long)inv[i + 1] * (i + 1)) % MOD;
}

long long binomial(long long n, long long k){
    if (k > n) return 0;
    return (fact[n] * inv[n - k] * inv[k]) % MOD;
}

int main(){
    Generate();
    long long n, m, i, j, res;
    while (scanf("%lld %lld", &n, &m) != EOF){
        m--, res = 0;
        for (i = 0; i <= (n + m); i++) res += binomial(i, m);
        printf("%lld\n", (res - 1 + MOD) % MOD);
    }
    return 0;
}


#include <limits.h>
#include <stdio.h>

#define N	16
#define A	120	/* N * (N - 1) / 2 */

int main() {
	static int dp[1 << N][A + 1], rr[N], bb[N];
	static char cc[N];
	int n, i, x, a, rsum, bsum, min;

	scanf("%d", &n);
	for (i = 0; i < n; i++)
		scanf("\n%c%d%d", &cc[i], &rr[i], &bb[i]);
	for (x = 0; x < 1 << n; x++)
		for (a = 0; a <= A; a++)
			dp[x][a] = -1;
	dp[0][0] = 0;
	for (x = 0; x < 1 << n; x++) {
		int rcnt, bcnt;

		rcnt = bcnt = 0;
		for (i = 0; i < n; i++)
			if ((x & 1 << i) > 0) {
				if (cc[i] == 'R')
					rcnt++;
				else
					bcnt++;
			}
		for (a = 0; a <= A; a++)
			if (dp[x][a] >= 0)
				for (i = 0; i < n; i++)
					if ((x & 1 << i) == 0) {
						int r_ = rr[i] < rcnt ? rr[i] : rcnt;
						int b_ = bb[i] < bcnt ? bb[i] : bcnt;

						if (dp[x | 1 << i][a + r_] < dp[x][a] + b_)
							dp[x | 1 << i][a + r_] = dp[x][a] + b_;
					}
	}
	rsum = bsum = 0;
	for (i = 0; i < n; i++) {
		rsum += rr[i];
		bsum += bb[i];
	}
	min = INT_MAX;
	for (a = 0; a <= A; a++)
		if (dp[(1 << n) - 1][a] >= 0) {
			int max = bsum - dp[(1 << n) - 1][a];

			if (max < rsum - a)
				max = rsum - a;
			if (min > max)
				min = max;
		}
	printf("%d\n", min + n);
	return 0;
}


/* practice with Dukkha */
#include <stdio.h>

int md;

long long power(int a, long long k) {
	long long p;

	if (k == 0)
		return 1;
	p = power(a, k / 2);
	p = p * p % md;
	if (k % 2)
		p = p * a % md;
	return p;
}

void mult(int aa[][2], int bb[][2], int cc[][2]) {
	int i, j, k;

	for (i = 0; i < 2; i++)
		for (j = 0; j < 2; j++) {
			long long c = 0;

			for (k = 0; k < 2; k++)
				c += (long long) aa[i][k] * bb[k][j];
			cc[i][j] = c % md;
		}
}

void power_(int aa[][2], int pp[][2], int tt[][2], long long k) {
	if (k == 0) {
		pp[0][0] = pp[1][1] = 1;
		pp[0][1] = pp[1][0] = 0;
		return;
	}
	if (k % 2 == 0) {
		power_(aa, tt, pp, k / 2);
		mult(tt, tt, pp);
	} else {
		power_(aa, pp, tt, k / 2);
		mult(pp, pp, tt);
		mult(tt, aa, pp);
	}
}

int main() {
	static int aa[2][2], pp[2][2], tt[2][2];
	long long n, k, x0, x1, ans;
	int l, h;

	scanf("%lld%lld%d%d", &n, &k, &l, &md);
	if (l < 63 && k >= 1LL << l) {
		printf("0\n");
		return 0;
	}
	aa[0][0] = 1, aa[0][1] = 1;
	aa[1][0] = 1, aa[1][1] = 0;
	power_(aa, pp, tt, n);
	x0 = (pp[0][0] + pp[0][1]) % md;
	x1 = (power(2, n) - x0 + md) % md;
	ans = 1 % md;
	for (h = 0; h < l; h++)
		ans = ans * ((k & 1LL << h) == 0 ? x0 : x1) % md;
	printf("%lld\n", ans);
	return 0;
}


#include<stdio.h>
int main()
{
    long long int n,i,k=0,x,d,r,p;
    scanf("%lld %lld",&n,&p);
    i=1;
    while(k==0)
    {
      d=0;

      x=n-i*p;
      if(x<=0)
      {printf("-1");
       break;
      }
      else
      {
       while(x!=0)
       {
         r=x%2;
         if(r==1)
         {d++;}
         if(d>i)
         {
          break;
         }
         x=x/2;
       }
       x=n-i*p;
       if(d<=i&&i<=x)
       {
        printf("%lld",i);
        break;
       }
      }
      i++;
    }
    return 0;
}


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

#define N	15
#define K	200000
#define L	19	/* L = ceil(log2(K * 2 + 1)) */
#define N_	(1 << L)
#define MD	998244353

long long power(long long a, int k) {
	long long p = 1;

	while (k) {
		if (k & 1)
			p = p * a % MD;
		a = a * a % MD;
		k >>= 1;
	}
	return p;
}

int vv_[L + 1], *wwu[L + 1], *wwv[L + 1];

void init() {
	int i, l, u, v;

	u = power(3, MD - 1 >> L), v = power(u, MD - 2);
	for (l = L; l > 0; l--) {
		int n = 1 << l, m = n >> 1;

		vv_[l] = power(1 << l, MD - 2);
		wwu[l] = (int *) malloc(m * sizeof *wwu[l]);
		wwv[l] = (int *) malloc(m * sizeof *wwv[l]);
		wwu[l][0] = wwv[l][0] = 1;
		for (i = 1; i < m; i++) {
			wwu[l][i] = (long long) wwu[l][i - 1] * u % MD;
			wwv[l][i] = (long long) wwv[l][i - 1] * v % MD;
		}
		u = (long long) u * u % MD;
		v = (long long) v * v % MD;
	}
	vv_[0] = 1;
}

void ntt_(int *aa, int l, int inverse) {
	if (l) {
		int n = 1 << l, m = n >> 1, *ww = inverse ? wwv[l] : wwu[l], i, j;

		ntt_(aa, l - 1, inverse), ntt_(aa + m, l - 1, inverse);
		for (i = 0; (j = i + m) < n; i++) {
			int a = aa[i], b = (long long) aa[j] * ww[i] % MD;

			if ((aa[i] = a + b) >= MD)
				aa[i] -= MD;
			if ((aa[j] = a - b) < 0)
				aa[j] += MD;
		}
	}
}

void ntt(int *aa, int l, int inverse) {
	int n = 1 << l, i, j;

	for (i = 0, j = 1; j < n; j++) {
		int b = n >> 1, tmp;

		while ((i ^= b) < b)
			b >>= 1;
		if (i < j)
			tmp = aa[i], aa[i] = aa[j], aa[j] = tmp;
	}
	ntt_(aa, l, inverse);
}

void multiply(int *aa, int *bb, int *cc, int n, int m) {
	static int aa_[N_], bb_[N_];
	int n_, l, i;

	l = 0;
	while (1 << l < n + m - 1)
		l++;
	n_ = 1 << l;
	for (i = 0; i < n_; i++) {
		aa_[i] = i < n ? aa[i] : 0;
		bb_[i] = i < m ? bb[i] : 0;
	}
	ntt(aa_, l, 0);
	ntt(bb_, l, 0);
	for (i = 0; i < n_; i++)
		aa_[i] = (long long) aa_[i] * bb_[i] % MD;
	ntt(aa_, l, 1);
	for (i = 0; i < n + m - 1; i++)
		cc[i] = (long long) aa_[i] * vv_[l] % MD;
}

int main() {
	static int dp[N][K + 1], dq[K * 2 + 1];
	int n, c_, k, i, c, x;

	init();
	scanf("%d%d%d", &n, &k, &c_);
	for (c = 0; c <= k; c++)
		dp[0][c] = 1;
	for (i = 0; i < n; i++) {
		multiply(dp[i], dp[i], dq, k + 1, k + 1);
		if (i + 1 == n)
			printf("%d\n", dq[c_]);
		else {
			x = 0;
			for (c = k + 1; c <= k * 2; c++)
				x = (x + dq[c]) % MD;
			for (c = k; c >= 0; c--) {
				x = (x + dq[c]) % MD;
				dp[i + 1][c] = (x + (long long) dq[c] * (k - c)) % MD;
			}
		}
	}
	return 0;
}


/* practice with Dukkha */
#include <stdio.h>

#define N	1000000
#define MD	1000000007

int d_, x_, y_;

void gcd(int a, int b) {
	if (b == 0)
		d_ = a, x_ = 1, y_ = 0;
	else {
		int t;

		gcd(b, a % b);
		t = x_ - a / b * y_, x_ = y_, y_ = t;
	}
}

int inv(int a) {
	gcd(a, MD);
	return x_;
}

int main() {
	static int dp[N];
	int n, k, h;
	long long x, ans, f;

	scanf("%d%d", &n, &k);
	if (k > n - 1)
		k = n - 1;
	f = 1;
	for (h = 0; h <= k; h++) {
		dp[h] = 1;
		f = f * (h + 1) % MD;
	}
	x = k, ans = k + 1;
	for (h = k + 1; h < n; h++) {
		dp[h] = x * inv(h) % MD;
		x = (x + dp[h] - dp[h - k]) % MD;
		ans = (ans + dp[h]) % MD;
		f = f * (h + 1) % MD;
	}
	ans = (n - ans) % MD;
	for (h = 1; h < n; h++)
		ans = ans * h % MD;
	if (ans < 0)
		ans += MD;
	printf("%lld\n", ans);
	return 0;
}


#include<ctype.h>
#include<limits.h>
#include<math.h>
#include<stdbool.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main(void)
{
    long long x, y, z, k;

    scanf("%I64d%I64d%I64d%I64d", &x, &y, &z, &k);

    long long a = 1, b = 1, c = 1;
    int i = 0;
    while(i < k)
    {
        if(i < k && x > 1)
        {
            x--;
            a++;
            i++;
        }
        if(i < k && y > 1)
        {
            y--;
            b++;
            i++;
        }
        if(i < k && z > 1)
        {
            z--;
            c++;
            i++;
        }
        if(x == 1 && y == 1 && z == 1)
            i = k;
    }

    printf("%I64d", a * b * c);

    return 0;
}


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

typedef int iii[3];

int main() {
	int i, j, n, m, cnt;
	static iii *ee;
	static int *aa;
	static char *s;

	scanf("%d%d", &n, &m);
	ee = calloc(n + 1, sizeof(*ee));
	aa = malloc(sizeof(*aa) * 4 * n);
	s = malloc((n + 2) * sizeof(*s));
	memset(s + 1, '0', n);
	s[n + 1] = '\0';
	while (m-- > 0) {
		int u, v;

		scanf("%d%d", &u, &v);
		for (j = 0; j < 3; j++)
			if (ee[u][j] == 0) {
				ee[u][j] = v;
				break;
			}
		for (j = 0; j < 3; j++)
			if (ee[v][j] == 0) {
				ee[v][j] = u;
				break;
			}
	}
	for (i = 0; i < n; i++)
		aa[i] = i + 1;
	cnt = n;
	while (cnt > 0) {
		int a, k;

		a = aa[--cnt];
		k = 0;
		for (j = 0; j < 3; j++)
			if (ee[a][j] > 0 && s[a] == s[ee[a][j]])
				k++;
		if (k > 1) {
			s[a] = s[a] == '1' ? '0' : '1';
			for (j = 0; j < 3; j++)
				if (ee[a][j] > 0)
					aa[cnt++] = ee[a][j];
		}
	}
	printf("%s\n", s + 1);
	return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<memory.h>
#include<assert.h>
#include<ctype.h>

typedef long long ll;

#define max(a,b) (((a)>(b))?(a):(b))
#define min(a,b) (((a)<(b))?(a):(b))
#define abs(k) (((k)>0)?(k):(-(k)))
#define sqr(k) ((k)*(k))
#define mod(a,b) (((a)%(b)+(b))%(b))
#define mode(a,b) a=mod(a,b)
#define mset(a) memset(a,0,sizeof(a))
#define mcpy(a,b) memcpy(a,b,sizeof(a))
#define inf (1e9)
#define linf (1e18)
#define pi 3.14159265358979323846

void fileio_in_out()
{
     freopen(".in","r",stdin);
     freopen(".out","w",stdout);
}

void fileio_txt()
{
     freopen("input.txt","r",stdin);
     freopen("output.txt","w",stdout);
}

//==================================================

#define N 333333

int n,m,x,y,head,tail,count;
int i,j,k,l;
int a[N][5],cnt[N],ans[N],q[N*33];

void move(int k)
{
     ans[k]=1-ans[k];
}

int main()
{
    //fileio_in_out();
    //fileio_txt();

    mset(cnt);
    mset(ans);

    scanf("%d%d",&n,&m);
    for(i=1;i<=m;i++)
      {
        scanf("%d%d",&x,&y);
        a[x][++cnt[x]]=y;
        a[y][++cnt[y]]=x;
      }

    for(i=1;i<=n;i++)
      q[i]=i;
    head=1;
    tail=n;

    while(head<=tail)
      {
        k=q[head];
        count=0;
        for(i=1;i<=cnt[k];i++)
          if(ans[k]==ans[a[k][i]])
            count++;
        if(count>=2)
          {
            move(k);
            for(i=1;i<=cnt[k];i++)
              q[++tail]=a[k][i];
          }
        head++;
      }

    for(i=1;i<=n;i++)
      printf("%d",ans[i]);
    printf("\n");

    //system("pause");
    return 0;
}


#include<stdio.h>

#define MOD 998244353
int fact[1000006], wpar[1000006<<1], n, level;

int mul(int a, int b){
  return (a*1ll*b)%MOD;
}

int powmod(int b, int p){
  int t = 1;
  while(p){
    if(p&1)t = mul(t, b);
    b = mul(b, b);
    p>>=1;
  }
  return t;
}

int ncr(int _n, int r){
  return mul(mul(fact[_n], powmod(fact[r], MOD - 2)), powmod(fact[_n - r], MOD - 2));
}

void dfs(int x, int bits){
  if(bits > level) return;
  int l, r;
  l = x<<1;
  r = l|1;
  if(x&1){
    // x is a right child
    wpar[l] = 1;
    wpar[r] = 1^wpar[x]^wpar[l];
  } else {
    // x is a left child
    wpar[r] = 0;
    wpar[l] = 1^wpar[x]^wpar[r];
  }
  dfs(l, bits + 1);
  dfs(r, bits + 1);
}

int getpossib(int lparity){
  int i, rem;
  wpar[2] = lparity;
  wpar[3] = 1^wpar[1]^wpar[2];
  dfs(2, 2);dfs(3, 2);

  rem = n - (1<<level) + 1;

  for(i = (1<<(level - 1));i < (1<<level);i++){
    //    printf("wpar[%d]: %d\n", i, wpar[i]);
    if(i&1){
      if(wpar[i]) return 0;
      rem--;
    } else {
      if(1^wpar[i]) rem--;
    }
  }
  return rem == 0;
}

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

  fact[0] = 1;
  for(i = 1;i < 1000006;i++)fact[i] = mul(fact[i - 1], i);

  if(n == 1 || n == 2){printf("1\n");return 0;}
  if(n == 3){printf("0\n");return 0;}

  for(level = 0; (1<<level) - 1 < n;level++);
  level--;

  wpar[1] = n&1;
  printf("%d\n", getpossib(0) + getpossib(1));

  return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef long long ll;
ll MIN =  1111111111111111111;
ll MAX = -1111111111111111111;
ll max = -1111111111111111111;

void merge(ll arr[], ll l, ll m, ll r)
{
	ll i, j, k;
	ll n1 = m - l + 1;
	ll n2 =  r - m;

	/* create temp arrays */
	ll L[n1], R[n2];

	/* Copy data to temp arrays L[] and R[] */
	for (i = 0; i < n1; i++)
		L[i] = arr[l + i];
	for (j = 0; j < n2; j++)
		R[j] = arr[m + 1+ j];

	/* Merge the temp arrays back llo arr[l..r]*/
	i = 0; // Initial index of first subarray
	j = 0; // Initial index of second subarray
	k = l; // Initial index of merged subarray
	while (i < n1 && j < n2)
	{
		if (L[i] <= R[j])
		{
			arr[k] = L[i];
			i++;
		}
		else
		{
			arr[k] = R[j];
			j++;
		}
		k++;
	}

	/* Copy the remaining elements of L[], if there
	   are any */
	while (i < n1)
	{
		arr[k] = L[i];
		i++;
		k++;
	}

	/* Copy the remaining elements of R[], if there
	   are any */
	while (j < n2)
	{
		arr[k] = R[j];
		j++;
		k++;
	}
}

/* l is for left index and r is right index of the
   sub-array of arr to be sorted */
void mergeSort(ll arr[], ll l, ll r)
{
	if (l < r)
	{
		// Same as (l+r)/2, but avoids overflow for
		// large l and h
		ll m = l+(r-l)/2;

		// Sort first and second halves
		mergeSort(arr, l, m);
		mergeSort(arr, m+1, r);

		merge(arr, l, m, r);
	}
}

ll power(ll x, ll y)
{
	if (y == 0)
		return 1;
	else if (y%2 == 0)
		return power(x, y/2)*power(x, y/2);
	else
		return x*power(x, y/2)*power(x, y/2);
}

ll kitna(ll a)
{
	ll count = 1;
	ll temp = a;
	while(temp<1000000000000000000&&(1000000000000000000/temp>a))
	{
	temp*=a ;
	count++;
	}
	return count;
}

int main(ll argc, char const *argv[])
{
	ll T;
	//   scanf("%lld",&T);
	T = 1;
	// printf("%lld %lld %lld",MIN,MAX,max);
//	  printf("%lld\n",kitna(510000000000));
	ll x,y,l,r ;
//	printf("%lld\n",kitna(2));
	scanf("%lld %lld %lld %lld",&x,&y,&l,&r);
	ll o = l;
	ll xcountl =  -1;
	while(o)
	{
		xcountl++;
		o = o/x ;
	}

	ll p = r;
	ll xcountr = 0;
	while(p)
	{
		xcountr++;
		p = p/x;
	}
	ll e = r;
	ll ycountr = 0;
	while(e)
	{
		ycountr++;
		e = e/y;
	}
	ll qw = l;
	ll ycountl = -1;
	while(qw)
	{
		ycountl++;
		qw = qw/y;
	}

//	printf("%lld %lld\n",xcountl,xcountr);
	// printf("%lldchck",power(x,2));
	ll totalcount = 0;
	ll A[1000000];
	ll idx = 0;
	for(ll s=xcountl;s<xcountr;s++)
	{
		ll u = 0;
		while(power(y,u)+power(x,s)<l)
		{
			u++;
		}

		while(power(y,u)+power(x,s)<=r&&u<=kitna(y))
		{
			//   printf(">kya%lld?",A[idx]);

			A[idx] = power(y,u)+power(x,s) ;
//			printf("n%lldn",A[idx]);

			if(A[idx]>MAX)
				MAX = A[idx] ;
			if(A[idx]<MIN)
				MIN = A[idx] ;
			idx++;
			totalcount++;
			u++;
		}
//		printf("u = %lld\n",u);
	}
	for(ll s=ycountl;s<ycountr;s++)
	{
		ll u = 0;
		while(power(x,u)+power(y,s)<l)
		{
			u++;
		}
		while(power(x,u)+power(y,s)<=r&&u<xcountl&&u<=kitna(x))
		{
			//  printf("ERd  %lld %lld\n",u,s);

			A[idx] = power(x,u)+power(y,s) ;

			if(A[idx]>MAX)
				MAX = A[idx] ;
			if(A[idx]<MIN)
				MIN = A[idx] ;
			idx++;
			totalcount++;
			u++;
		}
	}
//	for(ll g=0;g<idx;g++)
//		printf("|%lld|",A[g]);

	//       printf("\n|%lld %lld|",MIN,MAX);
	mergeSort(A,0,idx-1);
	//   for(ll g=0;g<idx;g++)
	// printf("|%lld|",A[g]);

	//  printf("\n|%lld %lld|",MIN,MAX);
	for(ll sd = 1;sd<idx;sd++)
	{
		if(A[sd]-A[sd-1]-1>max&&A[sd]>0&&A[sd-1]>0)
			max = A[sd]-A[sd-1]-1 ;
		//  if(max<0)max=0;
	}
	ll finalanswer = 0;
	ll left = MIN-l;
	if(left<0)left = 0;
	ll right = r-MAX;
	if(right<0)right = 0;
//	printf("l %lld r %lld %lld|",left,right,max);
	if(left>right)
	{
		if(left>max)
		{
			finalanswer = left;
		}
		else
		{
			finalanswer = max ;
		}
	}
	else
	{
		if(right>max)
		{
			finalanswer = right;
		}
		else
		{
			finalanswer = max ;
		}
	}
	//  printf("<%lld>",totalcount);
	if(totalcount==0)
	{
		finalanswer = r-l+1 ;
	}
	if(l==1)
	{
		if(r-l+1-totalcount==0)
			printf("0\n");
		else
		{printf("%lld\n",finalanswer);}
	}

	else{
		if(r-l+1-totalcount==0)
			printf("0\n");
		else
		{ printf("%lld\n",finalanswer);}
	}

	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include<limits.h>
long long Veci(long long a,long long b)
{
    if (a>b)
        return a;
    else return b;
}
void swap(long long* a,long long* b)
{
    long long c;
    c=*a;
    *a=*b;
    *b=c;
}
long long Partition(long long *A, long long left, long long right)
{
    long long pivot = A[right], i = left, x;

    for (x = left; x < right; x++){
        if (A[x] <= pivot){
            swap(&A[i], &A[x]);
            i++;
        }
    }

    swap(&A[i], &A[right]);
    return i;
}
void QuickSort(long long Niz[],long long Levo,long long Desno)
{
    if(Levo<Desno)
    {
        long long pos;
        pos=Partition(Niz,Levo,Desno);
        QuickSort(Niz,Levo,pos-1);
        QuickSort(Niz,pos+1,Desno);
    }
}
long long x,y,l,r,i,j,p,A[60],B[60],p1,C[3600],raz=-1,br2=0,i,j,praz=0;
int main()
{
    int br,br1;
    scanf("%lld%lld%lld%lld",&x,&y,&l,&r);
    p=1;
    br=0;
    while (p<r)
    {
        A[br]=p;
        if (LLONG_MAX/p<x)
            p=r;
        else p=p*x;
        br++;
    }
    br1=0;
    p1=1;
    while (p1<r)
    {
        B[br1]=p1;
        if (LLONG_MAX/p1<y)
            p1=r;
        else p1=p1*y;
        br1++;
    }
    for(i=0;i<br;i++)
        for(j=0;j<br1;j++)
        {
            C[br2]=A[i]+B[j];
            if (C[br2]<=r && C[br2]>=l)
                br2++;
        }
    if (br2==0)
        printf("%lld",r-l+1);
    else if (br2==r-l+1)
        printf("0");
    else {
        QuickSort(C,0,br2-1);
        for(i=1;i<br2;i++)
        {
            praz=C[i]-C[i-1]-1;
            if (praz>raz)
                raz=praz;
        }
        if (l!=C[0])
            raz=Veci(C[0]-l,raz);
        if (r!=C[br2-1])
            raz=Veci(r-C[br2-1],raz);
        printf("%lld",raz);
    }
    long long x;
    return 0;
}


#include <stdio.h>
#define N 1002
#define MOD 1000000007

typedef long long typec;

int n, m, k;
typec ftable[N];
typec facmod[1000001];
int com[N][N];

typec extend_gcd(typec a, typec b, typec *x, typec *y)
{
	if(!b) return *x = 1, *y = 0, a;
	typec res = extend_gcd(b, a % b, x, y), tmp = *x;
	*x = *y, *y = tmp - (a / b) * (*y);
	return res;
}

typec inverse(typec a, typec m)
{
	typec x, y;
	y = extend_gcd(a, m, &x, &y);
	while(x < 0) x += m;
	return x;
}

typec pow_mod(typec x, typec k, typec m)
{
	typec res = 1;
	while(x %= m, k) {
		if(k&1) res *= x, res %= m;
		x *= x, k >>= 1;
	}
	return res;
}

int mymin(int x, int y)
{
	return x < y? x : y;
}

typec combines(int i, int j)
{
	if(i < j) return 0;
	if(i < N) return com[i][j];
	return facmod[i] * inverse(facmod[j], MOD) % MOD * inverse(facmod[i - j], MOD) % MOD;
}

void init()
{
	int i, j;
	typec sum = 0;
	facmod[0] = 1;
	for(i = 1; i <= 1000000; i++) {
		facmod[i] = facmod[i - 1] * i;
		facmod[i] %= MOD;
	}
	for(i = 0; i < N; i++) {
		for(j = 0; j <= i; j++) {
			if(!i) com[i][j] = 1;
			else com[i][j] = (com[i - 1][j] + com[i - 1][j - 1]) % MOD;
		}
	}
	ftable[1] = 1;
	for(i = 2; i <= n; i++) {
		ftable[i] = pow_mod(i, n, MOD) + MOD;
		sum = 0;
		for(j = 1; j < i; j++) {
			sum += com[i][j] * ftable[j] % MOD;
			sum %= MOD;
		}
		ftable[i] -= sum;
		ftable[i] %= MOD;
	}
}

typec calc()
{
	int i, j, m1, m2;
	typec res = 0, tmp, stmp;
	m1 = mymin(n, k);
	for(i = 0; i <= m1; i++) {
		tmp = combines(k, i) * pow_mod(i, (m - 2) * n, MOD) % MOD;
		m2 = mymin(n - i, (k - i) / 2);
		stmp = 0;
		for(j = 0; j <= m2; j++) {
			if(!i && !j) continue;
			stmp += combines(k - i, j) * combines(k - i - j, j) % MOD * ftable[i + j] % MOD * ftable[i + j] % MOD;
			stmp %= MOD;
		}
		res += tmp * stmp % MOD;
		res %= MOD;
	}
	return res;
}
int main()
{
	scanf("%d%d%d", &n, &m, &k);
	if(m == 1) {
		printf("%d\n", (int)pow_mod(k, n, MOD));
		return 0;
	}
	init();
	printf("%d\n", (int)calc());
	return 0;
}


#include <stdio.h>
#define N 1002
#define MOD 1000000007

typedef long long typec;

int n, m, k;
typec ftable[N];
typec facmod[1000001];

typec extend_gcd(typec a, typec b, typec *x, typec *y)
{
	if(!b) return *x = 1, *y = 0, a;
	typec res = extend_gcd(b, a % b, x, y), tmp = *x;
	*x = *y, *y = tmp - (a / b) * (*y);
	return res;
}

typec inverse(typec a, typec m)
{
	typec x, y;
	y = extend_gcd(a, m, &x, &y);
	while(x < 0) x += m;
	return x;
}

typec pow_mod(typec x, typec k, typec m)
{
	typec res = 1;
	while(x %= m, k) {
		if(k&1) res *= x, res %= m;
		x *= x, k >>= 1;
	}
	return res;
}

int mymin(int x, int y)
{
	return x < y? x : y;
}

typec combines(int i, int j)
{
	if(i < j) return 0;
	return facmod[i] * inverse(facmod[j], MOD) % MOD * inverse(facmod[i - j], MOD) % MOD;
}

void init()
{
	int i, j;
	facmod[0] = 1;
	for(i = 1; i <= 1000000; i++) {
		facmod[i] = facmod[i - 1] * i;
		facmod[i] %= MOD;
	}
	typec sum = 0;
	ftable[1] = 1;
	for(i = 2; i <= n; i++) {
		ftable[i] = pow_mod(i, n, MOD) + MOD;
		sum = 0;
		for(j = 1; j < i; j++) {
			sum += combines(i, j) * ftable[j] % MOD;
			sum %= MOD;
		}
		ftable[i] -= sum;
		ftable[i] %= MOD;
	}
}

typec calc()
{
	int i, j, m1, m2;
	typec res = 0, tmp, stmp;
	m1 = mymin(n, k);
	for(i = 0; i <= m1; i++) {
		tmp = combines(k, i) * pow_mod(i, (m - 2) * n, MOD) % MOD;
		m2 = mymin(n - i, (k - i) / 2);
		stmp = 0;
		for(j = 0; j <= m2; j++) {
			if(!i && !j) continue;
			stmp += combines(k - i, j) * combines(k - i - j, j) % MOD * ftable[i + j] % MOD * ftable[i + j] % MOD;
			stmp %= MOD;
		}
		res += tmp * stmp % MOD;
		res %= MOD;
	}
	return res;
}
int main()
{
	scanf("%d%d%d", &n, &m, &k);
	if(m == 1) {
		printf("%d\n", (int)pow_mod(k, n, MOD));
		return 0;
	}
	init();
	printf("%d\n", (int)calc());
	return 0;
}


#include <stdio.h>
#include <string.h>

#define N	5000
#define INF	0x3f3f3f3f3f3f3f3fLL

long long min(long long a, long long b) { return a < b ? a : b; }
long long max(long long a, long long b) { return a > b ? a : b; }

int main() {
	static long long dp[N];
	int n, a, c, e, i, j;
	long long b, d, s, t, u, ans;

	scanf("%d%lld%d%lld%d%d", &a, &b, &c, &d, &n, &e), a -= e, c -= e;
	memset(dp, 0x3f, n * sizeof *dp), dp[0] = 0;
	ans = INF;
	for (i = 0; i < n; i++) {
		s = dp[i], t = dp[i], u = dp[i], j = i;
		while (j < n) {
			if (j + a + c + e < n)
				dp[j + a + c + e] = min(dp[j + a + c + e], max(s + b, t + d));
			else
				ans = min(ans, max(s + b, t + d));
			if (s + b < t + d)
				j += a, u = s += b;
			else
				j += c, u = t += d;
		}
		ans = min(ans, u);
	}
	printf("%lld\n", ans);
	return 0;
}


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

#define N	1000000

int tt[N + 1];

int compare(const void *a, const void *b) {
	int a_ = *(int *) a;
	int b_ = *(int *) b;

	return tt[a_] != tt[b_] ? tt[a_] - tt[b_] : a_ - b_;
}

int main() {
	static int aa[N - 4];
	int n, k, a, b, i;
	long long ans;

	scanf("%d%d", &n, &k);
	if (k == 1) {
		printf("3\n");
		return 0;
	}
	for (a = 2; a <= n; a++)
		tt[a] = a;
	for (a = 2; a <= n; a++) {
		if (tt[a] != a)
			continue;
		for (b = a; b <= n; b += a)
			tt[b] = tt[b] / a * (a - 1);
	}
	for (a = 5; a <= n; a++)
		aa[a - 5] = a;
	qsort(aa, n - 4, sizeof *aa, compare);
	ans = 6;
	for (i = 0; i < k - 2; i++)
		ans += tt[aa[i]];
	printf("%lld\n", ans);
	return 0;
}


#ifndef _TJYTLXWXHYZQFW_COMMON_H
#define _TJYTLXWXHYZQFW_COMMON_H
#include <assert.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define offset(t,m) ((unsigned long)&(((t*)0)->m))
#define container_of(a,t,m) ((t*)((unsigned long)a-offset(t,m)))
#define ULL1 ((unsigned long long )1)
#define LL(x) ((long long)(x))
#define SWP(a, b) if ((a) != (b)) do {(a) ^= (b); (b) ^= (a); (a) ^= (b);} while(0)
#define IOF(p) ((p) = ((p) < 0 ? INT_MAX : (p)))
#define LLOF(p) ((p) = ((p) < 0 ? LLONG_MAX :(p)))
#define ir(x, start, end) ((x) >= (start) && (x) < (end))
#define ire(x, start, end) ((x) >= (start) && (x) <= (end))
#define max(x,y) ((x) < (y) ? (y) : (x))
#define min(x, y) ((x) < (y) ? (x) : (y))
#define abv(x) ((x) < 0 ? (x) * (-1) : (x))
#define forn(i, n) for ((i) = 0; (i) < (n); (i) += 1)
#define fore(i, n) for ((i) = 0; (i) <= (n); (i) += 1)
#define forr(i, s, e) for((i) = (s); (i) < (e); (i) += 1)
#define forre(i, s, e) for((i) = (s); (i) <= (e); (i) += 1)
#endif
#ifndef _WCC_NUMTH_H
#define _WCC_NUMTH_H
#include <assert.h>
#define NUMTH_ENS (-1)
#define NUMTH_MODPLS(x, y, p) ((((x) % (p)) + ((y) % (p))) % p)
#define NUMTH_MODMUL(x, y, p) ((((X) % (P)) * ((y) % (p))) % p)
#define NUMTH_CORRECT(x, mod) ((x) += ((x) < 0 ? (mod) : 0))
long long numth_modexp(long long a, long long b, long long n);
long long numth_gcd(long long a, long long b);
void numth_gcd_e(long long a, long long b, long long *d, long long *x, long long *y);
int numth_mle(long long a, long long b, long long n, long long *x, long long *gcd);
#define numth_for_each_solution(i, x, n, d) \
	for ((i) = 0; (i) < (d); (i) += 1, (x) = ((x) + (n) / (d)) % (n))
int numth_divisors(long long k, long long divs[], long long *ndivs);
#endif
#define M 1000000007
#define T 100
#define K 1000
#define pls(a, b) NUMTH_MODPLS((a), (b), M)
int dp[2*T][4*T*K+1];
int a, b, t, k;
int delta, delta_min, delta_max, offset, index_max;
int dp_fill(int i, int j)
{
	int x;
	if (j == index_max-k) {
		dp[i][j] = 0;
		forre(x, j-k, j+k) dp[i][j] = pls(dp[i][j], dp[i+1][x]);
	} else {
		dp[i][j] = pls(pls(dp[i+1][j-k], dp[i][j+1]), -1*dp[i+1][j+k+1]);
		NUMTH_CORRECT(dp[i][j], M);
	}
	return dp[i][j];
}
int main(void)
{
	int i, j;
	scanf("%d%d%d%d", &a, &b, &k, &t);
	t <<= 1;
	delta = a - b;
	delta_min = delta - t * k;
	delta_max = delta + t * k;
	offset = -1 * delta_min;
	index_max = delta_max + offset;
	forre(i, 0, index_max) dp[t-1][i] = max(0, k-max(-1*k, offset-i+1)+1);
	for (i = t-2; i >= 0; --i)
		for (j = index_max-k; j >= k; --j)
			dp_fill(i, j);
	printf("%d\n", dp[0][delta+offset]);
	return 0;
}


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

#define N	200000
#define L_	19	/* L_ = ceil(log2(N * 2 + 1)) */
#define N_	(1 << L_)
#define MD	998244353

int min(int a, int b) { return a < b ? a : b; }

long long power(long long a, int k) {
	long long p = 1;

	while (k) {
		if (k & 1)
			p = p * a % MD;
		a = a * a % MD;
		k >>= 1;
	}
	return p;
}

int vv[N + 1], ff[N + 1], gg[N + 1], mobius[N + 1];
int *wwu[L_ + 1], *wwv[L_ + 1], vv_[L_ + 1];

void init() {
	int i, j, l, u, v;

	ff[0] = gg[0] = 1;
	for (i = 1; i <= N; i++) {
		vv[i] = i == 1 ? 1 : (long long) vv[i - MD % i] * (MD / i + 1) % MD;
		ff[i] = (long long) ff[i - 1] * i % MD;
		gg[i] = (long long) gg[i - 1] * vv[i] % MD;
	}
	for (i = 2; i <= N; i++) {
		if (mobius[i])
			continue;
		for (j = i; j <= N; j += i)
			mobius[j]++;
	}
	for (i = 2; i <= N / i; i++)
		for (j = i * i; j <= N; j += i * i)
			mobius[j] = -1;
	for (i = 1; i <= N; i++)
		mobius[i] = mobius[i] == -1 ? 0 : (mobius[i] % 2 == 0 ? 1 : -1);
	u = power(3, MD - 1 >> L_), v = power(u, MD - 2);
	for (l = L_; l > 0; l--) {
		int n = 1 << l, m = n >> 1;

		wwu[l] = (int *) malloc(m * sizeof *wwu[l]);
		wwv[l] = (int *) malloc(m * sizeof *wwv[l]);
		wwu[l][0] = wwv[l][0] = 1;
		for (i = 1; i < m; i++) {
			wwu[l][i] = (long long) wwu[l][i - 1] * u % MD;
			wwv[l][i] = (long long) wwv[l][i - 1] * v % MD;
		}
		vv_[l] = power(1 << l, MD - 2);
		u = (long long) u * u % MD, v = (long long) v * v % MD;
	}
	vv_[0] = 1;
}

int choose(int n, int k) {
	return (long long) ff[n] * gg[k] % MD * gg[n - k] % MD;
}

void ntt_(int *aa, int l, int inverse) {
	if (l) {
		int n = 1 << l, m = n >> 1, *ww = inverse ? wwv[l] : wwu[l], i, j;

		ntt_(aa, l - 1, inverse), ntt_(aa + m, l - 1, inverse);
		for (i = 0; (j = i + m) < n; i++) {
			int a = aa[i], b = (long long) aa[j] * ww[i] % MD;

			if ((aa[i] = a + b) >= MD)
				aa[i] -= MD;
			if ((aa[j] = a - b) < 0)
				aa[j] += MD;
		}
	}
}

void ntt(int *aa, int l, int inverse) {
	int n = 1 << l, i, j;

	for (i = 1, j = 0; i < n; i++) {
		int b = n >> 1, tmp;

		while ((j ^= b) < b)
			b >>= 1;
		if (i < j)
			tmp = aa[i], aa[i] = aa[j], aa[j] = tmp;
	}
	ntt_(aa, l, inverse);
}

void multiply(int *aa, int *bb, int n) {
	static int aa_[N_], bb_[N_];
	int n_, l_, i;

	l_ = 0;
	while (1 << l_ < n + n - 1)
		l_++;
	n_ = 1 << l_;
	for (i = 0; i < n_; i++) {
		aa_[i] = i < n ? aa[i] : 0;
		bb_[i] = i < n ? bb[i] : 0;
	}
	ntt(aa_, l_, 0), ntt(bb_, l_, 0);
	for (i = 0; i < n_; i++)
		aa_[i] = (long long) aa_[i] * bb_[i] % MD;
	ntt(aa_, l_, 1);
	for (i = 0; i < n; i++)
		aa[i] = (long long) aa_[i] * vv_[l_] % MD;
}

void inv(int *aa, int *bb, int n) {
	static int aa_[N_], bb_[N_];
	int m, n_, l_, i;

	if (n == 1) {
		bb[0] = 1;
		return;
	}
	m = (n + 1) / 2;
	inv(aa, bb, m);
	l_ = 0;
	while (1 << l_ < n * 2)
		l_++;
	n_ = 1 << l_;
	for (i = 0; i < n_; i++) {
		aa_[i] = i < n ? aa[i] : 0;
		bb_[i] = i < m ? bb[i] : 0;
	}
	ntt(aa_, l_, 0), ntt(bb_, l_, 0);
	for (i = 0; i < n_; i++)
		aa_[i] = bb_[i] * (MD + 2 - (long long) aa_[i] * bb_[i] % MD) % MD;
	ntt(aa_, l_, 1);
	for (i = 0; i < n; i++)
		bb[i] = (long long) aa_[i] * vv_[l_] % MD;
}

void log_(int *aa, int *bb, int n) {
	static int pp[N + 1], qq[N + 1];
	int i;

	for (i = 0; i < n; i++)
		pp[i] = i + 1 == n ? 0 : (long long) aa[i + 1] * (i + 1) % MD;
	inv(aa, qq, n);
	multiply(pp, qq, n);
	for (i = 0; i < n; i++)
		bb[i] = i == 0 ? 0 : (long long) pp[i - 1] * vv[i] % MD;
}

void exp_(int *aa, int *bb, int n) {
	static int aa_[N + 1], bb_[N + 1], cc_[N + 1];
	int m, i;

	if (n == 1) {
		bb[0] = 1;
		return;
	}
	m = (n + 1) / 2;
	exp_(aa, bb, m);
	for (i = 0; i < n; i++)
		bb_[i] = i < m ? bb[i] : 0;
	log_(bb_, cc_, n);
	for (i = 0; i < n; i++)
		aa_[i] = (aa[i] - cc_[i] + MD) % MD;
	aa_[0] = (aa_[0] + 1) % MD;
	multiply(aa_, bb_, n);
	for (i = 0; i < n; i++)
		bb[i] = aa_[i];
}

int bell[N + 1];

void compute_bell() {
	static int aa[N + 1];
	int i;

	for (i = 1; i <= N; i++)
		aa[i] = gg[i];
	exp_(aa, bell, N + 1);
	for (i = 0; i <= N; i++)
		bell[i] = (long long) bell[i] * ff[i] % MD;
}

int sum(int n, int k) {
	static int aa[N + 1], bb[N + 1];
	int i, ans;

	if (k > n)
		return (bell[n] - 1 + MD) % MD;
	for (i = 0; i <= k; i++)
		aa[i] = power(i, n) * gg[i] % MD;
	for (i = 0; i <= k; i++)
		bb[i] = (long long) (i % 2 == 0 ? 1 : MD - 1) * gg[i] % MD;
	multiply(aa, bb, k + 1);
	ans = MD - 1;
	for (i = 0; i <= k; i++)
		ans = (ans + aa[i]) % MD;
	return ans;
}

int main() {
	int n, k, d, ans;

	init();
	scanf("%d%d", &n, &k);
	if (k == 1 || n == 1) {
		printf("1\n");
		return 0;
	}
	compute_bell();
	ans = 0;
	for (d = 1; d < n; d++)
		if (mobius[d])
			ans = (ans + sum((n + d - 1) / d, k) * mobius[d]) % MD;
	if (ans < 0)
		ans += MD;
	printf("%d\n", ans);
	return 0;
}


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

#define N	200000
#define L_	19	/* L_ = ceil(log2(N * 2 + 1)) */
#define N_	(1 << L_)
#define MD	998244353

int min(int a, int b) { return a < b ? a : b; }

long long power(long long a, int k) {
	long long p = 1;

	while (k) {
		if (k & 1)
			p = p * a % MD;
		a = a * a % MD;
		k >>= 1;
	}
	return p;
}

int vv[N + 1], ff[N + 1], gg[N + 1], mobius[N + 1];
int *wwu[L_ + 1], *wwv[L_ + 1], vv_[L_ + 1];

void init() {
	int i, j, l, u, v;

	ff[0] = gg[0] = 1;
	for (i = 1; i <= N; i++) {
		vv[i] = i == 1 ? 1 : (long long) vv[i - MD % i] * (MD / i + 1) % MD;
		ff[i] = (long long) ff[i - 1] * i % MD;
		gg[i] = (long long) gg[i - 1] * vv[i] % MD;
	}
	for (i = 2; i <= N; i++) {
		if (mobius[i])
			continue;
		for (j = i; j <= N; j += i)
			mobius[j]++;
	}
	for (i = 2; i <= N / i; i++)
		for (j = i * i; j <= N; j += i * i)
			mobius[j] = -1;
	for (i = 1; i <= N; i++)
		mobius[i] = mobius[i] == -1 ? 0 : (mobius[i] % 2 == 0 ? 1 : -1);
	u = power(3, MD - 1 >> L_), v = power(u, MD - 2);
	for (l = L_; l > 0; l--) {
		int n = 1 << l, m = n >> 1;

		wwu[l] = (int *) malloc(m * sizeof *wwu[l]);
		wwv[l] = (int *) malloc(m * sizeof *wwv[l]);
		wwu[l][0] = wwv[l][0] = 1;
		for (i = 1; i < m; i++) {
			wwu[l][i] = (long long) wwu[l][i - 1] * u % MD;
			wwv[l][i] = (long long) wwv[l][i - 1] * v % MD;
		}
		vv_[l] = power(1 << l, MD - 2);
		u = (long long) u * u % MD, v = (long long) v * v % MD;
	}
	vv_[0] = 1;
}

int choose(int n, int k) {
	return (long long) ff[n] * gg[k] % MD * gg[n - k] % MD;
}

void ntt_(int *aa, int l, int inverse) {
	if (l) {
		int n = 1 << l, m = n >> 1, *ww = inverse ? wwv[l] : wwu[l], i, j;

		ntt_(aa, l - 1, inverse), ntt_(aa + m, l - 1, inverse);
		for (i = 0; (j = i + m) < n; i++) {
			int a = aa[i], b = (long long) aa[j] * ww[i] % MD;

			if ((aa[i] = a + b) >= MD)
				aa[i] -= MD;
			if ((aa[j] = a - b) < 0)
				aa[j] += MD;
		}
	}
}

void ntt(int *aa, int l, int inverse) {
	int n = 1 << l, i, j;

	for (i = 1, j = 0; i < n; i++) {
		int b = n >> 1, tmp;

		while ((j ^= b) < b)
			b >>= 1;
		if (i < j)
			tmp = aa[i], aa[i] = aa[j], aa[j] = tmp;
	}
	ntt_(aa, l, inverse);
}

int solve(int n, int k) {
	static int aa[N_], bb[N_];
	int n_, l_, i, ans;

	k = min(k, n);
	l_ = 0;
	while (1 << l_ < k + k + 1)
		l_++;
	n_ = 1 << l_;
	for (i = 0; i < n_; i++)
		aa[i] = i <= k ? (long long) power(i, n) * gg[i] % MD : 0;
	for (i = 0; i < n_; i++)
		bb[i] = i <= k ? (long long) (i % 2 == 0 ? 1 : MD - 1) * gg[i] % MD : 0;
	ntt(aa, l_, 0), ntt(bb, l_, 0);
	for (i = 0; i < n_; i++)
		aa[i] = (long long) aa[i] * bb[i] % MD;
	ntt(aa, l_, 1);
	ans = MD - 1;
	for (i = 0; i <= k; i++)
		ans = (ans + (long long) aa[i] * vv_[l_]) % MD;
	return ans;
}

int main() {
	int n, k, d, ans;

	init();
	scanf("%d%d", &n, &k);
	if (k == 1 || n == 1) {
		printf("1\n");
		return 0;
	}
	ans = 0;
	for (d = 1; d < n; d++)
		if (mobius[d])
			ans = (ans + solve((n + d - 1) / d, k) * mobius[d]) % MD;
	if (ans < 0)
		ans += MD;
	printf("%d\n", ans);
	return 0;
}


long long n,A,D=1;main(){scanf("%lld",&n);A=n-1;for(long long i=2;i<n;i*=2,D*=2)A+=(n-1)/i*D;printf("%lld\n",A);}


#include<stdio.h>
#include<stdlib.h>
typedef long long unsigned llu;
typedef unsigned u;
typedef struct S
{
	u val,ki,ka,min,max;llu sum;
	struct S *lc,*rc;
}v;
v*alloc(u ki,u ka,u val)
{
	v*x;
	x=(v*)malloc(sizeof(v));
	x->val=val;
	x->min=val;
	x->max=val;
	x->ki=ki;
	x->ka=ka;
	x->sum=(ka-ki+1)*(llu)val;
	x->lc=x->rc=NULL;
	return x;
}
llu que(v *x,u lo,u hi)
{
//	printf("que %u %u\n",lo,hi);
	if(hi<x->ki||lo>x->ka)
	{
//		printf("return 0\n");
		return 0;
	}
	if(lo<=x->ki&&hi>=x->ka)
	{
//		printf("Return %I64u\n",x->sum);
		return x->sum;
	}
	u ll,rr;
	if(x->val)
	{
		ll=lo<x->ki?x->ki:lo;
		rr=hi>x->ka?x->ka:hi;
		if(rr<ll)
		{
//			printf("return -0\n");
			return 0;
		}
//		printf("ll=%u rr=%u x->val=%u\n",ll,rr,x->val);
		return(rr-ll+1)*(llu)x->val;
	}
	llu a=que(x->lc,lo,hi)+que(x->rc,lo,hi);
//	printf("Retu5n %I64u\n",a);
	return a;
}
void set(v *x,u lo,u hi,u val)
{
//	printf("set [%u %u] %u %u %u min %u\n",x->ki,x->ka,lo,hi,val,x->min);
	if(hi<x->ki||lo>x->ka||x->min>=val)
	{
//		printf("return %u<%u || %u>%u || %u>=%u\n",hi,x->ki,lo,x->ka,x->min,val);
		return;
	}
	if(lo<=x->ki&&hi>=x->ka&&x->max<=val)
	{
//		printf("chg %u %u -> %u\n",x->ki,x->ka,val);
		x->val=x->min=x->max=val;
		x->sum=val*(llu)(x->ka-x->ki+1);
		return;
	}
	u m;
	if(x->val)
	{
		if(val<=x->val)
		{
//			printf("small return\n");
			return;
		}
		m=(x->ki+x->ka)>>1;
//		printf("split! m=%u x->val=%u val=%u\n",m,x->val,val);
		if(!x->lc)x->lc=alloc(x->ki,m,x->val);
		else{x->lc->val=x->lc->min=x->lc->max=x->val;x->lc->sum=(m-x->ki+1)*(llu)x->val;}
		if(!x->rc)x->rc=alloc(m+1,x->ka,x->val);
		else{x->rc->val=x->rc->min=x->rc->max=x->val;x->rc->sum=(x->ka-m)*(llu)x->val;}//grr! damn bug :( took hours to search it!
//		printf("lc_val: %u lc_min: %u val: %u min: %u\n",x->lc->val,x->lc->min,x->val,x->min);
		x->val=0;
	}
	set(x->lc,lo,hi,val);
	set(x->rc,lo,hi,val);
	if(x->lc->val&&x->lc->val==x->rc->val)
	{
//		printf("unit!\n");
		x->val=x->lc->val;
		x->sum=x->val*(llu)(x->ka-x->ki+1);
	}
	else x->sum=x->lc->sum+x->rc->sum;
	x->min=x->lc->min;
	x->max=x->rc->max;
	return;
}
v *p;
u A[222222],F[222222],G[222222];
int main()
{
	u n,h,i,j,k,x;llu r;
	scanf("%u",&n);
	if(n<=2){printf("0\n");return 0;}
	p=alloc(0,n,1);
	for(i=0;++i<=n;A[i]=x)
	{
		scanf("%u",&x);h=0;
		for(j=1;j*j<=x;++j)if(x%j==0)
		{
			if(!F[j])F[j]=i;
			else if(h<j)h=j;
			if(j!=(k=x/j))
			{
				if(!F[k])F[k]=i;
				else if(h<k)h=k;
			}
		}
//		printf("set %u %u %u\n",i,n,h);
		set(p,i,n-1,h);
	}
	r=que(p,2,n-1);
//	printf("r=%I64u\n",r);
	for(i=n;i>1;--i)
	{
		x=A[i];h=0;
		for(j=1;j*j<=x;++j)if(x%j==0)
		{
			if(!G[j])G[j]=i;
			else if(h<j)h=j;
			if(j!=(k=x/j))
			{
				if(!G[k])G[k]=i;
				else if(h<k)h=k;
			}
		}
//		printf("set %u %u %u\n",0,i-1,h);
		set(p,0,i-1,h);
		for(x=A[i],j=1;j*j<=x;++j)if(x%j==0)
		{
			if(F[j]&&F[j]<i)
			{
//				printf("set %u %u %u\n",F[j],i-1,j);
				set(p,F[j],i-1,j);
			}
			if(F[k=x/j]&&F[k]<i)
			{
//				printf("set %u %u %u\n",F[k],i-1,k);
				set(p,F[k],i-1,k);
			}
		}
//		printf("que %u %u\n",i==n?1:0,i-2);
//		if(i==12)
//		{
//			while(1)
//			{
//				scanf("%u%u",&i,&j);
//				printf("%I64u\n",que(p,i,j));
//			}
//		}
		r+=que(p,i==n?1:0,i-2);
//		printf("i=%u r=%I64u\n",i,r);
	}
	printf("%I64u\n",r);
	return 0;
}


#include<stdio.h>
#include<stdlib.h>
typedef long long unsigned llu;
typedef unsigned u;
typedef struct S
{
	u val,ki,ka,min,max;llu sum;
	struct S *lc,*rc;
}v;
v*alloc(u ki,u ka,u val)
{
	v*x;
	x=(v*)malloc(sizeof(v));
	x->val=val;
	x->min=val;
	x->max=val;
	x->ki=ki;
	x->ka=ka;
	x->sum=(ka-ki+1)*(llu)val;
	x->lc=x->rc=NULL;
	return x;
}
llu que(v *x,u lo,u hi)
{
	if(hi<x->ki||lo>x->ka)return 0;
	if(lo<=x->ki&&hi>=x->ka)return x->sum;
	u ll,rr;
	if(x->val)
	{
		ll=lo<x->ki?x->ki:lo;
		rr=hi>x->ka?x->ka:hi;
		if(rr<ll)return 0;
		return(rr-ll+1)*(llu)x->val;
	}
	llu a=que(x->lc,lo,hi)+que(x->rc,lo,hi);
	return a;
}
void set(v *x,u lo,u hi,u val)
{
	if(hi<x->ki||lo>x->ka||x->min>=val)
	{
		return;
	}
	if(lo<=x->ki&&hi>=x->ka&&x->max<=val)
	{
		x->val=x->min=x->max=val;
		x->sum=val*(llu)(x->ka-x->ki+1);
		return;
	}
	u m;
	if(x->val)
	{
		if(val<=x->val)return;
		m=(x->ki+x->ka)>>1;
		if(!x->lc)x->lc=alloc(x->ki,m,x->val);
		else
		{
			x->lc->val=x->lc->min=x->lc->max=x->val;
			x->lc->sum=(m-x->ki+1)*(llu)x->val;
		}
		if(!x->rc)x->rc=alloc(m+1,x->ka,x->val);
		else
		{
			x->rc->val=x->rc->min=x->rc->max=x->val;
			x->rc->sum=(x->ka-m)*(llu)x->val;
		}
		x->val=0;
	}
	set(x->lc,lo,hi,val);
	set(x->rc,lo,hi,val);
	if(x->lc->val&&x->lc->val==x->rc->val)
	{
		x->val=x->lc->val;
		x->sum=x->val*(llu)(x->ka-x->ki+1);
	}
	else x->sum=x->lc->sum+x->rc->sum;
	x->min=x->lc->min;
	x->max=x->rc->max;
	return;
}
v *p;
u A[222222],F[222222],G[222222];
int main()
{
	u n,h,i,j,k,x;llu r;
	scanf("%u",&n);
	p=alloc(0,n,1);
	for(i=0;++i<=n;A[i]=x)
	{
		scanf("%u",&x);h=0;
		for(j=1;j*j<=x;++j)if(x%j==0)
		{
			if(!F[j])F[j]=i;
			else if(h<j)h=j;
			if(j!=(k=x/j))
			{
				if(!F[k])F[k]=i;
				else if(h<k)h=k;
			}
		}
		set(p,i,n-1,h);
	}
	r=que(p,2,n-1);
	for(i=n;i>1;--i)
	{
		x=A[i];h=0;
		for(j=1;j*j<=x;++j)if(x%j==0)
		{
			if(!G[j])G[j]=i;
			else if(h<j)h=j;
			if(j!=(k=x/j))
			{
				if(!G[k])G[k]=i;
				else if(h<k)h=k;
			}
		}
		set(p,0,i-1,h);
		for(x=A[i],j=1;j*j<=x;++j)if(x%j==0)
		{
			if(F[j]&&F[j]<i)set(p,F[j],i-1,j);
			if(F[k=x/j]&&F[k]<i)set(p,F[k],i-1,k);
		}
		r+=que(p,i==n?1:0,i-2);
	}
	printf("%I64u\n",r);
	return 0;
}


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

int main()
{
    int n, m, x1, y1, x2, y2, x, y;

    scanf("%d %d %d %d %d %d", &n, &m, &x1, &y1, &x2, &y2);

    x = abs(x1 - x2);
    y = abs(y1 - y2);

    if (x > y) {
        int tmp = x;
        x = y;
        y = tmp;
    }

    if ((x <= 2 && y <= 4) || (x == 3 && y == 3)) {
        puts("First");
    } else {
        puts("Second");
    }

    return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)

int ab(int a){if(a<0)return -a; return a;}

int main(){
  int i,j,k,l,m,n;
  int x, y, x1, x2, y1, y2;
  int dx, dy;

  scanf("%d%d%d%d%d%d",&x,&y,&x1,&y1,&x2,&y2);

  dx = ab(x1-x2);
  dy = ab(y1-y2);

  if(dx <= 4 && dy <= 4 && dx+dy <= 6) puts("First");
  else puts("Second");

  return 0;
}


#include<stdio.h>
#include<stdbool.h>
#define sq(A) ((A)*(A))

int x[4], y[4], r[4];

bool intersect(int i, int j){
	int dist=sq(x[i]-x[j])+sq(y[i]-y[j]);
	if(dist>=sq(r[i]+r[j])) return false;
	if(r[i]<r[j] && dist<=sq(r[j]-r[i])) return false;
	if(r[j]<r[i] && dist<=sq(r[i]-r[j])) return false;
	return true;
}

bool in(int i, int j){
	int dist=sq(x[i]-x[j])+sq(y[i]-y[j]);
	if(r[i]<r[j] && dist<=sq(r[j]-r[i])) return false;
	return true;
}

int main(){
	int num=0;
	scanf("%d %d %d %d", x, y, r, r+1);
	x[1]=x[0]; y[1]=y[0];
	scanf("%d %d %d %d", x+2, y+2, r+2, r+3);
	x[3]=x[2]; y[3]=y[2];
	if(!intersect(0, 2) && !intersect(0, 3) && !(in(0, 2) ^ in(0, 3))) ++num;
	if(!intersect(1, 2) && !intersect(1, 3) && !(in(1, 2) ^ in(1, 3))) ++num;
	if(!intersect(2, 0) && !intersect(2, 1) && !(in(2, 0) ^ in(2, 1))) ++num;
	if(!intersect(3, 0) && !intersect(3, 1) && !(in(3, 0) ^ in(3, 1))) ++num;
	printf("%d\n", num);
	return 0;
}


#include <stdio.h>
#include <assert.h>

int n;

double p(double a, double e) {
    return a / e;
}
double q(double a, double b, double c, double d, double e, double f) {
    return (a / d) * (b / e) * (c / f);
}

double solve(int k) {
    const double q3 = q(k, k-1, k-2, n, n-1, n-2);
    const double q0 = q(n-k, n-k-1, n-k-2, n, n-1, n-2);
    const double q1 = 3 / 2. * q(k, n-k, n-k-1, n, n-1, n-2);
    const double q2 = 3 * q(k, k-1, n-k, n, n-1, n-2);
    const double res = q3 + q1 + q2;
    //printf("solve(%d) = %lf\n", k, res);
    //assert(q3 + q0 + 3 / 2. * q1 + 3 * q2 == 1.);
    assert(res >= 0.);
    assert(res <= 1.);
    return res;
}

int main(void) {
    double p;
    scanf("%d%lf", &n, &p);
    int l = 0, r = n - 1;
    if (p == 0) {
        puts("0");
        return 0;
    }
    while (l + 1 < r) {
        const int m = (l + r) >> 1;
        if (solve(m) >= p) {
            r = m;
        } else {
            l = m;
        }
    }
    assert(r > 0);
    assert(r < n);
    printf("%d\n", r);
}


#include<stdio.h>
long long dp(int,int,char);
long long dar[1003][102],MOD,powmod[1003],modmod[1003];
int n,k;
int main(){
	int i,j,l;
	scanf("%d %d %d",&n,&k,&i);
	MOD=(long long)i;
	powmod[0]=1%k;
	modmod[0]=1%MOD;
	for(i=1;i<=n;i++){
		powmod[i]=(powmod[i-1]*10)%k;
		modmod[i]=(modmod[i-1]*10)%MOD;
	}
	for(i=0;i<1003;i++){
		for(j=0;j<102;j++){
			dar[i][j]=-1;
		}
	}
	dar[n][0]=1;
	for(i=n-1;i>0;i--){
		dar[i][0]=(9*modmod[n-1-i])%MOD;
	}
	printf("%d\n",(int)dp(0,0,0));
	return 0;
}

long long dp(int dig,int mod,char fl){
	long long toret;
	int i;
	if(dig>n){
		return 0;
	}
	if(fl&&mod==0){
		return dar[dig][0];
	}
	if(fl==0||dar[dig][mod]==-1){
		toret=0;
		toret+=dp(dig+1,mod,fl);
		for(i=1;i<10;i++){
			toret=(toret+dp(dig+1,(mod+i*powmod[dig])%k,1))%MOD;
		}
		if(fl!=0){
			dar[dig][mod]=toret;
		}
	}
	else{
		toret=dar[dig][mod];
	}
	return toret;
}


#include<stdio.h>
/*#include<limits.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<vector>
#include<assert.h>
#include <map>
#include<set>
#include<stack>
#include<queue>
#include<algorithm>
#define PB push_back
#define MP make_pair
#define clr(x) x.clear()
#define sz(x) ((int)(x).size())
#define F first
#define S second*/
#define rep(i,b) for(i=0;i<b;i++)
#define rep1(i,b) for(i=1;i<=b;i++)
#define mod 1000000007
#define pi(n) printf("%d",n)
#define pin(n) printf("%d\n",n)
#define piw(n) printf("%d ",n)
#define pll(n) printf("%lld",n)
#define plln(n) printf("%lld\n",n)
#define pllw(n) printf("%lld ",n)
#define sll(n) scanf("%lld",&n)
#define ss(s) scanf("%s",s)
#define ps(s) printf("%s",s)
#define psn(s) printf("%s\n",s)
#define psw(s) printf("%s ",s)
#define si(n) scanf("%d",&n)
#define pn printf("\n")
#define pw printf(" ")
//using namespace std;
typedef long long int ll;
ll k,m,dp[1005][105][2];int n;
ll poo(ll a,ll b)
{
 ll res=1;
 while(b)
 {
  if(b&1)
  res=(res*1LL*a)%m;

  a=(a*1LL*a)%m;
  b/=2;
 }
 return res;
}
ll pok(ll a,ll b)
{
 ll res=1;
 while(b)
 {
  if(b&1)
  res=(res*1LL*a)%k;

  a=(a*1LL*a)%k;
  b/=2;
 }
 return res;
}
ll rec(int i,int rem,int f){int j;
//assert(rem>=0&&rem<100);
//cout<<i<<" "<<rem;pn;
if(i>=0){
if(dp[i][rem][f]!=-1){return dp[i][rem][f];}
}

if(i!=n-1&&rem==0&&f){if(i<0){return 1;}return dp[i][rem][f]=(9*poo(10,i))%m;}
if(i<0){return 0;}

ll ans=0;
if(i){

for(j=0;j<10;j++){if(j==0){ans+=rec(i-1,(rem+(j*pok(10,n-i-1))%k)%k,f);}
else{ans+=rec(i-1,(rem+(j*pok(10,n-i-1))%k)%k,1);}ans%=m;}
}
else{
for(j=1;j<10;j++){ans+=rec(i-1,(rem+(j*pok(10,n-i-1))%k)%k,1);ans%=m;}
}
return dp[i][rem][f]=ans;

}

int main(){
    int i,j;
    si(n);sll(k);sll(m);
    rep(i,1003){rep(j,103){dp[i][j][0]=-1;dp[i][j][1]=-1;}}
    pll(rec(n-1,0,0)%m);

return 0;
}


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

int main()
{
    long times,n,value,lvalue,ans,g,maxg=0,v[46],i,j,k;
    scanf("%ld%ld%ld",&times,&n,&value);
    for (i=1;i<=n;i++)
        scanf("%ld",&v[i]);
    for (i=1;i<n;i++)
        for (j=i+1;j<=n;j++)
            if (v[i]>v[j])
            {
                k=v[i];
                v[i]=v[j];
                v[j]=k;
            }
    ans=0;
    for (i=1;i<=n;i++)
        ans+=v[i];
    for (i=0;i<=times;i++)
        if (value-ans*i>=0)
        {
            g=(n+1)*i;
            lvalue=value-ans*i;
            for (j=1;j<n;j++)//can't solve any problem more
                for (k=1;k<=times-i;k++)
                    if (lvalue-v[j]>=0)
                    {
                        g++;
                        lvalue-=v[j];
                    }
                    else
                        break;
            if (g>maxg)
                maxg=g;
        }
        else
            break;
    printf("%ld\n",maxg);
    return 0;
}


#include <stdio.h>
#include <string.h>

#define N	6
#define M	6
#define B	(5 * 5 * 5 * 5 * 5 * 5)
#define INF	0x3f3f3f3f

int min(int a, int b) { return a < b ? a : b; }

int sum_[B], kk[B][M], p5[M + 1];

void init() {
	int b, j;

	p5[0] = 1;
	for (j = 1; j <= M; j++)
		p5[j] = p5[j - 1] * 5;
	for (b = 0; b < B; b++)
		for (j = 0; j < M; j++)
			sum_[b] += kk[b][j] = b / p5[j] % 5;
}

int main() {
	static int aa[N], bb[M], cc[N][M], dp[B];
	int n, m, i, j, k, l, b, sum, ans;

	init();
	scanf("%d%d", &n, &m);
	sum = 0;
	for (i = 0; i < n; i++) {
		scanf("%d", &aa[i]);
		sum += aa[i];
	}
	for (j = 0; j < m; j++) {
		scanf("%d", &bb[j]);
		sum -= bb[j];
	}
	if (sum > 0) {
		printf("-1\n");
		return 0;
	}
	for (i = 0; i < n; i++)
		for (j = 0; j < m; j++)
			scanf("%d", &cc[i][j]);
	b = 0, sum = 0;
	for (j = m - 1; j >= 0; j--)
		b = b * 5 + bb[j], sum += bb[j];
	memset(dp, 0x3f, B * sizeof *dp), dp[b] = 0;
	for (i = 0; i < 6; i++) {
		for (j = 0; j < 6; j++)
			for (b = 0; b < B; b++) {
				int x = dp[b];

				if (x == INF)
					continue;
				k = kk[b][j];
				for (l = 1; l <= k; l++)
					dp[b - l * p5[j]] = min(dp[b - l * p5[j]], x + cc[i][j]);
			}
		sum -= aa[i];
		for (b = 0; b < B; b++)
			if (sum_[b] != sum)
				dp[b] = INF;
	}
	ans = INF;
	for (b = 0; b < B; b++)
		ans = min(ans, dp[b]);
	printf("%d\n", ans);
	return 0;
}


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

#define K	1000000
#define L_	21	/* L_ = ceil(log2(K * 2 + 1)) */
#define N_	(1 << L_)
#define MD	998244353

long long power(long long a, int k) {
	long long p = 1;

	while (k) {
		if (k & 1)
			p = p * a % MD;
		a = a * a % MD;
		k >>= 1;
	}
	return p;
}

int inv(int a) {
	return a == 1 ? 1 : (long long) inv(a - MD % a) * (MD / a + 1) % MD;
}

int vv[K + 1], ff[K + 1], gg[K + 1];
int *wu[L_ + 1], *wv[L_ + 1];

void init() {
	int i, l, u, v;

	ff[0] = gg[0] = 1;
	for (i = 1; i <= K; i++) {
		vv[i] = i == 1 ? 1 : (long long) vv[i - MD % i] * (MD / i + 1) % MD;
		ff[i] = (long long) ff[i - 1] * i % MD;
		gg[i] = (long long) gg[i - 1] * vv[i] % MD;
	}
	u = power(3, MD - 1 >> L_), v = power(u, MD - 2);
	for (l = L_; l > 0; l--) {
		int n = 1 << l - 1;

		wu[l] = (int *) malloc(n * sizeof *wu[l]);
		wv[l] = (int *) malloc(n * sizeof *wv[l]);
		wu[l][0] = wv[l][0] = 1;
		for (i = 1; i < n; i++) {
			wu[l][i] = (long long) wu[l][i - 1] * u % MD;
			wv[l][i] = (long long) wv[l][i - 1] * v % MD;
		}
		u = (long long) u * u % MD, v = (long long) v * v % MD;
	}
}

int choose(int n, int k) {
	return k < 0 || k > n ? 0 : (long long) ff[n] * gg[k] % MD * gg[n - k] % MD;
}

void ntt_(int *aa, int l, int inverse) {
	if (l > 0) {
		int n = 1 << l, m = n >> 1, i, j, *ww = inverse ? wv[l] : wu[l];

		ntt_(aa, l - 1, inverse), ntt_(aa + m, l - 1, inverse);
		for (i = 0; (j = i + m) < n; i++) {
			int a = aa[i], b = (long long) aa[j] * ww[i] % MD;

			if ((aa[i] = a + b) >= MD)
				aa[i] -= MD;
			if ((aa[j] = a - b) < 0)
				aa[j] += MD;
		}
	}
}

void ntt(int *aa, int l_, int inverse) {
	int n = 1 << l_, i, j;

	for (i = 1, j = 0; i < n; i++) {
		int b, tmp;

		b = n >> 1;
		while ((j ^= b) < b)
			b >>= 1;
		if (i < j)
			tmp = aa[i], aa[i] = aa[j], aa[j] = tmp;
	}
	ntt_(aa, l_, inverse);
}

int main() {
#if 0
	static int dp[11][11], dq[11][11];
	int n = 10, l = 3, i, j, k;

	init();
	dp[0][0] = 1, dq[0][0] = 0;
	for (i = 1; i <= n; i++) {
		for (j = i; j <= n; j++)
			for (k = 0; k <= j; k++) {
				dp[i][j] = (dp[i][j] + (long long) dp[i - 1][k] * choose(j, k)) % MD;
				dq[i][j] = (dq[i][j] + (long long) dq[i - 1][k] * choose(j, k) + (long long) dp[i - 1][k] * choose(j, k) % MD * choose(j - k, l)) % MD;
			}
		printf("(%d %d)\n", dp[i][i], dq[i][i]);
	}
	/* guess: (# of more-equal arrays with a_1 = a_2 = ... = a_l) == (n + 1)^(n - l) */
#else
	static int aa[N_], bb[N_];
	int n, k, n_, l_, i, v, f, p, ans;

	init();
	scanf("%d%d", &n, &k);
	l_ = 0;
	while (1 << l_ <= k + k)
		l_++;
	n_ = 1 << l_;
	for (i = 0; i <= k; i++)
		aa[i] = (long long) (i % 2 == 0 ? 1 : MD - 1) * gg[i] % MD;
	for (i = 0; i <= k; i++)
		bb[i] = power(i, k) * gg[i] % MD;
	ntt(aa, l_, 0), ntt(bb, l_, 0);
	for (i = 0; i < n_; i++)
		aa[i] = (long long) aa[i] * bb[i] % MD;
	ntt(aa, l_, 1);
	v = inv(n_);
	for (i = 0; i < n_; i++)
		aa[i] = (long long) aa[i] * v % MD;
	ans = 0;
	f = 1, p = power(n + 1, n), v = inv(n + 1);
	for (i = 1; i <= k; i++) {
		f = (long long) f * (n - i + 1) % MD;
		p = (long long) p * v % MD;
		ans = (ans + (long long) aa[i] * f % MD * p) % MD;
	}
	printf("%d\n", ans);
#endif
	return 0;
}


#include <stdio.h>

typedef struct {
  int l, r;
} interval;

int max(int x, int y) {
  return x > y ? x : y;
}

int min(int x, int y) {
  return x < y ? x : y;
}

int interval_intersection_length(const interval *a, const interval *b) {
  return max(0, min(a->r, b->r) - max(a->l, b->l) + 1);
}

void extract(const interval *a, interval *a1, interval *a2, int i) {
  int s = 1 << i;
  int x = (a->l + s - 1) >> i << i;
  int y = a->r >> i << i;
  if (x < y) {
    a1->l = a2->l = 1;
    a1->r = a2->r = s - 1;
  } else if (x == y) {
    a1->l = a->l % s;
    if (a1->l == 0) a1->l = s;
    a1->r = s - 1;
    a2->l = 1;
    a2->r = a->r % s;
  } else {
    a1->l = a2->l = a->l % s;
    a1->r = a2->r = a->r % s;
  }
}

int main() {
  int i, ans;
  interval a, b, a1, a2, b1, b2;
  scanf("%d %d %d %d", &a.l, &a.r, &b.l, &b.r);
  ans = 0;
  for (i = 1; i <= 30; i++) {
    extract(&a, &a1, &a2, i);
    extract(&b, &b1, &b2, i);
    ans = max(ans, interval_intersection_length(&a1, &b1));
    ans = max(ans, interval_intersection_length(&a1, &b2));
    ans = max(ans, interval_intersection_length(&a2, &b1));
    ans = max(ans, interval_intersection_length(&a2, &b2));
  }
  printf("%d\n", ans);
  return 0;
}


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

#define N	35
#define K	(1 << ((N + 1) / 2))

int compare(const void *a, const void *b) {
	int ia = *(int *) a;
	int ib = *(int *) b;

	return ia - ib;
}

int main() {
	static int aa[N], ss[K], tt[K];
	int n, m, k1, k2, h, i, j, b, s, max;

	scanf("%d%d", &n, &m);
	h = n / 2;
	for (i = 0; i < n; i++)
		scanf("%d", &aa[i]);
	k1 = 1 << h;
	for (b = 0; b < k1; b++) {
		s = 0;
		for (i = 0; i < h; i++)
			if (b & 1 << i)
				s = (s + aa[i]) % m;
		ss[b] = s;
	}
	qsort(ss, k1, sizeof *ss, compare);
	k2 = 1 << (n - h);
	for (b = 0; b < k2; b++) {
		s = 0;
		for (i = h; i < n; i++)
			if (b & 1 << (i - h))
				s = (s + aa[i]) % m;
		tt[b] = s;
	}
	qsort(tt, k2, sizeof *tt, compare);
	max = 0;
	for (i = 0, j = k2 - 1; i < k1; i++) {
		while (j >= 0 && ss[i] + tt[j] >= m)
			j--;
		if (j >= 0 && max < ss[i] + tt[j])
			max = ss[i] + tt[j];
	}
	printf("%d\n", max);
	return 0;
}


#include <stdio.h>

int main(){

	int n,m;
	scanf("%d %d",&n,&m);
	int b;
	if(n>m){
		b=n;
		n=m;
		m=b;
	}
	if(n==1){
		int mo=m%6;
		switch(mo){
			case 0:printf("%d\n",m);break;
			case 1:printf("%d\n",m-mo);break;
			case 2:printf("%d\n",m-mo);break;
			case 3:printf("%d\n",m-mo);break;
			case 4:printf("%d\n",m-2);break;
			case 5:printf("%d\n",m-1);
		}
	}
	else if(n==2){
		if(m==2){
			printf("%d\n",0);
		}
		else if(m==3){
			printf("%d\n",4);
		}
		else if(m==7){
			printf("%d\n",12);
		}
		else{
			printf("%d\n",n*m);
		}
	}
	else{
		long long mul=n;
		mul*=m;
		if(mul%2==0){
			printf("%lld\n",mul);
		}
		else{
			printf("%lld\n",mul-1);
		}
	}

	return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdbool.h>

#define clr(ar) memset(ar, 0, sizeof(ar))
#define read() freopen("lol.txt", "r", stdin)

char str[77];
int n, vmax, kmax, xmax, ar[77], dp[77][77][77][3];

int fill(char visited[77], int c, int d){
	int i, x = 0;
	for (i = 0; i < n; i++){
		if (ar[i] == d){
			if (x == c) return i;
			x++;
			visited[i] = 1;
		}
	}
	return n;
}

int swap_count(int index, char visited[77]){
	int i, res = 0;
	for (i = 0; i < index; i++){
		if (!visited[i]) res++;
	}
	return res;
}

int F(int v, int k, int x, int l){
	if (v == vmax && k == kmax && x == xmax) return 0;
	if (dp[v][k][x][l] != -1) return dp[v][k][x][l];

	char visited[77] = {0};
	int a, b, c, r, res = 1 << 20;
	a = fill(visited, v, 0);
	b = fill(visited, k, 1);
	c = fill(visited, x, 2);

	if (v != vmax){
		r = F(v + 1, k, x, 0) + swap_count(a, visited);
		if (r < res) res = r;
	}
	if (k != kmax && l != 0){
		r = F(v, k + 1, x, 1) + swap_count(b, visited);
		if (r < res) res = r;
	}
	if (x != xmax){
		r = F(v, k, x + 1, 2) + swap_count(c, visited);
		if (r < res) res = r;
	}

	return (dp[v][k][x][l] = res);
}

int main(){
	int i, res;

	while (scanf("%d", &n) != EOF){
		scanf("%s", str);
		for (i = 0; i < n; i++){
			ar[i] = 2;
			if (str[i] == 'V') ar[i] = 0;
			if (str[i] == 'K') ar[i] = 1;
			vmax += (ar[i] == 0);
			kmax += (ar[i] == 1);
			xmax += (ar[i] == 2);
		}

		memset(dp, -1, sizeof(dp));
		res = F(0, 0, 0, 2);
		printf("%d\n", res);
	}
	return 0;
}


/* http://catb.org/esr/intercal/ick.htm */
/* find xor of largest and last array elements */
#include <stdio.h>

#define N	10

int main() {
	static int aa[N];
	int n, i, max;

	scanf("%d", &n);
	max = 0;
	for (i = 0; i < n; i++) {
		scanf("%d", &aa[i]);
		if (max < aa[i])
			max = aa[i];
	}
	printf("%d\n", max ^ aa[n - 1]);
	return 0;
}


#import<stdio.h>
#define swap(a,b) (a)^=(b)^=(a)^=(b)
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
long long gcd(long long a,long long b) {return !b?a:gcd(b,a%b);}
int main()
{
	long long l1,r1,t1,l2,r2,t2;
	scanf("%lld%lld%lld%lld%lld%lld",&l1,&r1,&t1,&l2,&r2,&t2);
	if (l1>l2) swap(l1,l2),swap(r1,r2),swap(t1,t2);
	long long k=gcd(t1,t2),p,ans=0;
	p=(l2-l1)/k;l1+=k*p,r1+=k*p;
	ans=max(ans,(min(r1,r2)-max(l1,l2))+1);
	l1+=k,r1+=k;
	ans=max(ans,(min(r1,r2)-max(l1,l2))+1);
	printf("%lld\n",ans);
	return 0;
}


#include<stdio.h>
#define N 1050000
#define M 2300000
#define T 1048576
int n,m;
int first[M];
int second[M];
int last[N];
int wei[N];
int pei[N];
int ans[N];
void add(int a[],int x)
{
    x+=T-1;
    while(x)
    {
        a[x]++;
        x>>=1;
    }
}
void del(int a[],int x)
{
    x+=T-1;
    while(x)
    {
        a[x]--;
        x>>=1;
    }
}
int find(int a[],int now,int l,int r,int x)
{
    if(l==r)
        return l;
    int mid=(l+r)/2;
    if(x<=a[now<<1])
        return( find(a,now<<1,l,mid,x) );
    return( find(a,now*2+1,mid+1,r,x-a[now<<1]) );
}
int main()
{
    int i,a,b;
    scanf("%d%d",&n,&m);
    for(i=1;i<=n;i++)
        add(second,i);
    for(i=1;i<=m;i++)
    {
        scanf("%d%d",&a,&b);
        if(first[1]>=b)
            b=wei[find(first,1,1,T,first[1]-b+1)];
        else
            b=find(second,1,1,T,b-first[1]);
        if(ans[b]==a)
        {
            wei[i]=b;
            del(first,last[b]);
            last[b]=i;
            add(first,i);
            continue;
        }
        if(ans[b] || pei[a])
        {
            printf("-1\n");
            return 0;
        }
        ans[b]=a;
        pei[a]=b;
        last[b]=i;
        wei[i]=b;
        del(second,b);
        add(first,i);
    }
    int now=0;
    for(i=1;i<=n;i++)
        if(!ans[i])
        {
            now++;
            while(pei[now])
                now++;
            ans[i]=now;
        }
    for(i=1;i<n;i++)
        printf("%d ",ans[i]);
    printf("%d\n",ans[n]);
    return 0;
}


#include<stdio.h>
#include<stdbool.h>

typedef long long LL;
bool inuse[10];
int x[2][10], y[2][10], R, B, guarding[10];

long long cprodukt(int x1, int y1, int x2, int y2, int x3, int y3){
	return (x3-x1)*(y2-y1)-(x2-x1)*(y3-y1);
}

bool cross(int g1, int s1, int g2, int s2){
	return (
		cprodukt(x[0][g1], y[0][g1], x[1][s1], y[1][s1], x[0][g2], y[0][g2])
		*
		cprodukt(x[0][g1], y[0][g1], x[1][s1], y[1][s1], x[1][s2], y[1][s2])
		<0)
		&&
		(
		cprodukt(x[0][g2], y[0][g2], x[1][s2], y[1][s2], x[0][g1], y[0][g1])
		*
		cprodukt(x[0][g2], y[0][g2], x[1][s2], y[1][s2], x[1][s1], y[1][s1])
		<0);
}

bool pos(int d){
	if(d==R) return true;
	for(int i=0;i<R;++i){
		if(inuse[i]) continue;
		inuse[i]=true;
		guarding[d]=i;
		for(int j=0;j<d;++j) if(cross(j, guarding[j], d, i)) continue;
		if(pos(d+1)) return true;
		inuse[i]=false;
		guarding[d]=-1;
	}
	return false;
}

int main(){
	scanf("%d %d", &R, &B);
	if(R!=B){
		puts("NO");
		return 0;
	}
	for(int j=0;j<2;++j) for(int i=0;i<R;++i) scanf("%d %d", x[j]+i, y[j]+i);
	if(pos(0)) puts("YES");
	else puts("NO");
	return 0;
}


/* practice with Dukkha */
#include <stdio.h>

int main() {
	int x1, x2, x, a, b, r, tmp, flipped;

	scanf("%d%d%d%d", &x1, &x2, &a, &b);
	x = x2 - x1;
	if (x >= a && x <= b) {
		printf("FIRST\n");
		printf("%d\n", x2);
		return 0;
	}
	flipped = 0;
	if (x < 0) {
		x = -x;
		flipped = 1;
		tmp = a, a = -b, b = -tmp;
	}
	if (a <= 0) {
		printf("DRAW\n");
		return 0;
	}
	r = x % (a + b);
	if (r == 0) {
		printf("SECOND\n");
		return 0;
	}
	if (r < a || r > b) {
		printf("DRAW\n");
		return 0;
	}
	printf("FIRST\n");
	printf("%d\n", x1 + (flipped ? -r : r));
	return 0;
}


#include <stdio.h>
#define N 1000005
const int P=998244353;
long long inv[N];
long long ex(long long b, int i) {
	long long p=1;
	for(; i; i>>=1, b=b*b%P) if(i&1) p=p*b%P;
	return p;
}
int main() {
	int n, i, ans, binom, power;
	scanf("%d", &n);
	inv[1]=1;
	for(i=2; i<=n; ++i) inv[i]=inv[P%i]*(P-P/i)%P;
	power=ex(3,n);
	ans=(ex((power+P-1)%P,n)*3-ex((power+P-3)%P,n)*2%P+P-ex(power,n)+P)%P;
	binom=n&1?1:-1;
	power=1;
	for(i=0; i<n; ++i) {
		if(i) {
			binom=(P-binom)*(n-i+1ll)%P*inv[i]%P;
			power=power*3ll%P;
		}
		ans=(ans+binom*ex(power-1, n)*3ll)%P;
	}
	printf("%d\n", ans<0?ans+P:ans);
	return 0;
}


#include <stdio.h>
int checkIsLucky(int num){
    while(num>0){
        if(num%10!=4&&num%10!=7)return 0;
        num/=10;
    }
    return 1;
}
int nextNum(int num){
    int nextN;
    int count=1;
    int maxNextD=0;
    nextN=num;
    if(num>=777777777) return 1000000001;
    while(num>0){
        if(num%10==4){
            nextN+=count*3;
            nextN=nextN/(count)*count+maxNextD;
            break;
        }else{
            maxNextD*=10;
            maxNextD+=4;
            num/=10;
            count*=10;
        }
    }
    if(num>0) return nextN;
    else{
        maxNextD*=10;
        maxNextD+=4;
        return maxNextD;
    }
}

int max(int a,int b){
    if(a>b) return a;
    else return b;
}
int min(int a,int b){
    if(a>b) return b;
    else return a;
}
int kthNum(int begin,int k){
    int count=1;
    int rt=0,tmp=begin;
    while (tmp>0) {
        tmp/=10;
        rt*=10;
        rt+=4;
    }
    while(rt<begin)rt=nextNum(rt);
    for(;count<k;count++){
        rt=nextNum(rt);
    }
    return rt;
}
int ma,mi;
double lenthRt(unsigned a,unsigned b,unsigned lk1,unsigned lk2){
    mi=max(a,lk1);
    ma=min(b,lk2);
    double kq;
    if(ma<mi) return 0;
    else {
        if(lk2==ma){
            kq=ma-mi;
            return kq;
        }

        kq= ma-mi+1;
        return kq;
    }
}
int main(){
    int pl,pr,vl,vr,k;
    int minN,maxN,lk1,lk2,i,after;
    double sum=0;

    double kq,mau;
    //pl=5;pr=6;
    //vl=8;vr=10;k=1;
    //printf("%d",nextNum(47));
    /*for(i=4;i<1000000000;i=nextNum(i)){
            printf("%d\n",i);

    }*/
    scanf("%d %d %d %d %d",&pl,&pr,&vl,&vr,&k);
    minN=min(pl,vl);
    maxN=max(pr,vr);

    //for p
    after=kthNum(pl,1);
    if(k==1){
        for(i=after;i<=pr;i=nextNum(i)) {
            //printf("%d\n",i);
            if(i>=vl&&i<=vr) sum-=1;
            getchar();
        }
    }
    lk1= kthNum(pl,k);
    lk2= nextNum(lk1);
    i=pl;
    for(;i<=pr;){//xet tat ca cac chu so trong [pl+1,pr]
        sum+=lenthRt(vl,vr,lk1,lk2)*min(after-i+1,pr-i+1);
        //printf("%d to %d-> %d %d + them %lf*%d->%lf\n",i,after,lk1,lk2,lenthRt(vl,vr,lk1,lk2),min(after-i+1,pr-i+1),sum);

        i=after+1;
        after=nextNum(after);
        lk1=lk2;
        if(lk1>vr) break;
        lk2=nextNum(lk2);
    }
    //if k==1

    //for p
    after=kthNum(vl,1);
    lk1= kthNum(vl,k);
    lk2= nextNum(lk1);
    i=vl;
    for(;i<=vr;){//xet tat ca cac chu so trong [pl+1,pr]
        //if(vl==vr&&pl==pr&&pl==vr) break;
        sum+=lenthRt(pl,pr,lk1,lk2)*min(after-i+1,vr-i+1);
        //printf("%d to %d-> %d %d + them %lf*%d ->%lf\n",i,after,lk1,lk2,lenthRt(pl,pr,lk1,lk2),min(after-i+1,pr-i+1),sum);

        i=after+1;
        after=nextNum(after);
        lk1=lk2;
        if(lk1>pr) break;
        lk2=nextNum(lk2);
    }
    mau=(pr-pl+1);
    mau*=     (vr-vl+1);
    //printf("%lf %lf");
    //printf("%lf %lf\n",sum,mau);
    kq=sum/mau;

    printf("%.12lf",kq);
    return 0;
}


#include<stdio.h>
char mp[9][9];
int dir;
int n,m,x,y;
int get_dis(int dir){
  if(dir==0&&y==m)return 100;
  if(dir==1&&x==n)return 100;
  if(dir==0){
    for(int i=y+1;i<=m;i++)
      if(mp[x][i]=='*')return i-y;
  }else
    for(int i=x+1;i<=n;i++)
      if(mp[i][y]=='*')return i-x;
  return 100;
}

int main(void){
  scanf("%d%d",&n,&m);
  for(int i=1;i<=n;i++)scanf("%s",mp[i]+1);
  dir=0;
  int ans=0;
  x=1;y=1;
  for(int i=0;i<n+m;i++){
    if(mp[x][y]=='*')ans++;
    //    printf("%d %d\n",x,y);
    if(get_dis(dir)<get_dis(!dir)){
      if(dir==0)y++;
      else x++;
    }else if(get_dis(dir)>get_dis(!dir)){
      if(dir==0)
	x++;
      else
	y++;
      dir=!dir;
    }else{
      y++;
    }
  }
  printf("%d\n",ans);
  return 0;
}


/* practice with Dukkha */
#include <stdio.h>
#include <stdlib.h>

#define N	8

int compare(const void *a, const void *b) {
	long long la = *(long long *) a;
	long long lb = *(long long *) b;

	return la == lb ? 0 : la < lb ? -1 : 1;
}

long long aa[N], aa_[N];
int bb[N], dp[N][1 << (N - 1)];
char pp[N];
int n, ans = 0x3f3f3f3f;

void solve(int i) {
	int j;

	if (i == n) {
		int cnt, sum;

		cnt = 0, sum = 0;
		for (i = 0; i < n; i++) {
			int k;

			if (!pp[i])
				cnt++;
			k = dp[i][bb[i]];
			sum += k == 1 && bb[i] == 0 ? 1 : k + 1;
		}
		if (cnt >= 2)
			sum++;
		if (ans > sum)
			ans = sum;
		return;
	}
	solve(i + 1);
	pp[i] = 1;
	for (j = i + 1; j < n; j++)
		if (aa_[j] % aa[i] == 0) {
			aa_[j] /= aa[i], bb[j] ^= 1 << i;
			solve(i + 1);
			aa_[j] *= aa[i], bb[j] ^= 1 << i;
		}
	pp[i] = 0;
}

int main() {
	int i, j;

	scanf("%d", &n);
	for (i = 0; i < n; i++)
		scanf("%lld", &aa[i]);
	qsort(aa, n, sizeof *aa, compare);
	for (i = 0; i < n; i++) {
		long long a;
		int k, p, b;

		a = aa[i];
		k = 0;
		for (p = 2; p <= a / p; p++)
			while (a % p == 0) {
				k++;
				a /= p;
			}
		if (a > 1)
			k++;
		dp[i][0] = k;
		for (b = 1; b < 1 << i; b++) {
			a = aa[i];
			k = dp[i][0];
			for (j = 0; j < i; j++)
				if ((b & 1 << j)) {
					if (a % aa[j] != 0) {
						k = -1;
						break;
					}
					a /= aa[j];
					k -= dp[j][0];
				}
			dp[i][b] = k;
		}
	}
	for (i = 0; i < n; i++)
		aa_[i] = aa[i];
	solve(0);
	printf("%d\n", ans);
	return 0;
}


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

#define A	5000
#define N_	(A * 3)
#define INF	0x3f3f3f3f3f3f3f3fLL

long long min(long long a, long long b) { return a < b ? a : b; }

int oo[1 + (N_ - 1) * 2], oj[1 + (N_ - 1) * 2];

int link(int o, int j) {
	static int _ = 1;

	oo[_] = o, oj[_] = j;
	return _++;
}

int count(int n, int p) {
	return n < p ? 0 : n / p + count(n / p, p);
}

int ae[N_], pp[N_], dd[N_], sz[N_], ii[A + 1], n;

void init() {
	static char composite[A + 1];
	static int primes[A + 1];
	int n_, a, b, cnt, i, j;

	for (a = 2; a <= A / a; a++) {
		if (composite[a])
			continue;
		for (b = a * a; b <= A; b += a)
			composite[b] = 1;
	}
	cnt = 0;
	for (a = 2; a <= A; a++)
		if (!composite[a])
			primes[cnt++] = a;
	n_ = 0;
	pp[n_] = -1, dd[n_] = 0;
	ii[1] = n_++;
	for (a = 2; a <= A; a++) {
		int h, d, d_;

		d = 0;
		for (h = 0; h < cnt; h++)
			d += count(a, primes[h]);
		d_ = 0;
		for (h = cnt - 1; h >= 0; h--) {
			d_ += count(a - 1, primes[h]);
			if (a % primes[h] == 0)
				break;
		}
		i = ii[a - 1], j = -1;
		while (1)
			if (dd[i] > d_)
				j = i, i = pp[i];
			else if (dd[i] == d_) {
				pp[n_] = i, dd[n_] = d, n_++;
				ii[a] = n_ - 1;
				break;
			} else {
				int k = n_++;

				pp[k] = i, dd[k] = d_;
				if (j != -1)
					pp[j] = k;
				pp[n_] = k, dd[n_] = d, n_++;
				ii[a] = n_ - 1;
				break;
			}
	}
	for (j = 1; j < n_; j++) {
		i = pp[j];
		ae[i] = link(ae[i], j), ae[j] = link(ae[j], i);
	}
}

long long dfs1(int p, int i) {
	int o;
	long long x;

	x = (long long) sz[i] * dd[i];
	for (o = ae[i]; o; o = oo[o]) {
		int j = oj[o];

		if (j != p) {
			x += dfs1(i, j);
			sz[i] += sz[j];
		}
	}
	return x;
}

long long ans;

void dfs2(int p, int i, long long x) {
	int o;

	ans = min(ans, x);
	for (o = ae[i]; o; o = oo[o]) {
		int j = oj[o];

		if (j != p) {
			int w = abs(dd[i] - dd[j]);

			dfs2(i, j, x - (long long) sz[j] * w + (long long) (n - sz[j]) * w);
		}
	}
}

int main() {
	int i;

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

		scanf("%d", &a);
		sz[ii[a > 0 ? a : 1]]++;
	}
	ans = INF;
	dfs2(-1, 0, dfs1(-1, 0));
	printf("%lld\n", ans);
	return 0;
}