#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  char a[n][m];
  for (int i = (0); i <= ((int)(n - 1)); i++) {
    cin >> a[i];
    bool flag = false;
    for (int j = (0); j <= ((int)(m - 1)); j++) {
      if (a[i][j] == '1') {
        flag = true;
        break;
      }
    }
    if (!flag) {
      cout << -1 << endl;
      return 0;
    }
  }
  int swaps[n][m];
  memset(swaps, 0, sizeof(swaps));
  for (int i = (0); i <= ((int)(n - 1)); i++) {
    int prev;
    for (int j = (m - 1); j >= (0); j--) {
      if (a[i][j] == '1') {
        prev = j;
        break;
      }
    }
    prev -= m;
    for (int j = (0); j <= ((int)(m - 1)); j++) {
      if (a[i][j] == '1') prev = j;
      swaps[i][j] = j - prev;
    }
  }
  for (int i = (0); i <= ((int)(n - 1)); i++) {
    int prev;
    for (int j = (0); j <= ((int)(m - 1)); j++) {
      if (a[i][j] == '1') {
        prev = j;
        break;
      }
    }
    prev += m;
    for (int j = (m - 1); j >= (0); j--) {
      if (a[i][j] == '1') prev = j;
      swaps[i][j] = (((swaps[i][j]) < (prev - j)) ? (swaps[i][j]) : (prev - j));
    }
  }
  int ans = 1e9;
  for (int j = (0); j <= ((int)(m - 1)); j++) {
    int sum = 0;
    for (int i = (0); i <= ((int)(n - 1)); i++) {
      sum += swaps[i][j];
    }
    ans = (((sum) < (ans)) ? (sum) : (ans));
  }
  cout << ans << endl;
  return 0;
}
