import java.io.FileNotFoundException; import java.util.Arrays; import java.util.Scanner; class Main { int[] p = { 0, 1, 2, 3, 4, 1, 2, 3, 4, 5 }; public void run() { Scanner sc = new Scanner(System.in); String str = sc.next(); int n = str.length(); int[][] a = new int[n + 2][2]; for (int i = 0; i < a.length; ++i) for (int j = 0; j < a[i].length; ++j) a[i][j] = Integer.MAX_VALUE / 3; a[0][0] = 0; for (int i = 0; i <= n; ++i) { int v = i == n ? 0 : str.charAt(n - 1 - i) - '0'; for (int j = 0; j <= 9; ++j) { for (int k = 0; k <= 1; ++k) { a[i + 1][(v + j + k) / 10] = Math.min(a[i + 1][(v + j + k) / 10], a[i][k] + p[(v + j + k) % 10] + p[j]); } } } System.out.println(a[n + 1][0]); } void tr(Object... objects) { System.out.println(Arrays.deepToString(objects)); } public static void main(String[] args) throws FileNotFoundException { new Main().run(); } }