#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, m;
  cin >> n >> m;
  string a[n];
  for (int i = 0; i < n; i++) cin >> a[i];
  int dp[n][m];
  for (int i = 0; i < n; i++) {
    int left[m];
    int right[m];
    int lc = -1;
    int rc = -1;
    for (int j = 0; j < m; j++) {
      if (a[i][j] == '1') {
        lc = j;
        break;
      }
    }
    for (int j = m - 1; j >= 0; j--) {
      if (a[i][j] == '1') {
        rc = j;
        break;
      }
    }
    if (lc == -1) {
      cout << -1 << '\n';
      return;
    }
    int curr1 = m - rc;
    for (int j = 0; j < m; j++) {
      if (a[i][j] == '1') curr1 = 0;
      left[j] = curr1;
      curr1++;
    }
    int curr2 = lc + 1;
    for (int j = m - 1; j >= 0; j--) {
      if (a[i][j] == '1') curr2 = 0;
      right[j] = curr2;
      curr2++;
      dp[i][j] = min(left[j], right[j]);
    }
  }
  int ans = 1e9;
  for (int j = 0; j < m; j++) {
    int temp = 0;
    for (int i = 0; i < n; i++) temp += dp[i][j];
    ans = min(ans, temp);
  }
  cout << ans << '\n';
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve();
}
