#include <bits/stdc++.h>
using namespace std;
bool cmp(pair<long long int, char> p1, pair<long long int, char> p2) {
  return p1.first > p2.first;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int n, m, i, j, com, temp, c1 = 0;
  cin >> n >> m;
  bool poss[10];
  for (i = 0; i <= 9; i++) poss[i] = false;
  bool ans = true;
  vector<pair<long long int, long long int> > p1(n), p2(m);
  for (i = 0; i < n; i++) cin >> p1[i].first >> p1[i].second;
  for (i = 0; i < m; i++) cin >> p2[i].first >> p2[i].second;
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      set<long long int> s;
      s.insert(p1[i].first);
      s.insert(p1[i].second);
      s.insert(p2[j].first);
      s.insert(p2[j].second);
      if (s.size() == 3) {
        for (long long int k = 0; k <= 9; k++) {
          if ((k == p1[i].first or k == p1[i].second) and
              (k == p2[j].first or k == p2[j].second))
            poss[k] = true;
        }
      }
    }
  }
  long long int cnt = 0, res = 0, res1 = 0;
  for (i = 0; i <= 9; i++)
    if (poss[i] == true) {
      cnt++;
      res = i;
    }
  if (cnt == 1) {
    return cout << res << "\n", 0;
  }
  for (i = 0; i < n; i++) {
    res = 0;
    res1 = 0;
    if (poss[p1[i].first] == true and poss[p1[i].second] == true) {
      for (j = 0; j < m; j++) {
        if (p1[i].first == p2[j].first and p1[i].second != p2[j].second)
          res = 1;
        if (p1[i].first == p2[j].second and p1[i].second != p2[j].first)
          res = 1;
        if (p1[i].second == p2[j].first and p1[i].first != p2[j].second)
          res1 = 1;
        if (p1[i].second == p2[j].second and p1[i].first != p2[j].first)
          res1 = 1;
      }
      if (res == 1 and res1 == 1) {
        ans = false;
        break;
      }
    }
  }
  for (i = 0; i < m; i++) {
    res = 0;
    res1 = 0;
    if (poss[p2[i].first] == true and poss[p2[i].second] == true) {
      for (j = 0; j < n; j++) {
        if (p2[i].first == p1[j].first and p2[i].second != p1[j].second)
          res = 1;
        if (p2[i].first == p1[j].second and p2[i].second != p1[j].first)
          res = 1;
        if (p2[i].second == p1[j].first and p2[i].first != p1[j].second)
          res1 = 1;
        if (p2[i].second == p1[j].second and p2[i].first != p1[j].first)
          res1 = 1;
      }
      if (res == 1 and res1 == 1) {
        ans = false;
        break;
      }
    }
  }
  if (ans == true)
    return cout << "0\n", 0;
  else
    return cout << "-1\n", 0;
  return 0;
}
