#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
  int n, k;
  cin >> n >> k;
  
  int a = n;
  
  int g1 = 0;
  int g2 = 0;
  
  for (int i = 0; i < k; i++) {
    // 各桁の数値を取り出す
    int num = a;
    vector<int> v1, v2;
    while (true) {
      v1.push_back(num % 10);
      if (num < 10) {
        break;
      }
      num /= 10;
    }
    v2 = v1;
    
    // g1をつくる
    g1 = 0;
    sort(v1.begin(), v1.end(), greater<int>());
    for (size_t j = 0; j < v1.size(); j++) {
      g1 *= 10;
      g1 += v1[j];
    }
    
    // g2をつくる
    g2 = 0;
    sort(v2.begin(), v2.end());
    for (size_t j = 0; j < v2.size(); j++) {
      g2 *= 10;
      g2 += v2[j];
    }
    
    a = g1 - g2;
  }
  
//  cout << g1 << endl;
//  cout << g2 << endl;
  cout << a << endl;
  
  return 0;
}

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); i++)
using namespace std; using ll = long long;
using ull = unsigned long long; using P = pair<int, int>;
const int INF = 1e9; const int MOD = 998244353;
// const int dx[] = {-1,0,1,0}; const int dy[] = {0,-1,0,1};
// const int dx[] = {-1,-1,-1, 0, 0, 1, 1, 1}; const int dy[] = {-1, 0, 1,-1, 1,-1, 0, 1};
#define PI 3.14159265358979323846264338327950L 
//cout << fixed << setprecision(10) << Min << endl << Max << endl;
int main() {
	int N; cin >> N;
	ll ans = 0;
	rep(i, N) {
		ll A, B;
		cin >> A >> B;
		ans += (B - A + 1)*(A+B)/2;
	}
	cout << ans << endl;
}