どこでもドア:http://poj.org/problem?id=1426
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<sstream>
#include<set>
#include<cstdlib>
#include<map>
#include<queue>
using namespace std;
typedef long long LL;
LL BFS(int n)
{
queue<LL> Q;
Q.push(1);
while(!Q.empty())
{
LL X = Q.front();
Q.pop();
if(X % n == 0)
return X;
Q.push(X * 10);
Q.push(X * 10 + 1);
}
return 0;
}
int main()
{
int n;
while(cin>>n,n)
{
cout << BFS(n) << endl;
}
}