#include <bits/stdc++.h>
using namespace std;
int r[10005][105];
char a[10005][105];
int main() {
  int n, m;
  cin >> n >> m;
  for (int j = 0; j < n; j++) {
    bool have1 = false;
    for (int i = 0; i < m; i++) {
      cin >> a[i][j];
      if (a[i][j] == '1') have1 = true;
    }
    if (!have1) {
      cout << -1 << endl;
      return 0;
    }
  }
  for (int j = 0; j < n; j++) {
    int x = m - 1;
    int t = 1;
    while (a[x][j] != '1') {
      t++;
      x--;
    }
    for (int i = 0; i < m; i++) {
      if (a[i][j] == '1') t = 0;
      r[i][j] = t;
      t++;
    }
    x = 0;
    t = 1;
    while (a[x][j] != '1') {
      t++;
      x++;
    }
    for (int i = m - 1; i >= 0; i--) {
      if (a[i][j] == '1') t = 0;
      r[i][j] = min(r[i][j], t);
      t++;
    }
  }
  int ans = -1;
  for (int i = 0; i < m; i++) {
    int need = 0;
    for (int j = 0; j < n; j++) need += r[i][j];
    if (ans == -1 || ans > need) ans = need;
  }
  cout << ans << endl;
}
