using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;

namespace AtCorder
{
	public class Program
	{
		static void Main(string[] args)
		{
			var cin = new Scanner2();
			string n = cin.Next();
			int m = n.Length;

			long ans = 0;
			bool kuri = false;
			for (int i = m - 1; i >= 0; i--) {
				int num = n[i] - '0';
				if (i == 0) {
					if (kuri && num == 9) {
						ans += 1;
					} else {
						if (kuri) {
							num += 1;
						}

						if (num > 5) {
							ans += 10 - num;
							ans += 1;
						} else {
							ans += num;
						}
					}
				} else {
					if (kuri && num == 9) {
						kuri = true;
						continue;
					}

					if (kuri) {
						num += 1;
					}

					if (num > 5) {
						ans += 10 - num;
						kuri = true;
					} else if (num == 5) {
						int num2 = n[i - 1] - '0';
						if (num2 >= 5) {
							ans += 10 - num;
							kuri = true;
						} else {
							ans += num;
							kuri = false;
						}
					} else {
						ans += num;
						kuri = false;
					}
				}
			}

			Console.WriteLine(ans);
		}
	}

	public class StringComparator : IComparer<string>
	{
		int IComparer<string>.Compare(string a, string b)
		{
			int length = Math.Min(a.Length, b.Length);
			for (int i = 0; i < length; i++) {
				if (a[i] < b[i]) {
					return -1;
				} else if (a[i] > b[i]) {
					return 1;
				}
			}

			if (a.Length < b.Length) {
				return -1;
			}

			if (a.Length > b.Length) {
				return 1;
			}

			return 0;
		}
	}

	public class Scanner2
	{
		private readonly char[] delimiter_ = new char[] { ' ' };
		private readonly string filePath_;
		private readonly Func<string> reader_;
		private string[] buf_;
		private int index_;

		public Scanner2(string file = "")
		{
			if (string.IsNullOrWhiteSpace(file)) {
				reader_ = Console.ReadLine;
			} else {
				filePath_ = file;
				var fs = new StreamReader(file);
				reader_ = fs.ReadLine;
			}
			buf_ = new string[0];
			index_ = 0;
		}

		public string Next()
		{
			if (index_ < buf_.Length) {
				return buf_[index_++];
			}

			string st = reader_();
			while (st == "") {
				st = reader_();
			}

			buf_ = st.Split(delimiter_, StringSplitOptions.RemoveEmptyEntries);
			if (buf_.Length == 0) {
				return Next();
			}

			index_ = 0;
			return buf_[index_++];
		}

		public int Int() => int.Parse(Next());
		public long Long() => long.Parse(Next());
		public double Double() => double.Parse(Next());

		public int[] ArrayInt(int N, int add = 0)
		{
			int[] Array = new int[N];
			for (int i = 0; i < N; i++) {
				Array[i] = Int() + add;
			}
			return Array;
		}

		public long[] ArrayLong(int N, long add = 0)
		{
			long[] Array = new long[N];
			for (int i = 0; i < N; i++) {
				Array[i] = Long() + add;
			}
			return Array;
		}

		public double[] ArrayDouble(int N, double add = 0)
		{
			double[] Array = new double[N];
			for (int i = 0; i < N; i++) {
				Array[i] = Double() + add;
			}
			return Array;
		}

		public void Save(string text)
		{
			if (string.IsNullOrWhiteSpace(filePath_)) {
				return;
			}

			File.WriteAllText(filePath_ + "_output.txt", text);
		}
	}
}

Measured memory_bytes: 14816