#include <bits/stdc++.h>
using namespace std;
template <typename Head>
inline void dout(Head in) {
  cerr << in << '\n';
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  vector<vector<char>> b(n, vector<char>(m + 2));
  for (int i = n - 1; ~i; i--) {
    for (int j = 0; j < m + 2; j++) {
      cin >> b[i][j];
    }
  }
  if (n == 1) {
    int f = -1, l = -1;
    for (int i = 0; i < m + 2; i++) {
      if (b[0][i] - '0') {
        if (f == -1) {
          f = i;
          l = i;
        } else {
          l = i;
        }
      }
    }
    if (f == -1) {
      cout << 0;
    } else {
      cout << l;
    }
    return 0;
  }
  vector<vector<int>> dp(n - 1, vector<int>(m + 2));
  int f = -1, l = -1;
  for (int i = 0; i < m + 2; i++) {
    if (b[0][i] - '0') {
      if (f == -1) {
        f = i;
        l = i;
      } else {
        l = i;
      }
    }
  }
  if (f == -1) {
    dp[0][0] = 0;
    dp[0][m + 1] = 0;
  } else {
    dp[0][0] = l << 1;
    dp[0][m + 1] = m + 1;
  }
  for (int i = 1; i < n - 1; i++) {
    f = -1;
    l = -1;
    for (int j = 0; j < m + 2; j++) {
      if (b[i][j] - '0') {
        if (f == -1) {
          f = j;
          l = j;
        } else {
          l = j;
        }
      }
    }
    if (f == -1) {
      dp[i][0] = min(dp[i - 1][0], dp[i - 1][m + 1] + m + 1) + 1;
      dp[i][m + 1] = min(dp[i - 1][0] + m + 1, dp[i - 1][m + 1]) + 1;
    } else {
      dp[i][0] = min(dp[i - 1][0] + (l << 1), dp[i - 1][m + 1] + m + 1) + 1;
      dp[i][m + 1] =
          min(dp[i - 1][0] + m + 1, dp[i - 1][m + 1] + ((m + 1 - f) << 1)) + 1;
    }
  }
  int cnt = 0;
  for (int i = n - 1; ~i; i--) {
    int ones = 0;
    for (char c : b[i]) {
      if (c - '0') {
        ones++;
      }
    }
    if (!ones) {
      cnt++;
    } else {
      break;
    }
  }
  if (cnt == n) {
    cout << 0;
    return 0;
  }
  if (cnt == n - 1) {
    int lm = -1;
    int rm = -1;
    for (int i = 0; i < m + 2; i++) {
      if (b[0][i] - '0') {
        if (lm == -1) {
          lm = i;
          rm = i;
        } else {
          rm = i;
        }
      }
    }
    cout << rm;
    return 0;
  }
  f = -1;
  l = -1;
  for (int i = 0; i < m + 2; i++) {
    if (b[n - cnt - 1][i] - '0') {
      if (f == -1) {
        f = i;
        l = i;
      } else {
        l = i;
      }
    }
  }
  cout << min(dp[n - 2 - cnt][0] + l, dp[n - 2 - cnt][m + 1] + (m + 1 - f)) + 1;
  dout("");
  return 0;
}
