#include <bits/stdc++.h>
using namespace std;
pair<int, int> a[105];
pair<int, int> b[105];
int n, m, res, ans;
bool me;
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> a[i].first >> a[i].second;
  }
  for (int i = 1; i <= m; i++) {
    cin >> b[i].first >> b[i].second;
  }
  me = true;
  for (int i = 1; i <= n; i++) {
    bool f = false;
    bool s = false;
    for (int j = 1; j <= m; j++) {
      if (a[i].first == b[j].first && a[i].second != b[j].second) {
        f = true;
        if (ans != 0 && ans != a[i].first) me = false;
        ans = a[i].first;
      }
      if (a[i].first == b[j].second && a[i].second != b[j].first) {
        f = true;
        if (ans != 0 && ans != a[i].first) me = false;
        ans = a[i].first;
      }
      if (a[i].second == b[j].first && a[i].first != b[j].second) {
        s = true;
        if (ans != 0 && ans != a[i].second) me = false;
        ans = a[i].second;
      }
      if (a[i].second == b[j].second && a[i].first != b[j].first) {
        s = true;
        if (ans != 0 && ans != a[i].second) me = false;
        ans = a[i].second;
      }
    }
    if (f ^ s) res++;
    if (f && s) return cout << -1, 0;
  }
  for (int i = 1; i <= max(n, m); i++) {
    swap(a[i], b[i]);
  }
  swap(n, m);
  for (int i = 1; i <= n; i++) {
    bool f = false;
    bool s = false;
    for (int j = 1; j <= m; j++) {
      if (a[i].first == b[j].first && a[i].second != b[j].second) {
        f = true;
        if (ans != 0 && ans != a[i].first) me = false;
        ans = a[i].first;
      }
      if (a[i].first == b[j].second && a[i].second != b[j].first) {
        f = true;
        if (ans != 0 && ans != a[i].first) me = false;
        ans = a[i].first;
      }
      if (a[i].second == b[j].first && a[i].first != b[j].second) {
        s = true;
        if (ans != 0 && ans != a[i].second) me = false;
        ans = a[i].second;
      }
      if (a[i].second == b[j].second && a[i].first != b[j].first) {
        s = true;
        if (ans != 0 && ans != a[i].second) me = false;
        ans = a[i].second;
      }
    }
    if (f ^ s) res++;
    if (f && s) return cout << -1, 0;
  }
  if (me)
    cout << ans;
  else
    cout << 0;
  return 0;
}
