#include <bits/stdc++.h>
using namespace std;
namespace p230CShifts {
int cnt[11111] = {0}, dist[11111], q[22222];
char str[11111];
void solve(int test_num) {
  int rows, cols;
  scanf("%d %d\r\n", &rows, &cols);
  for (int i = 0; i < rows; i++) {
    gets(str);
    memset(dist, 0x1, sizeof(dist));
    int qf = 0, qb = 0;
    for (int j = 0; j < cols; j++)
      if (str[j] == '1') dist[q[qb++] = j] = 0;
    while (qf < qb) {
      int cur = q[qf++];
      if (dist[cur] + 1 < dist[(cur + 1) % cols]) {
        dist[(cur + 1) % cols] = dist[cur] + 1;
        q[qb++] = (cur + 1) % cols;
      }
      if (dist[cur] + 1 < dist[(cur + cols - 1) % cols]) {
        dist[(cur + cols - 1) % cols] = dist[cur] + 1;
        q[qb++] = (cur + cols - 1) % cols;
      }
    }
    for (int j = 0; j < cols; j++) cnt[j] += dist[j];
  }
  int ans = 1 << 20;
  for (int i = 0; i < cols; i++) ans = min(ans, cnt[i]);
  if (ans >= (1 << 20))
    puts("-1");
  else
    printf("%d\n", ans);
}
void solve() { solve(1); }
}  // namespace p230CShifts
int main() {
  p230CShifts::solve();
  return 0;
}
