#include <iostream>
#include <deque>
#include <map>
#define ll long long
using namespace std;
ll m, a, b, first, n, l, i, j, t, nr,ft,st;
map<ll, ll> x;
deque <ll> v;
int main()
{
cin >> m >> first >> a >> b >> n >> l;
bool ok = true;
ft = 1;
st = 1;
for (i = 1; i <= 3 * m; ++i) ft *= 10;
for (i = 1; i <=m; ++i) st *= 10;
ll db = 0;
while (ok)
{
db++;
nr = a * first + b;
v.clear();
while (nr > 0)
{
v.push_front(nr % 10);
nr = nr / 10;
}
nr = 0;
while (v.size() < (4 * m)) v.push_front(0);
for (i = m; i < 3 * m; ++i) { nr *= 10, nr += v[i]; }
// cout << nr << " ";
if (x[nr] == 0) x[nr] = db;
else
{
t = db - x[nr];
cout << db - x[nr] << "\n";
ok = false;
break;
}
first = nr;
}
n -= x[nr] - 1;
ft = n / t;
st = n % t;
ll act = 0;
map<ll, ll>::iterator e;
e = x.end();
while(e!=x.begin())
{
--e;
if (e->second< x[nr])
{
act += 1;
if (act == l)
{
cout << e->first;
return 0;
}
}
else
{
act += ft;
if (l <= act)
{
cout << e->first;
return 0;
}
if (e->second < x[nr] + st)
{
act++;
if (l <= act)
{
cout << e->first;
return 0;
}
}
}
}
}
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu
// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file