import core.bitop;
import std.algorithm;
import std.array;
import std.ascii;
import std.container;
import std.conv;
import std.format;
import std.math;
import std.random;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

void main()
{
  int k = readln.chomp.to!int;
  alias Node = Tuple!(int, "cost", int, "value");
  alias RBT = RedBlackTree!(Node, (a, b) => a < b, false);
  RBT queue = new RBT();
  queue.insert(Node(0, k));
  while (queue.length) {
    Node node = queue.front;
    queue.removeFront;
    // stderr.writeln(node);
    if (node.value == 0) {
      node.cost.writeln;
      return;
    }
    foreach (x; 1..10) {
      Node next;
      next.value = (node.value * x) / 10;
      next.cost = (node.cost * x) + (node.value * x) % 10;
      // stderr.writeln("\tx : ", x, " : ", next);
      queue.insert(next);
    }
  }
}
