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

#define pb              push_back
#define SZ(x)           (int)x.size()
#define F               first
#define S               second
#define ALL(x)          x.begin(), x.end()
#define fast            ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define FOR(i, a, b)    for(int i = a; i < b; ++i)

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const ll INF = 1e18;
const int MOD = 1e9 + 7;
const ld PI = acosl(-1.0);
const ld EPS = 1e-9;
#define int long long

const int N = 1000 + 5;
int dp[N][N];
std::vector<int> a(N), b(N);

int f(int i, int j) {
        if(i == -1 && j == -1) return 0;
        if(i == -1) return j+1;
        if(j == -1) return i+1;
        if(dp[i][j] != -1) return dp[i][j];

        int &res = dp[i][j];
        res = INF;
        res = min(res, f(i-1, j) + 1);
        res = min(res, f(i, j-1) + 1);
        res = min(res, f(i-1, j-1) + (a[i] != b[j]));
        return res;
}

void solve() {
        memset(dp, -1, sizeof(dp));
        int n, m;
        cin >> n >> m;
        FOR(i, 0, n) {
                cin >> a[i];
        }
        FOR(j, 0, m) {
                cin >> b[j];
        }
        cout << f(n-1, m-1) << endl;
}       

signed main() {
        fast;
        int T = 1;
        // cin >> T;
        while(T--) {
                solve();
        }
}

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

#define fo(i, x, y) for (int i = (x); i <= (y); ++i)
#define fd(i, x, y) for (int i = (x); i >= (y); --i)

typedef long long ll;

const int p[10][4] = {{0, 0, 0, 0}
					, {0, 0, 0, 0}
					, {6, 2, 4, 8}
					, {1, 3, 9, 7}
					, {6, 4, 0, 0}
					, {0, 0, 0, 0}
					, {0, 0, 0, 0}
					, {1, 7, 9, 3}
					, {6, 8, 4, 2}
					, {1, 9, 0, 0}};
					
int getint()
{
	char ch;
	int res = 0, p;
	while (!isdigit(ch = getchar()) && (ch ^ '-'));
	p = ch == '-'? ch = getchar(), -1 : 1;
	while (isdigit(ch))
		res = (res << 3) + (res << 1) + (ch ^ 48), ch = getchar();
	return res * p;
}

ll pw(ll a, ll b, ll mod)
{
	ll res = 1;
	while (b)
	{
		if (b & 1) (res *= a) %= mod;
		(a *= a) %= mod;
		b >>= 1;
	}
	return res;
}

int main()
{
	ll a, b, c;
	cin >> a >> b >> c;
	a %= 10;
	int s2 = pw(b, c, 2), s4 = pw(b, c, 4);
	switch(a)
	{
		case 0: printf("0\n"); break;
		case 1: printf("1\n"); break;
		case 2: printf("%d\n", p[2][s4]); break;
		case 3: printf("%d\n", p[3][s4]); break;
		case 4: printf("%d\n", p[4][s2]); break;
		case 5: printf("5\n"); break;
		case 6: printf("6\n"); break;
		case 7: printf("%d\n", p[7][s4]); break;
		case 8: printf("%d\n", p[8][s4]); break;
		case 9: printf("%d\n", p[9][s2]); break;
	}
	return 0;
}