#include <bits/stdc++.h>
using namespace std;
long long n, m, a[1005][1005], k, ans, x, t, y;
bool check(int yy, int xx) {
  long long ts = 0;
  if (yy + 2 < n && xx + 2 < n) {
    for (int i = yy; i <= yy + 2; i++)
      for (int j = xx; j <= xx + 2; j++) ts += a[i][j];
    if (ts == 9) return true;
  }
  if (yy + 2 < n && xx - 1 >= 0 && xx + 1 < n) {
    ts = 0;
    for (int i = yy; i <= yy + 2; i++)
      for (int j = xx - 1; j <= xx + 1; j++) ts += a[i][j];
    if (ts == 9) return true;
  }
  if (yy + 2 < n && xx - 2 >= 0) {
    ts = 0;
    for (int i = yy; i <= yy + 2; i++)
      for (int j = xx - 2; j <= xx; j++) ts += a[i][j];
    if (ts == 9) return true;
  }
  if (yy + 1 < n && yy - 1 >= 0 && xx + 2 < n) {
    ts = 0;
    for (int i = yy - 1; i <= yy + 1; i++)
      for (int j = xx; j <= xx + 2; j++) ts += a[i][j];
    if (ts == 9) return true;
  }
  if (yy + 1 < n && yy - 1 >= 0 && xx - 1 >= 0 && xx + 1 < n) {
    ts = 0;
    for (int i = yy - 1; i <= yy + 1; i++)
      for (int j = xx - 1; j <= xx + 1; j++) ts += a[i][j];
    if (ts == 9) return true;
  }
  if (yy + 1 < n && yy - 1 >= 0 && xx - 2 >= 0) {
    ts = 0;
    for (int i = yy - 1; i <= yy + 1; i++)
      for (int j = xx - 2; j <= xx; j++) ts += a[i][j];
    if (ts == 9) return true;
  }
  if (yy - 2 >= 0 && xx + 2 < n) {
    ts = 0;
    for (int i = yy - 2; i <= yy; i++)
      for (int j = xx; j <= xx + 2; j++) ts += a[i][j];
    if (ts == 9) return true;
  }
  if (yy - 2 >= 0 && xx - 1 >= 0 && xx + 1 < n) {
    ts = 0;
    for (int i = yy - 2; i <= yy; i++)
      for (int j = xx - 1; j <= xx + 1; j++) ts += a[i][j];
    if (ts == 9) return true;
  }
  if (yy - 2 >= 0 && xx - 2 >= 0) {
    ts = 0;
    for (int i = yy - 2; i <= yy; i++)
      for (int j = xx - 2; j <= xx; j++) ts += a[i][j];
    if (ts == 9) return true;
  }
  return false;
}
int main() {
  cin >> n >> m;
  memset(a, 0, sizeof a);
  for (int i = 0; i < m; i++) {
    cin >> y >> x;
    y--;
    x--;
    a[y][x] = 1;
    if (check(y, x)) {
      cout << i + 1;
      return 0;
    }
  }
  cout << -1;
  return 0;
}
