#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:256000000")
using namespace std;
int ans[10005];
int main() {
  int n, m, s[10005];
  char str[10005];
  scanf("%d %d\n", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%s\n", str);
    for (int j = 0; j < m; j++) s[j] = 2000000;
    for (int j = 1; j < 2 * m; j++)
      s[j % m] = min(s[j % m], (str[j % m] - '0') ? 0 : s[(j - 1) % m] + 1);
    for (int j = 2 * m - 2; j >= 0; j--)
      s[j % m] = min(s[j % m], s[(j + 1) % m] + 1);
    for (int j = 0; j < m; j++) ans[j] += s[j];
  }
  int res = ans[0];
  for (int i = 0; i < m; i++) res = min(res, ans[i]);
  printf("%d", res < 2000000 ? res : -1);
  return 0;
}
