using System; using static System.Console; using System.Linq; using System.Collections.Generic; class Program { static int NN => int.Parse(ReadLine()); static int[] NList => ReadLine().Split().Select(int.Parse).ToArray(); static char[][] SList(long n) => Enumerable.Repeat(0, (int)n).Select(_ => ReadLine().ToCharArray()).ToArray(); public static void Main() { Solve(); } static void Solve() { var n = ReadLine(); var list = new int[n.Length * 2]; for (var i = 0; i < n.Length; ++i) { list[i * 2] = (n[i] - '0') / 5; list[i * 2 + 1] = (n[i] - '0') % 5; } var dp0 = 0; var dp1 = 0; var INF = int.MaxValue / 2; if (list[0] == 0) dp1 = INF; else { dp0 = 1; dp1 = 2; } for (var i = 1; i < list.Length; ++i) { var ndp0 = 0; var ndp1 = 0; if (i % 2 == 0) { ndp0 = Math.Min(dp0, dp1) + list[i]; ndp1 = Math.Min(dp0, dp1 - 2) + 3 - list[i]; } else { ndp0 = Math.Min(dp0, dp1) + list[i]; ndp1 = Math.Min(dp0, dp1 - 2) + 6 - list[i]; } dp0 = ndp0; dp1 = ndp1; } WriteLine(Math.Min(dp0, dp1)); } }