#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define REP(i,j) for(int i=0;i<j;i++)
#define REPA(i,j) for(int i=1;i<=j;i++)
#define FORN(i,j,k) for(int i=j;i<k;i++)
#define vi vector<int>
#define vvi vector<vi >
#define pii pair<int,int>
#define vpii vector<pii >
#define all(a) a.begin(),a.end()
using namespace std;
const int INF=1<<30;
inline void read(ll &x){
//	short neg=1;
	x=0;
	char c=getchar();
	/*while(c<'0'||c>'9'){
		if(c=='-')neg=-1;
		c=getchar();
	}*/
	while(c>='0'&&c<='9'){
		x=(x<<3)+(x<<1)+(c^48),c=getchar();
	}
//	x*=neg;
}
ll f(ll n,ll m){
	if(n==0)return 1; 
    if(n&1)return 10*f(n-1,m)%m;
    else{
        ll k=f(n/2,m);
        return k*k%m;
    }
}
ll n,m;

int main(void){
	read(n);
	read(m);
	ll A=f(n,m);
	ll B=f(n,m*m);
	printf("%lld",(B-A)/m);
	return 0;
}

/*
 ______________________     _______ _______ _______ _______ 
(  ____ \__   __(  __  \   (  ____ |  ___  |  ____ (  ___  )
| (    \/  ) (  | (  \  )  | (    )| (   ) | (    \/ (   ) |
| (_____   | |  | |   ) |  | (____)| (___) | (_____| (___) |
(_____  )  | |  | |   | |  |     __)  ___  (_____  )  ___  |
      ) |  | |  | |   ) |  | (\ (  | (   ) |     ) | (   ) |
/\____) |__) (__| (__/  )  | ) \ \_| )   ( /\____) | )   ( |
\_______)_______(______/___|/   \__//     \_______)/     \|
                      (_____)
I didn't come this far to only come this far */
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define pb push_back
#define mp make_pair
#define fl(i, a, b) for (int i = a; i < b; i++)
#define vi vector<int>
#define e1(a) \
    int a;    \
    cin >> a;
#define e2(a, b) \
    int a, b;    \
    cin >> a >> b;
#define e3(a, b, c) \
    int a, b, c;    \
    cin >> a >> b >> c;
#define av(arr, n)      \
    vector<int> arr(n); \
    fl(i, 0, n) cin >> arr[i];
#define aa(arr, n) \
    int arr[n];    \
    fl(i, 0, n) cin >> arr[i];
#define es(s)  \
    string(s); \
    cin >> (s);
#define rz resize
#define sz(s) s.size()
#define mod 1000000007
#define ff first
#define ss second
#define tn -10e15
#define bg 10e15
#define all(x) (x).begin(), (x).end()
#define pii pair<int, int>
#define mii map<int, int>
#define st(a, n) memset(a, n, sizeof(a))
#define vl(n) cout << n << "\n"
#define vs(n) cout << n << " "
#define el cout << "\n"
#define vn(n) cout << n
#define dsc greater<int>()
#define ps(x, y) fixed << setprecision(y) << x
#define R return
#define B break
#define C continue
#define YC cout << "YES" \
                << "\n"
#define YS cout << "Yes" \
                << "\n"
#define NC cout << "NO" \
                << "\n"
#define NS cout << "No" \
                << "\n"
#define lcm(a, b) (a / __gcd(a, b)) * b
#define pa(a)        \
    for (auto e : a) \
    cout << e << " "
const int N = 4e5 + 5;
// Twist the ques
// tweek well know algos
// if(eq)try to simplify
// Read the statements carefully

void solve()
{
    e2(n, m);
    int a[n][m];
    int mx = bg;
    fl(i, 0, n)
    {
        fl(j, 0, m)
        {
            cin >> a[i][j];
            mx = min(a[i][j], mx);
        }
    }
    int ans = 0;
    fl(i, 0, n)
    {
        fl(j, 0, m)
        {
            ans += a[i][j] - mx;
        }
    }
    vl(ans);
}

int32_t main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    solve();

    return 0;
}