#include <iostream>
#include <cassert>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <fstream>

using namespace std;

typedef pair< int, int > PII;

map< PII, int > sols;

int solve( int n, int s ) {
	int res;
	if( n == 1 ) return 0;
	if( n % 2 == 0 ) {
		if( s == 0 )
			res = solve( n/2, 0 ) * 2 + 1;
		else
			res = solve( n/2, 1 ) * 2;
	}
	else {
		if( s ) {
			res = solve( (n+1)/2, 0 )*2;
		}
		else {
			res = solve( n/2, 1 )*2+1;
		}
	}
	return res;
}

int main() {
//	ifstream file( "danger.in" );
	string line;
	while( cin >> line ) {
		int n = ( line[0] - '0' ) * 10 + ( line[1] - '0' );
		int z = line[3]-'0';
		while( z-- ) n *= 10;
		if( n == 0 ) break;

		cout << solve(n, 1 ) + 1 << endl;

	}
	return 0;
}
