print((function (n, m) if m=="0" then return 1 end m=tonumber(m) if n=="0" then return 0 end n=tonumber(n) local h={} for j=1,4 do local k=j~=4 and j or 0 h[k]={} for i=1,9 do h[k][i]=i^j%10 end end return h[m%4][n] end)(io.stdin:read("*l"):sub(-1), io.stdin:read("*l"):sub(-2)))