#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;
struct gg {
  int x, y;
  void input() { cin >> x >> y; }
  int same(const gg &g) const {
    return (x == g.x) + (y == g.y) + (x == g.y) + (y == g.x);
  }
  int same2(const gg &g) const {
    if (1 != same(g)) return 0;
    if (x == g.x || x == g.y) return x;
    return y;
  }
} g[15], h[15];
int n, m;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) g[i].input();
  for (int i = 1; i <= m; ++i) h[i].input();
  int f1 = 1, f2 = 1;
  set<int> S;
  for (int i = 1; i <= n; ++i) {
    set<int> ss;
    for (int j = 1; j <= m; ++j) {
      ss.insert(g[i].same2(h[j]));
    }
    ss.erase(0);
    if (1 == ss.size()) S.insert(*ss.begin());
    if (ss.size() > 1) f1 = 0;
  }
  for (int i = 1; i <= m; ++i) {
    set<int> ss;
    for (int j = 1; j <= n; ++j) {
      ss.insert(h[i].same2(g[j]));
    }
    ss.erase(0);
    if (ss.size() > 1) f2 = 0;
  }
  if (f1 && f2) {
    if (1 == S.size()) {
      printf("%d\n", *S.begin());
    } else {
      puts("0");
    }
  } else {
    puts("-1");
  }
  return 0;
}
