#include <bits/stdc++.h>
using namespace std;
inline void wait(double seconds) {
  double endtime = clock() + (seconds * CLOCKS_PER_SEC);
  while (clock() < endtime) {
    ;
  }
}
inline int fastIn() {
  register char c = 0;
  register int a = 0;
  while (c < 33) c = getchar();
  while (c > 33) {
    a = (a * 10) + (c - '0');
    c = getchar();
  }
  return a;
}
const int MX1 = 101;
const int MX2 = 10001;
int row_count, col_count;
char grid[101][10001];
inline void read() {
  scanf("%d %d\n", &row_count, &col_count);
  for (int i = 0, j1 = row_count; i < j1; ++i) gets(grid[i]);
}
inline bool is_impossible() {
  for (int i = 0, j1 = row_count; i < j1; ++i) {
    if (count(grid[i], grid[i] + col_count, '1') > 0) continue;
    return true;
  }
  return false;
}
int DL[MX1][MX2];
int DR[MX1][MX2];
inline void f1() {
  memset(DL, 63, sizeof(DL));
  memset(DR, 63, sizeof(DR));
  for (int i = 0, j1 = row_count; i < j1; ++i) {
    int cnt = DL[i][0];
    for (int j = 0, j1 = col_count; j < j1; ++j) {
      if (grid[i][j] == '1')
        cnt = 0;
      else
        ++cnt;
      DL[i][j] = min(DL[i][j], cnt);
    }
    cnt = DR[i][0];
    for (int j = col_count - 1, j1 = 0; j >= j1; --j) {
      if (grid[i][j] == '1')
        cnt = 0;
      else
        ++cnt;
      DR[i][j] = min(DR[i][j], cnt);
    }
  }
}
inline void print(int matrix[][MX2]) {
  for (int i = 0, j1 = row_count; i < j1; ++i) {
    for (int j = 0, j1 = col_count; j < j1; ++j) cout << matrix[i][j] << ' ';
    puts("");
  }
  puts("-----");
}
struct info {
  int L[MX2];
  int R[MX2];
  info() {
    memset(L, 63, sizeof(L));
    memset(R, 63, sizeof(R));
  }
};
info C[MX1];
inline void print() {
  for (int i = 0, j1 = row_count; i < j1; ++i) {
    for (int j = 0, j1 = col_count; j < j1; ++j) cout << C[i].L[j] << ' ';
    puts("");
  }
  puts("-------");
  for (int i = 0, j1 = row_count; i < j1; ++i) {
    for (int j = 0, j1 = col_count; j < j1; ++j) cout << C[i].R[j] << ' ';
    puts("");
  }
  puts("-------");
}
inline void preprocess() {
  for (int i = 0, j1 = row_count; i < j1; ++i) {
    for (int j = 0, j1 = col_count; j < j1; ++j) {
      C[i].L[j] = min(DL[i][j], DL[i][col_count - 1] + j + 1);
      if (j - 1 >= 0) C[i].L[j] = min(C[i].L[j], C[i].L[j - 1] + 1);
    }
    for (int j = col_count - 1, j1 = 0; j >= j1; --j) {
      C[i].R[j] = min(DR[i][j], DR[i][0] + (col_count - j));
      if (j + 1 <= col_count - 1) {
        C[i].R[j] = min(C[i].R[j], C[i].R[j + 1] + 1);
      }
    }
  }
}
const int inf = 1023456789;
inline void proc() {
  if (is_impossible()) {
    puts("-1");
  } else {
    f1();
    preprocess();
    int bst = inf;
    for (int col = 0, j1 = col_count; col < j1; ++col) {
      int local_bst = 0;
      for (int row = 0, j1 = row_count; row < j1; ++row) {
        local_bst += min(C[row].L[col], C[row].R[col]);
      }
      bst = min(bst, local_bst);
    }
    cout << bst << '\n';
  }
}
int main() {
  int kase = 1;
  for (int i = 0, j1 = kase; i < j1; ++i) {
    read();
    proc();
  }
  return 0;
}
