#include <bits/stdc++.h>
using namespace std;
const int mod = 20123;
int n, m, indicator[10001][101], cnt, Number_of_stairs[10001] = {}, ans = 0;
bool stair[10001][101];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++){
for (int j = 1; j <= m; j++){
cin >> stair[i][j] >> indicator[i][j];
Number_of_stairs[i] += stair[i][j];
}
}
cin >> cnt;
cnt++;
for (int i = 1; i <= n; i++){
ans += indicator[i][cnt];
int d = indicator[i][cnt] % Number_of_stairs[i];
while (d){
cnt++;
if (cnt > m){
cnt -= m;
}
if (stair[i][cnt]){
d--;
}
}
}
cout << (ans + mod) % mod;
return 0;
}
