#include using namespace std; #define REP(i,a,n) for(int i=(a); i<(int)(n); i++) #define rep(i,n) REP(i,0,n) #define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it) #define ALLOF(c) (c).begin(), (c).end() typedef long long ll; typedef unsigned long long ull; int calc_mod(const string& str, int mod){ int ret = 0; int now = 0; rep(i,str.size()){ now = now * 10 + (str[i]-'0'); if(now>=mod){ now = now%mod; } } return now; } int main(){ string N, K; cin >> N; cin >> K; int x = calc_mod(N, 6); int y = calc_mod(K, 2); if(y == 0) y = 2; int ret = 1; rep(i,y){ ret *= x; ret %= 6; } string str = "285714"; cout << str[(ret+5)%6] << endl; return 0; }