[USACO16JAN] Radio Contact G

题意翻译

FJ 失去了他最喜欢的牛铃,而 Bessie 已经同意帮助他找到它!他们用不同的路径搜索农场,通过无线电保持联系。 不幸的是,无线电中的电池电量不足,所以他们设法尽可能保持两者位置的距离最小,以节省电量。 FJ 从位置$(f_x,f_y)$ 开始,并计划遵循由 $N$ 步组成的路径.Bessie 从位置 $(b_x,b_y)$ 开始,并遵循由 $M$ 步组成的路径。每个步骤都是 `N`(北),`E`(东),`S`(南),或`W`(西)。其中,东方向为 $x$ 轴正方向,北方向为 $y$ 轴正方向。两个路径可以经过相同的点。 在每个时间段,FJ 可以不移动,也可以沿着他的道路前进一步。无论哪个方向恰好在下一个(假设他还没有到达他的路径的最后位置)。Bessie 可以做出类似的选择。 在每个时间点(不包括从初始位置开始的第一步),他们的无线电消耗的能量等于它们之间距离的平方。 请帮助 FJ 和 Bessie 计划行动策略,使双方达到各自终点时,最大限度地减少消耗的能量总量。输出所消耗的最小的能量。 ### 输入格式 第一行两个整数 $N$ 和 $M$ $(1\le N,M\le 1000)$。 第二行两个整数 $f_x$ 和 $f_y$。 第三行两个整数 $b_x$ 和 $b_y$ $(0\le f_x,f_y,b_x,b_y\le 1000)$。 下一行为一个长度为 $N$ 的字符串,描述 FJ 的路径。 最后一行为一个长度 $M$ 的字符串,描述 Bessie 的路径。 ### 输出格式 共一行一个整数,表示最小能量。

题目描述

Farmer John has lost his favorite cow bell, and Bessie the cow has agreed to help him find it! They both fan out and search the farm along different paths, but stay in contact via radio so they can keep in touch with each-other. Unfortunately, the batteries in their radios are running low, so they want to plan their movements so as to conserve power, by trying to stay always within a short distance apart. Farmer John starts at location ($f_x, f_y$) and plans to follow a path consisting of $N$ steps, each of which is either 'N' (north), 'E' (east), 'S' (south), or 'W' west. Bessie starts at location ($b_x, b_y$) and follows a similar path consisting of $M$ steps. Both paths may share points in common. At each time step, Farmer John can either stay put at his current location, or take one step forward along his path, in whichever direction happens to be next (assuming he has not yet reached the final location in his path). Bessie can make a similar choice. At each time step (excluding the first step where they start at their initial locations), their radios consume energy equal to the square of the distance between them. Please help FJ and Bessie plan a joint movement strategy that will minimize the total amount of energy consumed up to and including the final step where both of them first reach the final locations on their respective paths.

输入输出格式

输入格式


The first line of input contains $N$ and $M$ ($1 \leq N, M \leq 1000$). The second line contains integers $f_x$ and $f_y$, and the third line contains $b_x$ and $b_y$ ($0 \leq f_x, f_y, b_x, b_y \leq 1000$). The next line contains a string of length $N$ describing FJ's path, and the final line contains a string of length $M$ describing Bessie's path. It is guranteed that Farmer John and Bessie's coordinates are always in the range ($0 \leq x,y \leq 1000$) throughout their journey. Note that East points in the positive x direction and North points in the positive y direction.

输出格式


Output a single integer specifying the minimum energy FJ and Bessie can use during their travels.

输入输出样例

输入样例 #1

2 7
3 0
5 0
NN
NWWWWWN

输出样例 #1

28