#include <bits/stdc++.h>
using namespace std;
int n, m, minmove;
char arr[105][10005];
int search(int y) {
  int i, j, summove = 0;
  for (i = 0; i < n; i++) {
    int move;
    for (move = 0, j = 0; j < m; move++, j++) {
      if (y + j >= m) {
        if (arr[i][y + j - m] == '1') break;
      } else if (arr[i][y + j] == '1')
        break;
      if (y - j < 0) {
        if (arr[i][y - j + m] == '1') break;
      } else if (arr[i][y - j] == '1')
        break;
    }
    move = move < m - move ? move : m - move;
    summove += move;
  }
  return summove;
}
int judge(int y) {
  int i;
  for (i = 0; i < n; i++)
    if (arr[i][y] == '1') return 0;
  return 1;
}
void run() {
  int i, j, flag = 0;
  int move = 0;
  for (i = 0; i < n; i++) {
    int f = 0;
    for (j = 0; j < m; j++)
      if (arr[i][j] == '1') {
        f = 1;
        break;
      }
    if (f == 0) {
      flag = 1;
      break;
    }
  }
  if (flag == 0) {
    int x;
    for (j = 0; j < m; j++) {
      if (judge(j)) continue;
      x = search(j);
      if (minmove == -1)
        minmove = x;
      else
        minmove = minmove < x ? minmove : x;
    }
    printf("%d\n", minmove);
  } else
    puts("-1");
}
void get_data() {
  minmove = -1;
  int i;
  for (i = 0; i < n; i++) scanf("%s", arr[i]);
}
int main() {
  while (~scanf("%d%d", &n, &m)) {
    get_data();
    run();
  }
  return 0;
}
