#include <bits/stdc++.h>
int main() {
  int n, t, pt;
  std::cin >> n;
  t = n * 10;
  pt = t + n;
  std::vector<int> f(t);
  std::vector<int> p(pt);
  for (int i = 0; i < t; ++i) std::cin >> f[i];
  for (int i = 0; i < pt; ++i) std::cin >> p[i];
  int c = -1;
  for (int i = 0; i < 5; ++i) {
    if (f[i * 2] || f[i * 2 + 1]) {
      ++c;
    } else {
      c += 2;
    }
  }
  std::cout << p[c];
  return 0;
}
