#include <bits/stdc++.h>
using namespace std;
const int N = 111;
const int INF = (int)1e9;
const int mod = (int)1e9 + 7;
int n, m;
vector<int> v[N];
int get(int i, int j) {
  if (i > j) swap(i, j);
  int s = j - i;
  s = min(s, m - s);
  return s;
}
int get_res(int x, int y) {
  auto left = v[y].begin(), right = v[y].end();
  --right;
  int s = get(x, *left);
  s = min(s, get(x, *right));
  auto it = upper_bound(v[y].begin(), v[y].end(), x);
  if (it != v[y].end()) {
    s = min(s, get(x, *it));
  }
  if (it != v[y].begin()) {
    --it;
    s = min(s, get(x, *it));
  }
  return s;
}
int main() {
  scanf("%d%d\n", &n, &m);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      char x;
      x = getchar();
      if (x == '1') v[i].push_back(j);
    }
    scanf("\n");
  }
  int ans = (int)1e9;
  for (int j = 0; j < m; ++j) {
    int res = 0;
    for (int i = 0; i < n; ++i) {
      if (!v[i].size()) {
        puts("-1");
        return 0;
      }
      res += get_res(j, i);
    }
    ans = min(ans, res);
  }
  printf("%d", ans);
  return 0;
}
