Buggy python submission (verdict: Runtime Error):
while True:
    n=int(input())
    if n==0:break
    b=[[int(p)for p in input().split(" ")],[int(p)for p in input().split(" ")]]
    mem=[[1145141919 for i in range(n)]for j in range(2)]
    mem[1][0]=True
    que=[[0,0,0],[1,0,0]]
    count=1145141919
    while len(que)!=0:
        d=que.pop(0)
        state=b[d[0]][d[1]]
        if state!=2 and d[1]==n-1 and d[2]<count:
            count=d[2]
        mem[d[0]][d[1]]=d[2]
        case=-1
        if state==0:case=0
        elif state==1:
            if b[d[0]][d[1]+1]==1:case=1
            else:case=0
        elif state==2:case=2

        if case==0:
            for i in range(3):
                if d[1]+i>=n:break
                if d[0]==0 and mem[1][d[1]+i]>d[2]+1:que.append([1,d[1]+i,d[2]+1])
                elif d[0]==1 and mem[0][d[1]+i]>d[2]+1:que.append([0,d[1]+i,d[2]+1])
        elif case==1:
            if mem[d[0]][d[1]+1]>d[2]:que.append([d[0],d[1]+1,d[2]])
        elif case==2:
            if mem[d[0]][d[1]-1]>d[2]:que.append([d[0],d[1]-1,d[2]])
    print(str(count)if count!=1145141919 else "NA")

Fixed submission:
def bfs(b):
    mem=[[False for i in range(n)]for j in range(2)]
    st=[0,0]
    for i in range(2):
        if b[i][0]!=1:continue
        while st[i]<n-1 and b[i][st[i]+1]==1:
            st[i]+=1
        if st[i]==n-1:return 0
    mem[0][st[0]]=True
    mem[1][st[1]]=True
    que=[[0,st[0],0],[1,st[1],0]]
    while len(que)!=0:
        d=que.pop(0)
        des=(d[0]+1)%2
        cst=d[2]+1
        for i in range(3):
            fl=d[1]+i
            if fl>=n:break
            state=b[des][fl]
            if state!=2 and fl==n-1:
                return cst
            if state==0:
                if mem[des][fl]==False:
                    que.append([des,fl,cst])
                    mem[des][fl]=True
            elif state==1:
                k=1
                while True:
                    if fl+k>=n:return cst
                    if b[des][fl+k]!=1:
                        if mem[des][fl+k-1]==False:
                            que.append([des,fl+k-1,cst])
                            mem[des][fl+k-1]=True
                        break
                    else: k+=1

            elif state==2:
                k=1
                while True:
                    if b[des][fl-k]!=2:
                        if mem[des][fl-k]==False:
                            que.append([des,fl-k,cst])
                            mem[des][fl-k]=True
                        break
                    else:k+=1  
    return -1
while True:
    n=int(input())
    if n==0:break
    b=[[int(p)for p in input().split(" ")],[int(p)for p in input().split(" ")]]
    count=bfs(b)
    print(str(count)if count>=0 else "NA")

Buggy python submission (verdict: Runtime Error):
while True:
    n=int(input())
    if n==0:break
    b=[[int(p)for p in input().split(" ")],[int(p)for p in input().split(" ")]]
    mem=[[1145141919 for i in range(n)]for j in range(2)]
    que=[[0,0,0],[1,0,0]]
    count=1145141919
    while len(que)!=0:
        d=que.pop(0)
        state=b[d[0]][d[1]]
        if state!=2 and d[1]==n-1 and d[2]<count:
            count=d[2]
        mem[d[0]][d[1]]=d[2]
        case=-1
        if state==0:case=0
        elif state==1:
            if b[d[0]][d[1]+1]==1:case=1
            else:case=0
        elif state==2:case=2

        if case==0:
            for i in range(3):
                if d[1]+i>=n:break
                if d[0]==0 and mem[1][d[1]+i]>d[2]+1:que.append([1,d[1]+i,d[2]+1])
                elif d[0]==1 and mem[0][d[1]+i]>d[2]+1:que.append([0,d[1]+i,d[2]+1])
        elif case==1:
            if mem[d[0]][d[1]+1]>d[2]:que.append([d[0],d[1]+1,d[2]])
        elif case==2:
            if mem[d[0]][d[1]-1]>d[2]:que.append([d[0],d[1]-1,d[2]])
    print(str(count)if count!=1145141919 else "NA")

