#include <bits/stdc++.h>
using namespace std;
long long a[1005][1005];
long long n, m;
bool f1(long long x, long long y) {
  a[x][y] = 1;
  if (y + 2 <= n && x + 2 <= n && a[x][y + 1] == 1 && a[x][y + 2] == 1 &&
      a[x + 1][y] == 1 && a[x + 1][y + 1] == 1 && a[x + 1][y + 2] == 1 &&
      a[x + 2][y] == 1 && a[x + 2][y + 1] == 1 && a[x + 2][y + 2] == 1) {
    return 1;
  }
  return 0;
}
bool f2(long long x, long long y) {
  a[x][y] = 1;
  if (y - 1 > 0 && y + 1 <= n && x + 2 <= n && a[x][y - 1] == 1 &&
      a[x][y + 1] == 1 && a[x + 1][y - 1] == 1 && a[x + 1][y] == 1 &&
      a[x + 1][y + 1] == 1 && a[x + 2][y - 1] == 1 && a[x + 2][y] == 1 &&
      a[x + 2][y + 1] == 1) {
    return 1;
  }
  return 0;
}
bool f3(long long x, long long y) {
  a[x][y] = 1;
  if (y - 2 > 0 && x + 2 <= n && a[x][y - 1] == 1 && a[x][y - 2] == 1 &&
      a[x + 1][y - 2] == 1 && a[x + 1][y] == 1 && a[x + 1][y - 1] == 1 &&
      a[x + 2][y - 2] == 1 && a[x + 2][y - 1] == 1 && a[x + 2][y] == 1) {
    return 1;
  }
  return 0;
}
bool f4(long long x, long long y) {
  a[x][y] = 1;
  if (x - 1 > 0 && y + 2 <= n && x + 1 <= n && a[x - 1][y] == 1 &&
      a[x - 1][y + 1] == 1 && a[x - 1][y + 2] == 1 && a[x][y + 1] == 1 &&
      a[x][y + 2] == 1 && a[x + 1][y] == 1 && a[x + 1][y + 1] == 1 &&
      a[x + 1][y + 2] == 1) {
    return 1;
  }
  return 0;
}
bool f5(long long x, long long y) {
  a[x][y] = 1;
  if (x - 1 > 0 && x + 1 <= n && y - 1 > 0 && y + 1 <= n && a[x][y - 1] == 1 &&
      a[x][y + 1] == 1 && a[x - 1][y - 1] == 1 && a[x - 1][y] == 1 &&
      a[x - 1][y + 1] == 1 && a[x + 1][y - 1] == 1 && a[x + 1][y] == 1 &&
      a[x + 1][y + 1] == 1) {
    return 1;
  }
  return 0;
}
bool f6(long long x, long long y) {
  a[x][y] = 1;
  if (y - 2 > 0 && x + 1 <= n && x - 1 > 0 && a[x][y - 1] == 1 &&
      a[x][y - 2] == 1 && a[x - 1][y] == 1 && a[x - 1][y - 1] == 1 &&
      a[x - 1][y - 2] == 1 && a[x + 1][y - 1] == 1 && a[x + 1][y - 2] == 1 &&
      a[x + 1][y] == 1) {
    return 1;
  }
  return 0;
}
bool f7(long long x, long long y) {
  a[x][y] = 1;
  if (x - 2 > 0 && y + 2 <= n && a[x][y + 1] == 1 && a[x][y + 2] == 1 &&
      a[x - 1][y] == 1 && a[x - 1][y + 1] == 1 && a[x - 1][y + 2] == 1 &&
      a[x - 2][y] == 1 && a[x - 2][y + 1] == 1 && a[x - 2][y + 2] == 1) {
    return 1;
  }
  return 0;
}
bool f8(long long x, long long y) {
  a[x][y] = 1;
  if (x - 2 > 0 && y - 1 > 0 && y + 1 <= n && a[x][y - 1] == 1 &&
      a[x][y + 1] == 1 && a[x - 1][y] == 1 && a[x - 1][y - 1] == 1 &&
      a[x - 1][y + 1] == 1 && a[x - 2][y] == 1 && a[x - 2][y - 1] == 1 &&
      a[x - 2][y + 1] == 1) {
    return 1;
  }
  return 0;
}
bool f9(long long x, long long y) {
  a[x][y] = 1;
  if (x - 2 > 0 && y - 2 > 0 && a[x][y - 1] == 1 && a[x][y - 2] == 1 &&
      a[x - 1][y] == 1 && a[x - 1][y - 1] == 1 && a[x - 1][y - 2] == 1 &&
      a[x - 2][y] == 1 && a[x - 2][y - 1] == 1 && a[x - 2][y - 2] == 1) {
    return 1;
  }
  return 0;
}
int main() {
  cin >> n >> m;
  long long flag = 0;
  for (long long i = 0; i < m; i++) {
    long long x, y;
    cin >> x >> y;
    long long ans1 = f1(x, y);
    if (ans1) {
      if (!flag) flag = i + 1;
    }
    long long ans2 = f2(x, y);
    if (ans2) {
      if (!flag) flag = i + 1;
    }
    long long ans3 = f3(x, y);
    if (ans3) {
      if (!flag) flag = i + 1;
    }
    long long ans4 = f4(x, y);
    if (ans4) {
      if (!flag) flag = i + 1;
    }
    long long ans5 = f5(x, y);
    if (ans5) {
      if (!flag) flag = i + 1;
    }
    long long ans6 = f6(x, y);
    if (ans6) {
      if (!flag) flag = i + 1;
    }
    long long ans7 = f7(x, y);
    if (ans7) {
      if (!flag) flag = i + 1;
    }
    long long ans8 = f8(x, y);
    if (ans8) {
      if (!flag) flag = i + 1;
    }
    long long ans9 = f9(x, y);
    if (ans9) {
      if (!flag) flag = i + 1;
    }
  }
  if (!flag)
    cout << "-1";
  else
    cout << flag;
  return 0;
}
