#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200010;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-9;
int n, m;
pair<int, int> a[MAXN];
pair<int, int> b[MAXN];
int vis[10];
bool check1() {
  int cnt = 0, num;
  for (int i = (1); i <= (9); i++)
    if (vis[i]) cnt++, num = i;
  if (cnt == 1) {
    printf("%d\n", num);
    return true;
  }
  return false;
}
int main() {
  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;
  int fn = 0, fm = 0;
  for (int i = (1); i <= (n); i++) {
    int fx = 0, fy = 0;
    for (int j = (1); j <= (m); j++) {
      if (a[i].first == b[j].second && a[i].second != b[j].first)
        fx = vis[a[i].first] = 1;
      if (a[i].first == b[j].first && a[i].second != b[j].second)
        fx = vis[a[i].first] = 1;
      if (a[i].second == b[j].first && a[i].first != b[j].second)
        fy = vis[a[i].second] = 1;
      if (a[i].second == b[j].second && a[i].first != b[j].first)
        fy = vis[a[i].second] = 1;
    }
    if (fx && fy) fn = 1;
  }
  if (check1()) return 0;
  memset(vis, 0, sizeof(vis));
  for (int i = (1); i <= (m); i++) {
    int fx = 0, fy = 0;
    for (int j = (1); j <= (n); j++) {
      if (b[i].first == a[j].second && b[i].second != a[i].first)
        fx = vis[b[i].first] = 1;
      if (b[i].first == a[j].first && b[i].second != a[j].second)
        fx = vis[b[i].first] = 1;
      if (b[i].second == a[j].first && b[i].first != a[j].second)
        fy = vis[b[i].second] = 1;
      if (b[i].second == a[j].second && b[i].first != a[j].first)
        fy = vis[b[i].second] = 1;
    }
    if (fx && fy) fm = 1;
  }
  if (check1()) return 0;
  if (fn || fm)
    puts("-1");
  else
    puts("0");
  return 0;
}
