import std.algorithm;

void main()
{
    import std.stdio, std.conv, std.string;
    readln.chomp.to!int.solve.writeln;
}

int solve(int x)
{
    auto result = int.max;
    foreach (i; 0..x+1)
        result = min(result, simulate(x, i));
    return result;
}

int simulate(int x, int i)
{
    return i.withdraw(6) + (x - i).withdraw(9);
}

int withdraw(int x, int radix)
{
    int result;
    while (x)
    {
        result += x % radix;
        x /= radix;
    }
    return result;
}
