#include <bits/stdc++.h>
using namespace std;
struct pr {
  int a, b, n, x;
  pr(int a, int b, int n) : a(a), b(b), n(n) {}
  pr() : a(0), b(0), n(-1) {}
};
int n, m;
vector<pr> a, b;
void connect(int x1, int& x2, int& y2) {
  if ((x2 != -1 && x2 != x1) || (y2 != -1 && y2 != x1)) {
    cout << -1;
    exit(0);
  }
  x2 = y2 = x1;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    a.push_back(pr(x, y, -1));
  }
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    b.push_back(pr(x, y, -1));
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i].a == b[j].a && a[i].b != b[j].b)
        connect(a[i].a, a[i].n, b[j].n);
      else if (a[i].b == b[j].b && a[i].a != b[j].a)
        connect(a[i].b, a[i].n, b[j].n);
      else if (a[i].a == b[j].b && a[i].b != b[j].a)
        connect(a[i].a, a[i].n, b[j].n);
      else if (a[i].b == b[j].a && a[i].a != b[j].b)
        connect(a[i].b, a[i].n, b[j].n);
    }
  }
  int ans = -1;
  for (int i = 0; i < n; i++) {
    if (a[i].n != -1) {
      if (ans == -1 || ans == a[i].n)
        ans = a[i].n;
      else {
        cout << 0;
        return 0;
      }
    }
  }
  cout << ans;
  return 0;
}
