// Arup Guha
// 5/18/2016
// Solution to 2016 January USACO Gold Problem: Radio

import java.util.*;
import java.io.*;

public class radio {

	public static void main(String[] args) throws Exception {

		// Read in data.
		Scanner stdin = new Scanner(new File("radio.in"));
		int n = stdin.nextInt();
		int m = stdin.nextInt();

		int[][] john = new int[n+1][2];
		for (int i=0; i<2; i++) john[0][i] = stdin.nextInt();
		int[][] bessie = new int[m+1][2];
		for (int i=0; i<2; i++) bessie[0][i] = stdin.nextInt();
		String johnDir = stdin.next();
		String bessieDir = stdin.next();

		// Just store where John and Bessie are after each movement.
		fill(john, johnDir);
		fill(bessie, bessieDir);

		// Set up our DP.
		int[][] dp = new int[n+1][m+1];

		// Fill in first row and column for john or bessie making all of the first moves.
		for (int i=1; i<=n; i++)
			dp[i][0] = dp[i-1][0] + cost(john[i], bessie[0]);
		for (int j=1; j<=m; j++)
			dp[0][j] = dp[0][j-1] + cost(john[0], bessie[j]);

		// Run through all possible states.
		for (int i=1; i<=n; i++) {
			for (int j=1; j<=m; j++) {

				// Three options of how to get to this state - take the best one.
				int thisCost = cost(john[i], bessie[j]);
				int bothMove = dp[i-1][j-1] + thisCost;
				int johnMove = dp[i-1][j] + thisCost;
				int bessieMove = dp[i][j-1] + thisCost;
				dp[i][j] = Math.min(Math.min(bothMove, johnMove), bessieMove);
			}
		}

		// Write result.
		PrintWriter out = new PrintWriter(new FileWriter("radio.out"));
		out.println(dp[n][m]);
		out.close();
		stdin.close();
	}

	public static void fill(int[][] loc, String dir) {

		// Go through each move.
		for (int i=1; i<=dir.length(); i++) {

			// Copy old loc.
			loc[i][0] = loc[i-1][0];
			loc[i][1] = loc[i-1][1];

			// Update based on move.
			char c = dir.charAt(i-1);
			if (c == 'N') loc[i][1]++;
			else if (c == 'E') loc[i][0]++;
			else if (c == 'S') loc[i][1]--;
			else loc[i][0]--;
		}
	}

	public static int cost(int[] a, int[] b) {
		return (a[0]-b[0])*(a[0]-b[0]) + (a[1]-b[1])*(a[1]-b[1]);
	}
}