#include <bits/stdc++.h>
using namespace std;
const int MaxN = 105;
const int MaxM = 10005;
const int inf = 2000000000;
char cell[MaxN][3 * MaxM];
int minMove[MaxN][3 * MaxM];
int main() {
  int N, M;
  scanf("%d%d", &N, &M);
  for (int i = 0; i < N; ++i)
    for (int j = 0; j < 3 * M; ++j) minMove[i][j] = inf;
  for (int i = 0; i < N; ++i) {
    scanf("%s", cell[i]);
    for (int j = 0; j < M; ++j) {
      cell[i][j + M] = cell[i][j];
      cell[i][j + 2 * M] = cell[i][j];
    }
    int pMin = inf;
    for (int j = 0; j < 3 * M; ++j) {
      pMin++;
      if (cell[i][j] == '1') {
        pMin = 0;
      }
      minMove[i][j] = min(minMove[i][j], pMin);
    }
    pMin = inf;
    for (int j = 3 * M - 1; j >= 0; --j) {
      pMin++;
      if (cell[i][j] == '1') {
        pMin = 0;
      }
      minMove[i][j] = min(minMove[i][j], pMin);
    }
  }
  int ans = inf;
  for (int i = M; i < 2 * M; ++i) {
    int sum = 0;
    int F = 1;
    for (int j = 0; j < N; ++j) {
      if (minMove[j][i] != inf) {
        sum += minMove[j][i];
      } else {
        F = 0;
        break;
      }
    }
    if (F) ans = min(ans, sum);
  }
  if (ans == inf)
    puts("-1");
  else
    cout << ans << endl;
}
