#include <bits/stdc++.h>
const int SIZE_N = (100 + 10);
const int SIZE_M = (10000 + 10);
const int INF = (1000000000);
int n, m;
int mat[SIZE_N][SIZE_M * 2];
char buf[SIZE_M];
int dis[SIZE_N][SIZE_M * 2];
int main(void) {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    scanf("%s", buf + 1);
    for (int j = 1; j <= m; ++j) mat[i][j + m] = mat[i][j] = buf[j] - '0';
  }
  for (int i = 0; i <= n + 1; ++i)
    for (int j = 0; j <= m + m + 1; ++j) dis[i][j] = INF;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m + m; ++j) {
      if (mat[i][j] == 1)
        dis[i][j] = 0;
      else
        dis[i][j] = std::min(dis[i][j - 1] + 1, dis[i][j]);
    }
    for (int j = m + m; j >= 1; --j) {
      if (mat[i][j] == 1)
        dis[i][j] = 0;
      else
        dis[i][j] = std::min(dis[i][j + 1] + 1, dis[i][j]);
    }
  }
  int ans = INF;
  for (int j = 1; j <= m + m; ++j) {
    int t = 0;
    for (int i = 1; i <= n; ++i)
      if (dis[i][j] == INF)
        t = INF;
      else
        t += dis[i][j];
    ans = std::min(ans, t);
  }
  if (ans < INF)
    printf("%d\n", ans);
  else
    puts("-1");
  return 0;
}
