#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vtype;
int mod(int& x, int& m) {
  int r = x % m;
  return r < 0 ? r + m : r;
}
const int nx = 101;
const int mx = 10001;
int n, m;
char arr[nx][mx];
int moves[nx][mx];
bool check(int i) {
  for (int j = 0; j <= m - 1; j++) {
    if (arr[i][j] == '1') return true;
  }
  return false;
}
int main() {
  scanf("%d", &n);
  scanf("%d", &m);
  for (int i = 0; i < n; i++) {
    scanf("%s", arr[i]);
    if (!check(i)) {
      printf("-1");
      return 0;
    }
  }
  if (n == 1) {
    printf("0");
    return 0;
  }
  for (int i = 0; i <= n - 1; i++) {
    for (int first = 0; first < m; first++) {
      if (arr[i][first] == '1') {
        int dist = 0;
        for (int times = 1; times <= 2; times++, first = 0) {
          for (int j = first; j <= m - 1; j++) {
            if (arr[i][j] == '1') {
              moves[i][j] = 0;
              dist = 1;
            } else
              moves[i][j] = dist++;
          }
        }
        break;
      }
    }
  }
  for (int i = 0; i <= n - 1; i++) {
    for (int first = m - 1; first >= 0; first--) {
      if (arr[i][first] == '1') {
        int dist = 0;
        for (int times = 1; times <= 2; times++, first = m - 1) {
          for (int j = first; j >= 0; j--) {
            if (arr[i][j] == '1') {
              moves[i][j] = 0;
              dist = 1;
            } else
              moves[i][j] = min(moves[i][j], dist++);
          }
        }
        break;
      }
    }
  }
  int final_ans = n * m + 5;
  int ans;
  for (int j = 0; j <= m - 1; j++) {
    ans = 0;
    for (int i = 0; i <= n - 1; i++) ans += moves[i][j];
    final_ans = min(ans, final_ans);
  }
  printf("%d", final_ans);
  return 0;
}
