#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
char s[110][10010];
int dis[110][10010];
int n, m;
bool check() {
  for (int i = 1; i <= n; i++) {
    bool flag = 0;
    for (int j = 0; j < m; j++) {
      if (s[i][j] == '1') flag = 1;
    }
    if (!flag) return 1;
  }
  return 0;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%s", s[i]);
  }
  if (check()) return puts("-1"), 0;
  memset(dis, inf, sizeof(dis));
  for (int i = 1; i <= n; i++) {
    int last = -1;
    for (int j = 0; j < m; j++) {
      if (s[i][j] == '1') {
        dis[i][j] = 0;
        last = j;
      } else if (last != -1) {
        dis[i][j] = min(dis[i][j], j - last);
      }
    }
    last = m;
    for (int j = m - 1; j >= 0; j--) {
      if (s[i][j] == '1') {
        dis[i][j] = 0;
        last = j;
      } else if (last != m) {
        dis[i][j] = min(dis[i][j], last - j);
      }
    }
    for (int j = 0; j < m; j++) {
      if (s[i][j] == '1') {
        last = j;
        break;
      }
    }
    for (int j = m - 1; j >= 0; j--) {
      dis[i][j] = min(dis[i][j], last + m - j);
    }
    for (int j = m - 1; j >= 0; j--) {
      if (s[i][j] == '1') {
        last = j;
        break;
      }
    }
    for (int j = 0; j < m; j++) {
      dis[i][j] = min(dis[i][j], m - 1 - last + 1 + j);
    }
  }
  int ans = inf;
  for (int i = 0; i < m; i++) {
    int tmp = 0;
    for (int j = 1; j <= n; j++) {
      tmp += dis[j][i];
    }
    ans = min(ans, tmp);
  }
  printf("%d\n", ans);
  return 0;
}
