import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;


public class SpinningUp_opt0_sjz {
	
	private static final int MaxCost = 999999;
	private BufferedReader input;

	public SpinningUp_opt0_sjz(BufferedReader input) {
		this.input = input;
	}

	public static void main(String[] args) throws FileNotFoundException, IOException {
		if (args.length > 0) {
			try (BufferedReader input 
					= new BufferedReader(new FileReader(args[0])))
			{
				new SpinningUp_opt0_sjz(input).solve();
			}
		} else {
			BufferedReader input 
			    = new BufferedReader (new InputStreamReader(System.in));
			new SpinningUp_opt0_sjz(input).solve();
		}
	}

	private void solve() throws IOException {
		String line = input.readLine();
		int c = makePalindrome (line);
		System.out.println (c);
		System.err.println("memos: size=" + memos.size() 
			+ "  attempts=" + memoAttempts + "  hits: " + memoHits);
	}

	private int makePalindrome(String wheels) {
		int cost = makePalindrome(wheels, true);
		
		// Look at possibilities from leading zeros
		int i = 0;
		while (i < wheels.length() && wheels.charAt(i) == '0') {
			int c = makePalindrome(wheels.substring(i), false);
			cost = Math.min(cost, c);
			++i;
		}
		
		// Look at using all the wheels
		int c = makePalindrome(wheels.substring(i), false);
		cost = Math.min(c, cost);
		
		// Look at possibility of introducing leading zeros by spinning the
		// wheels towards the left.
		i = 0;
		int spins = 0;
		while (i < wheels.length()) {
			if (wheels.charAt(i) != '0') {
				int spins0 = 10 - ((int)wheels.charAt(i) - (int)'0');
				c = spins + spins0 + makePalindrome(wheels.substring(i+1));
				cost = Math.min(cost, c);
			}
			spins += 9 - ((int)wheels.charAt(i) - (int)'0');
			if (spins >= cost) {
				break;
			}
			++i;
		}		
		return cost;
	}
	
	private HashMap<String, Integer> memos = new HashMap<>();
	private int memoAttempts = 0;
	private int memoHits = 0;

	/**
	 * Compute the smallest number of changes required to make
	 * wheels into a palindrome.
	 * 
	 * @param wheels  sequence of digits
	 * @param withCarry true if the leftmost wheel MUST make a transition from 9 to 0,
	 *                  false if it must not
	 * @return  minimum number of wheel advances required
	 */
	private int makePalindrome(String wheels, boolean withCarry) {
		if (wheels.length() == 0) {
			return (withCarry) ? MaxCost : 0;
		}
		if (wheels.length() == 1) {
			if (withCarry) {
				return 10 - ((int)wheels.charAt(0) - (int)'0');
			} else {
				return 0;
			}
		}
		
		String journalKey = wheels + ":" + withCarry;
		Integer priorAnswer = memos.get(journalKey);
		++memoAttempts;
		if (priorAnswer != null) {
			++memoHits;
			return priorAnswer;
		}
			
		int answer = MaxCost;
		char c0 = wheels.charAt(0);
		char cn = wheels.charAt(wheels.length()-1);
		int k0 = (int)c0 - (int)'0';
		int kn = (int)cn - (int)'0'; 
		
		int cost1 = MaxCost;
		int cost2 = MaxCost;

		if (withCarry) {
			// If we need a carry, we may get it by spinning the leftmost
			// wheel to 0...
			String innerWheels = wheels.substring(1, wheels.length()-1);
			int spin0 = (10 - k0);
			// ... and then converting the resulting wheels to a palindrome
			// with no carry.
			String wheels1 = '0' + wheels.substring(1);
			cost1 = spin0 + makePalindrome(wheels1, false);
			// Or by spinning the leftmost wheel to 9, and 
			// converting the remainder of the wheels to a palindrome with
			// a carry.
			
			int spin1 = 9 - k0;
			cost2 = spin1 + makePalindrome(wheels.substring(1), true);

			answer = Math.min(cost1, cost2);
		} else {
			// If no carry is required, we need to match the 
			// two outermost characters.
			String innerWheels = wheels.substring(1, wheels.length()-1);
			int cost3 = MaxCost;
			
			if (c0 < cn) {
				// Three cases:
				// 1. Spin leftmost wheel cn-c0 times, get inner palindrome with no carry
				// 2. Spin leftmost wheel cn-c0-1 times, get inner palindrome with carry
				// 3. If rightmost wheel is non-zero, spin to zero and get a
				//     palindrome with no carry.  
				
				
				// Spinning the leftmost wheel
				int innerCostNoCarry = makePalindrome(innerWheels, false);
				int innerCostCarry = makePalindrome(innerWheels, true);
				cost1 = (int)cn - (int)c0  + innerCostNoCarry;
				cost2 = (int)cn - (int)c0 - 1 + innerCostCarry;
				
				if (kn > 0) {
					String allButLast = wheels.substring(0, wheels.length()-1);
					String increm = increment(allButLast);
					if (increm.length() == allButLast.length()) {
						cost3 = (10 - kn) + makePalindrome(increm + '0', false);
					}
				}
				
				answer = Math.min(Math.min(cost1, cost2), cost3);
			} else {
				// Two cases
				// 1. Spin rightmost wheel c0-cn times, get inner palindrome with no carry
				// 2. Spin rightmost wheel c0-cn+1 times, get inner palindrome with carry
			
				// Spinning the rightmost wheel
				int innerCostNoCarry = makePalindrome(innerWheels, false);
				int innerCostCarry = makePalindrome(innerWheels, true);
				cost1 = (int)c0 - (int)cn  + innerCostNoCarry;
				if (c0 != '9') {
					cost2 = (int)c0 - (int)cn + 1 + innerCostCarry;
				}
				answer = Math.min(cost1, cost2);
			}
		}
		
		//System.err.println (wheels + ' ' + withCarry + " => " + answer);
		memos.put(journalKey, answer);
		return answer;
	}

	
	
	
	
	
	
	
	// Increment the number in this string
	private String increment(String number) {
		StringBuilder result = new StringBuilder();
		int carry = 1;
		for (int i = number.length()-1; i >= 0; i--) {
			int d = (int)number.charAt(i) - (int)'0';
			d += carry;
			if (d > 9) {
				carry = 1;
				d = d - 10;
			} else {
				carry = 0;
			}
			char dc = (char)(d + '0');
			result.append(dc);
		}
		if (carry > 0) {
			result.append('1');
		}
		return result.reverse().toString();
	}
}

