#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<pair<int, int> > a(n);
  vector<pair<int, int> > b(m);
  for (int i = 0; i < n; i++) {
    cin >> a[i].first >> a[i].second;
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i].first >> b[i].second;
  }
  int i, c = -1, ans = 1, j;
  for (i = 0; i < n; i++) {
    int first = 0, second = 0;
    for (j = 0; j < m; j++) {
      if (min(a[i].first, a[i].second) == min(b[j].first, b[j].second) &&
          max(a[i].first, a[i].second) == max(b[j].first, b[j].second))
        continue;
      if (a[i].first == b[j].first || a[i].first == b[j].second) first++;
      if (a[i].second == b[j].first || a[i].second == b[j].second) second++;
    }
    if (first > 0 && second > 0) {
      ans = -1;
      break;
    } else if (ans == 1 && (first > 0 || second > 0)) {
      if (c == -1) {
        if (first > 0)
          c = a[i].first;
        else
          c = a[i].second;
      } else {
        if (first > 0 && c != a[i].first)
          ans = 0;
        else if (second > 0 && c != a[i].second)
          ans = 0;
      }
    }
  }
  for (i = 0; i < m; i++) {
    int first = 0, second = 0;
    for (j = 0; j < n; j++) {
      if (min(a[j].first, a[j].second) == min(b[i].first, b[i].second) &&
          max(a[j].first, a[j].second) == max(b[i].first, b[i].second))
        continue;
      if (b[i].first == a[j].first || b[i].first == a[j].second) first++;
      if (b[i].second == a[j].first || b[i].second == a[j].second) second++;
    }
    if (first > 0 && second > 0) {
      ans = -1;
      break;
    } else if (ans == 1 && (first > 0 || second > 0)) {
      if (c == -1) {
        if (first > 0)
          c = b[i].first;
        else
          c = b[i].second;
      } else {
        if (first > 0 && c != b[i].first)
          ans = 0;
        else if (second > 0 && c != b[i].second)
          ans = 0;
      }
    }
  }
  if (ans == 1)
    cout << c;
  else
    cout << ans;
  return 0;
}
