#include <bits/stdc++.h>
using namespace std;
int n, m, st, s = 0;
pair<int, int> a[10001][100];
vector<int> l[10001];
int main()
{
ios::sync_with_stdio(false);
cin >> n >> m;
for (int i = 1; i <= n; ++i)
for (int j = 0; j < m; ++j)
{
int x, y;
cin >> x >> y;
if (x == 1)
l[i].push_back(j);
a[i][j] = make_pair(y, l[i].size() - 1);
}
cin >> st;
for (int x = st, i = 1; i <= n; ++i)
{
int mv = (a[i][x].first - 1) % l[i].size() + 1;
s += a[i][x].first;
int st = a[i][x].second;
x = l[i][(st + mv) % l[i].size()];
}
cout << s;
}