import java.awt.Point;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Stack;


public class MazeSolver {
	private static final int HEIGHT = 9;
	private static final int WIDTH = 12;
	
	public static final int CELL_PASSAGE = 0;
	public static final int CELL_WALL = 1;
	public static final int CELL_EXPLORED = 2;
	public static final int CELL_EXIT = 3;
	public static final int CELL_START = 4;
	
	private int[][] grid = new int[WIDTH][HEIGHT];
	
	public static void main(String[] args) {
		MazeSolver solver = new MazeSolver("data.txt");
	}
	
	public MazeSolver(String fileName) {
		Point start = null;
		try {
			BufferedReader in = new BufferedReader(new FileReader(fileName));
			
			String nextLine;
			int y = 0;
			while ((nextLine = in.readLine()) != null) {
				String[] parts = nextLine.split(" ");
				for (int x = 0; x < parts.length; x++) {
					grid[x][y] = Integer.parseInt(parts[x]);
					if (grid[x][y] == CELL_START) {
						start = new Point(x, y);
						grid[x][y] = CELL_PASSAGE;
					}
				}
				y++;
			}
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(findExit(start));
	}
	
	public Point findExit(Point start) {
		Stack<Point> stack = new Stack<Point>();
		stack.push(start);
		
		while (!stack.isEmpty()) {
			Point next = stack.pop();
			
			if (next.x < WIDTH && next.y < HEIGHT && next.x >= 0 && next.y >= 0) {
				if (grid[next.x][next.y] == CELL_PASSAGE) {
					grid[next.x][next.y] = CELL_EXPLORED;
				
					stack.push(new Point(next.x, next.y + 1));
					stack.push(new Point(next.x, next.y - 1));
					stack.push(new Point(next.x + 1, next.y));
					stack.push(new Point(next.x - 1, next.y));
				} else if (grid[next.x][next.y] == CELL_EXIT) {
					return new Point(next.x, next.y);
				}
			}
		}
		return null;
	}
}
 
