// Arup Guha
// 11/3/2013
// Solution to 2013 South East Regional Problem J: You win!

import java.util.*;

public class youwin {

	final public static int MAX = 1000000000;
	final public static int NOTDONE = -1;

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		String s = stdin.next();

		while (!s.equals("0")) {
			System.out.println(solve(s));
			s = stdin.next();
		}
	}

	public static int solve(String s) {

		// dp[i][j] will store min cost of typing letters corresponding to bitmask i with the last position typed j.
		int n = s.length();
		int[][] dp = new int[1 << n][n];
		for (int i=0; i<dp.length; i++)
			Arrays.fill(dp[i], NOTDONE);

		// Initial table values for typing any of s's letters first.
		for (int i=0; i<n; i++)
			dp[1 << i][i] = dist('A', s.charAt(i));

		// Fill in table, building from smaller optimal subsets.
		for (int i=1; i<dp.length; i++) {

			// Try bit j as the last big visited in mask i.
			for (int j=0; j<n; j++) {

				int best = MAX;

				// This bit isn't part of our mask.
				if ((i & (1 << j)) == 0) continue;

				// Get our best to get to j by trying all possibilities of the mask to build to j.
				int prev = i - (1 << j);
				for (int k=0; k<n; k++) {
					if (dp[prev][k] != NOTDONE) {
						int cur = dp[prev][k] + dist(s.charAt(k), s.charAt(j)) + move(k, j, prev);
						if (cur < best) best = cur;
					}
				}

				// Store result.
				if (dp[i][j] == NOTDONE)
					dp[i][j] = best;
			}
		}

		// Find the best one.
		int ans = MAX;
		for (int i=0; i<n; i++)
			if (dp[dp.length-1][i] != NOTDONE && dp[dp.length-1][i] < ans)
				ans = dp[dp.length-1][i];

		return ans;
	}

	public static int move(int pos1, int pos2, int mask) {

		// Moving left to right in the word.
		if (pos1 < pos2) {

			int i = pos1+1, dist = 0;

			// Count each item in the word we must skip over.
			while (i < pos2) {
				int bit = (mask >> i) & 1;
				dist += bit;
				i++;
			}
			return dist;
		}

		// Moving right to left.
		else {

			int i = pos1, dist = 0;

			// Count each item in the word we must skip over.
			while (i > pos2) {
				int bit = (mask >> i) & 1;
				dist += bit;
				i--;
			}
			return dist;
		}
	}

	// The number of button presses to go from a to b and then hit fire.
	public static int dist(char a, char b) {
		int wrap = Math.abs(a - b);
		if (wrap > 13) wrap = 26 - wrap;
		return wrap+1;
	}
}