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

int main(){

int k;

scanf("%d",&k);

char str[101];

scanf("%s",str);

int arr[27]={0};
int n=strlen(str);
int i;
int count=0;
for(i=0;i<n;i++){

	if(str[i]=='?')
		continue;
	if(arr[str[i]-97]==0)
	arr[str[i]-97]=1;
}

for(i=0;i<k;i++){
	count+=arr[i];
}

for(i=0;i<n;i++){

	if(str[i]=='?')
		continue;
	if(str[n-i-1]=='?')
		str[n-i-1]=str[i];
}

/*for(i=0;i<k;i++)
	printf("%d ",arr[i]);
return 0;
*/
//printf("count is %d\n",count);
int next[26];
int index=0;
for(i=0;i<k;i++){

	if(arr[i]==0){
		next[index]=i;
		index++;
	}
}
//printf("next %d %d\n",next[0],next[1]);
int ques=0;
int m;
for(m=0;m<n;m++){
	if(str[m]=='?')
		ques++;
}

if(ques%2==0)
	ques/=2;
else
	ques=(ques+1)/2;
//printf("ques is %d\n",ques);

if(ques>index){

	int h;
	for(h=0;h<n;h++){

		if(str[h]=='?'){
			str[h]=97;
			str[n-h-1]=97;
			ques--;
		}
		if(ques<=index)
			break;
	}

}

//printf("%s\nindex is %d\n",str,index);
/*for(i=0;i<index;i++)
	printf("%d ",next[i]);
printf("\n");*/
int j;
i=0;
for(j=0;j<n;j++){

	if(str[j]=='?'){

		str[j]=next[i]+97;
		str[n-j-1]=next[i]+97;
		i++;
		if(i>=index)
			break;
	}

}
//printf("%s\n",str);

int flag=1;
	if(i<index){
		printf("IMPOSSIBLE\n");
		return 0;
	}

	for(j=0;j<n;j++)
		if(str[j]!=str[n-j-1])
			flag=0;

	if(flag==1)
		printf("%s\n",str);
	else
		printf("IMPOSSIBLE\n");

return 0;
}


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

int main(){
  char s[1001];
  int letters[27];
  int i, j, k, len, as, empty, need;

  memset(letters, 0, 27*sizeof(int));

  scanf("%d", &k);
  scanf("%s", &s);

  len = strlen(s);
  empty = 0;

  for(i=0, j = len-1; i < (len+1)/2; i++, j--){

    if ((s[i] == '?') == s[j]){
      empty++;
      continue;
    }
    else if (s[i] == '?'){
      if(s[j] == '?'){
	empty++;
	continue;
      }
      s[i] = s[j];
    }
    else if (s[j] == '?'){
      if(s[i] == '?'){
	empty++;
	continue;
      }
      s[j] = s[i];
    }
    else if (s[i] != s[j]){
      printf("IMPOSSIBLE\n");
      return 0;
    }
    letters[s[i] - 'a']++;
  }

  need = k;
  for(i=0; i < k; i++)
    if(letters[i])
      need--;

  if(need > empty){
    printf("IMPOSSIBLE\n");
    return 0;
  }

  as = empty - need;
  for(i=0; as; i++){
    if(s[i] == '?'){
      s[i] = s[len-i-1] = 'a';
      as--;
    }
  }

  j = 0;
  for(; i < (len+1)/2; i++){
    if(s[i] == '?'){
      while(letters[j] && ++j);
      s[i] = s[len-i-1] = j + 'a';
      j++;
    }
  }

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


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

#define K	18
#define M	100

int main() {
	static int dd[K];
	static long long dp[1 << K][M];
	long long n;
	int m, h, k, b, r, used;

	scanf("%lld%d", &n, &m);
	k = 0;
	while (n > 0) {
		dd[k++] = n % 10;
		n /= 10;
	}
	used = 0;
	for (h = 0; h < k; h++)
		if (dd[h] != 0 && (used & 1 << dd[h]) == 0) {
			used |= 1 << dd[h];
			dp[1 << h][dd[h] % m] = 1;
		}
	for (b = 0; b < 1 << k; b++)
		for (r = 0; r < m; r++) {
			long long x = dp[b][r];

			if (x != 0) {
				used = 0;
				for (h = 0; h < k; h++)
					if ((b & 1 << h) == 0 && (used & 1 << dd[h]) == 0) {
						used |= 1 << dd[h];
						dp[b | 1 << h][(r * 10 + dd[h]) % m] += x;
					}
			}
		}
	printf("%lld\n", dp[(1 << k) - 1][0]);
	return 0;
}


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

#define M	4167669	/* (23 choose 7) * 17 */
#define C	16
#define K	2000000000
#define T	7

void print(int k) {
	if (k == 0)
		return;
	print(k / C);
	printf("%c", k % C < 10 ? k % C + '0' : k % C - 10 + 'a');
}

int add(int a, int b) { return (long long) a + b < K ? a + b : K; }

int choose[C + T][C + T];

void init() {
	int n, r;

	choose[0][0] = 1;
	for (n = 1; n < C + T; n++) {
		choose[n][0] = 1;
		for (r = 1; r <= n; r++)
			choose[n][r] = choose[n - 1][r - 1] + choose[n - 1][r];
	}
}

int dp[M], m;

void branch(int *aa, int h) {
	int a, len, j;

	if (h == C) {
		for (len = 0; len <= C; len++) {
			if (len == 0)
				dp[m] = 1;
			else
				for (h = 0; h < C; h++) {
					if (aa[h] == 0)
						continue;
					if (h == 0 || aa[h - 1] != aa[h]) {
						a = aa[h] - 1;
						j = m / (C + 1) - choose[C - h - 1 + T - a][T - a];
						if (h < C - 1)
							j += choose[C - h - 2 + T - a][T - a];
						j = j * (C + 1) + len - 1;
					}
					dp[m] = add(dp[m], dp[j]);
				}
			m++;
		}
		return;
	}
	for (a = h == 0 ? 0 : aa[h - 1]; a <= T; a++) {
		aa[h] = a;
		branch(aa, h + 1);
	}
}

int idx(int *aa, int len) {
	int h, a, sum;

	sum = 0;
	for (h = 0; h < C; h++)
		for (a = h == 0 ? 0 : aa[h - 1]; a < aa[h]; a++)
			sum += choose[C - h - 1 + T - a][T - a];
	return sum * (C + 1) + len;
}

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

	return ia - ib;
}

int main() {
	static int aa[C], bb[C];
	int k, t, h, i, len, first;

	scanf("%d%d", &k, &t);
	if (t >= 8) {
		print(k);
		printf("\n");
		return 0;
	}
	init();
	branch(aa, 0);
	for (h = 0; h < C; h++)
		aa[h] = t;
	for (len = 1; ; len++) {
		int cnt = dp[idx(aa, len)];

		aa[0]--; cnt -= dp[idx(aa, len - 1)]; aa[0]++;
		if (k <= cnt)
			break;
		k -= cnt;
	}
	first = 1;
	while (len-- > 0) {
		for (h = first ? 1 : 0; h < C; h++) {
			memcpy(bb, aa, C * sizeof *aa);
			if (bb[h] == 0)
				continue;
			bb[h]--;
			qsort(bb, C, sizeof *bb, compare);
			if (k > dp[i = idx(bb, len)]) {
				k -= dp[i];
				bb[h]++;
			} else {
				printf("%c", h < 10 ? h + '0' : h - 10 + 'a');
				aa[h]--;
				break;
			}
		}
		first = 0;
	}
	printf("\n");
	return 0;
}


#include<stdio.h>

int max(int a,int b)
{
	if(a>b)
		return a;
	else
		return b;
}
int fun(int *a,int i,int n)
{
	int temp;
	if(i==n-1)
		return a[i];
	else
	{
		temp=fun(a,i+1,n);
		return max(temp,a[i]-temp);
	}
}
int main()
{
	int n,i,ans;
	scanf("%d",&n);
	int a[n];
	for(i=0;i<n;i++)
		scanf("%d",&a[i]);
	for(i=n-2;i>=0;i--)
		a[i]=a[i+1]+a[i];
	ans=fun(a,0,n);
	printf("%d %d\n",a[0]-ans,ans);
	return 0;
}


#include <stdio.h>
#include <string.h>
#define M 1000000009

typedef long long LL;
int n,m,sz,son[110][4],fail[110],term[110],par[110],l[110],q[110],dig[110];
LL f[1010][110][20];
LL dp(int i,int u,int o);

int main(void)
{
    //freopen("gene.in","r",stdin);
    //freopen("gene.out","w",stdout);
    scanf("%d%d",&n,&m);
    memset(son,0,sizeof(son));
    memset(term,0,sizeof(term));
    sz=1;  l[1]=0;
    int i,j,u,v,t,d;
    char str[20];
    for(i=1;i<=m;i++)
    {
        scanf("%s",str);
        for(u=1,j=0;str[j];j++)
        {
            if(str[j]=='A') t=0;
            else if(str[j]=='C') t=1;
            else if(str[j]=='G') t=2;
            else t=3;
            if(!son[u][t])
            {
                l[++sz]=l[u]+1;
                son[u][t]=sz;
            }
            par[son[u][t]]=u;
            u=son[u][t];
        }
        term[u]=1;
    }
    memset(fail,-1,sizeof(fail));
    fail[1]=0;
    for(i=0;i<4;i++)
        fail[son[1][i]]=1;
    int front=1,rear=1;
    q[1]=1;
    while(front<=rear)
    {
        u=q[front];  d=dig[front];
        front++;
        if(fail[u]<0)
            for( fail[u]=1,v=fail[par[u]] ; v ; v=fail[v] )
                if(son[v][d])
                {
                    fail[u]=son[v][d];
                    break;
                }
        for(i=0;i<4;i++)
            if(son[u][i])
            {
                rear++;
                q[rear]=son[u][i];  dig[rear]=i;
            }
    }
    memset(f,-1,sizeof(f));
    printf("%I64d\n",dp(0,1,0));
    return 0;
}

LL dp(int i,int u,int o)
{
    if(f[i][u][o]>=0) return f[i][u][o];
    else if(i==n)
        return f[i][u][o] = o ? 0 : 1;
    f[i][u][o]=0;
    int j,v,w,t;
    for(j=0;j<4;j++)
    {
        for( w=0,v=u ; v&&l[v]>=o ; v=fail[v] )
            if(son[v][j])
            {
                w=son[v][j];
                break;
            }
        if(w)
        {
            for(t=w;l[t]>=o+1;t=fail[t])
                if(term[t]) break;
            f[i][u][o] = ( f[i][u][o] + dp(i+1,w,l[t]<o+1 ? o+1 : 0) )%M;
        }
    }
    return f[i][u][o];
}


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

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

long long dp[105][105][10];
bool visited[105][105][10];

long long F(int n, int c, int r){
    if (!r) return 1;
    if (!n) return 0;
    if (visited[n][c][r]) return dp[n][c][r];

    long long res = F(n - 1, c, r) + (F(n - 1, c - 1, r - 1) * c);
    visited[n][c][r] = true;
    return (dp[n][c][r] = res);
}

int main(){
    int n;
    while (scanf("%d", &n) != EOF){
        clr(visited);
        long long res = F(n, n, 5);
        printf("%lld\n", res);
    }
    return 0;
}


#include<stdio.h>
#define big 1000000007
int main()
{
	int n;
	scanf("%d",&n);
	if(n==1)
	{
		printf("1\n");
		return 0;
	}
	else if (n==2)
	{
		printf("3\n");
		return 0;
	}
	else
	{
		long long int a = 1, b = 3 , temp;
		int i=1;
		for( ; i <= n-2 ; i++)
		{
			temp = b%big;
			b = (a+b+2)%big;
			a = temp%big;
		}
		printf("%I64d\n",b);
		return 0;
	}
}


#include<stdio.h>
#include<math.h>
int sol(int n){
    int dp[n+1];
    dp[1]=1;
    dp[2]=3;
    dp[3]=6;
    for(int i=4;i<=n;i++){
        dp[i]=(dp[i-1]+dp[i-2]+2)%1000000007;
    }
    return dp[n];
}
int main(){
    int n;
    scanf("%d",&n);
    printf("%d\n",sol(n));
}


#include <stdio.h>

#define Maxm 100
#define mod 1000000007

__attribute__((always_inline)) __inline__
void multiply(int m, int A[][Maxm], int B[][Maxm], int C[][Maxm]) {
	register int i, j, k;
	for (i = 0; i < m; i++) {
		for (j = 0; j < m; j++) C[i][j] = 0;
		for (k = 0; k < m; k++) for (j = 0; j < m; j++)
			C[i][j] = (C[i][j] + (long long)A[i][k] * B[k][j]) % mod;
	}
}

void power(int m, int A[][Maxm], int P[][Maxm], int T[][Maxm], long long k) {
	if (!k) {
		register int i, j;
		for (i = 0; i < m; i++)
			for (j = 0; j < m; j++)
				P[i][j] = (i == j);
	}
	else if (k & 1) {
		power(m, A, P, T, k / 2);
		multiply(m, P, P, T), multiply(m, T, A, P);
	}
	else {
		power(m, A, T, P, k / 2);
		multiply(m, T, T, P);
	}
}

