class Solution:
    def minSwap(self, A, B) -> int:
        n = len(A)
        no_swap, swap = [0 for i in range(n)], [0 for i in range(n)]
        swap[0] = 1
        for i in range(1, n):
            if A[i-1]<A[i] and B[i-1]<B[i] and B[i-1]<A[i] and A[i-1]<B[i]:
                no_swap[i]=min(no_swap[i-1], swap[i-1])
                swap[i]=min(no_swap[i-1], swap[i-1])+1
            elif A[i-1]<A[i] and B[i-1]<B[i]:
                no_swap[i]=no_swap[i-1]
                swap[i]=swap[i-1]+1
            else:
                no_swap[i]=swap[i-1]
                swap[i]=no_swap[i-1]+1
        return min(no_swap[-1],swap[-1])