import java.io.*; import java.util.*; class Main { public static void main(String args[])throws Exception { BufferedReader bu=new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb=new StringBuilder(); String s[]=bu.readLine().split(" "); int p=Integer.parseInt(s[0]),q=Integer.parseInt(s[1]),r=Integer.parseInt(s[2]); int ans[][]=power(Long.parseLong(s[3])-4); int f[][]={{r%M},{q%M},{p%M}}; ans=multiply(ans,f); System.out.println(ans[0][0]); } static int M=10; static int[][] power(long n) { int a[][]={{1,1,1},{1,0,0},{0,1,0}},res[][]=new int[3][3]; int i; for(i=0;i<3;i++) Arrays.fill(res[i],1); while(n!=0) { if(n%2==1) res=multiply(res,a); n>>=1; a=multiply(a,a); } return res; } static int[][] multiply(int a[][],int b[][]) { int i,j,k,ans[][]=new int[a.length][b[0].length]; //System.out.println(a.length+" "+a[0].length+" "+b.length+" "+b[0].length); for(i=0;i