#include <bits/stdc++.h>
using namespace std;
struct Point {
  int x;
  int y;
};
int check(Point a, Point b) {
  if ((a.x == b.x && a.y == b.y) || (a.x == b.y && a.y == b.x)) {
    return 0;
  } else if (a.x == b.x || a.x == b.y) {
    return a.x;
  } else if (a.y == b.x || a.y == b.y) {
    return a.y;
  }
  return 0;
}
int main() {
  int n, m, c, d, num[10];
  for (int i = 0; i < 10; i++) {
    num[i] = 0;
  }
  cin >> n >> m;
  Point a[n], b[m];
  for (int i = 0; i < n; i++) {
    cin >> c >> d;
    a[i].x = c;
    a[i].y = d;
  }
  for (int i = 0; i < m; i++) {
    cin >> c >> d;
    b[i].x = c;
    b[i].y = d;
  }
  for (int i = 0; i < n; i++) {
    int dummy = 0;
    for (int j = 0; j < m; j++) {
      int chk = check(a[i], b[j]);
      if (chk == 0) {
        continue;
      }
      if (dummy == 0) {
        dummy = chk;
      } else if (chk != dummy) {
        cout << "-1" << endl;
        return 0;
      }
      num[chk]++;
    }
  }
  for (int i = 0; i < m; i++) {
    int dummy = 0;
    for (int j = 0; j < n; j++) {
      int chk = check(b[i], a[j]);
      if (chk == 0) {
        continue;
      }
      if (dummy == 0) {
        dummy = chk;
      } else if (chk != dummy) {
        cout << "-1" << endl;
        return 0;
      }
      num[chk]++;
    }
  }
  int ans = 0, ans1 = 0;
  for (int i = 1; i < 10; i++) {
    if (num[i] > 0) {
      ans1 = i;
      ans++;
    }
  }
  if (ans > 1) {
    cout << "0" << endl;
  } else if (ans == 1) {
    cout << ans1 << endl;
  }
  return 0;
}
