import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop;


void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto M = s[1] + 2;
    string[] A;
    bool ok = false;
    foreach (i; 0..N) {
        auto t = readln.chomp;
        if (!ok && t.map!(tt => tt == '0').all) continue;
        ok = true;
        A ~= t;
    }
    N = A.length.to!int;
    A.reverse();

    int[] L = new int[](N);
    int[] R = new int[](N);
    foreach (i; 0..N) {
        L[i] = M-1, R[i] = 0;
        foreach (j; 0..M) {
            if (A[i][j] == '1') {
                L[i] = min(L[i], j);
                R[i] = max(R[i], j);
            }
        }
    }

    if (N == 0) {
        writeln(0);
        return;
    } else if (N == 1) {
        writeln(R[0]);
        return;
    }

    auto dp = new int[][](N, 2);

    dp[0][0] = R[0] * 2;
    dp[0][1] = M - 1;

    foreach (i; 1..N-1) {
        dp[i][0] = min(dp[i-1][0] + R[i] * 2 + 1, dp[i-1][1] + M);
        dp[i][1] = min(dp[i-1][1] + (M - L[i] - 1) * 2 + 1, dp[i-1][0] + M);
    }

    int ans = min(dp[N-2][0] + R[N-1] + 1, dp[N-2][1] + (M - L[N-1] - 1) + 1);
    ans.writeln;
}