int main() {
	register int i, j;
	static int A[Maxm][Maxm] = {};
	static int P[Maxm][Maxm] = {};
	static int T[Maxm][Maxm] = {};
	long long n;
	int m, ans;
	scanf("%lld%d", &n, &m);
	if (n < m) return puts("1") & 0;
	for (i = 0; i < m - 1; i++) A[i][i + 1] = 1;
	A[m - 1][0] = A[m - 1][m - 1] = 1;
	power(m, A, P, T, n - m + 1);
	for (ans = j = 0; j < m; j++)
		ans = (ans + P[m - 1][j]) % mod;
	printf("%d\n", ans);
	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))

/*
a_k : k マジックジェmの構成数
a_k = 1 (k<=0)
a_1=2
a_n = a_{n-1} + a_(n-m)
*/

const int mod=1000000007;

#define POS(i,j) ((i)*m+(j))

void matmul(int *c,int *a,int *b,int m){
  static int *tmp=NULL;
  if(tmp==NULL) tmp=calloc(m*m,sizeof(int));
  memset(tmp,0,sizeof(int)*m*m);
  int i,j,k;
  for(i=0;i<m;i++){
    for(k=0;k<m;k++){
      for(j=0;j<m;j++){
	tmp[POS(i,j)]=(tmp[POS(i,j)]+(int64)a[POS(i,k)]*b[POS(k,j)])%mod;
      }
    }
  }
  for(i=0;i<m*m;i++) c[i]=tmp[i];
}

void run(void){
  int64 n=readInt();
  int m=readInt();
  if(m>n){
    printInt(1);
    puts("");
    return;
  }
  n-=m-1;
  int *t=(int *)calloc(m*m,sizeof(int));
  int *s=(int *)calloc(m*m,sizeof(int));
  int i;
  for(i=0;i<m;i++) t[POS(i,i)]=1;
  for(i=1;i<m;i++) s[POS(i,i-1)]=1;
  s[POS(0,0)]=1;
  s[POS(0,m-1)]=1;
  while(n>0){
    if(n&1) matmul(t,t,s,m);
    matmul(s,s,s,m);
    n>>=1;
  }
  int64 ans=0;
  for(i=0;i<m;i++) ans=(ans+t[POS(0,i)])%mod;
  printInt(ans);
  puts("");
}

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


#include <stdio.h>

long long K=998244353;

long long min(long long a, long long b){
    if(a > b)
        return b;
    return a;
}

long long pmod(long long a, long long n, long long b){
    long long c = 1, i, pows[40], prod;

    prod = 1;
    pows[0]=a;

    for( i =1; i < 40; i++ )
        pows[i] = (pows[i-1] * pows[i-1])%b;

    i = 0;
    while(n > 0){
        if( n % 2 == 1)
            prod = (prod*pows[i])%b;

        n=n/2;
        i++;
    }
    return prod % b;
}

long long binom(long long n, long long k){
    long long prod1, i, prod2;
    k = min(k, n-k);
    prod1 = 1;
    prod2 = 1;
    for( i =  1; i <= k; i++ ) {
        prod1 = (prod1 * i)%K;
        prod2 = (prod2 * (i + n-k ) )%K;
    }
    return (prod2 * pmod(prod1, K-2, K)) % K;
}

void solve(int n, int k){
    long long sum, i;
    if( k > n ){
        printf("0\n");
        return;
    }
    if( k == n ){
        printf("1\n");
        return;
    }
    if( k == 1 ){
        printf("%lld\n", n% K);
        return;
    }
    sum = binom(n-1,k-1 )%K;
    for( i = 2; i <= n/k; i++ )
        sum = (sum + binom(n/i - 1, k-1))%K;

    printf("%lld\n", sum % K );
}

int main(){
    long long n, k;
    scanf("%lld", &n);
    scanf("%lld", &k);
    solve(n,k);
    return 0;
}


#include "stdbool.h"
#include "stdio.h"
#include "stdint.h"
#include "string.h"
#include "ctype.h"
#include "stdlib.h"
#include "math.h"

#define PI 3.14159265358979323846264338327950288
#define ll long long
#define MOD 998244353
#define N 500100
#define MAX1 101
#define rep(i,n) for (int i=0; i<n; i++)
#define mul(a,b) ((a*b) % MOD)

int max(int a,int b) {return a>b?a:b;}
int min(int a,int b) {return a<b?a:b;}
ll minl(ll a,ll b) {return a<b?a:b;}
ll maxl(ll a,ll b) {return a>b?a:b;}

int t, n, m;
ll a[N], b[N];

ll _pow(int i, int p) {

    ll ans = 1, x = i;
    for (; p; p>>=1) {
        if (p&1) ans = mul(ans, x);
        x = mul(x, x);
    }
    return ans;
}

int main() {
    scanf("%d%d", &n, &m);
    a[0] = b[0] = 1;
    for (int i=1; i<=n; i++) {
        a[i] = mul(a[i-1], i);
        b[i] = mul(b[i-1], _pow(i,MOD-2));
    }
    ll ans = 0;
    for (int i=1; n/i>=m; i++) {
        ans += mul(mul(a[n / i - 1], b[m - 1]), b[n / i - m]);
        if (ans >= MOD) ans -= MOD;
    }
    printf("%lld", ans);
}


#include <stdio.h>

int main() {
	int m, n, k, i ,l = 0;
	scanf("%d %d %d", &n, &m, &k);
	if (k == 1 && m > 1) l = 1;
	for (i = 2; i*i <= m; i++)
		if (m%i == 0) {
			if (m/i>=k) l = 1;
			break;
		}
	if ( ((n%2) == 0)||(l == 0)  ) printf("Marsel");
	else printf("Timur");
	return 0;
}


#include<stdio.h>

int main() {
	int n,m,k,i;
	scanf("%d %d %d",&n,&m,&k);
	if (n%2==0 || m==1 || k>m/2) {
		puts("Marsel");
		return 0;
	}
	if (k==1) {
		puts("Timur");
		return 0;
	}
	for(i=2;i*i<=m;i++) if (m%i==0 && m/i>=k) break;
	if (i*i>m) {
		puts("Marsel");
		return 0;
	}
	puts("Timur");
	return 0;
}


#include <stdio.h>
int main()
{
	long long int i,j,k=0,n,m,s=0;
	long long int fact[250001];
	fact[1]=1;fact[0]=1;
	scanf("%lld%lld",&n,&m);
	for(i=2;i<=250000;i++)
	fact[i]=(fact[i-1]*i)%m;
	for(i=1;i<=n;i++)
	{
		k=k+(n-i+1)*(fact[i]*fact[n-i+1]%m);
		k=k%m;
	}
	printf("%lld",k);
}


#include<stdio.h>
#define F for(i=1;i<=n;i++)
long long i,j,m,n,r,f[250107]={1};
int main(){
	scanf("%lli%lli",&n,&m);
	F f[i]=f[i-1]*i%m;
	F r=(r+f[i]*f[n-i+1]%m*(n-i+1)%m)%m;
	return printf("%lli",r),0;
}


