// Arup Guha
// 3/30/2014
// Solution to 2014 Chicago Invitational (NAIPC) Problem J: Two Knight's Poem

import java.util.*;

public class j {

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		state.output = stdin.nextLine();

		// Go through each case.
		while (!state.output.equals("*")) {

			// Just set up BFS from here!
			LinkedList<state> q = new LinkedList<state>();
			HashSet<Integer> used = new HashSet<Integer>();
			int[] k1 = {3, 0};
			int[] k2 = {3, 9};
			state start = new state(k1, k2, 0);
			q.offer(start);
			used.add(start.hash());

			// Run BFS from here.
			int ans = 0;
			while (q.size() > 0) {

				state front = q.poll();

				// Got to the end!
				if (front.n == state.output.length()) {
					ans = 1;
					break;
				}

				// Get all neighbors. Add unique ones to queue.
				ArrayList<state> neighbors = front.next();
				for (int i=0; i<neighbors.size(); i++) {
					if (!used.contains(neighbors.get(i).hash())) {
						q.offer(neighbors.get(i));
						used.add(neighbors.get(i).hash());
					}
				}

			}

			// Output and go to next case.
			System.out.println(ans);
			state.output = stdin.nextLine();
		}
	}
}

class state {

	// Keyboard size.
	final public static int COLS = 10;
	final public static int ROWS = 4;
	final public static int SIZE = COLS*ROWS;

	// Keyboard
	final public static String[] up   = {"QWERTYUIOP","ASDFGHJKL:","ZXCVBNM<>?","!!      !!"};
	final public static String[] down = {"qwertyuiop","asdfghjkl;","zxcvbnm,./","!!      !!"};

	// Knight moves.
	final public static int[] dx = {-2,-2,-1,-1,1,1,2,2};
	final public static int[] dy = {-1,1,-2,2,-2,2,-1,1};

	// What we need to keep track of.
	public int[] k1;
	public int[] k2;
	public int n;

	// One copy shared by all states.
	public static String output;

	public state(int[] myk1, int[] myk2, int myn) {
		k1 = myk1;
		k2 = myk2;
		n = myn;
	}

	public int hash() {
		int maxIndex = Math.max(COLS*k1[0] + k1[1], COLS*k2[0] + k2[1]);
		int minIndex = Math.min(COLS*k1[0] + k1[1], COLS*k2[0] + k2[1]);
		return n*SIZE*SIZE + minIndex*SIZE + maxIndex;
	}

	// Return all possibilities of moving from cur.
	public ArrayList<Integer[]> getNextLoc(int[] cur) {

		ArrayList<Integer[]> ans = new ArrayList<Integer[]>();

		// Try all knight moves.
		for (int i=0; i<dx.length; i++) {
			Integer[] tmp = new Integer[2];
			tmp[0] = cur[0] + dx[i];
			tmp[1] = cur[1] + dy[i];
			if (!inbounds(tmp[0], tmp[1])) continue;
			ans.add(tmp);
		}
		return ans;
	}

	// Returns true iff (row, col) is on the keyboard.
	public static boolean inbounds(int row, int col) {
		return row >= 0 && row < ROWS && col >= 0 && col < COLS;
	}

	// Get all the next states from here - method might return duplicates.
	public ArrayList<state> next() {

		ArrayList<state> list = new ArrayList<state>();

		ArrayList<Integer[]> movek1 = getNextLoc(k1);

		// Go through each of these.
		for (int i=0; i<movek1.size(); i++) {

			Integer[] next = movek1.get(i);
			int[] nextprim = new int[2];
			nextprim[0] = next[0];
			nextprim[1] = next[1];

			// Can't go to same square as other knight.
			if (COLS*next[0] + next[1] == COLS*k2[0] + k2[1]) continue;

			// Character to match.
			char nextChar = output.charAt(n);

			// Check no output.
			if (up[next[0]].charAt(next[1]) == '!') list.add(new state(nextprim, k2, n));

			// See if output matches.
			else {

				// See if shifted.
				char typed = '$';

				// Calculate shifted character.
				if (up[k2[0]].charAt(k2[1]) == '!')  typed = up[next[0]].charAt(next[1]);
				else								 typed = down[next[0]].charAt(next[1]);

				// Correctly typed the next character, move on!
				if (nextChar == typed) list.add(new state(nextprim, k2, n+1));
			}


		}

		// Do same for k2.
		ArrayList<Integer[]> movek2 = getNextLoc(k2);

		// Go through each of these.
		for (int i=0; i<movek2.size(); i++) {

			Integer[] next = movek2.get(i);
			int[] nextprim = new int[2];
			nextprim[0] = next[0];
			nextprim[1] = next[1];

			// Can't go to same square as other knight.
			if (COLS*next[0] + next[1] == COLS*k1[0] + k1[1]) continue;

			// Character to match.
			char nextChar = output.charAt(n);

			// Check no output.
			if (up[next[0]].charAt(next[1]) == '!') list.add(new state(k1, nextprim, n));

			// See if output matches.
			else {

				char typed = '$';

				if (up[k1[0]].charAt(k1[1]) == '!') typed = up[next[0]].charAt(next[1]);
				else 								typed = down[next[0]].charAt(next[1]);

				// Correctly typed the next character, move on!
				if (nextChar == typed) list.add(new state(k1, nextprim, n+1));
			}
		}

		// Finally!
		return list;
	}
}