#include <bits/stdc++.h>
using namespace std;
int n, m, arr[20][150];
int solve(int l_or_r, int toreach, int row) {
  if (row < 0) return 0;
  int ans = 0;
  if (!l_or_r) {
    for (int i = 1; i <= m; i++) {
      if (arr[row][i]) {
        ans += i - l_or_r + toreach;
        l_or_r = i;
        toreach = 0;
      }
    }
  } else {
    for (int i = m; i > 0; i--) {
      if (arr[row][i]) {
        ans += l_or_r + toreach - i;
        l_or_r = i;
        toreach = 0;
      }
    }
  }
  int a = solve(0, toreach + l_or_r + 1, row - 1);
  int b = solve(m + 1, toreach + m + 2 - l_or_r, row - 1);
  ans += min(a, b);
  return ans;
}
int main() {
  string s;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> s;
    for (int j = 0; j < m + 2; j++) arr[i][j] = s[j] - '0';
  }
  cout << solve(0, 0, n - 1);
  return 0;
}
