
fun main(args:Array<String>):Unit {
    while (true){
        val n = readLine()!!.trim().toInt()
        if (n == 0) return
        val buildingA = Building(readLine()!!.trim().split(' ').map(String::toInt).map{when(it){
            0 -> Wall.Normal
            1 -> Wall.Step(0)
            else -> Wall.Wet(0)
        }}.toTypedArray())
        val buildingB = Building(readLine()!!.trim().split(' ').map(String::toInt).map{when(it){
            0 -> Wall.Normal
            1 -> Wall.Step(0)
            else -> Wall.Wet(0)
        }}.toTypedArray())
        search(buildingA, buildingB)
        search(buildingB, buildingA)
        val min = minOf(buildingA.minTopFloor(), buildingB.minTopFloor())
        println(
                if (min == Int.MAX_VALUE) "NA"
                else min
        )
    }
}
class Building(private val mWall:Array<Wall>){
    init{
        var prev = 0
        for (i in 0 until mWall.size){
            when(mWall[i]){
                is Wall.Wet -> mWall[i] = Wall.Wet(prev)
                else -> {
                    prev = i
                }
            }
        }
        prev = mWall.size
        for (i in (0 until mWall.size).reversed()){
            when(mWall[i]){
                is Wall.Step -> mWall[i] = Wall.Step(prev - 1)
                else -> {
                    prev = i
                }
            }
        }
    }
    private val mMinStep = Array(mWall.size){Int.MAX_VALUE}
    fun minStep(floor:Int):Int = mMinStep[floor]
    fun setMinStep(floor:Int, step:Int):Unit {
        mMinStep[floor] = step
    }
    fun floorState(floor:Int):Wall = mWall[floor]
    val top:Int = mWall.size - 1
    fun minTopFloor():Int {
        return when(mWall.last()){
            is Wall.Wet -> Int.MAX_VALUE
            else -> mMinStep.last()
        }
    }
}
fun search(current:Building, other:Building, floor:Int = 0, step:Int = 0):Unit {
    if (floor > current.top) return
    if (current.minStep(floor) <= step) return
    current.setMinStep(floor, step)
    val floorState = current.floorState(floor)
    when(floorState){
        is Wall.Wet -> search(current, other, floorState.floor, step)
        is Wall.Step -> {
            search(current, other, floorState.floor, step)
            listOf(0, 1, 2).forEach { search(other, current, floorState.floor + it, step + 1) }
        }
        Wall.Normal -> listOf(0, 1, 2).forEach { search(other, current, floor + it, step + 1) }
    }
}
sealed class Wall{
    class Wet(val floor:Int):Wall()
    class Step(val floor:Int):Wall()
    object Normal:Wall()
}
