#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;

unsigned NumDigits(uint32_t x) {
	return // Num-># Digits->[0-9] 32->bits bs->Binary Search
		(x >= 100000u // [6-10] [1-5]
			?   // [6-10]
			(x >= 10000000u // [8-10] [6-7]
				?   // [8-10]
				(x >= 100000000u // [9-10] [8]
					? // [9-10]
					(x >= 1000000000u // [10] [9]
						? 10
						: 9
						)
					: 8
					)
				:   // [6-7]
				(x >= 1000000u // [7] [6]
					? 7
					: 6
					)
				)
			:   // [1-5]
			(x >= 100u // [3-5] [1-2]
				?   // [3-5]
				(x >= 1000u // [4-5] [3]
					? // [4-5]
					(x >= 10000u // [5] [4]
						? 5
						: 4
						)
					: 3
					)
				:   // [1-2]
				(x >= 10u // [2] [1]
					? 2
					: 1
					)
				)
			);
}

bool rowne(int current, int previous, int length, int postfix)
{
	if (current == previous && length == 0 && postfix == 0)
		return true;

	int current_length = NumDigits(current);
	int previous_length = (NumDigits(previous) + length);

	if (current_length != previous_length)
		return false;

	return (previous * powl(10, length) + postfix) == current;
}

bool wieksza(int current, int previous, int length, int postfix) //ostra wiekszosc
{
	int current_length = NumDigits(current);
	int previous_length = (NumDigits(previous) + length);

	if (current_length > previous_length)
		return true;
	if (current_length < previous_length)
		return false;
	//rowny size, wiec teraz trzeba skonstruowac previous i porownac wartosc
	//ten kod jest skomplikwoany, ale wykona sie niewiele razy
	int total_previous = previous * powl(10, length) + postfix;
	if (current > total_previous)
		return true;
	return false;
}

int normalize(int previous, int prev_length, int prev_postfix, int current)
{
	unsigned int rV = previous;

	int dlugosc_current = NumDigits(current);
	int dlugosc_prev = NumDigits(previous);
	int dlugosc_postfix = NumDigits(prev_postfix);
	int dlugosc_zer = prev_length - dlugosc_postfix;

	if (dlugosc_current == dlugosc_prev)
		return previous;

	if (dlugosc_current < dlugosc_prev)
	{
		//odcinamy ostatnie cyfry z prev
		return previous/powl(10,dlugosc_prev-dlugosc_current);
	}

	//przygotujmy ten postfix
	int ile_brakuje = dlugosc_current - dlugosc_prev;
	int ile_zer_dobieramy = min(ile_brakuje, dlugosc_zer);

	rV = rV * powl(10, ile_brakuje);


	int ile_liczb_z_postfixa = ile_brakuje - ile_zer_dobieramy; //moze byc ujemne
	if (ile_liczb_z_postfixa <= 0)
	{
		ile_liczb_z_postfixa = 0;
		return rV;
	}

	//ile odciac z posfixa
	int ile_odciac_z_postfixa = dlugosc_postfix - ile_liczb_z_postfixa;
	
	rV += prev_postfix / powl(10, ile_odciac_z_postfixa);
	
	return rV;
}

int main()
{
	unsigned int n,current, previous = 0;
	unsigned int prev_length = 0, prev_postfix = 0; //dlugos dodanego ciagu,  postfix - wartosc tego ciagu ,  zakladam ze beda tam zera na poczatku
	long long count = 0;
	cin >> n;

	for (int i = 0; i < n; i++)
	{ 
		cin >> current; 

		if (wieksza(current, previous, prev_length, prev_postfix))//biezaca wieksza od poprzedniej, nic nie robimy
		{
			count += 0;
			previous = current;
			prev_length = 0;
			prev_postfix = 0;
		}
		else
		{
			if (rowne(current, previous, prev_length, prev_postfix))//takie same
			{
				count += 1; previous = current;prev_length = 1; prev_postfix = 0;
			}
			else //current jest mniejsza (wartosciowo)
			{
				//A
				if (NumDigits(current) == NumDigits(previous) + prev_length)  //maja te samı dlugosc
				{
					count += 1; previous = current; prev_length = 1; prev_postfix = 0;
					continue;
				}

				//juz wiemy ze current jest mniejsza i jednoczesnie krotsza niz 'previous z dodatkami'
				//potrzebuje z previous wyciagnac fragment o dlugosci rownej current
				int normalized_previous = normalize(previous, prev_length, prev_postfix, current);

				//B1
				if (current > normalized_previous)
				{
					//dodaje zera tak zeby wyrownac dlugosc, count+=ilosc dodanych zer, previous = curent +'zera'
					int numCurrent = NumDigits(current);
					int numPrevious = NumDigits(previous)+prev_length;

					previous = current;
					prev_postfix = 0;
					prev_length = numPrevious - numCurrent;
					count += prev_length;
				}
				else
				{
					int numCurrent = NumDigits(current);
					int numPrevious = NumDigits(previous)+prev_length;

					if (current < normalized_previous)
					{//B2

						previous = current;
						prev_postfix = 0;
						prev_length = numPrevious - numCurrent + 1;
						count += prev_length;
					}
					else
					{
						//B3   current = normalized_previous

						if (prev_length == 0) //C1
						{
							int incr_prev = previous + 1;
							int normalized_previous_incr = normalize(incr_prev, 0, 0, current);
							
							if (normalized_previous_incr == current)
							{
								//previous = > prev_icr, length = 0, postf = 0, count += prev_length - current_length
								previous = incr_prev;
								prev_length = 0;
								prev_postfix = 0;
								count += NumDigits(previous) - NumDigits(current);
							}
							else
							{
								//prev = current, postfix = 0; length = numPrevious - numCurrent + 1; count + prev_length
								previous = current;
								prev_postfix = 0;
								prev_length = numPrevious - numCurrent + 1;
								count += prev_length;
							}
						}
						else
						{
							//C2
							int ile_jest_zer_doklejonych = prev_length - NumDigits(prev_postfix);
							int incremented_postfix = prev_postfix + 1;

							if (NumDigits(prev_postfix) == NumDigits(incremented_postfix))
							{
								//D1
								//prev = prev, prev_length = length, posftix+=1; count = length = numPrevious - numCurrent;
								previous = previous;
								prev_postfix = incremented_postfix;
								prev_length = prev_length;
								count += (numPrevious-numCurrent);
							}
							else
							{//D2
								if (ile_jest_zer_doklejonych > 0)
								{//D21
									previous = previous;
									prev_postfix = incremented_postfix;
									prev_length = prev_length;
									count += (numPrevious - numCurrent);
								}
								else
								{//D22  //ignorujemy postfix
									int incr_prev = previous + 1;
									int normalized_previous_incr = normalize(incr_prev, 0, 0, current);

									if (normalized_previous_incr == current) //incrementacja poprzedniego jest ok
									{
										//previous = > prev_icr, length = 0, postf = 0, count += prev_length - current_length
										previous = incr_prev;
										prev_length = prev_length;
										prev_postfix = 0;
										count += numPrevious - numCurrent;
									}
									else
									{ // inkremnteacja poprzedniego nie dziala, czyli byly pewnie dziewiatki
										//prev = current, postfix = 0; length = numPrevious - numCurrent + 1; count + prev_length
										previous = current;
										prev_postfix = 0;
										prev_length = numPrevious - numCurrent + 1;
										count += prev_length;
									}
								}

							}

						}
					}
				}

			}
		}
	}

    cout << count; 
	return 0;
}

