#include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


ll POW[17];
string line;

string get_string(ll num){

	string ret;
	stack<ll> S;

	while(true){
		S.push(num%10);
		num /= 10;
		if(num == 0)break;
	}

	while(!S.empty()){
		ret.append(to_string(S.top()));
		S.pop();
	}

	return ret;
}

ll getNUM(string tmp_str){

	ll ret = 0;

	for(int i = 0; i < tmp_str.length(); i++){
		ret = 10*ret+(tmp_str[i]-'0');
	}

	return ret;
}

ll get_pos(ll num){

	ll ret = 0;
	ll tmp_num = 0;

	for(int digit = 1; digit <= 16; digit++){

		if(tmp_num+9*POW[digit-1] >= num){
			ret += (num-POW[digit-1])*digit;
			break;
		}
		tmp_num += 9*POW[digit-1];
		ret += digit*9*POW[digit-1];
	}
	return ret;
}

bool is_match(string A,string B,int left,int right){

	for(int i = left; i <= right; i++){
		if(A[i] != B[i]){
			return false;
		}
	}
	return true;
}

bool is_all_9(string tmp_str){

	for(int i = 0; i < tmp_str.length(); i++){
		if(tmp_str[i] != '9'){
			return false;
		}
	}

	return true;
}

void func(){

	int LENGTH = line.length();
	ll ans = HUGE_NUM;

	if(getNUM(line) == 0){

		ans = get_pos(getNUM("1"+line))+2;
		printf("%lld\n",ans);
		return;
	}

	ll tmp_num;
	int tmp_length;

	for(int digit = 1; digit <= min(LENGTH,16); digit++){
		for(int start_pos = 0; start_pos < digit && start_pos+digit <= LENGTH; start_pos++){

			if(line[start_pos] == '0')continue;

			string tmp_str = line.substr(start_pos,digit);
			tmp_num = getNUM(tmp_str);

			if(start_pos > 0){
				string left_str = get_string(tmp_num-1);
				tmp_str = left_str+tmp_str;

				tmp_str = tmp_str.substr(tmp_str.length()-(start_pos+digit));
				if(!is_match(line,tmp_str,0,start_pos+digit-1)){
					continue;
				}
			}
			tmp_length = tmp_str.length();

			for(int diff = 1; tmp_str.length() < LENGTH; diff++){
				tmp_str += get_string(tmp_num+diff);
			}
			tmp_str = tmp_str.substr(0,LENGTH);

			if(is_match(line,tmp_str,0,LENGTH-1)){
				ans = min(ans,get_pos(tmp_num)-start_pos+1);
			}
		}
		if(ans != HUGE_NUM)break;
	}

	if(LENGTH >= 31){
		printf("%lld\n",ans);
		return;
	}

	int min_digit,max_digit = 16;

	for(int left_right = 0; left_right <= LENGTH-2; left_right++){

		string left_str = line.substr(0,left_right+1);
		string right_str = line.substr(left_right+1);

		if(right_str[0] == '0')continue;

		min_digit = max(left_str.length(),right_str.length())+1;

		for(int digit = min_digit; digit <= max_digit && left_str.length()+right_str.length() >= digit; digit++){

			string tmp_diff = left_str.substr(left_str.length()-(digit-right_str.length()));
			string line_tail = tmp_diff;
			tmp_num = getNUM(tmp_diff);
			tmp_diff = get_string(tmp_num+1);
			while(tmp_diff.length() < (digit-right_str.length())){
				tmp_diff = "0"+tmp_diff;
			}
			tmp_diff = tmp_diff.substr(tmp_diff.length()-(digit-right_str.length()));

			string tmp_right = right_str+tmp_diff;
			string tmp_left = get_string(getNUM(tmp_right)-1);
			tmp_left = tmp_left.substr(0,tmp_left.length()-left_str.length())+left_str;

			if(getNUM(tmp_right)-getNUM(tmp_left) == 1){
				ans = min(ans,get_pos(getNUM(tmp_right))-left_right);
				break;
			}
		}
	}

	printf("%lld\n",ans);
}

int main(){

	POW[0] = 1;

	for(int i = 1; i <= 16; i++){
		POW[i] = POW[i-1]*10;
	}

	while(true){

		getline(cin,line);

		if(line[0] == '#')break;

		func();
	}

	return 0;
}



Measured memory_bytes: 3236