inputs: Solve in Python:
Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the university building, then goes and turns all the lights off.

The building consists of n floors with stairs at the left and the right sides. Each floor has m rooms on the same line with a corridor that connects the left and right stairs passing by all the rooms. In other words, the building can be represented as a rectangle with n rows and m + 2 columns, where the first and the last columns represent the stairs, and the m columns in the middle represent rooms.

Sagheer is standing at the ground floor at the left stairs. He wants to turn all the lights off in such a way that he will not go upstairs until all lights in the floor he is standing at are off. Of course, Sagheer must visit a room to turn the light there off. It takes one minute for Sagheer to go to the next floor using stairs or to move from the current room/stairs to a neighboring room/stairs on the same floor. It takes no time for him to switch the light off in the room he is currently standing in. Help Sagheer find the minimum total time to turn off all the lights.

Note that Sagheer does not have to go back to his starting position, and he does not have to visit rooms where the light is already switched off.


-----Input-----

The first line contains two integers n and m (1 ≤ n ≤ 15 and 1 ≤ m ≤ 100) — the number of floors and the number of rooms in each floor, respectively.

The next n lines contains the building description. Each line contains a binary string of length m + 2 representing a floor (the left stairs, then m rooms, then the right stairs) where 0 indicates that the light is off and 1 indicates that the light is on. The floors are listed from top to bottom, so that the last line represents the ground floor.

The first and last characters of each string represent the left and the right stairs, respectively, so they are always 0.


-----Output-----

Print a...
targets: def coun(pref):
    now = 0
    for i in range(n):
        pos = pref[i]
        if pos == 'l':
            if i < n - 1 and sum(check[(i + 1):]) > 0:
                now += 1
                if "1" in mat[i]:
                    if pref[i + 1] == "r":
                        now += (m + 1)
                    else:
                        now += (2 * mat[i].rfind("1"))
                else:
                    if pref[i + 1] == 'r':
                        now += (m + 1)
            else:
                if "1" in mat[i]:
                    now += mat[i].rfind("1")
        else:
            if i < n - 1 and sum(check[(i + 1):]) > 0:
                now += 1
                if "1" in mat[i]:
                    if pref[i + 1] == "l":
                        now += (m + 1)
                    else:
                        now += (2 * (m + 1 - mat[i].find("1")))
                else:
                    if pref[i + 1] == 'l':
                        now += (m + 1)
            else:
                if "1" in mat[i]:
                    now += (m + 1 - mat[i].find("1"))
    return now
def gen(pref):
    nonlocal ans
    if len(pref) == n:
        ans = min(ans, coun(pref))
        return
    gen(pref + "l")
    gen(pref + "r")
n, m = map(int, input().split())
mat = [0] * n
for i in range(n):
    mat[i] = input()
mat.reverse()
check = [0] * n
for i in range(n):
    check[i] = mat[i].count("1")
ans = 1000000000
gen("l")
print(ans)