N=int(input()) K=int(input()) ans=pow(N,K,6) str="428571" print(str[ans])