#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>

using namespace std;

#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<" "; cerr<<endl; }
inline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const int CYCLE = 2 * 2 * 2 * 2 * 3 * 3 * 5 * 7 * 11;

int main(){
  int N;
  while(cin>>N && N){
    int access[25][25] = {};
    REP(i, N){
      int cycle, offset; cin>>cycle>>offset;
      REP(j, cycle){
        int volume; cin>>volume;
        access[cycle][(j - offset + cycle) % cycle] += volume;
      }
    }
    vector<int> access_sum(CYCLE);
    for(int cycle = 1; cycle <= 24; cycle++){
      if(cycle == 13 || cycle == 17 || cycle == 19 || cycle == 23){
        int max_a = 0;
        REP(i, cycle) max_a = max(max_a, access[cycle][i]);
        REP(i, CYCLE) access_sum[i] += max_a;
      }else{
        int idx = 0;
        REP(i, CYCLE){
          access_sum[i] += access[cycle][idx];
          idx = (idx + 1) % cycle;
        }
      }
    }
    cout<<*max_element(access_sum.begin(), access_sum.end())<<endl;
  }
  return 0;
}