#include <bits/stdc++.h>
using namespace std;
const int N = 12;
pair<int, int> a[N];
pair<int, int> b[N];
int same(pair<int, int>& p, pair<int, int>& q) {
  if (p.first == q.first && p.second == q.second) return 0;
  if (p.first == q.first || p.first == q.second) return p.first;
  if (p.second == q.first || p.second == q.second) return p.second;
  return 0;
}
int n, m;
bool can[10];
int main() {
  srand(time(NULL));
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  fill(can, can + 10, false);
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i].first >> a[i].second;
    if (a[i].second < a[i].first) swap(a[i].first, a[i].second);
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i].first >> b[i].second;
    if (b[i].second < b[i].first) swap(b[i].first, b[i].second);
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      int c = same(a[i], b[j]);
      if (c > 0) can[c] = true;
    }
  }
  int cnt = 0;
  for (int i = 0; i < 10; i++)
    if (can[i]) cnt++;
  if (cnt == 1) {
    for (int i = 0; i < 10; i++)
      if (can[i]) {
        cout << i << endl;
        return 0;
      }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      int c = same(a[i], b[j]);
      if (!c) continue;
      for (int k = 0; k < m; k++) {
        int d = same(a[i], b[k]);
        if (d != 0 && d != c) {
          cout << "-1\n";
          return 0;
        }
      }
      for (int k = 0; k < n; k++) {
        int d = same(a[k], b[j]);
        if (d != 0 && d != c) {
          cout << "-1\n";
          return 0;
        }
      }
    }
  }
  cout << "0\n";
  return 0;
}
