// Arup Guha
// 9/21/2013
// Solution to 2010 Arab Programming Contest Problem D: Tri graphs

import java.util.*;

public class d {
	final public static long MAX = 1000000000000000L;
	final public static int[][][] C_DELTA = { {{-1,0}, {-1,1}}, {{0,-1},
											  {-1,-1}, {-1,0}, {-1,1}},
											  {{0,-1}, {-1,-1}, {-1,0}}
										    };

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		int n = stdin.nextInt();
		int loop = 1;

		while (n != 0) {

			// Read data.
			long[][] grid = new long[n][3];
			for (int i=0; i<n; i++)
				for (int j=0; j<3; j++)
					grid[i][j] = stdin.nextLong();

			// Set up DP.
			long[][] dp = new long[n][3];
			dp[0][1] = grid[0][1];
			dp[0][2] = grid[0][1] + grid[0][2];

			// Run DP over positions [i][j].
			for (int i=1; i<n; i++) {
				for (int j=0; j<C_DELTA.length; j++) {

					// Default setting we will overwrite.
					dp[i][j] = MAX;
					for (int k=0; k<C_DELTA[j].length; k++) {

						// Square we are coming from.
						int buildx = i+C_DELTA[j][k][0];
						int buildy = j+C_DELTA[j][k][1];

						// Can not build off square (0,0)
						if (buildx == 0 && buildy == 0) continue;
						dp[i][j] = Math.min(dp[i][j], dp[buildx][buildy] + grid[i][j]);
					}

				}
			}

			// Print output.
			System.out.println(loop+". "+dp[n-1][1]);

			// Go to next case.
			loop++;
			n = stdin.nextInt();
		}
	}
}