#arc170a. A - Yet Another AB Problem

A - Yet Another AB Problem

Score: 400400 points

问题陈述

给定两个由 AB 组成的字符串 SSTT,它们的长度都为 NN。设 SiS_i 表示 SS 从左数第 ii 个字符。

你可以重复执行以下操作任意次,包括零次:

  • 选择整数 iijj,使得 1i<jN1\leq i < j \leq N。将 SiS_i 替换为 A,将 SjS_j 替换为 B

确定是否可能使 SS 等于 TT。如果可能,找出所需的最小操作次数。

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

You are given two strings SS and TT of length NN consisting of A and B. Let SiS_i denote the ii-th character from the left of SS.

You can repeat the following operation any number of times, possibly zero:

  • Choose integers ii and jj such that 1i<jN1\leq i < j \leq N. Replace SiS_i with A and SjS_j with B.

Determine if it is possible to make SS equal TT. If it is possible, find the minimum number of operations required.

Constraints

  • 2N2×1052 \leq N \leq 2 \times 10^5
  • Each of SS and TT is a string of length NN consisting of A and B.
  • All input numbers are integers.

Input

The input is given from Standard Input in the following format:

NN

SS

TT

Output

If it is impossible to make SS equal TT, print -1.

Otherwise, print the minimum number of operations required to do so.

Sample Input 1

5
BAABA
AABAB

Sample Output 1

2

Performing the operation with i=1i=1 and j=3j=3 changes SS to AABBA.

Performing the operation with i=4i=4 and j=5j=5 changes SS to AABAB.

Thus, you can make SS equal TT with two operations. It can be proved that this is the minimum number of operations required, so the answer is 22.

Sample Input 2

2
AB
BA

Sample Output 2

-1

It can be proved that no matter how many operations you perform, you cannot make SS equal TT.