#include<bits/stdc++.h>
#define PI 3.141592653589793238462
#define eps 1e-10
using namespace std;
typedef long long ll;
typedef long double db;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<db,db> pdd;
int main(){
    ll x,y,a,b;cin>>x>>y>>a>>b;
    ll now=0;
    while(x<(x+b)/a){
        if(x>=y/a) break;
        x*=a;now++;
    }
    if(x<y) now+=(y-x-1)/b;
    cout<<now<<endl;
}
#include<cmath>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
//#define N 

int f(int x,int y,int m)// x^y%m
{
	int ans=1;
//	int n=1;
	while(y)
	{
		if(y&1)
		{
			ans=ans*x%m;
		}
		x=x*x%m;
		y=y>>1;
	}
	return ans;
}

int a,b,c;
int h[25],mark[10];

int main()
{
	scanf("%d%d%d",&a,&b,&c);
	
	a=a%10;
	memset(mark,-1,sizeof mark);
	h[0]=a;mark[a]=0;
	int mod,j;
	for(int i=1;i<=10;++i)
	{
		h[i]=h[i-1]*a%10;
//		printf("		%d\n",h[i]);
		if(mark[h[i]]<0)mark[h[i]]=i;
		else{
			j=mark[h[i]];
			mod=i-mark[h[i]];
//			printf("		%d %d\n",i,j);
			break;
		}
	}
	//b^c
	int e=f(b%mod,c,mod)-1;
	if(e<0)e+=mod;
//	printf("	%d %d\n",mod,e);
	printf("%d",h[e]);
	return 0;
}
