import heapq def main(): N = int(input()) c = [] for _ in range(N): row = list(map(int, input().split())) c.append(row) full_mask = (1 << N) - 1 heap = [] # Initialize with all possible starting positions (each first choice of A) for k in range(N): mw = 1 << k mfb = 0 p = k flag = 0 # Initially, B has not acted after the first move heapq.heappush(heap, (0, mw, mfb, p, flag)) # Using a dictionary to keep track of minimum costs for each state dp = {} INF = float('inf') # Initialize the starting states for k in range(N): mw = 1 << k mfb = 0 p = k flag = 0 key = (mw, mfb, p, flag) dp[key] = 0 while heap: cost, mw, mfb, p, flag = heapq.heappop(heap) # Check if this state is the answer if mw == full_mask and flag == 1: print(cost) return # If current cost is higher than the known minimum, skip current_key = (mw, mfb, p, flag) if dp.get(current_key, INF) < cost: continue # Generate all possible next moves for q in range(N): # Check if q is currently red (bit not set in mw) if not (mw & (1 << q)): new_cost = cost + c[p][q] new_mw = mw | (1 << q) # A flips q to white # B's action: flip p (previous p) if not in mfb p_old = p # B will act on the previous p if (mfb & (1 << p_old)) == 0: # B flips p_old back to red new_mw_after_b = new_mw ^ (1 << p_old) new_mfb = mfb | (1 << p_old) new_flag = 0 # B took action else: # B does nothing new_mw_after_b = new_mw new_mfb = mfb new_flag = 1 # B did not act new_p = q new_state = (new_mw_after_b, new_mfb, new_p, new_flag) # Check if this new state has a lower cost if new_cost < dp.get(new_state, INF): dp[new_state] = new_cost heapq.heappush(heap, (new_cost, new_mw_after_b, new_mfb, new_p, new_flag)) # If no solution found (shouldn't happen as per problem statement) print(-1) if __name__ == "__main__": main()