#include <bits/stdc++.h>
using namespace std;
char c[200][20000];
int dp[200][20000];
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    int fl = 1;
    for (int j = 0; j < m; j++) {
      cin >> c[i][j];
      if (c[i][j] == '1') fl = 0;
    }
    if (fl) {
      cout << -1 << endl;
      return 0;
    }
  }
  for (int i = 0; i < n; i++) {
    int t = 1, now = m - 1;
    while (c[i][now--] == '0') t++;
    for (int j = 0; j < m; j++) {
      if (c[i][j] == '1') t = 0;
      dp[i][j] = t++;
    }
    t = 1;
    now = 0;
    while (c[i][now++] == '0') t++;
    for (int j = m - 1; j >= 0; j--) {
      if (c[i][j] == '1') t = 0;
      dp[i][j] = min(t++, dp[i][j]);
    }
  }
  long long ans = 1000 * 1000 * 1000, now = 0;
  for (int i = 0; i < m; i++) {
    now = 0;
    for (int j = 0; j < n; j++) now += dp[j][i];
    ans = min(ans, now);
  }
  cout << ans << endl;
  cin >> ans;
}
