Problem:
Problem D: Revenge of Champernowne Constant

Champernowne constant is an irrational number. Its decimal representation starts with "0.", followed by concatenation of all positive integers in the increasing order.

You will be given a sequence S which consists of decimal digits. Your task is to write a program which computes the position of the first occurrence of S in Champernowne constant after the decimal point.

Input

The input has multiple test cases. Each line of the input has one digit sequence. The input is terminated by a line consisting only of #.

It is guaranteed that each sequence has at least one digit and its length is less than or equal to 100.

Output

For each sequence, output one decimal integer described above. You can assume each output value is less than 1016.

Sample Input

45678
67891011
21
314159265358979
#

Output for the Sample Input

4
6
15
2012778692735799

Solution in C++ (judge verdict: Time Limit Exceeded)

#include<cstdio>
#include<string>
#include<iostream>
#include<algorithm>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

typedef long long ll;

const ll INF=(1LL<<63)-1;

ll to_ll(const string &s){
	ll a;
	sscanf(s.c_str(),"%lld",&a);
	return a;
}

string to_string(ll a){
	char s[32];
	sprintf(s,"%lld",a);
	return s;
}

ll calc_index(ll a){
	ll res=0,ten=1,len=1;
	while(1){
		if(a<10*ten){
			res+=(a-ten)*len;
			return res;
		}
		res+=9*len*ten;
		ten*=10;
		len++;
	}
}

int main(){
	for(string s;cin>>s,s[0]!='#';){
		int n=s.length();

		ll ans=INF;
		int ofset;
		// 注目する数が s に完全に含まれるケース ( 341235123612 など )
		rep(i,n) for(int j=i+1;j<=n;j++) {
			string t=s.substr(i,j-i);
			if(t[0]=='0') continue;

			ll a=to_ll(t);
			int len=0;
			string r=t;
			for(int k=1;k<30;k++){
				if(a-k>0){
					r=to_string(a-k)+r;
					len+=to_string(a-k).length();
				}
				r=r+to_string(a+k);
			}
			if(len-i>=0 && r.substr(len-i,s.length())==s && ans>a) ans=a, ofset=i;
		}

		// そうでないケース 3456712345
		for(int i=1;i<n;i++){
			string t=s.substr(0,i),r=s.substr(i);
			if(t[0]=='0') continue;

			t=to_string(to_ll(t)+1);
			if(count(t.begin(),t.end(),'9')==t.length()) t=t.substr(1);
			rep(d,min(t.length(),r.length())+1){
				if(r.substr(r.length()-d,d)==t.substr(0,d)){
					ll a=to_ll(r+t.substr(d));
					if(ans>a) ans=a, ofset=i;
				}
			}
		}

		cout<<calc_index(ans)-ofset+1<<endl;
	}

	return 0;
}