#include<bits/stdc++.h>
#define int long long
#define endl "\n"
#define lowbit(x), x&-x
#define quick_io cin.tie(0),cout.tie(0),ios::sync_with_stdio(0)
using namespace std;
int n,m,a[10005][105],x[10005][105],b[10005][105],t[10005],be,ans;
signed main()
{
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
quick_io;
cin>>n>>m;
for (int i=1;i<=n;i++)
{
for (int j=0;j<m;j++)
{
cin>>x[i][j]>>a[i][j];
if (x[i][j])
b[i][++t[i]]=j;
}
}
cin>>be;
for (int i=1;i<=n;i++)
{
ans=(ans+a[i][be]%20103)%20103;
int bee=be;
while (!x[i][be])
be=(be+1)%m;
int yu;
for (int j=1;j<=t[i];j++)
if (b[i][j]==be)
yu=j;
int y=a[i][bee],xy=yu;
y=(y-2+t[i])%t[i]+1;
while (y)
y--,xy=xy%t[i]+1;
be=b[i][xy];
}
cout<<ans;
return 0;
}
/*
2 3
1 2
0 3
1 4
0 1
1 5
1 2
1
*/
#1,#3AC,其余全WA。