// Arup Guha
// 11/2/2013
// Solution to 2013 South East Regional Division 2 Problem F: Decimal Representation

import java.util.*;

public class decimal {

	final static int MAX = 501;

	public static void main(String[] args) {

		// Pre-store all answers.
		int[] ans = new int[MAX];
		ans[1] = 1;
		for (int i=2; i<MAX; i++)
			ans[i] = Math.max(ans[i-1], solve(i));

		Scanner stdin = new Scanner(System.in);
		int n = stdin.nextInt();

		// Process each case.
		while (n != 0) {
			System.out.println(ans[n]);
			n = stdin.nextInt();
		}
	}

	// Solve the given problem when 1 of the two values is exaxtly n and the other
	// is less than or equal to n.
	public static int solve(int n) {

		// Just do simple brute force on both numerator and denominator.
		int max = 1;
		for (int i=1; i<=n; i++) {
			if (gcd(i,n) > 1) continue;
			int tmp = chars(i,n);
			if (tmp > max) max = tmp;
		}
		for (int i=1; i<=n; i++) {
			if (gcd(i,n) > 1) continue;
			int tmp = chars(n,i);
			if (tmp > max) max = tmp;
		}

		// This is our answer.
		return max;
	}

	public static int gcd(int a, int b) {
		if (b == 0) return a;
		if (a == 0) return b;
		return gcd(b, a%b);
	}

	public static int chars(int a, int b) {

		int q = a/b;

		if (a%b == 0) return digits(q);

		// Count of number plus decimal...
		int cnt = digits(q) + 1;

		// Set up simulating decimal digits.
		a = a%b;
		boolean[] used = new boolean[b];
		used[a] = true;

		// Break when we get a repeated remainder, or zero.
		cnt++;
		while (true) {
			a = (10*a)%b;
			if (used[a] || a == 0) break;
			used[a] = true;
			cnt++;
		}

		// This is a repeated fraction, so add two for the parens.
		if (a != 0) cnt += 2;

		return cnt;
	}

	// Returns the number of digits in n.
	public static int digits(int n) {
		int cnt = 1;
		while (n >= 10) {
			cnt++;
			n /= 10;
		}
		return cnt;
	}
}