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
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <tuple>
#include <vector>
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <functional>

using namespace std;

using ll	= long long;
using ull	= unsigned long long;
using pii	= pair<int,int>;
using vi	= vector<int>;
using vb	= vector<bool>;
using vc	= vector<char>;
using vvi	= vector<vi>;
using vpii	= vector<pii>;
using vll	= vector<ll>;
using vull	= vector<ull>;

#define ever (;;)
#define f first
#define s second
#define pb emplace_back
#define sz size()
#define graph vector<vertex>
#define deg(X) G[X].e.size()
#define INF 1234567890
#define INFll 2000000000000000000
#define maxe(X, Y) if((Y) > (X)) (X) = (Y)
#define mine(X, Y) if((Y) < (X)) (X) = (Y)
#define rep(i, begin, end) for(__typeof(end) i = (begin); i < (end); ++i)
#define repr(i, begin, end) for(__typeof(end) i = (begin)-1; i >= (end); --i)
#define bend(X) X.begin(), X.end()

int n;
vector<string> T;
vi CO;
vpii DP;

void in() {
	cin >> n;
	T.resize(n);
	DP.resize(n);
	CO.resize(n);
	rep(i, 0, n)
		cin >> T[i];
}

int getDiff(string a, string b) {
	rep(i, 0, (int)min(a.sz, b.sz))
		if(a[i] != b[i])
			return i;
	return -1;
}

int getSz(int x) {
	int R = 1;
	rep(i, 0, (int)min(6, x))
		R *= 10;
	return R-1;
}

int main() {
	in();
	
	CO[0] = 0;
	DP[0] = {0, 0};
	
	rep(i, 1, n) {
		//cout << T[i] << " : " << T[i-1] << "    ";
		if(T[i].sz > T[i-1].sz+CO[i-1])
			continue;
		
		int k = getDiff(T[i-1], T[i]);
		if(k < 0) {
			//cout << "sa takie same\n";
			CO[i] = T[i-1].sz+CO[i-1]-T[i].sz;
			if(T[i].sz > T[i-1].sz)
				DP[i] = {CO[i], getSz(CO[i])};
			else if(T[i].sz <= T[i-1].sz)
				DP[i] = {DP[i-1].f, DP[i-1].s-1};
		}
		else if(T[i][k] < T[i-1][k]) {
			//printf("jest mniejszy\n");
			CO[i] = T[i-1].sz - T[i].sz + CO[i-1] + 1;
			DP[i] = {CO[i], getSz(CO[i])};
		}
		else if(T[i][k] > T[i-1][k]) {
			//printf("jest wiekszy\n");
			CO[i] = T[i-1].sz - T[i].sz + CO[i-1];
			DP[i] = {CO[i], getSz(CO[i])};
		}
		else
			printf("popsute\n");
		
		if(DP[i].s < 0)
			++CO[i],
			DP[i] = {CO[i], getSz(CO[i])};
		
		//printf("CO[%d] = %d\n", i, CO[i]);
		//printf("DP[%d] = {%d, %d}\n", i, DP[i].f, DP[i].s);
	}
	
	ll R = 0;
	
	rep(i, 0, n)
		R += CO[i];
	
	printf("%lld\n", R);
	
	return 0;
}