#include #define rep(i, l, r) for (int i = (l); i < (r); i++) using namespace std; typedef long long ll; int main() { int p, q, r; ll K; vector ans(10000); rep(i, 1, 4) { cin >> ans[i]; ans[i] %= 10; } cin >> K; int a[10][10][10]; rep(i, 0, 10) rep(j, 0, 10) rep(k, 0, 10) a[i][j][k] = -1; for (ll i = 1; i < K; i++) { int x = a[ans[i]][ans[i + 1]][ans[i + 2]]; if (x == -1) { a[ans[i]][ans[i + 1]][ans[i + 2]] = i; } else { K = (K - i) % (i - x) + i; } ans[i + 3] = (ans[i] + ans[i + 1] + ans[i + 2]) % 10; } cout << ans[K] << endl; }