#include <bits/stdc++.h>
using namespace std;
void close() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
}
const int maxn = 1005;
struct node {
  int u, v;
};
node a[maxn], b[maxn];
int n, m;
int main() {
  close();
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i].u >> a[i].v;
    if (a[i].v > a[i].u) swap(a[i].v, a[i].u);
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i].u >> b[i].v;
    if (b[i].v > b[i].u) swap(b[i].v, b[i].u);
  }
  int ans = 1;
  int tmp = -1;
  for (int i = 0; i < n; i++) {
    int fir = 0, sec = 0;
    for (int j = 0; j < m; j++) {
      if (a[i].v == b[j].v && a[i].u == b[j].u) continue;
      if (a[i].u == b[j].u || a[i].u == b[j].v) fir++;
      if (a[i].v == b[j].u || a[i].v == b[j].v) sec++;
    }
    if (fir > 0 && sec > 0) {
      ans = -1;
      break;
    }
    if (fir > 0 || sec > 0) {
      if (tmp == -1) {
        if (fir > 0)
          tmp = a[i].u;
        else
          tmp = a[i].v;
      } else {
        if (fir > 0 && tmp != a[i].u)
          ans = 0;
        else if (sec > 0 && tmp != a[i].v)
          ans = 0;
      }
    }
  }
  for (int j = 0; j < m; j++) {
    int fir = 0, sec = 0;
    for (int i = 0; i < n; i++) {
      if (a[i].v == b[j].v && a[i].u == b[j].u) continue;
      if (a[i].u == b[j].u || a[i].v == b[j].u) fir++;
      if (a[i].v == b[j].v || a[i].u == b[j].v) sec++;
    }
    if (fir > 0 && sec > 0) {
      ans = -1;
      break;
    }
    if ((ans == 1) && (fir > 0 || sec > 0)) {
      if (tmp == -1) {
        if (fir > 0)
          tmp = b[j].u;
        else
          tmp = b[j].v;
      } else {
        if (fir > 0 && tmp != b[j].u)
          ans = 0;
        else if (sec > 0 && tmp != b[j].v)
          ans = 0;
      }
    }
  }
  if (ans > 0)
    cout << tmp << endl;
  else
    cout << ans << endl;
}
