# coding: utf-8 #yuki_167 n=int(raw_input()) m=int(raw_input()) if m==0: res=1 elif n%10==0: res=0 elif n%10==1: res=1 elif n%10==2: if m%4==1: res=2 elif m%4==2: res=4 elif m%4==3: res=8 elif m%4==0: res=6 elif n%10==3: if m%4==1: res=3 elif m%4==2: res=9 elif m%4==3: res=7 elif m%4==0: res=1 elif n%10==4: if m%2==1: res=4 elif m%2==0: res=6 elif n%10==5: res=5 elif n%10==6: res=6 elif n%10==7: if m%4==1: res=7 elif m%4==2: res=9 elif m%4==3: res=3 elif m%4==0: res=1 elif n%10==8: if m%4==1: res=8 elif m%4==2: res=4 elif m%4==3: res=2 elif m%4==0: res=6 elif n%10==9: if m%2==1: res=9 elif m%2==0: res=1 elif m%10==0: res=0 print res