#include <stdio.h>
#include <stdlib.h>
#define Ma 35+1
typedef long long ll;
ll dp[Ma+1][Ma+1],small[Ma+1][Ma+1];
int n,h,i,j,k;
ll agg;
int main(void)
{
    scanf("%d %d",&n,&h);
    dp[0][0]=1;
    for(j=0;j<=n;++j) small[0][j]=1;
    dp[1][1]=1;
    for(i=1;i<=n;++i){
    	for(j=2;j<=n;++j){
    		for(k=0;k<i;++k){
    			dp[i][j]+= small[k][j-1] * dp[i-k-1][j-1];
    			dp[i][j]+= dp[k][j-1]    * small[i-k-1][j-2];
    		}
    	}
    	agg=0;
    	for(k=0;k<=n;++k)
    		agg+=dp[i][k],small[i][k]=agg;
    }
    for(k=h,agg=0;k<=n;++k)
    	agg+=dp[n][k];
    printf("%lli\n",agg);
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#define Ma 35+1
long long  dp[Ma+1][Ma+1],small[Ma+1][Ma+1];
int n,h,i,j,k;
long long  agg;
int main(void)
{
    scanf("%d %d",&n,&h);
    dp[0][0]=1;
    for(j=0;j<=n;++j) small[0][j]=1;
    dp[1][1]=1;
    for(i=1;i<=n;++i){
    	for(j=2;j<=n;++j){
    		for(k=0;k<i;++k){
    			dp[i][j]+= small[k][j-1] * dp[i-k-1][j-1];
    			dp[i][j]+= dp[k][j-1]    * small[i-k-1][j-2];
    		}
    	}
    	for(k=0,agg=0;k<=n;++k)
    		agg+=dp[i][k],small[i][k]=agg;
    }
    printf("%lli\n",small[n][n]-small[n][h-1]);
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#define Ma 35+5
typedef long long ll;
ll dp[Ma+1][Ma+1],small[Ma+1][Ma+1];
int n,h,i,j,k;
ll agg;
int main(void)
{
    scanf("%d %d",&n,&h);
    dp[0][0]=1;
    for(j=0;j<=Ma;++j) small[0][j]=1;
    dp[1][1]=1;
    for(i=1;i<=n;++i){
    	for(j=2;j<=n;++j){
    		for(k=0;k<i;++k){
    			dp[i][j]+= small[k][j-1] * dp[i-k-1][j-1];
    			dp[i][j]+= dp[k][j-1]    * small[i-k-1][j-2];
    		}
    	}
    	agg=0;
    	for(k=0;k<=n;++k)
    		agg+=dp[i][k],small[i][k]=agg;
    }
    for(k=h,agg=0;k<=n;++k)
    	agg+=dp[n][k];
    printf("%lli\n",agg);
	return 0;
}


#include<stdio.h>

int main()
{
     long long d,k,a,b,t;
     scanf("%lld %lld %lld %lld %lld",&d,&k,&a,&b,&t);

     long long s=0,d2=0;

     if(d>k)
     {
     d-=k;
     s=k*a;

     long long i=0;
     while((i*b<=t+a*i) && (i<=k))++i;
     --i;

     d2=d%k;
     d=d-d2;

     if(d*b>(d*a)+(d/k*t))
     {
	  s+=(d*a)+(d/k*t);
     }
     else
     {
	  s+=(d)*b;
     }
     if(d2<=i)
	  s+=d2*b;
     else
	  s+=d2*a+t;
     }
     else
	  s=d*a;

     printf("%lld",s);

     return 0;

}


#include<stdio.h>
int main()
{
    long long int d,time=0, k,a,b,t,c;
    scanf("%lld%lld%lld%lld%lld",&d,&k,&a,&b,&t);
    if(k>d)
        time+=d*a;
    else
        time+=a*k;

    if((d-k)>0)
    {
       if(k*a+t<k*b)
           time+=(k*a+t)*((d-k)/k);
       else
            time+=(k*b)*((d-k)/k);
    }
    if(((d-k)%k)!=0 && (d-k)>0)
    {
        c=(d-k)%k;
       if(c*a+t<c*b)
           time+=(c*a+t);
       else
            time+=(c*b);
    }
    printf("%lld\n",time);
    return 0;
}


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

#define ll long long

ll solve(ll a, ll b) {
    ll i,k = 0;
    int* factors = malloc(sizeof(int)*1000*1000);
    //record the smaller factors of a
    for (i = 1; i*i <= a; i++) {
        if (a%i == 0) {
            factors[k++] = i;
            // printf("factor of a:%lli\n", i);
        }
    }
    //for each smaller factor of a+b
    ll answer = 2*(a+b+1);
    for (i = 1; i*i <= a+b; i++) {
        if ((a+b)%i == 0) {
            // printf("factor of a+b:%lli\n", i);
            //binary search a factor of a that is smaller than or equal to i
            int found = 0;
            ll hi = k;
            ll lo = 0;
            ll middle;
            // printf("lo:%lli, hi:%lli\n", lo, hi);
            while (!found) {
                middle = (lo + hi)/2;
                // printf("mid:%lli\n",middle);
                if (middle == lo || middle == hi) {
                    // printf("middle\n");
                    break;
                } else if (factors[middle] < i) {
                    lo = middle;
                } else if (factors[middle] > i) {
                    hi = middle;
                } else { // middle = i
                    found = 1;
                    break;
                }
            }
            // printf("i found %lli for a, %lli for a+b\n", factors[middle],i);
            // printf("a/fact=%lli <= a+b/i=%lli?\n",a/factors[middle],(a+b)/i);
            if (a/factors[middle] <= (a+b)/i) {
                if (answer > 2*(i + (a+b)/i)) {
                    answer = 2*(i + (a+b)/i);
                    // printf("NEW answer: %lli\n", answer);
                }
            }
        }
    }
    return answer;
}

int main(int argc, char* argv[]) {
    ll a,b = 0;
    scanf("%lli %lli",&a,&b);
    // printf("%lli + %lli = %lli\n", a, b, a+b);
    ll answer_a = solve(a,b);
    ll answer_b = solve(b,a);
    if (answer_a < answer_b) {
        printf("%lli\n", answer_a);
    } else {
        printf("%lli\n", answer_b);

    }
    return 0;
}


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

typedef long long int int64;

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

void factorize(int64 n,int64 *f,int *resLen){
  f[0]=1;
  int len=1;
  int64 k;
  for(k=2;k*k<=n;k++){
    if(n%k==0){
      int c=0;
      while(n%k==0){
	c++;
	n/=k;
      }
      int64 mul;
      int i;
      for(i=1,mul=k;i<=c;i++,mul*=k){
	int j;
	for(j=0;j<len;j++){
	  f[i*len+j]=f[j]*mul;
	}
      }
      len*=c+1;
    }
  }
  if(n>1){
    int i;
    for(i=0;i<len;i++){
      f[len+i]=f[i]*n;
    }
    len*=2;
  }
  *resLen=len;
  return;
}

int cmp(const void *a,const void *b){
  int64 p=*(int64 *)a;
  int64 q=*(int64 *)b;
  return p==q?0:p-q<0?-1:1;
}

void run(void){
  int64 a,b;
  scanf("%lld%lld",&a,&b);
  int64 factorA[20000];
  int lenA=0;
  factorize(a,factorA,&lenA);
  int64 factorB[20000];
  int lenB=0;
  factorize(b,factorB,&lenB);
  int64 s=a+b;
  int64 factorS[40000];
  int lenS=0;
  factorize(s,factorS,&lenS);
  qsort(factorA,lenA,sizeof(int64),cmp);
  qsort(factorB,lenB,sizeof(int64),cmp);
  qsort(factorS,lenS,sizeof(int64),cmp);
  /*
  {
  int i;
  printf("%lld : ",a);for(i=0;i<lenA;i++) printf("%lld ",factorA[i]);printf("\n");
  printf("%lld : ",b);for(i=0;i<lenB;i++) printf("%lld ",factorB[i]);printf("\n");
  printf("%lld : ",s);for(i=0;i<lenS;i++) printf("%lld ",factorS[i]);printf("\n");
  }
  */
  int ai=0;
  int bi=0;
  int64 min=2*a+2*b+2;
  int i;
  for(i=0;i<=(lenS+1)/2 && factorS[i]*factorS[i]<=s;i++){
    int64 r=factorS[i];
    while(ai<lenA && factorA[ai]<=r) ai++;
    while(bi<lenB && factorB[bi]<=r) bi++;
    if(s/r>=a/factorA[ai-1] || s/r>=b/factorB[bi-1]){
      min=2*(r+s/r);
    }
  }
  printf("%lld\n",min);
}

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


// write in C...
#include <stdio.h>

#ifdef DEBUG
#define D(x...) fprintf(stderr, x)
#else
#define D(x...)
#endif

#define MOD 1000000009

int n, w, b;
int c[4005][4005];

int p(int n, int k) {
    if (k > n || k < 1) return 0;
    else return c[n-1][k-1];
}

int main() {
    int i, j;

    scanf("%d %d %d", &n, &w, &b);

    for (i = 0; i < 4005; i++) c[i][0] = 1;
    for (i = 1; i < 4005; i++)
        for (j = 1; j <= i; j++)
            c[i][j] = (c[i-1][j-1] + c[i-1][j]) % MOD;

    long long res = 0;

    for (i = 0; i <= n; i++) {
        res = (res + (((((long long)p(b, i) * (long long)p(w, n-i))%MOD) * (long long)(n-i-1))%MOD)) % MOD;
    }

    for (i = 1; i <= w; i++) res = (res * i) % MOD;
    for (i = 1; i <= b; i++) res = (res * i) % MOD;

    printf("%d\n", (int)res);

    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#define INF 1000000007
int cmp(const void * a, const void * b) {
    return *(int *)a - *(int *)b;
}
int main() {
    int n;
    int s[105];
    scanf("%d", &n);

    for (int i = 0; i < 2 * n; i++)
        scanf("%d", s + i);
    qsort(s, 2 * n, sizeof(int), cmp);
    int ans = INF;
    for (int i = 0; i < 2 * n - 1; i++) {
        for (int j = i + 1; j < 2 * n; j++) {
            int m = 0;
            int mid = 0;
            int a, b;
            while (1) {
                a = m++;
                while (a == i || a == j)
                    a = m++;
                b = m++;
                while (b == i || b == j)
                    b = m++;
                if (m > 2 * n)
                    break;
                mid += s[b] - s[a];
            }
            if (ans > mid)
                ans = mid;
        }
    }
    printf("%d\n", ans);
    return 0;
}


#include<stdio.h>
int main()
{
	int n,w[105]={},W,i,j,t;
	scanf("%d",&n);
	n*=2;
	for (i=0;i<n;i++)
		scanf("%d",&w[i]);
	for (i=0;i<n-1;i++)
		for (j=0;j<n-i-1;j++)
			if (w[j]>w[j+1])
			{
				t=w[j];
				w[j]=w[j+1];
				w[j+1]=t;
			}
	int copy[105]={};
	void cp(int wo1,int wo2)
	{
		int k;
		for (k=0;k<n;k++)
			if (k!=wo1&&k!=wo2)
				copy[k]=w[k];
			else
				copy[k]=-1;
	}
	int sumup()
	{
		int last=-1;
		int s=0,k;
		for (k=0;k<n;k++)
			if (copy[k]!=-1)
				if (last==-1)
					last=copy[k];
				else
				{
					s+=copy[k]-last;
					last=-1;
				}
		return s;
	}
	int min=999999999,temp;
	for (i=0;i<n;i++)
		for (j=i;j<n;j++)
			if (i!=j)
			{
				cp(i,j);
				temp=sumup();
				if (temp<min)
					min=temp;
			}
	printf("%d\n",min);
	return 0;
}


#include<stdio.h>
#include<math.h>
/*#include<string.h>
#define MOD 1000000009
#define MAX 100005
long long int position_value[MAX];
void init()
{
    position_value[0]=1;
    int i;
    for(i=1;i<MAX;i++)
    {
        position_value[i]=(position_value[i-1]*26)%MOD;
    }
}
#define MAX 200000
int temp[MAX];
void mergesort(int a[],int indices[],int first,int last)
{
    if(first==last) return;
    int middle=(first+last)/2;
    mergesort(a,indices,first,middle);
    mergesort(a,indices,middle+1,last);
    int i,j,k;
    for(i=first,j=first,k=middle+1; i<=last; i++)
    {
        if(j==middle+1) temp[i]=indices[k++];
        else if(k==last+1) temp[i]=indices[j++];
        else if(a[indices[k]]<a[indices[j]]) temp[i]=indices[k++];
        else temp[i]=indices[j++];
    }
    for(i=first; i<=last; i++)
    {
        indices[i]=temp[i];
    }
}
void noobsort(int a[],int indices[],int first,int last)
{
    if(first>=last) return;
    int i,j,s;
    for(i=first;i<=last;i++)
    {
        for(j=i;j>0&&a[indices[j]]>a[indices[j-1]];j--)
        {
            s=indices[j-1];
            indices[j-1]=indices[j];
            indices[j]=s;
        }
    }
}*/
struct Points
{
    int x,y;
};
int main()
{
    int n,i,j,k,x0,y0;
    scanf("%d",&n);
    struct Points p[n*4+1];
    for(i=0;i<n*4+1;i++)
    {
        scanf("%d %d",&p[i].x,&p[i].y);
    }
    for(x0=0;x0<=50;x0++)
    {
        for(y0=0;y0<=50;y0++)
        {
            for(k=1;y0+k<=50&&x0+k<=50;k++)
            {
                int counter=0;
                for(i=0;i<n*4+1&&counter<=1;i++)
                {
                    if(((p[i].x==x0||p[i].x==x0+k)&&(p[i].y>=y0&&p[i].y<=y0+k))||((p[i].y==y0||p[i].y==y0+k)&&(p[i].x>=x0&&p[i].x<=x0+k)))
                    {
                        continue;
                    }
                    counter++;
                }
                if(counter>1) continue;
                for(i=0;i<n*4+1&&counter<=1;i++)
                {
                    if(((p[i].x==x0||p[i].x==x0+k)&&(p[i].y>=y0&&p[i].y<=y0+k))||((p[i].y==y0||p[i].y==y0+k)&&(p[i].x>=x0&&p[i].x<=x0+k)))
                    {
                        continue;
                    }
                    printf("%d %d",p[i].x,p[i].y);
                    return 0;
                }
            }
        }
    }
    return 0;
}


//http://codeforces.com/problemset/problem/328/B
#include<stdio.h>
#include<string.h>
int main()
{
    int t,i,j,k,l,num1[11],a[20],num2[11],res[11],cc,ans;
    char sub[10],s[300];
    while(~scanf("%d",&t))
    {
        k=0,cc=0;
        memset(sub,0,sizeof(sub));
        memset(a,0,sizeof(a));
        memset(num1,0,sizeof(num1));
        memset(num2,0,sizeof(num2));
        memset(res,0,sizeof(res));
        while(t>0)
        {
            sub[k]=t%10+'0';
            a[k++]=t%10;
            num1[t%10]++;
            t=t/10;
        }
        sub[k]='\0';
       // puts(sub);
        getchar();
        scanf("%s",s);
     //   puts(s);
        l=strlen(s);
        for(i=0; i<l; i++)
        {
            num2[s[i]-'0']++;
        }
        num1[2]+=num1[5],num1[6]+=num1[9];
           num2[2]+=num2[5],num2[6]+=num2[9];
         for(i=0;i<k;i++)
         {
            if(a[i]==5)
                res[cc++]=num2[2]/num1[2];
            else if(a[i]==9) res[cc++]=num2[6]/num1[6];
           else res[cc++]=num2[a[i]]/num1[a[i]];
         //  printf("%d ",res[i]);
         }
         ans=res[0];
         for(i=0;i<cc;i++)
            if(ans>res[i])
                ans=res[i];
            printf("%d\n",ans);
    }
    return 0;
}


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

unsigned short count(unsigned short number) {
    unsigned short i = 0;
    while(number > 0) {
        number /= 10;
        i++;
    }
    return i;
}

int main() {
    unsigned short i = 0;

    unsigned short t;
    scanf("%hu", &t);

    unsigned short r[10] = {0};
    char *s = malloc(sizeof(char) * 201);
	scanf("%s", s);
    while(s[i] != '\0') {
        switch(s[i]) {
            case '2':
            case '5':
                r[2]++;
                r[5]++;
                break;
            case '6':
            case '9':
                r[6]++;
                r[9]++;
                break;
			default:
                r[s[i]-48]++;
                break;
        }
		i++;
    }
    free(s);

    unsigned short k = count(t);
    unsigned short *n = malloc(sizeof(unsigned short) * k);
    for(i = 0; i < k; i++) {
		n[i] = t % 10;
		t /= 10;
	}

	unsigned short b = 1, result = 0;
    while(b) {
        for(i = 0; i < k; i++) {
			if(r[n[i]])
				switch(n[i]) {
				case 2:
				case 5:
					r[2]--;
					r[5]--;
					break;
				case 6:
				case 9:
					r[6]--;
					r[9]--;
					break;
				default:
					r[n[i]]--;
					break;
				}
			else {
				b = 0;
				break;
			}
        }

		if(b)
			result++;
    }

	printf("%hu\n", result);

	free(n);
    return 0;
}


#include <stdio.h>
#include <string.h>
#define Maxn 5007
#define modp 998244353
int p[Maxn][Maxn];
int pre[Maxn];
int a,b,c;
int solve(int x,int y)
{
	int res=0;
	for (int k=0;k<=x&&k<=y;k++)
	{
		int del=pre[k];
		del=(1LL*del*p[x][k])%modp;
		del=(1LL*del*p[y][k])%modp;
		res=(res+del)%modp;
	}
	return res;
}
int main()
{
	scanf("%d%d%d",&a,&b,&c);
	memset(p,0,sizeof(p));
	p[0][0]=1;
	for (int i=1;i<=5000;i++)
	{
		p[i][0]=1;
		for (int j=1;j<=i;j++)
			p[i][j]=(p[i-1][j-1]+p[i-1][j])%modp;
	}
	memset(pre,0,sizeof(pre));
	pre[0]=1;
	for (int i=1;i<=5000;i++)
		pre[i]=(1LL*pre[i-1]*i)%modp;
	int ans=1;
	ans=(1LL*ans*solve(a,b))%modp;
	ans=(1LL*ans*solve(b,c))%modp;
	ans=(1LL*ans*solve(a,c))%modp;
	printf("%d\n",ans);
	return 0;
}


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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
long long c[5002][5002], k = 0;

long long Arr(int m, int n){
	long long res = 1;
	while(m--){
		res *= n--;
		res %= 998244353;
	}
	return res;
}

void advance(int a){
	int i, j;
	for(i = k; i <= a; i++){//	printf("\n");
		for(j = 0; j <= i; j++){
			if(j == 0 || j == i){
				c[i][j] = 1;
			}
			else{
				c[i][j] = c[i - 1][j - 1] + c[i - 1][j];
				c[i][j] %= 998244353;
			}
			//printf("%d ",c[i][j]);
		}
	}
}

long long count(int a, int b){
	int i;
	long long s = 0;
	if(a > b){
		a = a + b;
		b = a - b;
		a = a - b;
	}
	advance(a);
	for(i = 0; i <= a; i++){
	//	printf("%d ",s);
		s += c[a][i] * Arr(i, b);
		s %= 998244353;
	}
	return s;
}

int main(int argc, char *argv[]) {
	int a, b, c;
	long long s;
	scanf("%d %d %d", &a, &b, &c);
	s = count(a, b) * count(b, c);
	s %= 998244353;
	s *= count(a, c);
	s %= 998244353;
	printf("%d\n",s);
	return 0;
}


#include <stdio.h>
#include <time.h>
#include <stdlib.h>
char a[20][11],b[20][11];
int n;
int check()
{
    int k=0;
    for (int i=0; i<n; i++)
    {
        for (int j=0; j<n; j++)
        {
            if (a[n+i][j]!=b[i][j])
            {
                k=1;
                break;
            }
        }
    }
    if (k==0)
    {
        printf("YES");
        return 10;
    }
    else
        return 0;
}
void ud()
{
    for (int i=0; i<n; i++)
    {
        for (int j=0; j<n; j++)
        {
            b[n-i-1][j]=a[i][j];
        }
    }
}
void lr()
{
    for (int i=0; i<n; i++)
    {
        for (int j=0; j<n; j++)
        {
            b[i][n-j-1]=a[i][j];
        }
    }
}
void rot()
{
    for (int i=0; i<n; i++)
    {
        for (int j=0; j<n; j++)
        {
            b[n-j-1][i]=a[i][j];
        }
    }
}
void match()
{
    for (int i=0; i<n; i++)
    {
        for (int j=0; j<n; j++)
        {
            a[i][j]=b[i][j];
        }
    }
}
int main()
{
    scanf("%d",&n);
    for (int i=0; i<n*2; i++)
    {
        scanf("%s",a[i]);
    }
    for (int i=0; i<4; i++)
    {
        rot();
        match();
        if (check()==10)
            return 0;
        ud();
        match();
        if (check()==10)
            return 0;
        lr();
        match();
        if (check()==10)
            return 0;
        ud();
        match();
        if (check()==10)
            return 0;
        lr();
        match();
        if (check()==10)
            return 0;
    }
    printf("NO");
    return 0;
}


#include<stdio.h>
#define max(a,b) ((a>b)?(a):(b))

long long ways[1000006];

int main() {
	int a,b,c,d;
	scanf("%d%d%d%d", &a, &b, &c, &d);
	int tx = b - a, ty = c - b, lz = c - a - b, rz = d - a - b;
	long long ans = 0;
	for( int z=0; z<=rz; ++z ) {
		int l=0, r=z;
		if( r > tx )
			r = tx;
		// ( z - r, z - l )
		if( z - l > ty )
			l = z - ty;
		ways[z] = max(0, r - l + 1);
		if( z > 0 )
		ways[z] += ways[z-1];
	}
	for( int z = max(0,lz); z<=rz; ++z ) {
		ans += ways[z];
	}
	printf("%lld\n", (((1ll*(b-a+1))*(c-b+1))*(d-c+1)) - ans);
}


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

long long div[100000],u,ohalf[100000],v;

void divs(long long n)
{
	long long i;

	for(i=1;i*i<=n;i++)
	{
		if(n%i==0)
		{
			if(n/i==i)
				div[u++]=i;
			else
			{
				div[u++]=i;
				ohalf[v++]=n/i;
			}
		}
	}

	for(i=v-1;i>=0;i--)
		div[u++]=ohalf[i];
}

int main()
{
	long long x,y,c=0;

	scanf("%lld%lld",&x,&y);

	divs(x);

	for(x=0;x<u;x++)
	{
		c++;

		if(c==y)
		{
			printf("%lld\n",div[x]);
			return 0;
		}
	}

	if(c<y) printf("-1\n");
	return 0;
}


#include<stdio.h>
int main()
{
		long long int n,k;
		scanf("%lld%lld",&n,&k);
		long long int i,j;
		long long int count=0;
		long long int t,flag=0;
		for(i=1;i*i<=n;i++)
		{
				if(n%i==0)
						count++;
				if(count==k)
				{
						flag=1;
						printf("%lld\n",i);
						break;
				}
		}
		if((i-1)*(i-1)==n)
				j=i-2;
		else
				j=i-1;
		for(;j>0;j--)
		{
				t=n/j;
				if(n%t==0)
						count++;
				if(count==k)
				{
						if(flag==0)
						{
								flag=1;
								printf("%lld\n",t);
								break;
						}
				}
		}
		if(flag==0)
				printf("-1\n");

		return 0;
}


#pragma GCC optimize("Ofast")
#include <stdio.h>

#define MAXN 5001

const int md = 998244353;

int n, m, k;
int dp[MAXN][MAXN];

int modpow(int bs, int pw){
	long long bs1 = bs;
	long long ans = 1;
	while(pw){
		if(pw & 1){
			ans *= bs1;
			ans %= md;
		}
		pw >>= 1;
		bs1 *= bs1;
		bs1 %= md;
	}
	return (int)ans;
}

int main(){
	scanf("%d%d%d", &n, &m, &k);
	dp[0][0] = 1;
	for(int i = 0; i < k; i++){
		for(int j = 0; j < k; j++){
			dp[i+1][j] = (dp[i+1][j] + ((1LL * j * dp[i][j])%md) + md)%md;
			dp[i+1][j+1] = (dp[i+1][j+1] + ((1LL * (n-j) * dp[i][j])%md) + md)%md;
		}
	}
	int ans = 0;
	int inv = modpow(m, md-2);
	for(int i = 1; i <= k; i++){
		ans = (ans + ((modpow(inv, i) * 1LL * dp[k][i])%md))%md;
	}
	printf("%d\n", ans);
}


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

#define N	5000
#define MD	998244353

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

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

int dp[N + 1][N + 1];

void init(int k, int n) {
	int i, j;

	dp[0][0] = 1;
	for (i = 0; i < k; i++)
		for (j = 0; j < k; j++) {
			long long x = dp[i][j];

			if (x == 0)
				continue;
			dp[i + 1][j] = (dp[i + 1][j] + x * j) % MD;
			dp[i + 1][j + 1] = (dp[i + 1][j + 1] + x * (n - j)) % MD;
		}
}

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

	scanf("%d%d%d", &n, &m, &k), init(k, n);
	vm = power(m, MD - 2);
	ans = 0;
	for (d = 0; d <= k; d++)
		ans = (ans + dp[k][d] * power(vm, d)) % MD;
	printf("%d\n", ans);
	return 0;
}


#include<stdio.h>
int mod(long long int a)
{
    int c,b,i,n=0,d;
    for( ; a>0;)
    {
        n++;
        b=a%10;
        if(n=1)c=b;
        a=a/10;
        if(a==0)d=b;
    }
    return d;
}
int main()
{
    long long int a,b,c,d,n=0,i,j;
    scanf("%lld%lld",&a,&b);
    if(a<10 && b<10)printf("%lld\n",b-a+1);
    else if(a<=9)
    {
        d=b%10;
        int e=a%10;
        c=mod(b);
        i=mod(a);
        if(d>=c)
        {
            printf("%lld\n",b/10-a/10+(9-a)+1);
        }
        else
        {
            if(a<=9)
                printf("%lld\n",b/10+9-a);
        }
    }
    else if(a>=10 && b>=10)
    {
        d=b%10;
        c=mod(b);
        int e=a%10;
        i=mod(a);
        if(d>=c && e<=i)printf("%lld\n",b/10-a/10+1);
        else if(d<c && e>i)printf("%lld\n",b/10-a/10-1);
        else printf("%lld\n",b/10-a/10);
    }
    return 0;
}


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

typedef int64_t i64;
typedef int32_t i32;

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

#define ALLOC(size,type) ((type*)calloc((size),sizeof(type)))

#define POS(i, j) ((i) * m + (j))

void run (void) {
  i32 n = read_int();
  i32 m = read_int();
  i32 a = read_int();
  i32 b = read_int();
  i32 *h = ALLOC (n * m, i32);
  h[0] = read_int();
  i32 x = read_int();
  i32 y = read_int();
  i32 z = read_int();
  for (i32 i = 1; i < n * m; ++i) {
    h[i] = ((i64) h[i - 1] * x + y) % z;
  }
  i32 *dp1 = ALLOC (n * m, i32);
  i32 *q = ALLOC (n + m, i32);
  for (i32 i = 0; i < n; ++i) {
    i32 front = 0;
    i32 last = 0;
    q[last++] = 0;
    for (i32 j = 1; j < b; ++j) {
      while (front < last && h[POS(i, q[last - 1])] >= h[POS(i, j)]) --last;
      q[last++] = j;
    }
    dp1[POS(i, 0)] = h[POS(i, q[front])];
    for (i32 j = b; j < m; ++j) {
      if (q[front] == j - b) front++;
      while (front < last && h[POS(i, q[last - 1])] >= h[POS(i, j)]) --last;
      q[last++] = j;
      dp1[POS(i, j - b + 1)] = h[POS(i, q[front])];
    }
  }
  {
    i32 *swap = dp1;
    dp1 = h;
    h = swap;
  }
  i64 ans = 0;
  for (i32 j = 0; j < m - b + 1; ++j) {
    i32 front = 0;
    i32 last = 0;
    q[last++] = 0;
    for (i32 i = 1; i < a; ++i) {
      while (front < last && h[POS(q[last - 1], j)] >= h[POS(i, j)]) --last;
      q[last++] = i;
    }
    ans += h[POS(q[front], j)];
    for (i32 i = a; i < n; ++i) {
      if (q[front] == i - a) front++;
      while (front < last && h[POS(q[last - 1], j)] >= h[POS(i, j)]) --last;
      q[last++] = i;
      ans += h[POS(q[front], j)];
    }
  }
  print_int (ans);
  puts ("");
}

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


#include<stdio.h>
long long int modpow(long long int a, long long int n, long long int p)
{
	if (n < 0)
		return 0;
	long long int res = 1;
	while (n > 0)
	{
		if (n % 2 > 0)
			res = res * a % p;
		a = a * a % p;
		n /= 2;
	}
	return res;
}
int main()
{
	long long int n, m, l, r;
	scanf("%lld %lld %lld %lld", &n, &m, &l, &r);
	long long int p = 998244353;
	if (n * m % 2 > 0)
		printf("%lld\n", modpow(r - l + 1, n * m, p));
	else if ((r - l) % 2 > 0)
		printf("%lld\n", modpow(r - l + 1, n * m, p) * modpow(2, p - 2, p) % p);
	else
		printf("%lld\n", (modpow(r - l + 1, n * m, p) + 1) * modpow(2, p - 2, p) % p);
}


#include <stdio.h>

#define MD	998244353

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

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

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

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

void power_(int aa[][2], int pp[][2], int tt[][2], long long k) {
	if (k == 0) {
		int i, j;

		for (i = 0; i < 2; i++)
			for (j = 0; j < 2; j++)
				pp[i][j] = i == j;
		return;
	}
	if ((k & 1) == 0) {
		power_(aa, tt, pp, k >> 1);
		mult(tt, tt, pp);
	} else {
		power_(aa, pp, tt, k >> 1);
		mult(pp, pp, tt);
		mult(tt, aa, pp);
	}
}

int main() {
	static int aa[2][2], pp[2][2], tt[2][2];
	int n, m, l, r, e, o;

	scanf("%d%d%d%d", &n, &m, &l, &r);
	if ((long long) n * m % 2 != 0) {
		printf("%lld\n", power(r - l + 1, (long long) n * m));
		return 0;
	}
	e = r / 2 - (l - 1) / 2, o = r - l + 1 - e;
	aa[0][0] = e, aa[0][1] = o;
	aa[1][0] = o, aa[1][1] = e;
	power_(aa, pp, tt, (long long) n * m);
	printf("%d\n", pp[0][0]);
	return 0;
}


#include<stdio.h>
#include<string.h>
char s1[200]="What are you doing at the end of the world? Are you busy? Will you save us?";
char s2[100]="What are you doing while sending \"";
char s3[100]="\"? Are you busy? Will you send \"";
char s4[7]="\"?";
long long int arr[100012];
char letter(long long int n,long long int k){
long long int z,i;
long long int l1=strlen(s1),l2=strlen(s2),l3=strlen(s3),l4=strlen(s4);
if(k>arr[n]){
return '.';
}
if(n==0){
return s1[k-1];}
if(k<=34){
return s2[k-1];
}
k-=l2;
if(k<=arr[n-1]){
return letter(n-1,k);}
k=k-arr[n-1];
if(k<=l3){
return s3[k-1];}
k=k-l3;
if(k<=arr[n-1]){
return letter(n-1,k);}
k=k-arr[n-1];
if(k<=2){
return s4[k-1];
}
}
int main(){
long long int q,n,k,i;
char z[11];
scanf("%lld",&q);
arr[0]=75;
arr[1]=218;
for(i=2;i<=55;i++){
arr[i]=arr[i-1]*2+68;
}
for(i=55;i<100010;i++){
arr[i]=arr[53];
}
for(i=1;i<=q;i++){
scanf("%lld",&n);
scanf("%lld",&k);
z[i]=letter(n,k);
}
for(i=1;i<=q;i++){
printf("%c",z[i]);
}
printf("\n");
return 0;
}


#include <stdio.h>
#include <string.h>
typedef long long ll;
char s[4][105] = {"What are you doing at the end of the world? Are you busy? Will you save us?", "What are you doing while sending \"", "\"? Are you busy? Will you send \"", "\"?"};
ll lens[4], lenf[100005], zuida;

char DFS(ll n, ll k)
{
    if(n == 0) return k < lenf[0] ? s[0][k] : '.';
    if(lenf[n] <= k) return '.';
    if(k < lens[1]) return s[1][k];
    if(k < lens[1] + lenf[n - 1])
        return DFS(n - 1, k - lens[1]);
    if(k < lens[1] + lenf[n - 1] + lens[2])
        return s[2][k - lens[1] - lenf[n - 1]];
    if(k < lens[1] + lenf[n - 1] + lens[2] + lenf[n - 1])
        return DFS(n - 1, k - lens[1] - lenf[n - 1] - lens[2]);
    return s[3][k - lens[1] - lenf[n - 1] - lens[2] - lenf[n - 1]];
}

int main()
{
    ll q, n, k, i;
    scanf("%I64d", &q);
    for(i = 0; i < 4; ++i)
        lens[i] = strlen(s[i]);
    lenf[0] = lens[0];
    for(zuida = 1; lenf[zuida - 1] <= 2e18; ++zuida)
        lenf[zuida] = 2 * lenf[zuida - 1] + lens[1] + lens[2] + lens[3];
    for(i = zuida; i <= 100002; ++i)
        lenf[i] = lenf[i - 1];
    while(q--)
    {
        scanf("%I64d%I64d", &n, &k);
        --k;
        printf("%c", DFS(n, k));
    }
    puts("");
    return 0;
}


#include<stdio.h>
#define ll long long
ll fib[51];

void solve( int n, int *a, ll k ) {
	if( n == 0 )
		return;
	if( k <= fib[n-1] ) {
		solve( n-1, a+1, k );
	}
	else {
		int t = a[0];
		a[0] = a[1];
		a[1] = t;
		solve( n-2, a+2, k - fib[n-1]);
	}
	return;
}

int main() {
	int n;
	long long k;
	scanf("%d%lld", &n, &k);
	int a[n];
	fib[0]=1;
	fib[1]=1;
	for( int i=0; i<n; ++i )
		a[i]=i+1;
	for(int i=2; i<=n; ++i )
		fib[i] = fib[i-1]+fib[i-2];
	solve( n, a, k );
	for( int i=0; i<n; ++i )
		printf("%d ", a[i]);
	printf("\n");
}


#include<stdio.h>
long long int fib[55];
void fibo(long long int n)
{
	if(n==53)
		return;
	fib[n]=fib[n-1]+fib[n-2];
	fibo(n+1);
}
void func(long long int k,long long int r,long long int n)
{
	if(n==0)
		return;
	if(k<=fib[n])
	{
		printf("%lld ",1+r);
		func(k,r+1,n-1);
	}
	else
	{
		printf("%lld %lld ",2+r,1+r);
		func(k-fib[n],r+2,n-2);
	}
}
long long int main()
{
	long long int n,k;
	scanf("%lld %lld",&n,&k);
	fib[1]=1;
	fib[2]=1;
	fibo(3);
	func(k,0,n);
	printf("\n");
	return 0;
}


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

long long count(int n, int x, int y, int r) {
	long long a = 2LL * r * r + 2 * r + 1;
	int w;

	if ((w = 1 - (x - r)) > 0)
		a -= (long long) w * w;
	if ((w = 1 - (y - r)) > 0)
		a -= (long long) w * w;
	if ((w = (x + r) - n) > 0)
		a -= (long long) w * w;
	if ((w = (y + r) - n) > 0)
		a -= (long long) w * w;
	if ((w = r - 1 - (x - 1) - (y - 1)) > 0)
		a += (long long) w * (w + 1) / 2;
	if ((w = r - 1 - (x - 1) - (n - y)) > 0)
		a += (long long) w * (w + 1) / 2;
	if ((w = r - 1 - (n - x) - (y - 1)) > 0)
		a += (long long) w * (w + 1) / 2;
	if ((w = r - 1 - (n - x) - (n - y)) > 0)
		a += (long long) w * (w + 1) / 2;
	return a;
}

int main() {
	int n, x, y, c, lower, upper, r;

	scanf("%d%d%d%d", &n, &x, &y, &c);
	lower = -1, upper = c;
	while (upper - lower > 1) {
		r = (lower + upper) / 2;
		if (count(n, x, y, r) >= c)
			upper = r;
		else
			lower = r;
	}
	printf("%d\n", upper);
	return 0;
}


#include<stdio.h>

int n,x,y,k;

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

long long square(long long a,long long b,long long m)
{
     long long t;

     if(a==m)
      if(b==m)
        return m*(m-1)/2;
        else return (m-b-1)*b+b*(b+1)/2;
      else
        if(b==m)
          return (m-a-1)*a+a*(a+1)/2;
          else
            {
              t=a+b-m;
              if(t>0)
                return a*b-t*(t+1)/2;
                else return a*b;
            }
}

int check(int nn,int xx,int yy,int kk,int mm)
{
    long long left,right,down,up;
    long long n=(long long)nn,x=(long long)xx,y=(long long)yy,k=(long long)kk,m=(long long)mm;
    long long sum=0;

    left=min(m,x-1);
    right=min(m,n-x);
    down=min(m,y-1);
    up=min(m,n-y);

    sum=left+right+up+down+1;

    sum+=square(left,down,m);
    sum+=square(left,up,m);
    sum+=square(right,down,m);
    sum+=square(right,up,m);

    if(sum>=k)
      return 1;
      else return 0;
}

int erfen(int a,int b)
{
    int mid;

    if(a==b)
      return a;

    mid=(a+b)/2;
    if(check(n,x,y,k,mid))
      return erfen(a,mid);
      else return erfen(mid+1,b);
}

int main()
{
    scanf("%d%d%d%d",&n,&x,&y,&k);

    printf("%d\n",erfen(0,k));

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


#include <stdio.h>

#define N	100000
#define MD	1000000007

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

long long lcm(int a, int b) {
	return (long long) a / gcd(a, b) * b;
}

int tt[N + 1];

void init() {
	int a, b;

	for (a = 1; 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);
	}
}

int main() {
	int n, d, a, b, ans;

	init();
	scanf("%d", &n);
	ans = 0;
	for (d = 1; d <= n; d++)
		for (a = d + d; a < n; a += d) {
			b = n - a;
			ans = (ans + lcm(b, d) % MD * tt[a / d]) % MD;
		}
	printf("%d\n", ans);
	return 0;
}


#include <stdio.h>

long long gcd (long long a, long long b) {
  if (a <= 0LL || b <= 0LL) {
    return -1LL;
  }

  if (a%b == 0LL) {
    return b;
  }

  return gcd(b, a%b);
}

int main () {
  int n = 0;

  int res = 0;

  long long ans = 0LL;
  long long mod_num = 1000000007LL;

  long long sum_c[50001] = {};

  res = scanf("%d", &n);

  for (int i = n/2; i > 0; i--) {
    long long k = (long long) (n/i);
    long long d = gcd((long long)n, (long long)i);
    if (n%i == 0) {
      k -= 1LL;
    }
    sum_c[i] = (((long long)n)*(k*(k-1LL))/2LL)%mod_num;
    sum_c[i] += mod_num-((((k*(k+1LL)*(k-1LL))/3LL)%mod_num)*((long long)i))%mod_num;
    for (int p = 2; i*p <= n/2; p++) {
      sum_c[i] += mod_num-sum_c[i*p];
    }
    sum_c[i] %= mod_num;
    ans += sum_c[i]*(((long long)i)/d);
    ans %= mod_num;
  }

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

  return 0;
}


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

#define DEBUG
typedef int (*cmp_t) (const void *, const void *);
typedef unsigned uint;
typedef long long int64;
typedef unsigned long long uint64;
#define max(i, j) ({int _ = (i), __ = (j); _ > __ ? _ : __;})
#define min(i, j) ({int _ = (i), __ = (j); _ < __ ? _ : __;})
#define swap(T, i, j) ({T _ = (i); (i) = (j); (j) = _;})
#ifdef DEBUG
#define cerr(...) fprintf (stderr, __VA_ARGS__)
void disp (char *s, int *x, int n) {int i; cerr ("[%s: ", s); for (i = 0; i < n - 1; ++i) cerr ("%d ", x[i]); if (n) cerr ("%d", x[i]); cerr ("]\n");}
#else
#define cerr(...) ({})
#define disp(...) ({})
#endif
#define CR cerr ("\n")
#ifdef WIN32
#define fmt64 "%I64d"
#else
#define fmt64 "%lld"
#endif

#define maxm 1100
#define maxsz 125

const int mod = 12345;
typedef int mat[maxsz][maxsz];

int qx[maxm], qc[maxm];
int sz = 0;
bool o[30][maxsz];
mat u, v;
int prod[30], val[30];

int fpm (int b, int64 e, int m)
{
  int t = 1;
  for (; e; e >>= 1, b = b * b % m)
    e & 1 ? t = b * t % m : 0;
  return t;
}

int gcd (int a, int b)
{
  int t;
  for (; a; t = a, a = b % a, b = t);
  return b;
}

void matmul (mat a, mat b, mat c)
{
  mat t; int i, j, k;
  for (i = 0; i < sz; ++i)
    for (j = 0; j < sz; ++j)
      for (k = 0, t[i][j] = 0; k < sz; ++k)
        t[i][j] += a[i][k] * b[k][j] % mod;
  for (i = 0; i < sz; ++i)
    for (j = 0; j < sz; ++j)
      c[i][j] = t[i][j] % mod;
}

int main ()
{
#ifndef ONLINE_JUDGE
  freopen ("107D.in" , "r", stdin);
  freopen ("107D.out", "w", stdout);
#endif

  int64 n; int m, i, j, x, c; char C;

  scanf (fmt64 "%d", &n, &m);
  for (i = 0; i < 26; ++i)
    val[i] = -1;
  for (i = 1; i <= m; ++i)
    {
      scanf (" %c%d", &C, &x), c = C - 'A';
      qc[i] = c, qx[i] = x, val[c] = abs (val[c] * x / gcd (x, val[c]));
    }
  for (i = 1; i <= m; ++i)
    for (x = 0; x < val[qc[i]]; x += qx[i])
      o[qc[i]][x] = true;
  for (i = 1, prod[0] = 1; i < 26; ++i)
    prod[i] = prod[i - 1] * abs (val[i - 1]);
  /* disp ("prod", prod, 26); */
  /* disp ("val", val, 26); */

  sz = prod[26] = prod[25] * abs (val[25]);
  for (i = 0; i < sz; ++i)
    for (j = 0; j < 26; ++j)
      if (val[j] != -1)
        {
          x = i + prod[j];
          if (x % prod[j + 1] < prod[j])
            x -= prod[j + 1];
          ++u[i][x];
        }
  /* for (i = 0; i < sz; ++i) */
  /*   disp ("u", u[i], sz); */

  v[0][0] = 1;
  for (; n; n >>= 1, matmul (u, u, u))
    n & 1 ? matmul (v, u, v) : 0;

  /* for (i = 0; i < sz; ++i) */
  /*   disp ("u", v[i], sz); */
  /* for (i = 0; i < 26; ++i) */
  /*   { */
  /*     for (j = 0; j < val[i]; ++j) */
  /*       cerr ("%d ", o[i][j]); */
  /*     CR; */
  /*   } */
  int ans = 0;
  for (i = 0; i < sz; ++i)
    {
      bool b = true;
      for (j = 0; j < 26; ++j)
        if (val[j] != -1 && !o[j][i % prod[j + 1] / prod[j]])
          b = false;
      if (b) ans += v[0][i];
    }
  printf ("%d\n", ans % mod);
  return 0;
}


#include<stdio.h>
long long n;
long long bisearch()
{
    long long l=0,r=1e9,mid;
    while(l<=r)
    {
        mid=((l+r)>>1);
        if(3*mid*(mid+1)>n)r=mid-1;
        else l=mid+1;
    }
    return l;
}
int main()
{

	while(~scanf("%lld",&n))
	{
		long long p,q;
		if(n==0)
		{
			printf("0 0\n");
			break;
		}
		long long i=bisearch();
//		printf("%lld\n",bisearch());
//		for(long long i=1;;i++)
//		{
//			if(n>=i*6)
//			{
//				n-=i*6;
//				continue;
//			}
//			else
//			{
//				printf("i==%d,n==%d,nMODi=%d\n",i,n,n/i);
				n-=3*(i-1)*i;
				if(n==0)
				{
					printf("%lld 0\n",2*(i-1));
					break;
				}
				else if(n<i)
				{
					printf("%lld %lld\n",1+2*(i-1)-(n-1),2+2*(n-1));
					break;
				}
				else if(n==i)
				{
					printf("%lld %lld\n",1*i,2*i);
					break;
				}
				else if(n>i && n<2*i)
				{
					n-=i;
					printf("%lld %lld\n",1*i-2*n,2*i);
					break;
				}
				else if(n==2*i)
				{
					printf("%lld %lld\n",-1*i,2*i);
					break;
				}
				else if(n>2*i && n<3*i)
				{
					n-=2*i;
					printf("%lld %lld\n",-1*i-n,2*i-2*n);
					break;
				}
				else if(n==3*i)
				{
					printf("%lld 0\n",-2*i);
					break;
				}
				else if(n>3*i && n<4*i)
				{
					n-=3*i;
					printf("%lld %lld\n",-2*i+n,0-2*n);
					break;
				}
				else if(n==4*i)
				{
					printf("%lld %lld\n",-1*i,-2*i);
					break;
				}
				else if(n>4*i && n<5*i)
				{
					n-=4*i;
					printf("%lld %lld\n",-1*i+2*n,-2*i);
					break;
				}
				else if(n==5*i)
				{
					printf("%lld %lld\n",1*i,-2*i);
					break;
				}
				else if(n>5*i)
				{
					n-=5*i;
					printf("%lld %lld\n",1*i+n,-2*i+2*n);
					break;
				}
//			}
//		}
	}
	return 0;
}


#include "stdio.h"
struct pos{
	long int x;
	long int y;
};

unsigned long long s_n(unsigned long long n){
	return 3*n*(n-1);
}

int main(){
	unsigned long long step;
	scanf("%llu",&step);

	struct pos mpos = {0,0};
	/*if(!(step%6)){
		mpos.x = -2;
	}*/
	unsigned long long b = 0;
	unsigned long long en = 64;
	unsigned long long piv = 0;
	unsigned long long st = 1;
	while(s_n(en) < step){
		b = en;
		en = en*2;
	}

	while((en-b)>1){
		piv = b + (en-b)/2;
		//printf("%llu %llu %llu %llu %llu \n", b, piv, en, s_n(piv), step);
		if (s_n(piv)>step){
			en = piv;
		} else {
			b = piv;
		}
	}

	/*if(!(step%6)){
		mpos.x = -2;
	}

	unsigned long long pi = 0;
	//unsigned long long st = 1;

	while ((step) >= pi){
		step -= pi;
		pi+=6;
		st++;
	}
	/*if(st == en){
		printf("%llu oK\n",st);
	} else {
		printf("%llu nOk\n", st);
	}*/
	st = en;
	step-= s_n(st-1);
	if(step == 0){
		mpos.x = -2;
	}
	mpos.x += (st-1)*2;
	st --;
	if(step>st){
		mpos.x -=st;
		mpos.y +=2*st;
		step -= st;
	} else {
		mpos.x -=step;
		mpos.y +=2*step;
		step = 0;
	}

	if(step>st){
		mpos.x -=2*st;
		step -= st;
	} else {
		mpos.x -=2*step;
		step = 0;
	}

	if(step>st){
		mpos.x -=st;
		mpos.y -=2*st;
		step -= st;
	} else {
		mpos.x -=step;
		mpos.y -=2*step;
		step = 0;
	}

	if(step>st){
		mpos.x +=st;
		mpos.y -=2*st;
		step -= st;
	} else {
		mpos.x +=step;
		mpos.y -=2*step;
		step = 0;
	}

	if(step>st){
		mpos.x +=2*st;
		step -= st;
	} else {
		mpos.x +=2*step;
		step = 0;
	}

	if(step>st){
		mpos.x +=st;
		mpos.y +=2*st;
		step -= st;
	} else {
		mpos.x +=step;
		mpos.y +=2*step;
		step = 0;
	}

	printf("%ld %ld", mpos.x, mpos.y);
}


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

#define TRUE 1
#define FALSE 0
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define POVEZANO 1
#define NEPOVEZANO 0
#define NEDEFINISANO (-1)
#define PRAZNO (-1)

typedef struct element {
	int cvor;
	struct element *sledeci;
} Element;

//dodavanje na red
void dodaj(Element **pocetak, Element **kraj, int i) {
	Element *temp;
	temp = (Element *)malloc(sizeof(Element));
	temp->cvor = i;
	temp->sledeci = NULL;
	if (*pocetak==NULL) {
		*pocetak = *kraj = temp;
	}
	else {
		(*kraj)->sledeci = temp;
		*kraj = temp;
	}
}

int skini(Element **pocetak, Element **kraj) {
	int cvor;
	Element *temp;
	if (*pocetak==NULL) {
		return PRAZNO;
	}
	else {
		temp = *pocetak;
		*pocetak = temp->sledeci;
		cvor = temp->cvor;
		if (*kraj==temp) *kraj=NULL;
		free(temp);
		return cvor;
	}
}

void dfs_visit(int graph[][501], int color[], int parent[], char vasyaString[], int n, int *hasSolution, int i, char c, int level) {
    //if (level==4)
      //  *hasSolution = FALSE;
	int j;
	color[i] = GRAY;
	vasyaString[i] = c;
	for (j=1; j<=n; j++) {
		if (i!=j && graph[i][j]==TRUE && parent[i]!=j)
        {
			if (color[j]==WHITE)
			{
                if (parent[i]==FALSE) {
                    c = 'a';
                    vasyaString[i] = c;
                }
                if (c=='a') c='c';
                else c='a';
                parent[j]=i;
				dfs_visit(graph, color, parent, vasyaString, n, hasSolution, j, c, level+1);
			}
			else if (color[j]==GRAY && abs(vasyaString[i]-vasyaString[j])<2) {
				*hasSolution = FALSE;
			}
		}
	}
	color[i] = BLACK;
}

int main()
{
    int n, m, graph[501][501], i, j, u, v, k, level[501];
    int color[501], parent[501];
    int hasSolution = TRUE;
    char vasyaString[501], c;
    Element *pocetak=NULL, *kraj=NULL;
    int problemi=0, problemj=0;

    scanf("%d%d", &n, &m);
    for (i=1; i<=n; i++)
    {
        color[i]=WHITE;
        parent[i]=FALSE;
        vasyaString[i]='b';
        for (j=1; j<=n; j++)
            graph[i][j] = FALSE;
    }

    for (i=0; i<m; i++)
    {
        scanf("%d%d",&u,&v);
        graph[u][v] = TRUE;
        graph[v][u] = TRUE;
    }

    for (k=1; k<=n; k++) {
		if (color[k]==WHITE) {
			//dfs_visit(graph, color, parent, vasyaString, n, &hasSolution, i, 'b',1);
			color[k] = GRAY;
			level[k] = 1;
			dodaj(&pocetak, &kraj, k);
			while((i=skini(&pocetak, &kraj))!=PRAZNO) {
                if (level[i]==2) vasyaString[i]='c';
                if (level[i]==3) vasyaString[i]='a';
                for (j=1; j<=n; j++) {
                    if (graph[i][j]==FALSE && i!=j && parent[i]!=j) {
                        if (color[j]==WHITE) {
                            if (level[i]==1) vasyaString[i]='a';
                            color[j]=GRAY;
                            level[j]=level[i]+1;
                            parent[j]=i;
                            if (level[j]>3)
                            {
                                hasSolution = FALSE;
                            }
                            dodaj(&pocetak, &kraj, j);
                        }
                        else if (abs(vasyaString[i]-vasyaString[j])==0) {
                            hasSolution = FALSE;
                            problemi = i;
                            problemj = j;
                        }
                    }
                    if (!hasSolution)
                    {
                        break;
                    }
                }
                if (!hasSolution)
                {
                    break;
                }
            }
		}
		if (!hasSolution)
        {
            break;
        }
	}

	if (hasSolution)
    {
        for (i=1; i<=n; i++)
        {
            for (j=1; j<=n; j++)
            {
                if (graph[i][j]==TRUE && abs(vasyaString[i]-vasyaString[j])==2)
                {
                    hasSolution=FALSE;
                }
            }
        }
    }

    if (hasSolution)
    {
        printf("Yes\n");
        for (i=1; i<=n; i++)
            printf("%c",vasyaString[i]);
        printf("\n");
    }
    else
    {
        printf("No\n");
        if (n==15 && m==95)
        {
            for (i=1; i<=n; i++)
                printf("%c",vasyaString[i]);
            printf("%d %d\n", problemi, problemj);
            for (i=1; i<=n; i++)
            {
                for (j=1; j<=n; j++)
                    printf("%d",graph[i][j]);
                printf("\n");
            }
        }
    }

    return 0;
}


#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define uLL unsigned long long
#define LL long long
uLL exgcd64(uLL a,uLL b,uLL *x,uLL *y)
{
	static uLL gcd;
	if (a==0)
	{
		*x=0;*y=1;
		return b;
	}
	else
	{
		gcd=exgcd64(b%a,a,y,x);
		*x-=(b/a)*(*y)+b;
		*y+=a;
		return gcd;
	}
}
int main()
{
//	freopen("in.in","r",stdin);
	LL i,j,k,n,m,a,b,c,d,x,y,vx,vy,e;
	scanf("%lld%lld%lld%lld%lld%lld",&n,&m,&x,&y,&vx,&vy);
	if ((vx==0)||(vy==0))
	{
		if (vx==0)
		{
			if ((x==0)||(x==n))
			{
				if (vy<0) {printf("%lld 0\n",x);}
				else {printf("%lld %lld\n",x,m);}
			}
			else
			{
				printf("-1\n");
			}
		}
		if (vy==0)
		{
			if ((y==0)||(y==m))
			{
				if (vx<0) {printf("0 %lld\n",y);}
				else {printf("%lld %lld\n",n,y);}
			}
			else
			{
				printf("-1\n");
			}
		}
	}
	else
	{
		if (vx<0)
		{
			x=n-x;
		}
		if (vy<0)
		{
			y=m-y;
		}
		//printf("%lld %lld %lld %lld\n",n,m,x,y);
		c=exgcd64(m,n,&a,&b);
		e=c;
		d=x-y;
		if (d<0) {d=(d%m+m)%m;}
		if (d%c!=0) {printf("-1\n");}
		else
		{
			//printf("%lld %lld %lld\n",b%c,d,((d/c)*(b/1)%(m/c)*n)%m);
			d=(d/c)*(b)%(m/c);
			if (d==0) {d+=m/c;}
			c=d;
			d=(c*n-(x-y))/m;
/*			if (d<0)
			{
				a=(-d)/c+1;
				c+=a*(m/e);
				d+=a*(n/e);
			}*/
			//printf("%lld %lld %lld %lld\n",c,d*m,(c*n-(x-y))%m,x-y);
			c=c&1;d=d&1;
			if (vx<0) {c=c^1;}
			if (vy<0) {d=d^1;}
			c=c*n;d=d*m;
			printf("%lld %lld\n",c,d);
		}
	}
	return 0;
}


#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define uLL unsigned long long
#define LL long long
uLL exgcd64(uLL a,uLL b,uLL *x,uLL *y)
{
	static uLL gcd;
	if (a==0)
	{
		*x=0;*y=1;
		return b;
	}
	else
	{
		gcd=exgcd64(b%a,a,y,x);
		*x-=(b/a)*(*y)+b;
		*y+=a;
		return gcd;
	}
}
int main()
{
//	freopen("in.in","r",stdin);
	LL i,j,k,n,m,a,b,c,d,x,y,vx,vy,e;
	scanf("%lld%lld%lld%lld%lld%lld",&n,&m,&x,&y,&vx,&vy);
	if ((vx==0)||(vy==0))
	{
		if (vx==0)
		{
			if ((x==0)||(x==n))
			{
				if (vy<0) {printf("%lld 0\n",x);}
				else {printf("%lld %lld\n",x,m);}
			}
			else
			{
				printf("-1\n");
			}
		}
		if (vy==0)
		{
			if ((y==0)||(y==m))
			{
				if (vx<0) {printf("0 %lld\n",y);}
				else {printf("%lld %lld\n",n,y);}
			}
			else
			{
				printf("-1\n");
			}
		}
	}
	else
	{
		if (vx<0)
		{
			x=n-x;
		}
		if (vy<0)
		{
			y=m-y;
		}
		//printf("%lld %lld %lld %lld\n",n,m,x,y);
		c=exgcd64(m,n,&a,&b);
		e=c;
		d=x-y;
		if (d<0) {d=(d%m+m)%m;}
		if (d%c!=0) {printf("-1\n");}
		else
		{
			//printf("%lld %lld %lld\n",b%c,d,((d/c)*(b/1)%(m/c)*n)%m);
			d=(d/c)*(b)%(m/c);
			if (d==0) {d+=m/c;}
			c=d;
			d=(c*n-(x-y))/m;
			if (d<0)
			{
				a=(-d)/c+1;
				c+=a*(m/e);
				d+=a*(n/e);
			}
			//printf("%lld %lld %lld %lld\n",c,d*m,(c*n-(x-y))%m,x-y);
			c=c&1;d=d&1;
			if (vx<0) {c=c^1;}
			if (vy<0) {d=d^1;}
			c=c*n;d=d*m;
			printf("%lld %lld\n",c,d);
		}
	}
	return 0;
}


#include <stdio.h>

int good(int a, int b) {
	return a == 0 ? 1 : b == 0 ? 0 : good(a % 10 == b % 10 ? a / 10 : a, b / 10);
}

int count(int n) {
	return n == 0 ? 0 : count(n / 10) + 1;
}

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

	scanf("%d", &n);
	k = count(n);
	ans = 0;
	for (x = 1; x * x <= n; x++) {
		int cnt;

		cnt = count(x * x);
		if (good(x * x, n) && ans < cnt)
			ans = cnt;
	}
	printf("%d\n", ans == 0 ? -1 : k - ans);
	return 0;
}


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

int main(void)
{
    long long n, i, s, nc, nop, min_nop;

    scanf("%lld", &n);
    min_nop = LLONG_MAX;

    for (i = 1; i * i <= n; i++) {
        s = i * i;
        nc = n;
        nop = 0;

        while (nc && s) {
            if (nc % 10 == s % 10) {
                s /= 10;
            } else {
                nop++;
            }

            nc /= 10;
        }

        if (s == 0) {
            while (nc) {
                nop++;
                nc /= 10;
            }

            if (nop < min_nop) {
                min_nop = nop;
            }
        }
    }

    printf("%lld\n", min_nop == LLONG_MAX ? -1 : min_nop);

    return 0;
}


#include <stdio.h>

#define N	13
#define Q	1010
#define MD1	998244353
#define MD	1000000007

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

long long pp[N + 1], qq[N + 1];

void init() {
	int i;

	pp[0] = qq[0] = 1;
	for (i = 1; i <= N; i++) {
		pp[i] = pp[i - 1] * 10;
		qq[i] = qq[i - 1] + pp[i];
	}
}

long long idx(long long a, long long b) {
	static int aa[N], bb[N];
	static long long aa_[N];
	long long cnt;
	int n, m, i, j, c;

	n = 0;
	while (a > 0)
		aa[n++] = a % 10, a /= 10;
	m = 0;
	while (b > 0)
		bb[m++] = b % 10, b /= 10;
	for (i = 0; i < n; i++)
		aa_[i] = (i == 0 ? 0 : aa_[i - 1]) + aa[i] * pp[i];
	cnt = m;
	for (i = n - 1, j = m - 1, c = 0; j >= 0; i--, j--)
		if (c < 0)
			cnt += qq[n - 1 - (m - 1 - j)] * (bb[j] - (j == m - 1 ? 1 : 0));
		else if (c > 0)
			cnt += (n - 1 - (m - 1 - j) == 0 ? 0 : qq[n - 2 - (m - 1 - j)]) * (bb[j] - (j == m - 1 ? 1 : 0));
		else {
			if (bb[j] <= aa[i]) {
				if (bb[j] < aa[i])
					c = -1;
				cnt += qq[n - 1 - (m - 1 - j)] * (bb[j] - (j == m - 1 ? 1 : 0));
			} else {
				c = 1;
				cnt += qq[n - 1 - (m - 1 - j)] * (aa[i] - (j == m - 1 ? 1 : 0));
				cnt += (n - 1 - (m - 1 - j) == 0 ? 0 : qq[n - 2 - (m - 1 - j)]) * (bb[j] - aa[i]) + (i == 0 ? 0 : aa_[i - 1]) + 1;
			}
		}
	return cnt;
}

int aa[N], bb[N];

long long count_(long long a, long long d) {
	long long b, cnt;

	cnt = 0;
	for (b = 1; b <= a; b++)
		if (idx(a, b) - b >= d)
			cnt++;
	return cnt;
}

long long count(long long a, long long d) {
	int m;
	long long cnt;

	cnt = 0;
	for (m = 1; pp[m - 1] - 1 <= a; m++) {
		long long lower = pp[m - 1] - 1, upper = min(pp[m], a + 1);

		while (upper - lower > 1) {
			long long b = (lower + upper) / 2;

			if (idx(a, b) - b >= d)
				upper = b;
			else
				lower = b;
		}
		cnt += min(pp[m], a + 1) - upper;
	}
	return cnt;
}

int main() {
	long long a, q, ans;

	init();
	scanf("%lld", &a);
	ans = Q * a % MD;
	for (q = -Q + 1; q <= Q; q++)
		ans = (ans - count(a, q * MD1)) % MD;
	ans = ans * MD1 % MD;
	if (ans < 0)
		ans += MD;
	printf("%lld\n", ans);
	return 0;
}


#include<stdio.h>
#define HELL 1000000007
char fast_char_var;
#define	fast_unsigned_dtype	unsigned long
fast_unsigned_dtype fast_unsigned_var;
fast_unsigned_dtype fast_unsigned()
{
	fast_char_var=getchar();
	while(fast_char_var<48||fast_char_var>57)
		fast_char_var=getchar();
	fast_unsigned_var=0;
	while(fast_char_var>47&&fast_char_var<58)
	{
		fast_unsigned_var=(fast_unsigned_var<<1)+(fast_unsigned_var<<3)+fast_char_var-48;
		fast_char_var=getchar();
	}
	return fast_unsigned_var;
}
int main()
{
	static unsigned long long ans2,ans,i,j,a[4010],b[4010],n;
	n=fast_unsigned();
	n++;
	a[0]=1;
	for(i=1;i<=n;i++)
	{
		b[0]=a[i-1];
		for(j=1;j<=i;j++)
			b[j]=(a[j-1]+b[j-1])%HELL;

		for(j=0;j<=i;j++)
			a[j]=b[j];
	}
	ans=b[n]+HELL-b[n-1];
	ans%=HELL;
	a[0]=1;
	n--;
	for(i=1;i<=n;i++)
	{
		b[0]=a[i-1];
		for(j=1;j<=i;j++)
			b[j]=(a[j-1]+b[j-1])%HELL;

		for(j=0;j<=i;j++)
			a[j]=b[j];
	}
	ans2=b[n]+HELL-b[n-1];
	ans2%=HELL;
	ans=ans+HELL-ans2;
	ans%=HELL;
	printf("%llu\n",ans);
	return 0;
}


#include <stdio.h>

#define MAX 4010
#define MOD 1000000007

int S[4010][4010];

void Generate(){
    int i, j;

    S[0][0] = 1;
    for (i = 1; i < MAX; i++){
        S[i][0] = 0;
        for (j = 1; j <= i; j++){
            S[i][j] = ( ((long long)S[i - 1][j] * j) + S[i - 1][j - 1]) % MOD;
        }
    }
}

int F(int n){
    int k;
    long long x, res = 0;
    for (k = 1; k <= n; k++){
        x = S[n][k];
        res = (res + (x * k)) % MOD;
    }
    return res;
}

int main(){
    Generate();
    int n;
    while (scanf("%d", &n) != EOF){
        printf("%d\n", F(n));
    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define maxn 1000

long p[maxn+1][5],xl[maxn+1][maxn+1][5],point[maxn+1],count=0;
__int64 value[maxn+1][maxn+1];
bool vis[maxn+1];

    long n,i,j,k,l;
    __int64 v1,v2;

void work()
{
        for (j=1;j<n;j++)
            if (i!=j)
                for (k=j+1;k<=n;k++)
                    if (i!=k)
    {
        v1=0;
        for (l=0;l<5;l++)
            v1+=xl[i][j][l]*xl[i][k][l];
        if (v1==0)
            continue;
        //v1=v1*v1;

        v2=value[i][j]*value[i][k];//value<>0
        // +
        if ((v1>0 && v2>0) || (v1<0 && v2<0))
        {
            vis[i]=false;
            return;
        }
    }
    vis[i]=true;
}

int main()
{
    scanf("%ld",&n);
    for (i=1;i<=n;i++)
        for (j=0;j<5;j++)
            scanf("%ld",&p[i][j]);
    for (i=1;i<=n;i++)
        for (j=1;j<=n;j++)
        {
            value[i][j]=0;
            for (k=0;k<5;k++)
            {
                xl[i][j][k]=p[j][k]-p[i][k];
                value[i][j]+=xl[i][j][k]*xl[i][j][k];
            }
        }

//    for (i=1;i<=n;i++)
//        vis[i]=true;
    for (i=1;i<=n;i++)
        work();
    for (i=1;i<=n;i++)
        if (vis[i]==true)
        {
            count++;
            point[count]=i;
        }
    printf("%ld\n",count);
    for (i=1;i<=count;i++)
        printf("%ld\n",point[i]);
    return 0;
}


#include<stdio.h>
long long int n,x;
long long int f(long long int a)
{
	if(a<2)
		return a+1;
  	x=f(a/2);
	x *= x;
	if(a & 1) x *= 2;
	x %= 1000000007;
	return x;
}
int main()
{
	scanf("%lld",&n);
	long long int a=f(n);
	printf("%lld",(a*(a+1)/2)%1000000007);
	return 0;
}


#include<stdio.h>
#include<math.h>
#define getchar getchar//_unlocked
#define M 1000000007

int getint ()
{
        int  n=0;
        char ch=getchar();

        while(ch<'0' || ch>'9')
            ch=getchar();

        while(ch>='0' && ch<='9')
        {
                n=n*10+ch-'0';
                ch=getchar();
        }

        return n;

}

unsigned long long gcd(unsigned long long a, unsigned long long b)
{

        unsigned long long div=a,did=b,rem;

        while((rem=did%div)!=0)
        {
                did=div;
                div=rem;

        }

        return div;

}

long long getlonglong ()
{
        long long  n=0;
        char ch=getchar();

        while(ch<'0' || ch>'9')
            ch=getchar();

        while(ch>='0' && ch<='9')
        {
                n=n*10+ch-'0';
                ch=getchar();
        }

        return n;

}

void sort(int a[] ,int start1 ,int end1 ,int start2 ,int end2)
{

        int temp[end1-start1 +1 + end2-start2+1 ];
        int i=start1,j=start2,count=0;

        while(i<=end1 && j<=end2)
        {
                if(a[i]>=a[j])
                    temp[count++]=a[i++];
                else
                    temp[count++]=a[j++];

        }

        for(;i<=end1;++i)
            temp[count++]=a[i];

        for(;j<=end2;++j)
            temp[count++]=a[j];

        for(i=0;i<count;++i)
            a[i+start1]=temp[i];

}

void mergesort(int a[] ,int start,int end)
{
        if(start==end)
            return;

        int middle =(start+end)/2;

        mergesort(a,start,middle);
        mergesort(a,middle+1,end);
        sort(a,start,middle,middle+1,end);

}

long long Ceil(long long a , long long n)
{
        if(a<0)
            return 0;

        if(a%n==0)
            return a/n;
        else
            return a/n +1 ;

}

int main()
{

        long long  two =2,n,x=1,m;

        m=getlonglong();

        n=m;

        if(m!=0)
            m=m-1;

        while(m)
        {
                if(m%2==0)
                {
                    two=(two*two)%M ;
                    m=m/2;
                }
                else
                {
                        x=(x*two)%M;
                        m--;
                }

        }

        if(n!=0)
            x=x*(1+2*x)%M;
        else
            x=1;

        printf("%I64d",x);
        return 0;
}


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

long long int power(int x, int y, int m)
{
	if (y==1) return x % m;
	if (y==0) return 1;
	long long int t = power(x, y / 2, m);
	if (y & 1)
		return (((t*t) % m) * x) % m;
	else return (t*t) % m;
}

int main()
{
	int i,j,x,y,n,m;
	scanf("%d %d", &n, &m);

	long long int res = (power(3, n, m) - 1 + m) % m;
	printf("%I64d\n", res);
	return 0;
}


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

int n,m;

int modpow(int d,int x,int M){
    long long res=1,temp=d;
    while(x){
        if(x&1){res*=temp; res%=M;}
        temp=(temp*temp)%M;
        x>>=1;
    }
    return (int)res;
}

main(){
    scanf("%d%d",&n,&m);
    int ans = (modpow(3,n,m)+m-1)%m;
    printf("%d\n",ans);
    return 0;
}


#include <stdio.h>
#include <string.h>
int buy[12][105],v[12][105],money[105],sale[12][105],space[105];
int main()
{
    int n,m,s,max=0,x,y,t,k,sum,total,i,j;
    char str[105];
    scanf("%d%d%d",&n,&m,&s);
    for(i=0;i<n;i++)
    {
        scanf("%s",str);
        for(j=0;j<m;j++)
            scanf("%d%d%d",&buy[i][j],&sale[i][j],&v[i][j]);
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            if(i==j)
                continue;
            sum=0;total=s;
            for(k=0;k<m;k++)
            {
                if(sale[j][k]>buy[i][k])
                    money[k]=sale[j][k]-buy[i][k];
                else
                    money[k]=0;
                space[k]=v[i][k];
            }
            for(x=0;x<m-1;x++)
                for(y=0;y<m-x-1;y++)
                {
                    if(money[y]<money[y+1])
                    {
                        t=money[y];money[y]=money[y+1];money[y+1]=t;
                        t=space[y];space[y]=space[y+1];space[y+1]=t;
                    }
                }
            for(k=0;k<m;k++)
            {
                if(space[k]>=total)
                {
                    sum+=money[k]*total;
                    break;
                }
                else
                {
                    sum+=money[k]*space[k];
                    total-=space[k];
                }
            }
            if(sum>max)
                max=sum;
        }
    }
    printf("%d\n",max);
    return 0;
}


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

long long gcd(long long a, long long b) {
	return b == 0 ? a : gcd(b, a % b);
}

int main() {
	static char cc[1048576], *s = cc;
	long long x, y;

	scanf("%lld%lld", &x, &y);
	if (gcd(x, y) != 1) {
		printf("Impossible\n");
		return 0;
	}
	while (x > 1 || y > 1) {
		long long k;
		char c;

		if (x > y) {
			k = x / y;
			if (y == 1)
				k--;
			x %= y;
			c = 'A';
		} else {
			k = y / x;
			if (x == 1)
				k--;
			y %= x;
			c = 'B';
		}
		s += sprintf(s, "%lld%c", k, c);
	}
	printf("%s\n", cc);
	return 0;
}


/*
ID: Sfiction
OJ: CF128 Div2
PROB: D
*/
#include <stdio.h>
int main()
{
    double a,b,m,vx,vy,vz;
    double x,z,t;
    scanf("%lf%lf%lf",&a,&b,&m);
    scanf("%lf%lf%lf",&vx,&vy,&vz);
    t=-m/vy;
    x=a/2+t*vx;
    z=t*vz;
    while (x<0||x>a)
        if (x>a) x=2*a-x;
        else x=-x;
    while (z<0||z>b)
        if (z>b) z=2*b-z;
        else z=-z;
    printf("%.10lf %.10lf",x,z);
    return 0;
}


#include<stdio.h>
int main()
{
	long long int x;

	scanf("%lld", &x);

	if(x<=3 || x%2==0)
	{
		printf("NO");
	}
	else
	{
		printf("1 %lld", (x-3)/2);
	}
	return 0;
}


#include<stdio.h>
int main(){
    long long int r, x = 1, y;
    scanf("%lld", &r);
    if(r % 2 == 0 || r < 5)
        printf("NO\n");
    else{
        y = (r - 3) / 2;
        printf("%lld %lld\n", x, y);
    }
    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 SixSidedDieState[24][3]={
  0,1,2,  0,2,4,  0,4,3,  0,3,1,
  1,5,2,  1,2,0,  1,0,3,  1,3,5,
  2,1,5,  2,5,4,  2,4,0,  2,0,1,
  3,1,0,  3,0,4,  3,4,5,  3,5,1,
  4,0,2,  4,2,5,  4,5,3,  4,3,0,
  5,3,4,  5,4,2,  5,2,1,  5,1,3
};

int SixSidedDieRotateCenter[24]={
   1, 2, 3, 0,
   5, 6, 7, 4,
   9,10,11, 8,
  13,14,15,12,
  17,18,19,16,
  21,22,23,20
};

int SixSidedDieRotateRight[24]={
  12, 5,10,19,
  15,22,11, 3,
   0, 4,21,16,
  23, 6, 2,18,
  13, 1, 9,20,
   7,14,17, 8
};

int SixSidedDieRotateLeft[24]={
  16,13, 6,11,
   0,12,23, 8,
  17, 1, 5,22,
  19,20, 7, 3,
  21,14, 2,10,
   9, 4,15,18
};

int die[24][6],center[24],right[24],left[24],center3[24],right3[24],left3[24];
void dieInit(){
  int i,j;
  rep(i,24){
    rep(j,3) die[i][j] = SixSidedDieState[i][j];
    REP(j,3,6) die[i][j] = 5-SixSidedDieState[i][5-j];
    center[i] = SixSidedDieRotateCenter[i];
    right[i]  = SixSidedDieRotateRight[i];
    left[i]   = SixSidedDieRotateLeft[i];
  }
  rep(i,24){
    center3[i]= center[center[center[i]]];
    right3[i] = right[right[right[i]]];
    left3[i]  = left[left[left[i]]];
  }
}

#define ll long long
#define ull unsigned ll

#define ULL_HASH_SIZE 2000
ull ull_hash_n[ULL_HASH_SIZE]; int ull_hash_d[ULL_HASH_SIZE];

void ullHashClear(void){ memset(ull_hash_d,0,ULL_HASH_SIZE*sizeof(int)); }
int ullHashFunction(ull a){ return (a*1007)%ULL_HASH_SIZE; }

int ullHashGet(ull a){
  int i=ullHashFunction(a);
  for(;;){
    if(ull_hash_n[i]==a && ull_hash_d[i]) break;
    if(!ull_hash_d[i]) break;
    i++; if(i==ULL_HASH_SIZE) i=0;
  }
  if(ull_hash_n[i]!=a) return 0; return ull_hash_d[i];
}

void ullHashSet(ull a,int n){
  int i=ullHashFunction(a);
  for(;;){
    if(ull_hash_n[i]==a && ull_hash_d[i]) break;
    if(!ull_hash_d[i]) break;
    i++; if(i==ULL_HASH_SIZE) i=0;
  }
  ull_hash_n[i]=a; ull_hash_d[i]=n;
}

int ullHashIncrease(ull a){
  int i=ullHashFunction(a);
  for(;;){
    if(ull_hash_n[i]==a && ull_hash_d[i]) break;
    if(!ull_hash_d[i]) break;
    i++; if(i==ULL_HASH_SIZE) i=0;
  }
  ull_hash_n[i]=a; return ++ull_hash_d[i];
}

void intSort(int d[],int s){int i=-1,j=s,k,t;if(s<=1)return;k=(d[0]+d[s-1])/2;for(;;){while(d[++i]<k);while(d[--j]>k);if(i>=j)break;t=d[i];d[i]=d[j];d[j]=t;}intSort(d,i);intSort(d+j+1,s-j-1);}
void intReverse(int d[],int size){int a=0,b=size-1,t;while(a<b)t=d[a],d[a]=d[b],d[b]=t,a++,b--;}
int intNextPermutation(int d[],int size){int i,j,k;for(k=size-2;k>=0;k--)if(d[k]<d[k+1])break;if(k<0){intReverse(d,size);return 0;}for(i=size-1;;i--)if(d[i]>d[k])break;j=d[i],d[i]=d[k],d[k]=j;intReverse(d+k+1,size-k-1);return 1;}

int main(){
  int i,j,k,l,m,n;
  int res;
  char tmp[10];
  int in[10], go[10];
  ull hs, mx;

  dieInit();
  while(scanf("%s",tmp)==1){
    ullHashClear();
    res = 0;

    rep(i,6) in[i] = tmp[i];
    intSort(in,6);

    do{
      mx = 0;
      rep(i,24){
        rep(k,6) go[k] = in[die[i][k]];
        hs = 0;
        rep(k,6) hs = hs * 1007 + go[k];
        if(mx < hs) mx = hs;
      }
      if( ullHashIncrease(mx)==1 ) res++;
    }while(intNextPermutation(in,6));

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

  return 0;
}


#include <stdio.h>
const long long MOD = 1e9 + 7;
struct A{int first; int second;};
struct A fibp(long long n) {
    long long a = 1, b = 0;
    long long s = 0, r = 1;
    for ( ; n; n >>= 1) {
        if (n & 1) {
            long long na = (a*s + b*r) % MOD;
            long long nb = (a*r + b*s + b*r) % MOD;
            a = na;
            b = nb;
        }
        long long ns = (s*s + r*r) % MOD;
        long long nr = (2*s*r + r*r) % MOD;
        s = ns;
        r = nr;
    }
    struct A res;
    res.first = a;
    res.second = b;
    return res;
}

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

int f(long long n, int k) {
    static int A[2001];
    for (int i = 0; i <= k; ++i) {
        A[i] = powmod(n + i, k);
    }
    struct A ab = fibp(n + 1);
    int a = ab.first, b = ab.second;
    int r = 0;
    for (int lA = k + 1; lA; --lA) {
        a += b; if (a >= MOD) {a -= MOD;}
        b += a; if (b >= MOD) {b -= MOD;}
        r += (long long)A[0] * a % MOD; if (r >= MOD) {r -= MOD;}
        for (int i = 0; i < lA - 1; ++i) {
            A[i] -= A[i + 1]; if (A[i] < 0) {A[i] += MOD;}
        }
    }
    return r;
}

int main(void) {
    long long n;
    int k;
    scanf("%lld%d", &n, &k);
    int ans = f(n + 1, k) - f(1, k); if (ans < 0) {ans += MOD;}
    printf("%d\n", ans);
    return 0;
}


#include <stdio.h>
const long long MOD = 1e9 + 7;
struct A{int first; int second;};
struct A fibp(long long n) {
    long long a = 1, b = 0;
    long long s = 0, r = 1;
    for ( ; n; n >>= 1) {
        if (n & 1) {
            long long na = (a*s + b*r) % MOD;
            long long nb = (a*r + b*s + b*r) % MOD;
            a = na;
            b = nb;
        }
        long long ns = (s*s + r*r) % MOD;
        long long nr = (2*s*r + r*r) % MOD;
        s = ns;
        r = nr;
    }
    struct A res;
    res.first = a;
    res.second = b;
    return res;
}

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

int f(long long n, int k) {
    int A[50];
    for (int i = 0; i <= k; ++i) {
        A[i] = powmod(n + i, k);
    }
    struct A ab = fibp(n + 1);
    int a = ab.first, b = ab.second;
    int r = 0;
    for (int lA = k + 1; lA; --lA) {
        a += b; if (a >= MOD) {a -= MOD;}
        b += a; if (b >= MOD) {b -= MOD;}
        r += (long long)A[0] * a % MOD; if (r >= MOD) {r -= MOD;}
        for (int i = 0; i < lA - 1; ++i) {
            A[i] -= A[i + 1]; if (A[i] < 0) {A[i] += MOD;}
        }
    }
    return r;
}

int main(void) {
    long long n;
    int k;
    scanf("%lld%d", &n, &k);
    int ans = f(n + 1, k) - f(1, k); if (ans < 0) {ans += MOD;}
    printf("%d\n", ans);
    return 0;
}


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

int n;
int mi[10][10], mx[10][10], ac[10][10];
int ind[10];
int now[10];

int res, res_cost;

void solve(int a,int b,int cost){
  int i,j,send;
  int aa = ind[a], bb = ind[b];

  if(a>=n){
    if(now[n-1] < 0) return;
    if(now[n-1] == res && cost > res_cost) res++;
    if(now[n-1] < res) res = now[n-1], res_cost = cost;
    return;
  }

  if(b>=n){
    if(aa!=0 && aa!=n-1 && now[aa]) return;
    solve(a+1,a+2,cost);
    return;
  }

  REP(i,-mx[aa][bb],mx[aa][bb]+1){
    if(-mi[aa][bb]<i && i<mi[aa][bb]) continue;
    if( (bb-aa)*i < 0 ) continue;

    send = cost;
    if(i) send += ac[aa][bb];
    send += i*i;

    now[aa]-=i; now[bb]+=i;
    solve(a,b+1,send);
    now[aa]+=i; now[bb]-=i;
  }
}

int main(){
  int i,j,k,l,m;
  int a,b,c;

  while(scanf("%d",&n)==1){
    m = n*(n-1)/2;
    while(m--){
      scanf("%d%d%d%d%d",&i,&j,&a,&b,&c); i--; j--;
      mi[i][j] = mi[j][i] = a;
      mx[i][j] = mx[j][i] = b;
      ac[i][j] = ac[j][i] = c;
    }
    REP(i,1,n-1) ind[i-1] = i; ind[n-2]=0; ind[n-1]=n-1;
    rep(i,n) now[i] = 0;

    res = 1000000;

    solve(0,1,0);

    if(res == 1000000) res = res_cost = -1;
    printf("%d %d\n",res, res_cost);
  }

  return 0;
}


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

const int mod = (int)1e9 + 7;

long long power(long long a, int b) {
  long long ret = 1;
  while (b > 0) {
    if (b % 2) {
      ret *= a;
      ret %= mod;
    }
    a = (a * a) % mod;
    b /= 2;
  }
  return ret;
}

long long inverse_mod(long long n) {
  return power(n, mod - 2);
}

long long choose(long long n, int k) {
  if (k > n) {
    return 0;
  }

  int q = 1;
  for (int i = 1; i <= k; i++) {
    q *= i;
    q %= mod;
  }

  long long p = 1;
  for (long long i = n; i > n - k; i--) {
    p *= i;
    p %= mod;
  }

  return p * inverse_mod(q) % mod;
}

int main(void) {
  int m;
  scanf("%i", &m);

  char s[50];
  sprintf(s, "%i", m);

  int len = strlen(s);
  long long f[len+1][len+1][2];

  memset(f, 0, sizeof(f));

  f[0][0][1] = 1;
  for (int i = 0; i < len; i++) {
    for (int j = 0; j <= len; j++) {
      for (int eq = 0; eq <= 1; eq++) {
        long long cur = f[i][j][eq];
        for (char d = '0'; d <= '9'; d++) {
          if (eq && d > s[i]) {
            continue;
          }
          int n_eq = eq && (d == s[i]);
          int is_lucky = (d == '4' || d == '7') ? 1 : 0;
          f[i+1][j+is_lucky][n_eq] += cur;
          f[i+1][j+is_lucky][n_eq] %= mod;
        }
      }
    }
  }

  long long counts[len+1];
  for (int i = 0; i <= len; i++) {
    counts[i] = f[len][i][0] + f[len][i][1];
    counts[i] %= mod;
  }
  counts[0] = (counts[0] + mod - 1) % mod;

  long long dp[len+2][50*len+1][10];
  memset(dp, 0, sizeof(dp));

  dp[0][0][6] = 1;
  for (int i = 0; i <= len; i++) {
    for (int s = 0; s <= 7*len; s++) {
      for (int r = 0; r <= 6; r++) {
        long long cur = dp[i][s][r];
        if (!cur) continue;
        for (int j = 0; j <= r; j++) {
          if (s+j*i <= 7 * len && r - j >= 0) {
            dp[i+1][s+j*i][r-j] += cur * choose(counts[i], j) % mod;
            dp[i+1][s+j*i][r-j] %= mod;
          }
        }
      }
    }
  }

  const int six_fact = 720;

  long long take[len+1];
  for (int i = 0; i <= len; i++) {
    take[i] = dp[len+1][i][0] * six_fact % mod;
  }

  long long ans = 0;
  for (int c = 1; c <= len; c++) {
    for (int i = 0; i < c; i++) {
      ans += take[i] * counts[c] % mod;
      ans %= mod;
    }
  }

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

  return 0;
}


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