#include using namespace std; typedef long long int ll; typedef pair P; typedef vector VI; typedef vector VVI; #define REP(i,n) for(ll i=0;i<(n);i++) #define ALL(v) v.begin(),v.end() template bool chmax(T &x, const T &y) {return (x bool chmin(T &x, const T &y) {return (x>y)?(x=y,true):false;}; constexpr ll MOD=10; constexpr ll INF=2e18; VVI matmult(VVI a, VVI b){ int p=a.size(), q=b[0].size(), r=a[0].size(); VVI ret(p,VI(q)); REP(i,p)REP(j,q){ ll sum=0; REP(k,r){ sum=(sum+a[i][k]*b[k][j]%MOD)%MOD; } ret[i][j]=sum; } return ret; } VVI matpower(VVI x, ll y){ int n=x.size(); VVI ret(n,VI(n,0)); REP(i,n) ret[i][i]=1; while(y){ if(y&1) ret=matmult(ret,x); x=matmult(x,x); y>>=1; } return ret; } int main(){ ll p, q, r, k; cin >> p >> q >> r >> k; p%=10; q%=10; r%=10; if(k<=3){ if(k==1) cout << p << endl; else if(k==2) cout << q << endl; else cout << r << endl; return 0; } VVI x(3,VI(3,0)); x[0][2]=1; x[1][2]=1; x[2][2]=1; x[2][1]=1; x[1][0]=1; VVI a(1,VI(3,0)); a[0][0]=p; a[0][1]=q; a[0][2]=r; x=matpower(x,k-3); cout << matmult(a,x)[0][2] << endl; return 0; }