#include using i64 = long long; int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int N, X, Y; std::cin >> N >> X >> Y; std::vector> a(X), b(Y); std::vector ida(N + 2, -1), idb(N + 2, -1), oa(N + 2, -1), ob(N + 2, -1); std::vector pa(X), pb(X); for (int i = 0; i < X; i++) { int k; std::cin >> k; a[i].resize(k + 1); for (int j = 0; j <= k; j++) { std::cin >> a[i][j]; ida[a[i][j]] = i; oa[a[i][j]] = j; } pa[i] = k; } for (int i = 0; i < Y; i++) { int k; std::cin >> k; b[i].resize(k + 1); for (int j = 0; j <= k; j++) { std::cin >> b[i][j]; idb[b[i][j]] = i; ob[b[i][j]] = j; } pb[i] = k; } std::function extend = [&](int i) { while (pb[i]) { int x = b[i][pb[i]]; if (ida[x] != -1 && pa[ida[x]] > oa[x]) { break; } pb[i]--; } if (!pb[i]) { return; } int x = b[i][pb[i]]; int u = a[ida[x]][pa[ida[x]]]; pa[ida[x]] = oa[x]; if (u != N + 1 && idb[u] != -1) { extend(idb[u]); } }; for (int i = 0; i < Y; i++) { extend(i); } int ans = 0; for (int i = 0; i < X; i++) { ans += pa[i]; } std::cout << ans << "\n"; return 0; }