n=int(input()) m=int(input()) pos=n%10 if m==0: print(1) else: if pos==0: print(0) elif pos==1: print(1) elif pos==2: if m%4==1: print(2) elif m%4==2: print(4) elif m%4==3: print(8) elif m%4==0: print(6) elif pos==3: if m%4==1: print(3) elif m%4==2: print(9) elif m%4==3: print(7) elif m%4==0: print(1) elif pos==4: if m%2==1: print(4) else : print(6) elif pos==5: print(5) elif pos==6: print(6) elif pos==7: if m%4==1: print(7) elif m%4==2: print(9) elif m%4==3: print(3) elif m%4==0: print(1) elif pos==8: if m%4==1: print(8) elif m%4==2: print(4) elif m%4==3: print(2) elif m%4==0: print(6) elif pos==9: if m%2==1: print(9) else : print(1)