#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
using namespace std;
typedef long long unsigned int ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

int main() {
    int h,w;
    cin >> h >> w;

    int min_ = INF;

    vector<vector<int> > a(h,vector<int>(w));

    for(int i = 0;i<h; i++){
        for(int j = 0; j<w; j++){
            cin >> a[i][j];
            min_ = min(a[i][j],min_);
        }
    }

    int ans = 0;
        for(int i = 0;i<h; i++){
        for(int j = 0; j<w; j++){
            ans +=  (a[i][j]-min_);
        }
    }
    cout << ans << endl;
}
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  int N, M;
  cin >> N >> M;
  
  int A[N];
  int B[M];
  int dp[N][M];
  
  for(int i = 0; i < N; i++) cin >> A[i];
  for(int i = 0; i < M; i++) cin >> B[i];
    
  if(A[0] == B[0]) dp[0][0] = 0;
    else dp[0][0] = 1;
  
  for(int i = 1; i < N; i++){
    if(A[i] == B[0]) dp[i][0] = i;
    else dp[i][0] = dp[i-1][0] + 1;
  }
  for(int i = 1; i < M; i++){
    if(A[0] == B[i]) dp[0][i] = i;
    else dp[0][i] = dp[0][i-1] + 1;
  }
    
    
  
  for(int i = 1; i < N; i++){
    for(int j = 1; j < M; j++){
      int tmp = min(dp[i-1][j] + 1, dp[i][j-1] + 1);
      if(A[i] == B[j]) dp[i][j] = min(tmp, dp[i-1][j-1]);
      else dp[i][j] = min(tmp, dp[i-1][j-1] + 1);
        }
  }

  
  cout << dp[N-1][M-1] << endl;
  
return 0;

}