#include <bits/stdc++.h>
using namespace std;
int MAX = 100000;
int a[100][10000];
int b[100][10000];
int main() {
  int n, m;
  scanf("%d", &n);
  scanf("%d", &m);
  string st;
  for (int i = 0; i < n; i++) {
    cin >> st;
    for (int j = 0; j < m; j++) {
      a[i][j] = st[j] - '0';
      b[i][j] = MAX;
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i][j] == 1) {
        b[i][j] = 0;
      } else if (j) {
        b[i][j] = min(b[i][j], b[i][j - 1] + 1);
      } else {
        b[i][j] = min(b[i][j], b[i][m - 1] + 1);
      }
    }
    for (int j = 0; j < m; j++) {
      if (a[i][j] == 1) {
        b[i][j] = 0;
      } else if (j) {
        b[i][j] = min(b[i][j], b[i][j - 1] + 1);
      } else {
        b[i][j] = min(b[i][j], b[i][m - 1] + 1);
      }
    }
    for (int j = m - 1; j >= 0; j--) {
      if (a[i][j] == 1) {
        b[i][j] = 0;
      } else if (j < m - 1) {
        b[i][j] = min(b[i][j], b[i][j + 1] + 1);
      } else {
        b[i][j] = min(b[i][j], b[i][0] + 1);
      }
    }
    for (int j = m - 1; j >= 0; j--) {
      if (a[i][j] == 1) {
        b[i][j] = 0;
      } else if (j < m - 1) {
        b[i][j] = min(b[i][j], b[i][j + 1] + 1);
      } else {
        b[i][j] = min(b[i][j], b[i][0] + 1);
      }
    }
    for (int j = 0; j < m; j++) {
      if (a[i][j] == 1) {
        b[i][j] = 0;
      } else if (j) {
        b[i][j] = min(b[i][j], b[i][j - 1] + 1);
      } else {
        b[i][j] = min(b[i][j], b[i][m - 1] + 1);
      }
    }
    for (int j = 0; j < m; j++) {
      if (a[i][j] == 1) {
        b[i][j] = 0;
      } else if (j) {
        b[i][j] = min(b[i][j], b[i][j - 1] + 1);
      } else {
        b[i][j] = min(b[i][j], b[i][m - 1] + 1);
      }
    }
    for (int j = m - 1; j >= 0; j--) {
      if (a[i][j] == 1) {
        b[i][j] = 0;
      } else if (j < m - 1) {
        b[i][j] = min(b[i][j], b[i][j + 1] + 1);
      } else {
        b[i][j] = min(b[i][j], b[i][0] + 1);
      }
    }
    for (int j = m - 1; j >= 0; j--) {
      if (a[i][j] == 1) {
        b[i][j] = 0;
      } else if (j < m - 1) {
        b[i][j] = min(b[i][j], b[i][j + 1] + 1);
      } else {
        b[i][j] = min(b[i][j], b[i][0] + 1);
      }
    }
  }
  for (int i = 0; i < n; i++) {
    bool v = true;
    for (int j = 0; j < m; j++) {
      if (b[i][j] < MAX) {
        v = false;
        break;
      }
    }
    if (v) {
      printf("-1\n");
      return 0;
    }
  }
  int res = 10 * MAX;
  for (int j = 0; j < m; j++) {
    int tempres = 0;
    for (int i = 0; i < n; i++) {
      tempres += b[i][j];
    }
    res = min(res, tempres);
  }
  printf("%d\n", res);
  return 0;
}
