#include <bits/stdc++.h>
using namespace std;

int main() {
	string n, m;
	cin >> n >> m;
	int n2 = n[n.size() - 1] - '0';
	int m2;
	if (m.size() == 1) {
		m2 = m[0] - '0';
	}
	else {
		m2 = (m[m.size() - 2] - '0') * 10 + (m[m.size() - 1] - '0');
	}
	cout << (int)pow(n2, m2 % 4) % 10 << endl;

	return 0;
}