program main implicit none character*10000::SM,SN integer::N,M,i,nlen,mlen integer::ntail,mtail,mtail2,mtail3 data mtail2/0/ read *,SN read *,SM nlen = LEN_TRIM(SN) mlen = LEN_TRIM(SM) ntail = ICHAR(SN(nlen:nlen))-ICHAR('0') if(mlen.eq.1.and.SM(1:1).eq.'0') then print '(i0)', 1 return end if select case (ntail) case(0,1,5,6) print '(i0)', ntail case(4,9) mtail = ICHAR(SM(mlen:mlen))-ICHAR('0') print '(i0)', MOD(ntail**(MOD(2+mtail-1,2)+1),10) case(2,3,7,8) mtail = ICHAR(SM(mlen:mlen))-ICHAR('0') if(mlen.ge.2) mtail2 = ICHAR(SM(mlen-1:mlen-1))-ICHAR('0') print '(i0)', MOD(ntail**(MOD(4+mtail2*10+mtail-1,4)+1),10) end select end program main