#include using namespace std; int s[] = {2, 8, 5, 7, 1, 4}; int main() { string a, b; cin >> a >> b; int x = 0; for ( char c:a ) { x = (x * 10 + (c - '0')) % 6; } if ( (b.back() - '0')%2==0 ) x = x * x % 6; cout << s[(x + 5) % 6] << '\n'; return 0; }