#include <bits/stdc++.h> using namespace std; int main() { int n, m, k, x; cin >> n >> m >> k >> x; m %= n; while(k >= 1) { m *= 10; m = m * m % n; k--; } m += x; m %= n; cout << m << endl; return 0; }