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

#define MAX_N 1005
#define MAX_M 1005

int dp[MAX_N][MAX_M];

int main() {
  int N, M;
  cin >> N >> M;
  
  vector<int> A(N), B(M);
  for (auto &i : A) cin >> i;
  for (auto &i : B) cin >> i;
  
  for (int i = 0; i <= N; ++i) {
    for (int j = 0; j <= M; ++j) dp[i][j] = 1001001001;
  }
  for (int i = 0; i <= N; ++i) dp[i][0] = i;
  for (int j = 0; j <= M; ++j) dp[0][j] = j;
  
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < M; ++j) {
      int ni = i+1, nj = j+1;
      dp[ni][nj] = min(dp[ni][nj], dp[i][nj]+1);
      dp[ni][nj] = min(dp[ni][nj], dp[ni][j]+1);
      dp[ni][nj] = min(dp[ni][nj], dp[i][j] + (A[i] == B[j] ? 0 : 1));
    }
  }
  
  cout << dp[N][M] << endl;
}  
/*

   author : dunk_066

*/

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int mod=1e9+7;
#define FAST ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define all(c) c.begin(),c.end()
#define pb push_back
#define vi(a,x) vector<int>a(x)
#define vl(a,x) vector<ll>a(x)
#define ss second
#define ff first

void solve(){
     int n,m;cin>>n>>m;
     int sum = 0,mnm = 100;
     for(int i = 0;i < n;i++){
          for(int j = 0;j < m;j++){
               int x;cin>>x;
               sum += x;
               mnm = min(mnm,x);
          }
     }
     cout<<(sum - mnm*(m * n));
}

int main(){
        FAST;
	int t=1;//cin>>t;
	while(t--)
	solve();
        #ifndef ONLINE_JUDGE
        cout << "\nTime Elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " sec\n";
        #endif
	return 0;
}

// ============== Notes =============== //
// common bugs
// - int overflow (especially intermediate expressionns)
// - array bounds (indices bigger than MAXN?)
// - special cases (n=1? graph not connected?)