#include <bits/stdc++.h>
using namespace std;
pair<int, int> a[15], b[15];
int rec[10], f[5], ans[10];
int main() {
  int n, m, sum;
  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;
  memset(ans, 0, sizeof(ans));
  for (int i = 1; i <= n; i++) {
    memset(rec, 0, sizeof(rec));
    for (int j = 1; j <= m; j++) {
      f[1] = (a[i].first == b[j].first);
      f[2] = (a[i].second == b[j].second);
      f[3] = (a[i].first == b[j].second);
      f[4] = (a[i].second == b[j].first);
      if (f[1] + f[2] + f[3] + f[4] == 1) {
        if (f[1] || f[3]) rec[a[i].first] = 1;
        if (f[2] || f[4]) rec[a[i].second] = 1;
      }
    }
    sum = 0;
    for (int j = 1; j <= 9; j++) sum += rec[j];
    if (sum > 1) {
      cout << -1 << endl;
      return 0;
    }
    if (sum == 1) {
      for (int j = 1; j <= 9; j++) {
        if (rec[j]) {
          ans[j] = 1;
          break;
        }
      }
    }
  }
  for (int i = 1; i <= m; i++) {
    memset(rec, 0, sizeof(rec));
    for (int j = 1; j <= n; j++) {
      f[1] = (b[i].first == a[j].first);
      f[2] = (b[i].second == a[j].second);
      f[3] = (b[i].first == a[j].second);
      f[4] = (b[i].second == a[j].first);
      if (f[1] + f[2] + f[3] + f[4] == 1) {
        if (f[1] || f[3]) rec[b[i].first] = 1;
        if (f[2] || f[4]) rec[b[i].second] = 1;
      }
    }
    sum = 0;
    for (int j = 1; j <= 9; j++) sum += rec[j];
    if (sum > 1) {
      cout << -1 << endl;
      return 0;
    }
    if (sum == 1) {
      for (int j = 1; j <= 9; j++) {
        if (rec[j]) {
          ans[j] = 1;
          break;
        }
      }
    }
  }
  sum = 0;
  for (int i = 1; i <= 9; i++) sum += ans[i];
  if (sum > 1)
    cout << 0 << endl;
  else {
    for (int i = 1; i <= 9; i++) {
      if (ans[i]) {
        cout << i << endl;
        break;
      }
    }
  }
  return 0;
}
