#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  string a[101];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int dp[101][10002];
  memset(dp, 0, sizeof dp);
  for (int i = 0; i < n; i++) {
    size_t found = a[i].find('1');
    if (found == string::npos) {
      cout << -1 << endl;
      return 0;
    }
    int it1 = a[i].find_first_of('1');
    int temp = 0;
    for (int j = 0; j < m; j++) {
      if (a[i][(j + it1) % m] == '1') {
        temp = 0;
      } else {
        temp++;
      }
      dp[i][(j + it1) % m] = temp;
    }
    int it2 = a[i].find_last_of('1');
    temp = 0;
    for (int j = m; j >= 1; j--) {
      if (a[i][(j + it2) % m] == '1') {
        temp = 0;
      } else {
        temp++;
      }
      dp[i][(j + it2) % m] = min(temp, dp[i][(j + it2) % m]);
    }
  }
  int Min = 10000000;
  for (int j = 0; j < m; j++) {
    int ans = 0;
    for (int i = 0; i < n; i++) {
      ans += dp[i][j];
    }
    Min = min(ans, Min);
  }
  cout << Min << endl;
  return 0;
}
