#include <cstdio>
#include<iostream>
using namespace std;
struct student {
int wake;
int sleep;
int loc;
} stu[10];
int main() {
int n;
int index=1;
while(cin>>n) {
if(n==0) break;
int start[10];
int sleep=0;//睡觉的人数
for(int i=0; i<n; i++) {
cin>>stu[i].wake>>stu[i].sleep>>stu[i].loc;
stu[i].loc--;
start[i]=stu[i].loc;
if(stu[i].loc>stu[i].wake-1)
sleep++;
}
long long time=0;
while(++time) {
bool flag=0;
//如果睡觉的的人多
if(sleep>n-sleep) flag=1;
for(int i=0; i<n; i++) {
//同时处于马上要睡着的人,这时就睡着
//注意这里要判断是不是第一次,要是第一次就不进行计算
if(flag && time!=1) {
if(stu[i].loc==stu[i].wake)
sleep++;
if(stu[i].loc==0)
sleep--;
} else if(time!=1) {
if(stu[i].loc==stu[i].wake)
stu[i].loc=0;
else if(stu[i].loc==0)
sleep--;
}
}
bool flag2=1;
if(time==1) flag2=0;
for(int i=0; i<n; i++) {
//判断是否与开始时的状态相同
if(stu[i].loc!=start[i]) flag2=0;
//然后对每个同学所处的位置自增
stu[i].loc++;
//不过要保证在这个区间内
stu[i].loc%=(stu[i].sleep+stu[i].wake);
}
if(sleep==0)
break;
if(flag2) {
//cout<<time<<endl;
time=-1;
break;
}
//cout<<"sleep:"<<sleep<<endl;
}
cout<<"Case "<<index++<<": "<<time<<endl;
}
return 0;
}