#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 7;
const long long INFL = 1e18 + 123;
const double PI = atan2(0, -1);
mt19937 tw(960172);
long long rnd(long long x, long long y) {
  static uniform_int_distribution<long long> d;
  return d(tw) % (y - x + 1) + x;
}
void solve() {
  int n, m;
  cin >> n >> m;
  vector<pair<int, int> > arr1, arr2;
  for (int i = 0; i < n; ++i) {
    int a, b;
    cin >> a >> b;
    if (a > b) {
      swap(a, b);
    }
    arr1.push_back({a, b});
  }
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    if (a > b) {
      swap(a, b);
    }
    arr2.push_back({a, b});
  }
  int cnt = 0, any = -1;
  for (int i = 1; i <= 9; ++i) {
    bool fl = false;
    for (auto tmp1 : arr1) {
      for (auto tmp2 : arr2) {
        if (tmp2.first == tmp1.first && tmp2.second == tmp1.second) {
          continue;
        }
        if ((tmp2.first == i || tmp2.second == i) &&
            (tmp1.first == i || tmp1.second == i)) {
          fl = true;
          break;
        }
      }
      if (fl) {
        break;
      }
    }
    if (fl) {
      cnt++;
      any = i;
    }
  }
  if (cnt == 1) {
    cout << any << "\n";
    return;
  }
  for (auto tmp1 : arr1) {
    int a, b;
    tie(a, b) = tmp1;
    for (auto tmp2 : arr2) {
      int c, d;
      tie(c, d) = tmp2;
      if (a == c && b == d) {
        continue;
      }
      if (a != c && a != d && b != c && b != d) {
        continue;
      }
      int cnt1 = 0;
      for (int i : {a, b}) {
        bool fl2 = false;
        for (auto p : arr2) {
          if (p.first == a && p.second == b) {
            continue;
          }
          fl2 |= p.first == i || p.second == i;
        }
        if (fl2) {
          ++cnt1;
        }
      }
      if (cnt1 > 1) {
        cout << "-1\n";
        return;
      }
      int cnt2 = 0;
      for (int i : {c, d}) {
        bool fl1 = false;
        for (auto p : arr1) {
          if (p.first == c && p.second == d) {
            continue;
          }
          fl1 |= p.first == i || p.second == i;
        }
        if (fl1) {
          ++cnt2;
        }
      }
      if (cnt2 > 1) {
        cout << "-1\n";
        return;
      }
    }
  }
  cout << "0\n";
}
int main() {
  cerr << fixed << setprecision(15);
  cout << fixed << setprecision(15);
  ios::sync_with_stdio(false);
  int tests = 1;
  for (int it = 1; it <= tests; ++it) {
    solve();
  }
  return 0;
}
