#include<bits/stdc++.h>
#define MAXA 2000500
#define MOD 20123
using namespace std;
typedef long long LL;
int n,m;
int Tatemono[10005][105][2];//[][][1]CanUp [][][2]Tip
int Hajime,Kagi;
int main() {
//freopen("treasure.in","r",stdin);
//freopen("treasure.out","w",stdout);
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++) {
for(int j=0;j<=m-1;j++) {
scanf("%d %d",&Tatemono[i][j][1],&Tatemono[i][j][2]);
if(Tatemono[i][j][1])
Tatemono[i][0][0];
}
}
scanf("%d",&Hajime);
for(int i=1;i<=n;i) {
int Step = Tatemono[i][Hajime][2];
Kagi += Step;
Step = Step % Tatemono[i][0][0];
if(Step == 0)
Step += Tatemono[i][0][0];
for(Hajime;Hajime<=m;Hajime++) {
if(Hajime == m)
Hajime = 0;
if(!Tatemono[i][Hajime][1])
continue;
else Step--;
if(Step == 0)
break;
}
}
printf("%d",Kagi%MOD);
}