a,b=$<.map &:to_i c=b%4 d=a%10 p b<1?1:d<2?d: d<3?[6,2,8,4][c]:d<4?[1,3,9,7][c]:d<5?[6,4,6,4][c]:d<7?d: d<8?[1,7,9,3][c]:d<9?[6,8,4,2][c]:[1,9,1,9][c]