#include <bits/stdc++.h>
using ll = unsigned long long;
using namespace std;
map<int, vector<int>> tree;
map<int, vector<int>> rooted;
map<int, int> primes;
void debugi(vector<int>& vec) {
  int n = (int)vec.size();
  for (int i = 0; i < n; i++) {
    cout << vec[i] << " ";
  }
  cout << endl;
}
void debugl(vector<ll>& vec) {
  int n = (int)vec.size();
  for (int i = 0; i < n; i++) {
    cout << vec[i] << " ";
  }
  cout << endl;
}
void debugmap(map<int, int>& mp) {
  for (auto x : mp) {
    cout << x.first << " " << x.second << " , ";
  }
  cout << endl;
}
void debugset(set<int>& st) {
  for (auto x : st) {
    cout << x << " ";
  }
  cout << endl;
}
void debugpi(vector<pair<int, int>>& arr) {
  for (int i = 0; i < (int)arr.size(); i++) {
    cout << "[" << arr[i].first << " : " << arr[i].second << "]"
         << " ";
  }
  cout << endl;
}
int main(int argc, const char* argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  map<int, vector<int>> mp1, mp2;
  for (int i = 0; i < n; i++) {
    int u, v;
    cin >> u >> v;
    mp1[u].push_back(v);
    mp1[v].push_back(u);
  }
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    mp2[u].push_back(v);
    mp2[v].push_back(u);
  }
  int com = 0;
  int num = 0;
  for (int i = 1; i <= 9; i++) {
    int m = (int)mp1[i].size();
    int cnt = 0;
    for (int j = 0; j < m; j++) {
      int x = mp1[i][j];
      int a = (int)mp2[i].size(), b = (int)mp2[x].size();
      if (a > 1 && b > 1) {
        cout << -1 << endl;
        return 0;
      }
      int cnt1 = 0, cnt2 = 0;
      for (int k = 0; k < a; k++) {
        if (mp2[i][k] != x) {
          cnt += 1;
          cnt1 += 1;
        }
      }
      for (int k = 0; k < b; k++) {
        if (mp2[x][k] != i) {
          cnt2 += 1;
        }
      }
      if (cnt1 != 0 && cnt2 != 0) {
        cout << -1 << endl;
        return 0;
      }
    }
    m = (int)mp2[i].size();
    for (int j = 0; j < m; j++) {
      int x = mp2[i][j];
      int a = (int)mp1[i].size(), b = (int)mp1[x].size();
      if (a > 1 && b > 1) {
        cout << -1 << endl;
        return 0;
      }
      int cnt1 = 0, cnt2 = 0;
      for (int k = 0; k < a; k++) {
        if (mp1[i][k] != x) {
          cnt += 1;
          cnt1 += 1;
        }
      }
      for (int k = 0; k < b; k++) {
        if (mp1[x][k] != i) {
          cnt2 += 1;
        }
      }
      if (cnt1 != 0 && cnt2 != 0) {
        cout << -1 << endl;
        return 0;
      }
    }
    if (cnt > 0) {
      com += 1;
      num = i;
    }
  }
  if (com == 1) {
    cout << num << endl;
    return 0;
  }
  cout << 0 << endl;
  return 0;
  return 0;
}
