#include <bits/stdc++.h>
using namespace std;
const int INF = 2000000000;
const double pi = acos(-1.0);
int main() {
  std::ios_base::sync_with_stdio(false);
  int n, m;
  while (cin >> n >> m) {
    int a[n][m], dp[n][m], dp1[n][m];
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) dp[i][j] = dp1[i][j] = INF;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) a[i][j] = 0;
    int flag = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        char c;
        cin >> c;
        if (c == '1') a[i][j] = 1;
      }
    }
    for (int i = 0; i < n; i++) {
      flag = 0;
      for (int j = 0; j < m; j++) {
        if (a[i][j] == 1) flag = 1;
      }
      if (!flag) break;
    }
    if (!flag) {
      cout << -1 << endl;
      continue;
    }
    for (int i = 0; i < n; i++)
      if (a[i][0]) dp[i][0] = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 1; j < 2 * m; j++) {
        if (a[i][j % m] == 1)
          dp[i][j % m] = 0;
        else
          dp[i][j % m] = dp[i][(j - 1) % m] + 1;
      }
    }
    for (int i = 0; i < n; i++) {
      int x = INF;
      for (int j = 2 * m - 1; j >= 0; j--) {
        x++;
        if (a[i][j % m]) x = 0;
        dp1[i][j % m] = min(dp1[i][j % m], x);
      }
    }
    int ans = INF;
    for (int j = 0; j < m; j++) {
      int sum = 0;
      for (int i = 0; i < n; i++) {
        sum += ((dp[i][j]) < (dp1[i][j]) ? (dp[i][j]) : (dp1[i][j]));
      }
      ans = ((sum) < (ans) ? (sum) : (ans));
    }
    cout << ans << endl;
  }
  return 0;
}
