#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;
    }
}
