n = io.read() m = io.read() nl = tonumber(string.sub(n, #n, #n)) mnum = 0 if(#m == 1) then mnum = tonumber(m) else mnum = 100 + tonumber(string.sub(m, #m - 1, #m)) end if(mnum == 0) then print(1) elseif(nl == 0 or nl == 1 or nl == 5 or nl == 6) then print(nl) elseif(nl == 2) then a = {6, 2, 4, 8} print(a[mnum % 4 + 1]) elseif(nl == 3) then a = {1, 3, 9, 7} print(a[mnum % 4 + 1]) elseif(nl == 4) then print(mnum % 2 == 0 and 6 or 4) elseif(nl == 7) then a = {1, 7, 9, 3} print(a[mnum % 4 + 1]) elseif(nl == 8) then a = {6, 8, 4, 2} print(a[mnum % 4 + 1]) else print(mnum % 2 == 0 and 1 or 9) end