#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  int f[n][n];
  char c;
  for (int i = 0; i <= n - 1; i++) {
    for (int j = 0; j <= n / 4 - 1; j++) {
      scanf(" %c", &c);
      if (c == '0' || c == '1' || c == '2' || c == '3' || c == '4' ||
          c == '5' || c == '6' || c == '7') {
        f[i][4 * j + 1] = 0;
      } else
        f[i][4 * j + 1] = 1;
      if (c == '0' || c == '1' || c == '2' || c == '3' || c == '8' ||
          c == '9' || c == 'A' || c == 'B') {
        f[i][4 * j + 2] = 0;
      } else
        f[i][4 * j + 2] = 1;
      if (c == '0' || c == '1' || c == '4' || c == '5' || c == '8' ||
          c == '9' || c == 'C' || c == 'D') {
        f[i][4 * j + 3] = 0;
      } else
        f[i][4 * j + 3] = 1;
      if (c == '0' || c == '2' || c == '4' || c == '6' || c == '8' ||
          c == 'A' || c == 'C' || c == 'E') {
        f[i][4 * j + 4] = 0;
      } else
        f[i][4 * j + 4] = 1;
    }
  }
  int kq = n;
  int i = 0;
  int a = 0;
  for (int j = 1; j <= n - 1; j++) {
    if (f[j][i] != f[j - 1][i]) {
      kq = min(kq, j - a);
      a = j;
    }
  }
  kq = min(kq, n - a);
  cout << kq;
}
