#include <bits/stdc++.h>
using namespace std;
char mat[100][11000];
int n, m, have[100], Left[100][10000], Right[100][10000];
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%s", mat[i]);
    for (int j = 0; j < m; j++)
      if (mat[i][j] == '1') have[i] = 1;
    if (!have[i]) {
      printf("-1\n");
      return 0;
    }
  }
  for (int i = 0; i < n; i++) {
    int rr = -1;
    for (int j = m - 1; j >= 0; j--)
      if (mat[i][j] == '1') {
        rr = j;
        break;
      }
    for (int j = 0; j < m; j++)
      if (mat[i][j] == '1') {
        Left[i][j] = 0;
        rr = j;
      } else {
        Left[i][j] = (rr < j) ? (j - rr) : (j + m - rr);
      }
    int ll = -1;
    for (int j = 0; j < m; j++)
      if (mat[i][j] == '1') {
        ll = j;
        break;
      }
    for (int j = m - 1; j >= 0; j--)
      if (mat[i][j] == '1') {
        Right[i][j] = 0;
        ll = j;
      } else {
        Right[i][j] = (ll > j) ? (ll - j) : (m - j + ll);
      }
  }
  int ans = 1000000000;
  for (int wh = 0; wh < m; wh++) {
    int cnt = 0;
    for (int i = 0; i < n; i++) {
      cnt += min(Left[i][wh], Right[i][wh]);
    }
    ans = min(ans, cnt);
  }
  printf("%d\n", ans);
  return 0;
}
