#include <bits/stdc++.h>
using namespace std;
char cell[101][10001];
int main() {
  int val[10001];
  int res[10001];
  int n, m, i, j;
  cin >> n >> m;
  int counter = 0, flag;
  for ((i) = (0); (i) < (int)(n); (i)++) {
    flag = 0;
    for ((j) = (0); (j) < (int)(m); (j)++) {
      res[j] = 0;
      val[j] = -1;
      cin >> cell[i][j];
      if (cell[i][j] == '1') flag = 1;
    }
    if (flag == 0) break;
  }
  if (flag == 0)
    cout << "-1" << endl;
  else {
    for ((i) = (0); (i) < (int)(n); (i)++) {
      flag = 0;
      for ((j) = (0); (j) < (int)(m); (j)++) {
        if (cell[i][j] == '1') {
          flag = 1;
          val[j] = 0;
          counter = 1;
          continue;
        }
        if (flag == 1) {
          val[j] = counter;
          counter++;
        }
      }
      flag = 0;
      for (j = m - 1; j >= 0; j--) {
        if (cell[i][j] == '1') {
          flag = 1;
          val[j] = 0;
          counter = 1;
          continue;
        }
        if (flag == 1) {
          if (val[j] == -1 || counter < val[j]) {
            val[j] = counter;
            counter++;
          } else
            flag = 0;
        }
      }
      flag = 0;
      counter = val[m - 1];
      counter++;
      for ((j) = (0); (j) < (int)(m); (j)++) {
        if (cell[i][j] == '1') {
          val[j] = 0;
          break;
        }
        if (val[j] == -1 || counter < val[j]) {
          val[j] = counter;
          counter++;
        } else
          break;
      }
      flag = 0;
      counter = val[0];
      counter++;
      for (j = m - 1; j >= 0; j--) {
        if (cell[i][j] == '1') {
          val[j] = 0;
          break;
        }
        if (val[j] == -1 || counter < val[j]) {
          val[j] = counter;
          counter++;
        } else
          break;
      }
      int k;
      for ((k) = (0); (k) < (int)(m); (k)++) {
        res[k] += val[k];
        val[k] = -1;
      }
    }
    long long int sum, MAX;
    MAX = 999999999;
    for ((i) = (0); (i) < (int)(m); (i)++) {
      res[i];
      if (res[i] < MAX) MAX = res[i];
    }
    cout << MAX << endl;
  }
  return 0;
}
