#include<bits/stdc++.h>
using namespace std;
const int mod=20123;
struct ont
{
bool tz;
int num;
}a[100005][105];
int n, m, j, ans, key, x, ind;
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 >> a[i][j].tz >> a[i][j].num;
}
}
cin >> x;
j = ++ x;
for (int i = 1; i <= n; i++)
{
key = 0;
ans=ans%mod+a[i][j].num%mod;
for (j = 1; j <= m + 1; j++)
{
if (!ind)
{
ind++;
j=x;
}
if (j > m)
{
j = 1;
}
if (!key)
{
key = a[i][j].num;
}
if (a[i][j].tz == 1 && key==1)
{
goto loop;
}
if(key && a[i][j].tz==1)
{
key--;
}
}
loop:;
}
cout << ans%20123;
return 0;
}