#include <bits/stdc++.h>
using namespace std;
const int INF = 2e9;
const long long LINF = 2e18;
void inv(bool e);
template <typename T>
T sign(T a);
template <typename T>
void die(T &a);
template <typename T>
void umax(T &a, T b);
template <typename T>
void umin(T &a, T b);
bool brute, alter;
int cnt_tests = 1;
const int N = 201, M = 2e4 + 1;
int n, m;
string table[N];
void inp() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> table[i];
  }
}
int ans[N][M];
void solve() {
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      ans[i][j] = INF;
    }
  }
  for (int i = 0; i < n; i++) {
    int last, first;
    bool exist = false;
    for (int j = 0; j < m; j++) {
      if (table[i][j] == '1') {
        ans[i][j] = 0;
        if (!exist) {
          exist = true;
          first = j;
        }
        last = j;
      } else if (exist) {
        ans[i][j] = min(j - last, m - (j - first));
      }
    }
    exist = false;
    for (int j = m - 1; j >= 0; j--) {
      if (table[i][j] == '1') {
        ans[i][j] = 0;
        if (!exist) {
          exist = true;
          first = j;
        }
        last = j;
      } else if (exist) {
        umin(ans[i][j], min(last - j, m - (first - j)));
      }
    }
  }
  long long otv = INF;
  for (int j = 0; j < m; j++) {
    long long res = 0;
    for (int i = 0; i < n; i++) {
      if (ans[i][j] == INF) {
        res = INF;
        break;
      }
      res += ans[i][j];
    }
    umin(otv, res);
  }
  if (otv >= INF) {
    cout << -1;
    return;
  }
  cout << otv;
}
void stress() {}
void naive() {}
void run();
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  if (0) {
    freopen("substrcmp.in", "r", stdin);
    freopen("substrcmp.out", "w", stdout);
  }
  srand(time(0));
  brute = false;
  for (int i = 0; (i < cnt_tests); i++) {
    run();
  }
  cerr << '\n' << "Time: " << clock() / 1000.0 << " ms";
  return 0;
}
void run() {
  if (!brute) {
    inp();
  } else {
    stress();
  }
  solve();
  cout << '\n';
}
template <typename T>
void die(T &a) {
  cout << a;
  exit(0);
}
template <typename T>
T sign(T a) {
  if (a == 0) return 0;
  if (a < 0) return -1;
  return 1;
}
template <typename T>
void umax(T &a, T b) {
  if (b > a) a = b;
}
template <typename T>
void umin(T &a, T b) {
  if (b < a) a = b;
}
void inv(bool e) {
  if (!e) {
    vector<int> a;
    a[-1] += 1;
  }
}
