#include <bits/stdc++.h>
using namespace std;
int m, n;
string c[100];
int l[100][10000];
int r[100][10000];
int main() {
  cin >> m >> n;
  for (int i = 0; i < m; i++)
    for (int j = 0; j < n; j++) {
      l[i][j] = r[i][j] = 1000000000;
    }
  for (int row = 0; row < m; row++) {
    cin >> c[row];
    string s = c[row] + c[row];
    int k = c[row].length();
    for (int i = 0; i < s.length(); i++) {
      if (s[i] == '1') {
        r[row][i % k] = 0;
      } else if (i > 0) {
        r[row][i % k] = min(r[row][i % k], r[row][(i - 1) % k] + 1);
      }
    }
    for (int i = s.length() - 1; i >= 0; i--) {
      if (s[i] == '1') {
        l[row][i % k] = 0;
      } else if (i < s.length() - 1) {
        l[row][i % k] = min(l[row][i % k], l[row][(i + 1) % k] + 1);
      }
    }
  }
  int res = 1000000000;
  for (int col = 0; col < n; col++) {
    int tres = 0;
    for (int row = 0; row < m; row++) {
      tres = tres + min(l[row][col], r[row][col]);
      if (tres > res) {
        break;
      }
    }
    res = min(res, tres);
  }
  if (res == 1000000000) {
    res = -1;
  }
  cout << res << endl;
  return 0;
}
