# -*- coding: utf-8 -*- N = int(input()) M = int(input()) n = N%10 net_m = (M-1)%4 ans = n**net_m % 10 print(ans)