t = gets.to_i

s, level, arr = 0, 0, [0]
while s <= t
    s += (3 ** level) + (3 ** (level + 1))
    level += 1
    arr[-1] += 1
    arr << 1
end
level -= 1
s -= (3 ** level) + (3 ** (level + 1))
arr.pop
arr[-1] -= 1

while t > s
    level -= 1 while (t - s) < (3 ** level)
    s += (3 ** level)
    arr[level] += 1
end
puts arr.inject(:+)

