1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <algorithm>
#include <iostream>
#include <string>

long long int next(std::string &x, const std::string &s) {
  if (x.size() > s.size())
    return 0;
  if (x.size() == s.size()) {
    if (x > s)
      return 0;
    x.push_back('0');
    return 1;
  }
  
  for (int i = 0; i < (int)x.size(); ++i) {
    if (x[i] > s[i]) {
      int cnt = 0;
      while (x.size() < s.size()) {
        x.push_back('0');
        ++cnt;
      }
      return cnt;
    }
    if (x[i] < s[i]) {
      int cnt = 0;
      while (x.size() <= s.size()) {
        x.push_back('0');
        ++cnt;
      }
      return cnt;
    }
  }

  int idx = -1;
  for (int i = s.size() - 1; i >= x.size(); --i) {
    if (s[i] != '9') {
      idx = i;
      break;
    }
  }

  if (idx == -1) {
    int cnt = 0;
    while (x.size() <= s.size()) {
      x.push_back('0');
      ++cnt;
    }
    return cnt;
  }

  int cnt = s.size() - x.size();

  x = s;
  
  idx = x.size() - 1;
  x[idx]++;
  if (x[idx] > '9') {
    x[idx] = '0';
    x[idx - 1]++;
  }
  --idx;
  for (;x[idx] > '9'; --idx) {
    x[idx] = '0';
    x[idx - 1]++;
  }

  return cnt;
}

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(NULL);
  int n;
  std::cin >> n;
  std::string s;
  std::cin >> s;
  long long int result = 0;
  for (int i = 1; i < n; ++i) {
    std::string x;
    std::cin >> x;
    result += next(x, s);
    //std::cerr << x << '\n';
    std::swap(x, s);
  }
  std::cout << result;
  return 0;
}