#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define forn(i, n) for (int i = 0; i < n; i++)
#define fori(i, a, b) for (int i = a; i <= b; i++)
#define ff first
#define ss second

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    LL n, m;
    cin >> n >> m;
    vector<LL> a(n), b(m);
    forn(i, n) cin >> a[i];
    forn(i, m) cin >> b[i];
    LL dp[1005][1005];
    forn(i, n + 1) forn(j, m + 1)
    {
        if (i == 0)
            dp[i][j] = j;
        else if (j == 0)
            dp[i][j] = i;
        else
            dp[i][j] = min(dp[i][j - 1] + 1, min(dp[i - 1][j] + 1, dp[i - 1][j - 1] + (a[i - 1] != b[j - 1])));
    }
    cout << dp[n][m] << endl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
	cin.tie(nullptr)->sync_with_stdio(false);
	int n,k;
	cin >> n >> k;
	for(int i = 0; i<k; i++){
		string s = to_string(n);
		sort(s.begin(),s.end());
		int low = stoll(s);
		reverse(s.begin(),s.end());
		int high = stoll(s);
		n = high-low;
	}
	cout << n << "\n";
	return 0;
}