#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const int N = 1200031;
int n, m, for1[100][100][15], for2[100][100][15], votes_for[1000];
int has[5][100][100];
bool bad_pairs(int a, int b, int c, int d) {
  vector<int> v;
  if (a == c || a == d) v.push_back(a);
  if (b == c || b == d) v.push_back(b);
  return (v.size() != 1);
}
int common(int a, int b, int c, int d) {
  vector<int> v;
  if (a == c || a == d) v.push_back(a);
  if (b == c || b == d) v.push_back(b);
  return (v[0]);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    int a, b;
    cin >> a >> b;
    if (a > b) swap(a, b);
    has[1][a][b] = 1;
  }
  for (int i = 1; i <= m; i++) {
    int a, b;
    cin >> a >> b;
    if (a > b) swap(a, b);
    has[2][a][b] = 1;
  }
  for (int v1 = 1; v1 <= 9; v1++) {
    for (int v2 = v1 + 1; v2 <= 9; v2++) {
      for (int v3 = 1; v3 <= 9; v3++) {
        for (int v4 = v3 + 1; v4 <= 9; v4++) {
          if (bad_pairs(v1, v2, v3, v4)) continue;
          if (has[1][v1][v2] == 0) continue;
          if (has[2][v3][v4] == 0) continue;
          int T = common(v1, v2, v3, v4);
          ++votes_for[T];
          for1[v1][v2][T]++;
          for2[v3][v4][T]++;
        }
      }
    }
  }
  int candidates = 0;
  int solution;
  for (int i = 1; i <= 9; i++) {
    if (votes_for[i]) ++candidates, solution = i;
  }
  if (candidates == 1) {
    cout << solution << endl;
    return 0;
  }
  for (int i = 1; i <= 9; i++) {
    for (int j = 1; j <= 9; j++) {
      int s = 0;
      for (int q = 1; q <= 9; q++) {
        if (for1[i][j][q]) ++s;
      }
      if (s > 1) {
        cout << -1 << endl;
        return 0;
      }
      s = 0;
      for (int q = 1; q <= 9; q++) {
        if (for2[i][j][q]) ++s;
      }
      if (s > 1) {
        cout << -1 << endl;
        return 0;
      }
    }
  }
  cout << 0 << endl;
  return 0;
}
