#include <bits/stdc++.h>
using namespace std;
int const N = 22;
int a[N][2];
int b[N][2];
int n, m;
bool dt[N];
int main() {
  cin >> n >> m;
  for (int i = 0; i < (n); ++i)
    for (int j = 0; j < (2); ++j) cin >> a[i][j];
  for (int i = 0; i < (m); ++i)
    for (int j = 0; j < (2); ++j) cin >> b[i][j];
  for (int i = 0; i < (n); ++i)
    for (int j = 0; j < (m); ++j) {
      int c = 0;
      for (int k = 0; k < (2); ++k)
        for (int l = 0; l < (2); ++l) c += a[i][k] == b[j][l];
      if (c == 1)
        for (int k = 0; k < (2); ++k)
          for (int l = 0; l < (2); ++l)
            if (a[i][k] == b[j][l]) dt[a[i][k]] = 1;
    }
  int q = 0;
  for (int i = 1; i <= (9); ++i) q += dt[i];
  if (q == 1) {
    for (int i = 1; i <= (9); ++i)
      if (dt[i]) printf("%d\n", i);
    return 0;
  }
  bool q1 = 0, q2 = 0, r1 = 1, r2 = 1;
  for (int i = 0; i < (n); ++i) {
    memset(dt, (0), sizeof dt);
    for (int j = 0; j < (m); ++j) {
      int t = 0;
      for (int k = 0; k < (2); ++k)
        for (int l = 0; l < (2); ++l) t += a[i][k] == b[j][l];
      if (t == 1)
        for (int k = 0; k < (2); ++k)
          for (int l = 0; l < (2); ++l)
            if (a[i][k] == b[j][l]) dt[a[i][k]] = 1;
    }
    int c = 0;
    for (int j = 1; j <= (9); ++j) c += dt[j];
    if (c > 0) {
      q1 = 1;
      if (c != 1) r1 = 0;
    }
  }
  for (int i = 0; i < (m); ++i) {
    memset(dt, (0), sizeof dt);
    for (int j = 0; j < (n); ++j) {
      int t = 0;
      for (int k = 0; k < (2); ++k)
        for (int l = 0; l < (2); ++l) t += a[j][k] == b[i][l];
      if (t == 1)
        for (int k = 0; k < (2); ++k)
          for (int l = 0; l < (2); ++l)
            if (a[j][k] == b[i][l]) dt[a[j][k]] = 1;
    }
    int c = 0;
    for (int j = 1; j <= (9); ++j) c += dt[j];
    if (c > 0) {
      q2 = 1;
      if (c != 1) r2 = 0;
    }
  }
  if (q1 && q2 && r1 && r2)
    puts("0");
  else
    puts("-1");
  return 0;
}
