#include <bits/stdc++.h>
using namespace std;
int digitcount(int n) {
  int result = 0;
  for (int i = n; i > 0; i /= 10) {
    result++;
  }
  return result;
}
int power(int a, int b) {
  if (b == 0) return 1;
  if (b == 1) return a;
  return a * power(a, b - 1);
}
bool pal(string str) {
  for (int i = 0; i < str.size() / 2; i++) {
    if (str[i] != str[str.size() - 1 - i]) return false;
  }
  return true;
}
int rev(int num) {
  if (num < 10) return num;
  int i, p = 0;
  for (i = num; i > 9; i /= 10) {
    p++;
  }
  int nextnum = power(10, p) * (num % 10);
  return nextnum + rev(num / 10);
}
typedef struct {
  int start, end, index;
  int check;
} info;
int main() {
  int n, m;
  cin >> n >> m;
  string arr[n];
  int arr2[n][m * 3];
  for (int i = 0; i < n; i++) {
    string next;
    cin >> next;
    arr[i] = next + next + next;
    bool exists = false;
    for (int j = 0; j < m; j++) {
      if (next[j] == '1') {
        exists = true;
      }
    }
    if (!exists) {
      cout << -1 << endl;
      exit(0);
    }
  }
  for (int j = 0; j < n; j++) {
    int k = 0;
    for (int i = 0; i < m * 3; i++) {
      if (arr[j][i] == '1')
        k = 0;
      else
        k++;
      arr2[j][i] = k;
    }
  }
  for (int j = 0; j < n; j++) {
    int k = 0;
    for (int i = m * 3 - 1; i >= 0; i--) {
      if (arr[j][i] == '1')
        k = 0;
      else
        k++;
      if (k < arr2[j][i]) arr2[j][i] = k;
    }
  }
  long long res = LONG_MAX;
  for (int i = m; i < 2 * m; i++) {
    int cur = 0;
    for (int j = 0; j < n; j++) cur += arr2[j][i];
    if (cur < res) res = cur;
  }
  cout << res << endl;
}