Fixed submission:
def bfs(b):
    mem=[[False for i in range(n)]for j in range(2)]
    st=[0,0]
    for i in range(2):
        if b[i][0]!=1:continue
        while st[i]<n-1 and b[i][st[i]+1]==1:
            st[i]+=1
        if st[i]==n-1:return 0
    mem[0][st[0]]=True
    mem[1][st[1]]=True
    que=[[0,st[0],0],[1,st[1],0]]
    while len(que)!=0:
        d=que.pop(0)
        des=(d[0]+1)%2
        cst=d[2]+1
        for i in range(3):
            fl=d[1]+i
            if fl>=n:break
            state=b[des][fl]
            if state!=2 and fl==n-1:
                return cst
            if state==0:
                if mem[des][fl]==False:
                    que.append([des,fl,cst])
                    mem[des][fl]=True
            elif state==1:
                k=1
                while True:
                    if fl+k>=n:return cst
                    if b[des][fl+k]!=1:
                        if mem[des][fl+k-1]==False:
                            que.append([des,fl+k-1,cst])
                            mem[des][fl+k-1]=True
                        break
                    else: k+=1

            elif state==2:
                k=1
                while True:
                    if b[des][fl-k]!=2:
                        if mem[des][fl-k]==False:
                            que.append([des,fl-k,cst])
                            mem[des][fl-k]=True
                        break
                    else:k+=1  
    return -1
while True:
    n=int(input())
    if n==0:break
    b=[[int(p)for p in input().split(" ")],[int(p)for p in input().split(" ")]]
    count=bfs(b)
    print(str(count)if count>=0 else "NA")

Buggy python submission (verdict: Runtime Error):
while True:
#for t in range(1):
    n=int(input())
    #n=8
    if n==0:break
    b=[[int(p)for p in input().split(" ")],[int(p)for p in input().split(" ")]]
    #b=[[0,0,0,2,2,2,0,0],[1,1,1,1,0,0,0,0]]
    mem=[[False for i in range(n)]for j in range(2)]
    mem[0][0]=True
    mem[1][0]=True
    que=[[0,0,0],[1,0,0]]
    count=0
    while len(que)!=0:
        d=que.pop(0)
        state=b[d[0]][d[1]]
        if state!=2 and d[1]==n-1:
            count=d[2]
            break
        if state==0:
            for i in range(3):
                if d[1]+i>=n:break
                if d[0]==0 and mem[1][d[1]+i]==False:
                    que.append([1,d[1]+i,d[2]+1])
                    mem[1][d[1]+i]=True
                elif d[0]==1 and mem[0][d[1]+i]==False:
                    que.append([0,d[1]+i,d[2]+1])
                    mem[0][d[1]+i]=True
        elif state==1:
            k=1
            while True:
                if b[d[0]][d[1]+k]!=1:
                    for i in range(3):
                        if d[1]+k+i-1>=n:break
                        if d[0]==0 and mem[1][d[1]+k+i-1]==False:
                            que.append([1,d[1]+k+i-1,d[2]+1])
                            mem[1][d[1]+k+i-1]=True
                        elif d[0]==1 and mem[0][d[1]+k+i-1]==False:
                            que.append([0,d[1]+k+i-1,d[2]+1])
                            mem[0][d[1]+k+i-1]=True
                    break
                else: k+=1

        elif state==2:
            k=1
            while True:
                if b[d[0]][d[1]-k]!=2:
                    for i in range(3):
                        if d[1]-k+i>=n:break
                        if d[0]==0 and mem[1][d[1]-k+i]==False:
                            que.append([1,d[1]-k+i,d[2]+1])
                            mem[1][d[1]-k+i]=True
                        elif d[0]==1 and mem[0][d[1]-k+i]==False:
                            que.append([0,d[1]-k+i,d[2]+1])
                            mem[0][d[1]-k+i]=True
                    break
                else:k+=1
       
    print(str(count)if count!=0 else "NA")

