import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop;

void main() {
    auto N = readln.chomp.to!long;
    while (N % 2 == 0) N /= 2;
    while (N % 5 == 0) N /= 5;

    long ans = 1L << 59;
    long one = 0;

    foreach (i; 1..10^^8) {
        one = one * 10 + 1;
        one = one % N;
        if (one == 0) {
            ans = min(ans, i);
        }
    }

    foreach (i; 0..10^^6) {
        long M = N * i;
        long x = M.to!string.map!(a => a - '0').sum;
        ans = min(ans, x);
    }

    ans.writeln;
}
