#include <bits/stdc++.h>
using namespace std;
const int INF = 0x7fffffff;
const int N = 1e5 + 5;
inline int read() {
  int x = 0, rev = 0, ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') rev = 1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + ch - '0';
    ch = getchar();
  }
  return rev ? -x : x;
}
int n, m, ans;
int cnt;
pair<int, int> a[N], b[N];
int vis[N];
int judge(int i, int j) {
  return (a[i].first == b[j].first) + (a[i].second == b[j].first) +
         (a[i].first == b[j].second) + (a[i].second == b[j].second);
}
int get(int i, int j) {
  if (a[i].first == b[j].first || a[i].first == b[j].second) return a[i].first;
  return a[i].second;
}
int main() {
  n = read(), m = read();
  for (int i = 1; i <= n; i++) {
    a[i].first = read(), a[i].second = read();
  }
  for (int i = 1; i <= m; i++) {
    b[i].first = read(), b[i].second = read();
  }
  for (int i = 1; i <= n; i++) {
    int c1 = 0, c2 = 0;
    for (int j = 1; j <= m; j++) {
      if (judge(i, j) != 1) continue;
      vis[get(i, j)] = 1;
    }
  }
  for (int i = 0; i <= 9; i++) {
    if (vis[i]) ++cnt, ans = i;
  }
  if (cnt == 1) return cout << ans << endl, 0;
  for (int i = 1; i <= n; i++) {
    map<int, int> mp;
    int c1 = 0, c2 = 0;
    for (int j = 1; j <= m; j++) {
      if (judge(i, j) != 1) continue;
      mp[get(i, j)]++;
    }
    if (mp.size() > 1) return cout << -1 << endl, 0;
  }
  for (int i = 1; i <= m; i++) {
    map<int, int> mp;
    int c1 = 0, c2 = 0;
    for (int j = 1; j <= n; j++) {
      if (judge(j, i) != 1) continue;
      mp[get(j, i)]++;
    }
    if (mp.size() > 1) return cout << -1 << endl, 0;
  }
  return cout << 0 << endl, 0;
}