Fixed submission:
def bfs(b):
    mem=[[False for i in range(n)]for j in range(2)]
    st=[0,0]
    for i in range(2):
        if b[i][0]!=1:continue
        while st[i]<n-1 and b[i][st[i]+1]==1:
            st[i]+=1
        if st[i]==n-1:return 0
    mem[0][st[0]]=True
    mem[1][st[1]]=True
    que=[[0,st[0],0],[1,st[1],0]]
    while len(que)!=0:
        d=que.pop(0)
        des=(d[0]+1)%2
        cst=d[2]+1
        for i in range(3):
            fl=d[1]+i
            if fl>=n:break
            state=b[des][fl]
            if state!=2 and fl==n-1:
                return cst
            if state==0:
                if mem[des][fl]==False:
                    que.append([des,fl,cst])
                    mem[des][fl]=True
            elif state==1:
                k=1
                while True:
                    if fl+k>=n:return cst
                    if b[des][fl+k]!=1:
                        if mem[des][fl+k-1]==False:
                            que.append([des,fl+k-1,cst])
                            mem[des][fl+k-1]=True
                        break
                    else: k+=1

            elif state==2:
                k=1
                while True:
                    if b[des][fl-k]!=2:
                        if mem[des][fl-k]==False:
                            que.append([des,fl-k,cst])
                            mem[des][fl-k]=True
                        break
                    else:k+=1  
    return -1
while True:
    n=int(input())
    if n==0:break
    b=[[int(p)for p in input().split(" ")],[int(p)for p in input().split(" ")]]
    count=bfs(b)
    print(str(count)if count>=0 else "NA")

Buggy python submission (verdict: Runtime Error):
def bfs(b):
    mem=[[False for i in range(n)]for j in range(2)]
    st=[0,0]
    for i in range(2):
        while b[i][st[i]+1]==1:
            st[i]+=1
    mem[0][st[0]]=True
    mem[1][st[1]]=True
    que=[[0,0,0],[1,0,0]]
    while len(que)!=0:
        d=que.pop(0)
        des=(d[0]+1)%2
        cst=d[2]+1
        for i in range(3):
            fl=d[1]+i
            if fl>n:break
            state=b[des][fl]
            if state!=2 and fl==n-1:
                return cst
            if state==0:
                if mem[des][fl]==False:
                    que.append([des,fl,cst])
                    mem[des][fl]=True
            elif state==1:
                k=1
                while True:
                    if fl+k>=n:return cst
                    if b[des][fl+k]!=1:
                        if mem[des][fl+k-1]==False:
                            que.append([des,fl+k-1,cst])
                            mem[des][fl+k-1]=True
                        break
                    else: k+=1

            elif state==2:
                k=1
                while True:
                    if b[des][fl-k]!=2:
                        if mem[des][fl-k]==False:
                            que.append([des,fl-k,cst])
                            mem[des][fl-k]=True
                        break
                    else:k+=1  
    return 0
while True:
    n=int(input())
    if n==0:break
    b=[[int(p)for p in input().split(" ")],[int(p)for p in input().split(" ")]]
    count=bfs(b)
    print(str(count)if count!=0 else "NA")

Fixed submission:
def bfs(b):
    mem=[[False for i in range(n)]for j in range(2)]
    st=[0,0]
    for i in range(2):
        if b[i][0]!=1:continue
        while st[i]<n-1 and b[i][st[i]+1]==1:
            st[i]+=1
        if st[i]==n-1:return 0
    mem[0][st[0]]=True
    mem[1][st[1]]=True
    que=[[0,st[0],0],[1,st[1],0]]
    while len(que)!=0:
        d=que.pop(0)
        des=(d[0]+1)%2
        cst=d[2]+1
        for i in range(3):
            fl=d[1]+i
            if fl>=n:break
            state=b[des][fl]
            if state!=2 and fl==n-1:
                return cst
            if state==0:
                if mem[des][fl]==False:
                    que.append([des,fl,cst])
                    mem[des][fl]=True
            elif state==1:
                k=1
                while True:
                    if fl+k>=n:return cst
                    if b[des][fl+k]!=1:
                        if mem[des][fl+k-1]==False:
                            que.append([des,fl+k-1,cst])
                            mem[des][fl+k-1]=True
                        break
                    else: k+=1

            elif state==2:
                k=1
                while True:
                    if b[des][fl-k]!=2:
                        if mem[des][fl-k]==False:
                            que.append([des,fl-k,cst])
                            mem[des][fl-k]=True
                        break
                    else:k+=1  
    return -1
while True:
    n=int(input())
    if n==0:break
    b=[[int(p)for p in input().split(" ")],[int(p)for p in input().split(" ")]]
    count=bfs(b)
    print(str(count)if count>=0 else "NA")