#include <bits/stdc++.h>
using namespace std;
int rows;
typedef long long ll;
ll dp[5][100005];
ll cost[5][100005];
ll func(int i,int j)
{
if(i<1 || i>rows || j<1 || j>3)
return 100000000;
if(i==rows && j==2)
return cost[i][j];
if(i==rows && j==3)
return 100000000;
if(i==rows && j==1)
return cost[i][j]+cost[i][j+1];
if(dp[i][j]!=0)
return dp[i][j];
ll answer=100000000;
if(j==1)
{
answer=min(answer,func(i,j+1));
answer=min(answer,func(i+1,j));
answer=min(answer,func(i+1,j+1));
return dp[i][j]=cost[i][j]+answer;
}
if(j==2)
{
answer=min(answer,func(i,j+1));
answer=min(answer,func(i+1,j));
answer=min(answer,func(i+1,j+1));
answer=min(answer,func(i+1,j-1));
return dp[i][j]=cost[i][j]+answer;
}
if(j==3)
{
answer=min(answer,func(i+1,j));
answer=min(answer,func(i+1,j-1));
return dp[i][j]=cost[i][j]+answer;
}
}
int main()
{
for(int i=1;;i++)
{
cin >> rows;
if(rows==0)
break;
memset(dp,0,sizeof(dp));
memset(cost,100000000,sizeof(cost));
for(int i=1;i<=rows;i++)
{
for(int j=1;j<=3;j++)
{
cin >> cost[i][j];
}
}
ll answer=func(1,2);
cout << i << ". ";
cout << answer << endl;
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CmludCByb3dzOwp0eXBlZGVmIGxvbmcgbG9uZyBsbDsKbGwgZHBbNV1bMTAwMDA1XTsKbGwgY29zdFs1XVsxMDAwMDVdOwpsbCBmdW5jKGludCBpLGludCBqKQp7CiAgICBpZihpPDEgfHwgaT5yb3dzIHx8IGo8MSB8fCBqPjMpCiAgICAgICAgcmV0dXJuIDEwMDAwMDAwMDsKICAgIGlmKGk9PXJvd3MgJiYgaj09MikKICAgICAgICByZXR1cm4gY29zdFtpXVtqXTsKICAgICAgICBpZihpPT1yb3dzICYmIGo9PTMpCiAgICAgICAgICAgIHJldHVybiAxMDAwMDAwMDA7CiAgICAgICAgICAgIGlmKGk9PXJvd3MgJiYgaj09MSkKICAgICAgICAgICAgICAgIHJldHVybiBjb3N0W2ldW2pdK2Nvc3RbaV1baisxXTsKICAgICAgICAgICAgaWYoZHBbaV1bal0hPTApCiAgICAgICAgICAgICAgICByZXR1cm4gZHBbaV1bal07CiAgICBsbCBhbnN3ZXI9MTAwMDAwMDAwOwogICAgaWYoaj09MSkKICAgIHsKICAgICAgICBhbnN3ZXI9bWluKGFuc3dlcixmdW5jKGksaisxKSk7CiAgICAgICAgYW5zd2VyPW1pbihhbnN3ZXIsZnVuYyhpKzEsaikpOwogICAgICAgIGFuc3dlcj1taW4oYW5zd2VyLGZ1bmMoaSsxLGorMSkpOwogICAgICAgIHJldHVybiBkcFtpXVtqXT1jb3N0W2ldW2pdK2Fuc3dlcjsKICAgIH0KICAgIGlmKGo9PTIpCiAgICB7CiAgICAgICAgYW5zd2VyPW1pbihhbnN3ZXIsZnVuYyhpLGorMSkpOwogICAgICAgIGFuc3dlcj1taW4oYW5zd2VyLGZ1bmMoaSsxLGopKTsKICAgICAgICBhbnN3ZXI9bWluKGFuc3dlcixmdW5jKGkrMSxqKzEpKTsKICAgICAgICBhbnN3ZXI9bWluKGFuc3dlcixmdW5jKGkrMSxqLTEpKTsKICAgICAgICByZXR1cm4gZHBbaV1bal09Y29zdFtpXVtqXSthbnN3ZXI7CiAgICB9CiAgICBpZihqPT0zKQogICAgewogICAgICAgIGFuc3dlcj1taW4oYW5zd2VyLGZ1bmMoaSsxLGopKTsKICAgICAgICBhbnN3ZXI9bWluKGFuc3dlcixmdW5jKGkrMSxqLTEpKTsKICAgICAgICByZXR1cm4gZHBbaV1bal09Y29zdFtpXVtqXSthbnN3ZXI7CiAgICB9Cn0KaW50IG1haW4oKQp7CiAgICBmb3IoaW50IGk9MTs7aSsrKQogICAgewogICAgICAgIGNpbiA+PiAgcm93czsKICAgICAgICBpZihyb3dzPT0wKQogICAgICAgICAgICBicmVhazsKICAgICAgICBtZW1zZXQoZHAsMCxzaXplb2YoZHApKTsKICAgICAgICBtZW1zZXQoY29zdCwxMDAwMDAwMDAsc2l6ZW9mKGNvc3QpKTsKICAgICAgICBmb3IoaW50IGk9MTtpPD1yb3dzO2krKykKICAgICAgICB7CiAgICAgICAgICAgIGZvcihpbnQgaj0xO2o8PTM7aisrKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBjaW4gPj4gY29zdFtpXVtqXTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBsbCBhbnN3ZXI9ZnVuYygxLDIpOwogICAgICAgIGNvdXQgPDwgaSA8PCAiLiAiOwogICAgICAgIGNvdXQgPDwgYW5zd2VyIDw8IGVuZGw7CiAgICB9Cn0K