#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  char arr[n][m];
  int ar1[n][m], ar2[n][m];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) cin >> arr[i][j];
  for (int i = 0; i < n; i++) {
    int k;
    for (k = 0; k < m; k++)
      if (arr[i][k] == '1') break;
    if (k == m) {
      cout << -1;
      return 0;
    }
    if (arr[i][m - 1] == '1')
      ar1[i][m - 1] = 0;
    else
      ar1[i][m - 1] = k + 1;
    for (int j = m - 2; j >= 0; j--) {
      if (arr[i][j] == '1')
        ar1[i][j] = 0;
      else
        ar1[i][j] = ar1[i][j + 1] + 1;
    }
  }
  for (int i = 0; i < n; i++) {
    int k;
    for (k = m - 1; k >= 0; k--)
      if (arr[i][k] == '1') break;
    if (k == -1) {
      cout << -1;
      return 0;
    }
    if (arr[i][0] == '1')
      ar2[i][0] = 0;
    else
      ar2[i][0] = m - k;
    for (int j = 1; j < m; j++) {
      if (arr[i][j] == '1')
        ar2[i][j] = 0;
      else
        ar2[i][j] = ar2[i][j - 1] + 1;
    }
  }
  int ans = INT_MAX;
  int temp = 0;
  for (int j = 0; j < m; j++) {
    temp = 0;
    for (int i = 0; i < n; i++) {
      temp += min(ar1[i][j], ar2[i][j]);
    }
    ans = min(ans, temp);
  }
  cout << ans;
  return 0;
}
