#include<bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define ll long long
#define ull unsigned long long
#define db double
#define pii pair<int,int>
#define pli pair<ll,int>
#define pil pair<int,ll>
#define pll pair<ll,ll>
#define ti3 tuple<int,int,int>
#define mat vector<vector<int>>
const int inf = 1 << 30;
const ll mod = 1e9 + 7;
const ll linf = 1LL << 62;
const db EPS = 1e-7;
template<class T> void chmin(T& x, T y){if(x > y) x = y;}
template<class T> void chmax(T& x, T y){if(x < y) x = y;}

int N, M;
int dp[1010][1010];
int A[1010], B[1010];
int main() {
    cin >> N >> M;
    for (int i = 0; i < N; i++) cin >> A[i];
    for (int i = 0; i < M; i++) cin >> B[i];
    for (int i = 0; i <= N; i++) {
        for (int j = 0; j <= M; j++) {
            dp[i][j] = inf;
        }
    }
    dp[0][0] = 0;
    for (int i = 0; i <= N; i++) {
        for (int j = 0; j <= M; j++) {
            if (A[i] == B[j]) chmin(dp[i + 1][j + 1], dp[i][j]);
            chmin(dp[i + 1][j], dp[i][j] + 1);
            chmin(dp[i][j + 1], dp[i][j] + 1);
            chmin(dp[i + 1][j + 1], dp[i][j] + 1);
        }
    }
    cout << dp[N][M] << endl;
}
#include <bits/stdc++.h>
int main (){
    int H, W, CNT=0, min=101;
    std::cin >> H >> W;
    int A[H][W];
    for (int i=0; i<H; i++) {for (int j=0; j<W; j++) std::cin >> A[i][j];}
    for (int i=0; i<H; i++) {for (int j=0; j<W; j++) if(min>A[i][j]) min=A[i][j];}
    for (int i=0; i<H; i++) {for (int j=0; j<W; j++) CNT=CNT+(A[i][j]-min);}
    std::cout << CNT << std::endl;
}