using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Ryouko_s_Memory_Note
{
    internal class Program
    {
        private static readonly StreamReader reader =
            new StreamReader(Console.OpenStandardInput(1024*10), Encoding.ASCII, false, 1024*10);

        private static readonly StreamWriter writer =
            new StreamWriter(Console.OpenStandardOutput(1024*10), Encoding.ASCII, 1024*10);

        private static void Main(string[] args)
        {
            string[] ss = reader.ReadLine().Split(' ');
            int n = int.Parse(ss[0]);
            int m = int.Parse(ss[1]);

            var mm = new int[m];
            var nn = new List<int>[n + 1];
            for (int i = 0; i < nn.Length; i++)
            {
                nn[i] = new List<int>();
            }
            ss = reader.ReadLine().Split(' ');
            for (int i = 0; i < m; i++)
            {
                mm[i] = int.Parse(ss[i]);
                nn[mm[i]].Add(i);
            }

            var h = new Hashtable();

            long min = 0;
            for (int i = 1; i < m; i++)
            {
                min += Math.Abs(mm[i - 1] - mm[i]);
            }

            long con = min;
            for (int i = 1; i < m; i++)
            {
                long hash = ((long)mm[i - 1] << 32) + mm[i];
                if (h[hash] == null)
                {
                    h[hash] = 1;

                    long count = con;

                    foreach (int index in nn[mm[i - 1]])
                    {
                        if (index != 0)
                        {
                            int deltaold = Math.Abs(mm[index - 1] - mm[index]);
                            int deltanew = Math.Abs(mm[index - 1] - mm[i]);
                            count = count - deltaold + deltanew;
                        }
                        if (index !=m-1)
                        {
                            int deltaold = Math.Abs(mm[index + 1] - mm[index]);
                            int deltanew = Math.Abs(mm[index + 1] - mm[i]);
                            count = count - deltaold + deltanew;
                        }
                    }

                    if (count < min)
                        min = count;
                }
            }

            for (int i = 0; i < m - 1; i++)
            {
                long hash = (mm[i + 1] << 32) + mm[i];
                if (h[hash] == null)
                {
                    h[hash] = 1;

                    long count = con;

                    foreach (int index in nn[mm[i + 1]])
                    {
                        if (index != 0)
                        {
                            int deltaold = Math.Abs(mm[index - 1] - mm[index]);
                            int deltanew = Math.Abs(mm[index - 1] - mm[i]);
                            count = count - deltaold + deltanew;
                        }
                        if (index != m - 1)
                        {
                            int deltaold = Math.Abs(mm[index + 1] - mm[index]);
                            int deltanew = Math.Abs(mm[index + 1] - mm[i]);
                            count = count - deltaold + deltanew;
                        }
                    }

                    if (count < min)
                        min = count;
                }
            }

            writer.WriteLine(min);
            writer.Flush();
        }
    }
}


using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Ryouko_s_Memory_Note
{
    internal class Program
    {
        private static readonly StreamReader reader =
            new StreamReader(Console.OpenStandardInput(1024*10), Encoding.ASCII, false, 1024*10);

        private static readonly StreamWriter writer =
            new StreamWriter(Console.OpenStandardOutput(1024*10), Encoding.ASCII, 1024*10);

        private static void Main(string[] args)
        {
            string[] ss = reader.ReadLine().Split(' ');
            int n = int.Parse(ss[0]);
            int m = int.Parse(ss[1]);

            var mm = new int[m];
            var nn = new List<int>[n + 1];
            for (int i = 0; i < nn.Length; i++)
            {
                nn[i] = new List<int>();
            }
            ss = reader.ReadLine().Split(' ');
            for (int i = 0; i < m; i++)
            {
                mm[i] = int.Parse(ss[i]);
                nn[mm[i]].Add(i);
            }
            for (int i = 0; i < nn.Length; i++)
            {
                nn[i].Sort();
            }

            var h = new Hashtable();

            long min = 0;
            for (int i = 1; i < m; i++)
            {
                min += Math.Abs(mm[i - 1] - mm[i]);
            }

            long con = min;
            for (int i = 1; i < m; i++)
            {
                if (mm[i - 1] == mm[i])
                    continue;
                long hash = (((long) mm[i - 1]) << 32) + mm[i];
                if (h[hash] == null)
                {
                    h[hash] = 1;

                    long count = con;

                    foreach (int index in nn[mm[i - 1]])
                    {
                        if (index != 0)
                        {
                            if (mm[index - 1] != mm[index])
                            {
                                int deltaold = Math.Abs(mm[index - 1] - mm[index]);
                                int deltanew = Math.Abs(mm[index - 1] - mm[i]);
                                count = count - deltaold + deltanew;
                            }
                        }
                        if (index != m - 1)
                        {
                            if (mm[index + 1] != mm[index])
                            {
                                int deltaold = Math.Abs(mm[index + 1] - mm[index]);
                                int deltanew = Math.Abs(mm[index + 1] - mm[i]);
                                count = count - deltaold + deltanew;
                            }
                        }
                    }

                    if (count < min)
                        min = count;
                }
            }

            for (int i = 0; i < m - 1; i++)
            {
                if (mm[i + 1] == mm[i])
                    continue;
                long hash = (((long) mm[i + 1]) << 32) + mm[i];
                if (h[hash] == null)
                {
                    h[hash] = 1;

                    long count = con;

                    foreach (int index in nn[mm[i + 1]])
                    {
                        if (index != 0)
                        {
                            if (mm[index - 1] != mm[index])
                            {
                                int deltaold = Math.Abs(mm[index - 1] - mm[index]);
                                int deltanew = Math.Abs(mm[index - 1] - mm[i]);
                                count = count - deltaold + deltanew;
                            }
                        }
                        if (index != m - 1)
                        {
                            if (mm[index + 1] != mm[index])
                            {
                                int deltaold = Math.Abs(mm[index + 1] - mm[index]);
                                int deltanew = Math.Abs(mm[index + 1] - mm[i]);
                                count = count - deltaold + deltanew;
                            }
                        }
                    }

                    if (count < min)
                        min = count;
                }
            }

            writer.WriteLine(min);
            writer.Flush();
        }
    }
}


using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Ryouko_s_Memory_Note
{
    internal class Program
    {
        private static readonly StreamReader reader =
            new StreamReader(Console.OpenStandardInput(1024*10), Encoding.ASCII, false, 1024*10);

        private static readonly StreamWriter writer =
            new StreamWriter(Console.OpenStandardOutput(1024*10), Encoding.ASCII, 1024*10);

        private static void Main(string[] args)
        {
            string[] ss = reader.ReadLine().Split(' ');
            int n = int.Parse(ss[0]);
            int m = int.Parse(ss[1]);

            var mm = new int[m];
            var nn = new List<int>[n + 1];
            for (int i = 0; i < nn.Length; i++)
            {
                nn[i] = new List<int>();
            }
            ss = reader.ReadLine().Split(' ');
            for (int i = 0; i < m; i++)
            {
                mm[i] = int.Parse(ss[i]);
                nn[mm[i]].Add(i);
            }

            var h = new Hashtable();

            long min = 0;
            for (int i = 1; i < m; i++)
            {
                min += Math.Abs(mm[i - 1] - mm[i]);
            }

            long con = min;
            for (int i = 1; i < m; i++)
            {
                long hash = ((long)mm[i - 1] << 32) + mm[i];
                if (h[hash] == null)
                {
                    h[hash] = 1;

                    long count = con;

                    foreach (int index in nn[mm[i - 1]])
                    {
                        if (index != 0)
                        {
                            int deltaold = Math.Abs(mm[index - 1] - mm[index]);
                            int deltanew = Math.Abs(mm[index - 1] - mm[i]);
                            count = count - deltaold + deltanew;
                        }
                        if (index !=m-1)
                        {
                            int deltaold = Math.Abs(mm[index + 1] - mm[index]);
                            int deltanew = Math.Abs(mm[index + 1] - mm[i]);
                            count = count - deltaold + deltanew;
                        }
                    }

                    if (count < min)
                        min = count;
                }
            }

            for (int i = 0; i < m - 1; i++)
            {
                long hash = ((long)mm[i + 1] << 32) + mm[i];
                if (h[hash] == null)
                {
                    h[hash] = 1;

                    long count = con;

                    foreach (int index in nn[mm[i + 1]])
                    {
                        if (index != 0)
                        {
                            int deltaold = Math.Abs(mm[index - 1] - mm[index]);
                            int deltanew = Math.Abs(mm[index - 1] - mm[i]);
                            count = count - deltaold + deltanew;
                        }
                        if (index != m - 1)
                        {
                            int deltaold = Math.Abs(mm[index + 1] - mm[index]);
                            int deltanew = Math.Abs(mm[index + 1] - mm[i]);
                            count = count - deltaold + deltanew;
                        }
                    }

                    if (count < min)
                        min = count;
                }
            }

            writer.WriteLine(min);
            writer.Flush();
        }
    }
}


using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Ryouko_s_Memory_Note
{
    internal class Program
    {
        private static readonly StreamReader reader =
            new StreamReader(Console.OpenStandardInput(1024*10), Encoding.ASCII, false, 1024*10);

        private static readonly StreamWriter writer =
            new StreamWriter(Console.OpenStandardOutput(1024*10), Encoding.ASCII, 1024*10);

        private static void Main(string[] args)
        {
            string[] ss = reader.ReadLine().Split(' ');
            int n = int.Parse(ss[0]);
            int m = int.Parse(ss[1]);

            var mm = new int[m];
            var nn = new List<int>[n + 1];
            for (int i = 0; i < nn.Length; i++)
            {
                nn[i] = new List<int>();
            }
            ss = reader.ReadLine().Split(' ');
            for (int i = 0; i < m; i++)
            {
                mm[i] = int.Parse(ss[i]);
            }
            for (int i = 1; i < m; i++)
            {
                if (mm[i] == mm[i - 1])
                    continue;
                nn[mm[i]].Add(mm[i - 1]);
                nn[mm[i - 1]].Add(mm[i]);
            }
            for (int i = 0; i < nn.Length; i++)
            {
                nn[i].Sort();
            }

            long min = 0;
            for (int i = 1; i < m; i++)
            {
                min += Math.Abs(mm[i - 1] - mm[i]);
            }

            long con = min;
            for (int i = 0; i < nn.Length; i++)
            {
                if (nn[i].Count == 0)
                    continue;

                int next = nn[i][(nn[i].Count + 1)/2];
                long ans = con;
                foreach (int ii in nn[i])
                {
                    ans -= Math.Abs(ii - i);
                    ans += Math.Abs(ii - next);
                }
                if (ans < min)
                    min = ans;
            }

            writer.WriteLine(min);
            writer.Flush();
        }
    }
}


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _248c
{
    class Program
    {
        static void Main(string[] args)
        {
            //Console.SetIn(File.OpenText("in.txt"));
            var d = Console.ReadLine().Split(' ').Select(i => long.Parse(i)).ToArray();
            var n = d[0];
            var m = d[1];

            var a = Console.ReadLine().Split(' ').Select(i => long.Parse(i)).ToArray();
            var cs = new long[n + 1];
           // var max = new long[n + 1];
            var co = new long[n + 1];

            var ps = new List<long>[n + 1];
            for (int i = 0; i < n + 1; i++)
            {
                ps[i] = new List<long>();
            }
            for (int i = 0; i < m - 1; i++)
            {
                var f = a[i];
                var s = a[i + 1];
                if (f == s)
                {
                    continue;
                }

                var diff = Math.Abs(f - s);
                cs[f] += diff;
                cs[s] += diff;

                //max[f] += s;
                //max[s] += f;

                co[f]++;
                co[s]++;

                ps[f].Add(s);
                ps[s].Add(f);
            }

            var disp = new long[n + 1];
            for (int i = 0; i < n + 1; i++)
            {
                disp[i] = -1;
            }

            for (int i = 0; i < n + 1; i++)
            {
                if (co[i] != 0)
                {
                    ps[i].Sort();
                    var l = ps[i].Count;
                    var med = ps[i][l / 2];

                    //var sum = max[i];
                    //var id = sum / co[i];

                    disp[i] = med;
                }
            }

            var cs2 = new long[n + 1];
            for (int i = 0; i < m - 1; i++)
            {
                var f = a[i];
                var s = a[i + 1];
                if (f == s)
                {
                    continue;
                }
                var diff = Math.Abs(f - s);

                if (disp[f] != -1)
                {
                    cs2[f] += Math.Abs(disp[f] - s);
                }
                else
                {
                    cs2[f] += diff;
                }

                if (disp[s] != -1)
                {
                    cs2[s] += Math.Abs(disp[s] - f);
                }
                else
                {
                    cs2[s] += diff;
                }
            }

            long maxd = -1;
            long maxi = 0;
            long newval = 0;
            long oldval = -1;
            for (int i = 0; i < m; i++)
            {
                var f = a[i];
                var di = Math.Abs(cs[f] - cs2[f]);

                if (di > maxd)
                {
                    maxd = di;
                    maxi = i;
                    newval = disp[f];
                    oldval = f;
                }
            }

            for (int i = 0; i < m ; i++)
            {
                if (a[i] == oldval)
                {
                    a[i] = newval;
                }
            }

            //a[maxi] = newval;
            long maxc = 0;
            for (int i = 0; i < m - 1; i++)
            {
                var f = a[i];
                var s = a[i + 1];
                var diff = Math.Abs(f - s);
                maxc += diff;
            }

            Console.WriteLine(maxc);
            //var last = a[m - 1];
            //var prev = a[m - 2];

        }
    }
}


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _248c
{
	class Program
	{
		static void Main(string[] args)
		{
			//Console.SetIn(File.OpenText("in.txt"));
			var d = Console.ReadLine().Split(' ').Select(i => long.Parse(i)).ToArray();
			var n = d[0];
			var m = d[1];

			var a = Console.ReadLine().Split(' ').Select(i => long.Parse(i)).ToArray();
			var cs = new long[n + 1];
			var max = new long[n + 1];
			var co = new long[n + 1];
			for (int i = 0; i < m - 1; i++)
			{
				var f = a[i];
				var s = a[i + 1];
				var diff = Math.Abs(f - s);
				cs[f] += diff;
				cs[s] += diff;

				max[f] += s;
				max[s] += f;

				co[f]++;
				co[s]++;
			}

			var disp = new long[n + 1];
			for (int i = 0; i < n + 1; i++)
			{
				disp[i] = -1;
			}

			for (int i = 0; i < n + 1; i++)
			{
				if (co[i] != 0)
				{
					var sum = max[i];
					var id = sum / co[i];

					disp[i] = id;// Math.Abs(cs[i] - id);
					//if (disp[i] > maxd)
					//{
					//	maxd = disp[i];
					//	maxi = i;
					//	newval = id;
					//}
				}
			}

			var cs2 = new long[n + 1];
			for (int i = 0; i < m - 1; i++)
			{
				var f = a[i];
				var s = a[i + 1];
				var diff = Math.Abs(f - s);

				if (disp[f] != -1)
				{
					cs2[f] += Math.Abs(disp[f] - s);
				}
				else
				{
					cs2[f] += diff;
				}

				if (disp[s] != -1)
				{
					cs2[s] += Math.Abs(disp[s] - f);
				}
				else
				{
					cs2[s] += diff;
				}
			}

			long maxd = -1;
			long maxi = 0;
			long newval = 0;
			for (int i = 0; i < m; i++)
			{
				var f = a[i];
				var di = Math.Abs(cs[f] - cs2[f]);

				if (di > maxd)
				{
					maxd = di;
					maxi = i;
					newval = disp[f];
				}
			}

			a[maxi] = newval;
			long maxc = 0;
			for (int i = 0; i < m - 1; i++)
			{
				var f = a[i];
				var s = a[i + 1];
				var diff = Math.Abs(f - s);
				maxc += diff;
			}

			Console.WriteLine(maxc);
			//var last = a[m - 1];
			//var prev = a[m - 2];

		}
	}
}


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _248c
{
    class Program
    {
        static void Main(string[] args)
        {
            //Console.SetIn(File.OpenText("in.txt"));
            var d = Console.ReadLine().Split(' ').Select(i => long.Parse(i)).ToArray();
            var n = d[0];
            var m = d[1];

            var a = Console.ReadLine().Split(' ').Select(i => long.Parse(i)).ToArray();
            var cs = new long[n + 1];
           // var max = new long[n + 1];
            var co = new long[n + 1];

            var ps = new List<long>[n + 1];
            for (int i = 0; i < n + 1; i++)
            {
                ps[i] = new List<long>();
            }
            for (int i = 0; i < m - 1; i++)
            {
                var f = a[i];
                var s = a[i + 1];
                if (f == s)
                {
                    continue;
                }

                var diff = Math.Abs(f - s);
                cs[f] += diff;
                cs[s] += diff;

                //max[f] += s;
                //max[s] += f;

                co[f]++;
                co[s]++;

                ps[f].Add(s);
                ps[s].Add(f);
            }

            var disp = new long[n + 1];
            for (int i = 0; i < n + 1; i++)
            {
                disp[i] = -1;
            }

            for (int i = 0; i < n + 1; i++)
            {
                if (co[i] != 0)
                {
                    ps[i].Sort();
                    var l = ps[i].Count;
                    var med = ps[i][l / 2];

                    //var sum = max[i];
                    //var id = sum / co[i];

                    disp[i] = med;
                }
            }

            var cs2 = new long[n + 1];
            for (int i = 0; i < m - 1; i++)
            {
                var f = a[i];
                var s = a[i + 1];
                var diff = Math.Abs(f - s);

                if (disp[f] != -1)
                {
                    cs2[f] += Math.Abs(disp[f] - s);
                }
                else
                {
                    cs2[f] += diff;
                }

                if (disp[s] != -1)
                {
                    cs2[s] += Math.Abs(disp[s] - f);
                }
                else
                {
                    cs2[s] += diff;
                }
            }

            long maxd = -1;
            long maxi = 0;
            long newval = 0;
            long oldval = -1;
            for (int i = 0; i < m; i++)
            {
                var f = a[i];
                var di = Math.Abs(cs[f] - cs2[f]);

                if (di > maxd)
                {
                    maxd = di;
                    maxi = i;
                    newval = disp[f];
                    oldval = f;
                }
            }

            for (int i = 0; i < m ; i++)
            {
                if (a[i] == oldval)
                {
                    a[i] = newval;
                }
            }

            //a[maxi] = newval;
            long maxc = 0;
            for (int i = 0; i < m - 1; i++)
            {
                var f = a[i];
                var s = a[i + 1];
                var diff = Math.Abs(f - s);
                maxc += diff;
            }

            Console.WriteLine(maxc);
            //var last = a[m - 1];
            //var prev = a[m - 2];

        }
    }
}


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _248c
{
    class Program
    {
        static void Main(string[] args)
        {
            //Console.SetIn(File.OpenText("in.txt"));
            var d = Console.ReadLine().Split(' ').Select(i => long.Parse(i)).ToArray();
            var n = d[0];
            var m = d[1];

            var a = Console.ReadLine().Split(' ').Select(i => long.Parse(i)).ToArray();
            var cs = new long[n + 1];
           // var max = new long[n + 1];
            var co = new long[n + 1];

            var ps = new List<long>[n + 1];
            for (int i = 0; i < n + 1; i++)
            {
                ps[i] = new List<long>();
            }
            for (int i = 0; i < m - 1; i++)
            {
                var f = a[i];
                var s = a[i + 1];
                var diff = Math.Abs(f - s);
                cs[f] += diff;
                cs[s] += diff;

                //max[f] += s;
                //max[s] += f;

                co[f]++;
                co[s]++;

                ps[f].Add(s);
                ps[s].Add(f);
            }

            var disp = new long[n + 1];
            for (int i = 0; i < n + 1; i++)
            {
                disp[i] = -1;
            }

            for (int i = 0; i < n + 1; i++)
            {
                if (co[i] != 0)
                {
                    ps[i].Sort();
                    var l = ps[i].Count;
                    var med = ps[i][l / 2];

                    //var sum = max[i];
                    //var id = sum / co[i];

                    disp[i] = med;
                }
            }

            var cs2 = new long[n + 1];
            for (int i = 0; i < m - 1; i++)
            {
                var f = a[i];
                var s = a[i + 1];
                var diff = Math.Abs(f - s);

                if (disp[f] != -1)
                {
                    cs2[f] += Math.Abs(disp[f] - s);
                }
                else
                {
                    cs2[f] += diff;
                }

                if (disp[s] != -1)
                {
                    cs2[s] += Math.Abs(disp[s] - f);
                }
                else
                {
                    cs2[s] += diff;
                }
            }

            long maxd = -1;
            long maxi = 0;
            long newval = 0;
            long oldval = 0;
            for (int i = 0; i < m; i++)
            {
                var f = a[i];
                var di = Math.Abs(cs[f] - cs2[f]);

                if (di > maxd)
                {
                    maxd = di;
                    maxi = i;
                    newval = disp[f];
                    oldval = f;
                }
            }

            for (int i = 0; i < m ; i++)
            {
                if (a[i] == oldval)
                {
                    a[i] = newval;
                }
            }

            //a[maxi] = newval;
            long maxc = 0;
            for (int i = 0; i < m - 1; i++)
            {
                var f = a[i];
                var s = a[i + 1];
                var diff = Math.Abs(f - s);
                maxc += diff;
            }

            Console.WriteLine(maxc);
            //var last = a[m - 1];
            //var prev = a[m - 2];

        }
    }
}


import kotlin.math.abs
import kotlin.math.min

private fun readLn() = readLine()!! // string line
private fun readInt() = readLn().toInt() // single int
private fun readStrings() = readLn().split(" ") // list of strings
private fun readInts() = readStrings().map { it.toInt() } // list of ints
private fun readLongs() = readStrings().map { it.toLong() } // list of ints

fun main() {
    val (n, k) = readInts()

    val pages = readInts()
    val neighbors = Array<MutableList<Int>>(n + 1) { mutableListOf() }

    for (i in pages.indices) {
        val p = pages[i]

        if (i != 0 && p != pages[i - 1]) {
            neighbors[p].add(pages[i - 1])
        }
        if (i != k - 1 && p != pages[i + 1]) {
            neighbors[p].add(pages[i + 1])
        }
    }

    var total = 0L
    var bestDiff = 0L

    for (i in 1..n) {
        if (neighbors[i].isEmpty()) { continue; }

        neighbors[i].sort()
        val orig = neighbors[i].fold(0L) { sum, neigh ->
            sum + abs(i - neigh)
        }

        var first = true
        var best = Long.MAX_VALUE
        var bestTo = i
        var cur = Long.MAX_VALUE
        var prevNeigh = Int.MIN_VALUE

        for (pos in neighbors[i].indices) {
            val target = neighbors[i][pos]
            if (first) {
                best = neighbors[i].fold(0L) { sum, neigh ->
                    sum + abs(target - neigh)
                }
                cur = best
                first = false
                bestTo = target
            } else {
                cur += (target - prevNeigh) * (pos - (neighbors[i].size - pos))
                if (cur < best) {
                    best = cur
                    bestTo = target
                }
            }
            prevNeigh = target
        }

        val diff = best - orig
        //println("$i to $bestTo gives $diff")

        total += orig
        bestDiff = min(bestDiff, diff)
    }

    println(total / 2 + bestDiff)
}


#include<cstring>
#include<cstdio>
#include<algorithm>
#define rep(i,a,b) for (int i=a;i<=b;++i)
#define N 100005
#include<vector>
#include<cmath>
#define pb push_back
#include<iostream>
using namespace std;
typedef long long ll;
int n,m;
ll a[N],res,ans;
vector<ll> G[N];
int main() {
    cin>>n>>m;
    rep(i,1,m) {
        cin>>a[i];
        if (i>1&&a[i]!=a[i-1]) G[a[i]].pb(a[i-1]),G[a[i-1]].pb(a[i]);
    }
    rep(i,2,m) ans+=abs(a[i]-a[i-1]);
    rep(i,1,n) {
        if (G[i].empty()) continue;
        sort(G[i].begin(),G[i].end());
        ll x=G[i][(G[i].size()+1)/2];
        ll sum=0;
        //printf("%d %d\n",i,G[i].size());
        rep(j,0,G[i].size()-1) sum+=abs(G[i][j]-x)-abs(G[i][j]-i);
        res=min(res,sum);
    }
    cout<<res+ans<<endl;
    return 0;
}


#include<cstring>
#include<cstdio>
#include<algorithm>
#define rep(i,a,b) for (int i=a;i<=b;++i)
#define N 100005
#include<vector>
#include<cmath>
#define pb push_back
#include<iostream>
using namespace std;
typedef long long ll;
int n,m;
ll a[N],res,ans;
vector<ll> G[N];
int main() {
    cin>>n>>m;
    rep(i,1,m) {
        cin>>a[i];
        if (i>1&&a[i]!=a[i-1]) G[a[i]].pb(a[i-1]),G[a[i-1]].pb(a[i]);
    }
    rep(i,2,m) ans+=abs(a[i]-a[i-1]);
    rep(i,1,n) {
        if (G[i].empty()) continue;
        sort(G[i].begin(),G[i].end());
        ll x=G[i][G[i].size()/2];
        ll sum=0;
        rep(j,0,G[i].size()-1) sum+=abs(G[i][j]-x)-abs(G[i][j]-i);
        res=min(res,sum);
    }
    cout<<res+ans<<endl;
    return 0;
}


#include <bits/stdc++.h>
#define fastio()                  \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0)
#define pb push_back
#define show(x) cout << (#x) << " : " << x << endl;
#define ll long long
#define ld long double

#define mp make_pair
#define ff first
#define ss second
#define pii pair<ll, ll>
#define sq(x) ((x) * (x))
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define For(i,a,b) for(int (i)=(a);(i) < (b); ++(i))
const ll MOD = 1000 * 1000 * 1000 + 7;
const ll MOD1 = 998244353;
using namespace std;

ll power(unsigned ll x, unsigned ll y)
{
   unsigned  ll res = 1;

    while (y > 0)
    {

        if (y & 1)
            res = (unsigned long long)(res*x);

        y = y>>1;

        if (x<=100000000)
        x = (unsigned long long)(x*x);

        //cout<<x<<'\n';
    }
    return res;
}

int main()
{

#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif

fastio();

ll n;
cin>>n;

string A[n];

for(int i=0;i<n;i++)
cin>>A[i];

map <ll,ll> H;

ll sum1=0;

for(int i=0;i<n;i++)
{
ll sum=0;

for(int j=0;j<A[i].size();j++)
{
//cout<<A[i][j];

if (A[i][j]=='(')
{
sum+=1;

}
else
{
sum+=-1;

}

if (sum<0)
break;
}

if(sum>=0)
H[sum]++;

sum=0;

for(int j=A[i].size()-1;j>=0;j--)
{
//cout<<A[i][j];

if (A[i][j]==')')
{
sum+=1;

}
else
{
sum+=-1;

}

if (sum<0)
break;
}

if(sum>0)
H[-sum]++;

}

for(int i=0;i<=300000;i++)
{

sum1+=H[i]*H[-i];
}

cout<<sum1<<'\n';

return 0;
}


#include <bits/stdc++.h>
#define fastio()                  \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0)
#define pb push_back
#define show(x) cout << (#x) << " : " << x << endl;
#define ll long long
#define ld long double

#define mp make_pair
#define ff first
#define ss second
#define pii pair<ll, ll>
#define sq(x) ((x) * (x))
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define For(i,a,b) for(int (i)=(a);(i) < (b); ++(i))
const ll MOD = 1000 * 1000 * 1000 + 7;
const ll MOD1 = 998244353;
using namespace std;

ll power(unsigned ll x, unsigned ll y)
{
   unsigned  ll res = 1;

    while (y > 0)
    {

        if (y & 1)
            res = (unsigned long long)(res*x);

        y = y>>1;

        if (x<=100000000)
        x = (unsigned long long)(x*x);

        //cout<<x<<'\n';
    }
    return res;
}

int main()
{

#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif

fastio();

ll n;
cin>>n;

string A[n];

for(int i=0;i<n;i++)
cin>>A[i];

map <ll,ll> H;

ll sum1=0;

for(int i=0;i<n;i++)
{
ll sum=0;
for(int j=0;j<A[i].size();j++)
{
//cout<<A[i][j];

if (A[i][j]=='(')
sum+=1;
else
sum+=-1;

}

if((sum>0&&A[i][0]!=')')||(sum<0&&A[i][0]==')'&&A[i].size()==1))
H[sum]++;

//cout<<sum<<'\n';

if (sum==0&&A[i][0]=='(')
sum1+=1;
}

sum1=sum1+sum1*(sum1-1);

for(int i=1;i<=300000;i++)
sum1+=H[i]*H[-i];

cout<<sum1<<'\n';

return 0;
}


#include <bits/stdc++.h>
#define fastio()                  \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0)
#define pb push_back
#define show(x) cout << (#x) << " : " << x << endl;
#define ll long long
#define ld long double

#define mp make_pair
#define ff first
#define ss second
#define pii pair<ll, ll>
#define sq(x) ((x) * (x))
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define For(i,a,b) for(int (i)=(a);(i) < (b); ++(i))
const ll MOD = 1000 * 1000 * 1000 + 7;
const ll MOD1 = 998244353;
using namespace std;

ll power(unsigned ll x, unsigned ll y)
{
   unsigned  ll res = 1;

    while (y > 0)
    {

        if (y & 1)
            res = (unsigned long long)(res*x);

        y = y>>1;

        if (x<=100000000)
        x = (unsigned long long)(x*x);

        //cout<<x<<'\n';
    }
    return res;
}

int main()
{

#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif

fastio();

ll n;
cin>>n;

string A[n];

for(int i=0;i<n;i++)
cin>>A[i];

map <ll,ll> H;

ll sum1=0;

for(int i=0;i<n;i++)
{
ll sum=0;
ll flag=0;
ll flag2=0;
for(int j=0;j<A[i].size();j++)
{
//cout<<A[i][j];

if (A[i][j]=='(')
{
sum+=1;
if (A[i][0]==')')
flag=1;
}
else
{
sum+=-1;
if (A[i][j-1]==')')
flag2=1;

}
}
if (flag==0)
H[sum]++;

//cout<<sum<<'\n';

if (sum==0&&A[i][0]=='('&&flag2==0)
sum1+=1;
}

sum1=sum1+sum1*(sum1-1);

for(int i=1;i<=300000;i++)
sum1+=H[i]*H[-i];

cout<<sum1<<'\n';

return 0;
}


#include <bits/stdc++.h>
#define fastio()                  \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0)
#define pb push_back
#define show(x) cout << (#x) << " : " << x << endl;
#define ll long long
#define ld long double

#define mp make_pair
#define ff first
#define ss second
#define pii pair<ll, ll>
#define sq(x) ((x) * (x))
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define For(i,a,b) for(int (i)=(a);(i) < (b); ++(i))
const ll MOD = 1000 * 1000 * 1000 + 7;
const ll MOD1 = 998244353;
using namespace std;

ll power(unsigned ll x, unsigned ll y)
{
   unsigned  ll res = 1;

    while (y > 0)
    {

        if (y & 1)
            res = (unsigned long long)(res*x);

        y = y>>1;

        if (x<=100000000)
        x = (unsigned long long)(x*x);

        //cout<<x<<'\n';
    }
    return res;
}

int main()
{

#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif

fastio();

ll n;
cin>>n;

string A[n];

for(int i=0;i<n;i++)
cin>>A[i];

map <ll,ll> H;

ll sum1=0;

for(int i=0;i<n;i++)
{
ll sum=0;
ll flag=0;
for(int j=0;j<A[i].size();j++)
{
//cout<<A[i][j];

if (A[i][j]=='(')
{
sum+=1;
if (A[i][0]==')')
flag=1;
}
else
sum+=-1;

}

if (flag==0)
H[sum]++;

//cout<<sum<<'\n';

if (sum==0&&A[i][0]=='(')
sum1+=1;
}

sum1=sum1+sum1*(sum1-1);

for(int i=1;i<=300000;i++)
sum1+=H[i]*H[-i];

cout<<sum1<<'\n';

return 0;
}


#include <bits/stdc++.h>
#define fastio()                  \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0)
#define pb push_back
#define show(x) cout << (#x) << " : " << x << endl;
#define ll long long
#define ld long double

#define mp make_pair
#define ff first
#define ss second
#define pii pair<ll, ll>
#define sq(x) ((x) * (x))
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define For(i,a,b) for(int (i)=(a);(i) < (b); ++(i))
const ll MOD = 1000 * 1000 * 1000 + 7;
const ll MOD1 = 998244353;
using namespace std;

ll power(unsigned ll x, unsigned ll y)
{
   unsigned  ll res = 1;

    while (y > 0)
    {

        if (y & 1)
            res = (unsigned long long)(res*x);

        y = y>>1;

        if (x<=100000000)
        x = (unsigned long long)(x*x);

        //cout<<x<<'\n';
    }
    return res;
}

int main()
{

#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif

fastio();

ll n;
cin>>n;

string A[n];

for(int i=0;i<n;i++)
cin>>A[i];

map <ll,ll> H;

ll sum1=0;

for(int i=0;i<n;i++)
{
ll sum=0;
for(int j=0;j<A[i].size();j++)
{
//cout<<A[i][j];

if (A[i][j]=='(')
sum+=1;
else
sum+=-1;

}

H[sum]++;

//cout<<sum<<'\n';

if (sum==0&&A[i][0]=='(')
sum1+=1;
}

sum1=sum1+sum1*(sum1-1);

for(int i=1;i<=300000;i++)
sum1+=H[i]*H[-i];

cout<<sum1<<'\n';

return 0;
}


#include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<string>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#define fi first
#define se second
#define ll long long
#define PI acos(-1.0)
#define pb push_back
#define mp make_pair
#define PII pair<string, int>

const int maxx = 1e6 + 100;
const double eps = 1e-10;
const int mod = 10007;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;

using namespace std;

ll n, m, T;
ll l, r;
long long ans;
string s, t;
stack<char> q;
map<int, ll> p;
map<int, ll>::iterator it;

int main(){
	int i, j, k, len;
	while( scanf("%d", &n) == 1){
		getchar();
		p.clear(); m = ans = 0;
		for(i = 0; i < n; i++){
			cin >> s; len = s.size();
			while(!q.empty()) q.pop();
			for(j = 0; j < len; j++){
				if(q.size() == 0) q.push(s[j]);
				else{
					if( q.top() == '(' && s[j] == ')' ) q.pop();
					else q.push(s[j]);
				}
			}
			char now = '#'; bool ok = true; int cnt = 0;
			if(!q.empty()){ now = q.top(); q.pop(); cnt++; }
			else ans++;
			while(!q.empty()){
				if(q.top() == now){ cnt++; q.pop(); }
				else{ ok = false; break; }
			}
			if(ok){
				if(now == '('){ p[cnt] += 1; }
				else if(now == ')'){ p[-cnt] += 1; }
			}
		}
		for(it = p.begin(); it != p.end(); it++){
			k = it -> fi; k = -k;
			if(p[-k] && p[k]){ m += p[k] * p[-k]; p[k] = p[-k] = 0; }
		}
		ans = ans * (ans - 1) + ans + m;
		cout << ans << endl;
	}
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<string>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#define fi first
#define se second
#define ll long long
#define PI acos(-1.0)
#define pb push_back
#define mp make_pair
#define PII pair<string, int>

const int maxx = 1e6 + 100;
const double eps = 1e-10;
const int mod = 10007;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;

using namespace std;

long long n, m, T;
ll l, r;
long long ans;
string s, t;
queue<char> q;
map<ll, ll> p;
map<ll, ll>::iterator it;

int main(){
	ll i, j, k, len;
	while( scanf("%lld", &n) == 1){
		getchar();
		p.clear(); m = ans = 0;
		for(i = 0; i < n; i++){
			cin >> s; len = s.size();
			while(!q.empty()) q.pop();
			for(j = 0; j < len; j++){
				if(q.size() == 0) q.push(s[j]);
				else{
					if( q.front() == '(' && s[j] == ')' ) q.pop();
					else q.push(s[j]);
				}
			}
			char now = '#'; bool ok = true; ll cnt = 0;
			if(!q.empty()){ now = q.front(); q.pop(); cnt++; }
			else ans++;
			while(!q.empty()){
				if(q.front() == now){ cnt++; q.pop(); }
				else{ ok = false; break; }
			}
			if(ok){
				if(now == '('){ p[cnt] += 1; }
				else if(now == ')'){ p[-cnt] += 1; }
			}
		}
		m = 0;
		for(it = p.begin(); it != p.end(); it++){
			k = it -> fi; k = -k;
			if(p[-k] && p[k]){ m += p[k] * p[-k]; p[k] = p[-k] = 0; }
		}
		ans = ans * (ans - 1) + ans + m;
		cout << ans << endl;
	}
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<string>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#define fi first
#define se second
#define ll long long
#define PI acos(-1.0)
#define pb push_back
#define mp make_pair
#define PII pair<string, int>

const int maxx = 1e6 + 100;
const double eps = 1e-10;
const int mod = 10007;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;

using namespace std;

int n, T;
int l, r;
long long ans, m;
string s, t;
queue<char> q;
map<int, ll> p;
map<int, ll>::iterator it;

int main(){
	int i, j, k, len;
	while( scanf("%d", &n) == 1){
		getchar();
		p.clear(); m = ans = 0;
		for(i = 0; i < n; i++){
			cin >> s; len = s.size();
			while(!q.empty()) q.pop();
			for(j = 0; j < len; j++){
				if(q.size() == 0) q.push(s[j]);
				else{
					if( q.front() == '(' && s[j] == ')' ) q.pop();
					else q.push(s[j]);
				}
			}
			char now = '#'; bool ok = true; int cnt = 0;
			if(!q.empty()){ now = q.front(); q.pop(); cnt++; }
			else ans++;
			while(!q.empty()){
				if(q.front() == now){ cnt++; q.pop(); }
				else{ ok = false; break; }
			}
			if(ok){
				if(now == '('){ p[cnt] += 1; }
				else if(now == ')'){ p[-cnt] += 1; }
			}
		}
		m = 0;
		for(it = p.begin(); it != p.end(); it++){
			k = it -> fi;
			if(p.count(-k) ) m += (ll)(it -> se) * p[-k];
		}
		ans = ans * (ans - 1) + ans + m / 2;
		cout << ans << endl;
	}
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<string>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#define fi first
#define se second
#define ll long long
#define PI acos(-1.0)
#define pb push_back
#define mp make_pair
#define PII pair<string, int>

const int maxx = 1e6 + 100;
const double eps = 1e-10;
const int mod = 10007;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;

using namespace std;

long long n, m, T, l, r;
long long ans;
char s[maxx + 5];
queue<char> q;
map<ll, ll> p;
map<ll, ll>::iterator it;

int main(){
	int i, j, k, len;
	while( scanf("%lld", &n) == 1){
		p.clear(); m = ans = 0;
		for(i = 0; i < n; i++){
			scanf(" %s", s); len = strlen(s);
			while(!q.empty()) q.pop();
			for(j = 0; j < len; j++){
				if(!q.empty()){
					if( q.front() == '(' && s[j] == ')' ) q.pop();
					else q.push(s[j]);
				}else q.push(s[j]);
			}
			if(q.empty()) ans++;
			else{
				l = q.size(); r = 0;
				while(!q.empty()){
					if(q.front() == '(') r++;
					q.pop();
				}
				if(r == 0) p[l]++;
				else if(r == l) p[-l]++;
			}
/*			char now = '#'; bool ok = true; int cnt = 0;
			if(!q.empty()){ now = q.front(); q.pop(); cnt++; }
			else ans++;
			while(!q.empty()){
				if(q.front() == now){ cnt++; q.pop(); }
				else{ ok = false; break; }
			}
			if(ok){
				if(now == '('){ p[cnt] += 1; }
				else if(now == ')'){ p[-cnt] += 1; }
			} */
		}
		m = 0;
		for(it = p.begin(); it != p.end(); it++){
			k = it -> fi; k = -k;
			if(p[k]) m += p[k] * p[-k];
		}
		ans = ans * (ans - 1) + ans + m / 2;
		cout << ans << endl;
	}
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<string>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#define fi first
#define se second
#define ll long long
#define PI acos(-1.0)
#define pb push_back
#define mp make_pair
#define PII pair<string, int>

const int maxx = 1e6 + 100;
const double eps = 1e-10;
const int mod = 10007;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;

using namespace std;

long long n, m, T;
ll l, r;
long long ans;
string s, t;
queue<char> q;
map<ll, ll> p;
map<ll, ll>::iterator it;

int main(){
	ll i, j, k, len;
	while( scanf("%lld", &n) == 1){
		getchar();
		p.clear(); m = ans = 0;
		for(i = 0; i < n; i++){
			cin >> s; len = s.size();
			while(!q.empty()) q.pop();
			for(j = 0; j < len; j++){
				if(q.empty()) q.push(s[j]);
				else{
					if( q.front() == '(' && s[j] == ')' ){ q.pop(); j++; }
					else q.push(s[j]);
				}
			}
			char now = '#'; bool ok = true; ll cnt = 0;
			if(!q.empty()){ now = q.front(); q.pop(); cnt++; }
			else ans++;
			while(!q.empty()){
				if(q.front() == now){ cnt++; q.pop(); }
				else{ ok = false; break; }
			}
			if(ok){
				if(now == '('){ p[cnt] += 1; }
				else if(now == ')'){ p[-cnt] += 1; }
			}
		}
		for(it = p.begin(); it != p.end(); it++){
			k = it -> fi;
			if(p[-k] && p[k]){ m += p[k] * p[-k]; p[k] = p[-k] = 0; }
		}
		ans = ans * (ans - 1) + ans + m;
		cout << ans << endl;
	}
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<string>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#define fi first
#define se second
#define ll long long
#define PI acos(-1.0)
#define pb push_back
#define mp make_pair
#define PII pair<string, int>

const int maxx = 1e6 + 100;
const double eps = 1e-10;
const int mod = 10007;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;

using namespace std;

int n, T;
int l, r;
long long ans, m;
string s, t;
queue<char> q;
map<int, ll> p;
map<int, ll>::iterator it;

int main(){
	int i, j, k, len;
	while( scanf("%d", &n) == 1){
		getchar();
		p.clear(); m = ans = 0;
		for(i = 0; i < n; i++){
			cin >> s; len = s.size();
			while(!q.empty()) q.pop();
			for(j = 0; j < len; j++){
				if(q.size() == 0) q.push(s[j]);
				else{
					if( q.front() == '(' && s[j] == ')' ) q.pop();
					else q.push(s[j]);
				}
			}
			char now = '#'; bool ok = true; int cnt = 0;
			if(!q.empty()){ now = q.front(); q.pop(); cnt++; }
			else ans++;
			while(!q.empty()){
				if(q.front() == now){ cnt++; q.pop(); }
				else{ ok = false; break; }
			}
			if(ok){
				if(now == '('){ p[cnt] += 1; }
				else if(now == ')'){ p[-cnt] += 1; }
			}
		}
		m = 0;
		for(it = p.begin(); it != p.end(); it++){
			k = it -> fi; k = -k;
			if(p[-k] && p[k]){ m += p[k] * p[-k]; p[k] = p[-k] = 0; }
		}
		ans = ans * (ans - 1) + ans + m;
		cout << ans << endl;
	}
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<string>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#define fi first
#define se second
#define ll long long
#define PI acos(-1.0)
#define pb push_back
#define mp make_pair
#define PII pair<string, int>

const int maxx = 1e6 + 100;
const double eps = 1e-10;
const int mod = 10007;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;

using namespace std;

int n, m, T;
int l, r;
long long ans;
string s, t;
queue<char> q;
map<int, int> p;
map<int, int>::iterator it;

int main(){
	int i, j, k, len;
	while( scanf("%d", &n) == 1){
		getchar();
		p.clear(); m = ans = 0;
		for(i = 0; i < n; i++){
			cin >> s; len = s.size();
			while(!q.empty()) q.pop();
			for(j = 0; j < len; j++){
				if(q.size() == 0) q.push(s[j]);
				else{
					if( q.front() == '(' && s[j] == ')' ) q.pop();
					else q.push(s[j]);
				}
			}
//			cout << q.size() << endl;
			char now = '#'; bool ok = true; int cnt = 0;
			if(!q.empty()){ now = q.front(); q.pop(); cnt++; }
			else ans++;
//			cout << q.size() << " " << cnt << " " << now << endl;
			while(!q.empty()){
				if(q.front() == now){ cnt++; q.pop(); }
				else{ ok = false; break; }
			}
//			cout << cnt << endl;
			if(ok){
				if(now == '('){ p[cnt] += 1; }
				else if(now == ')'){ p[-cnt] += 1; }
			}
//			cout << ans << endl;
		}
		m = 0;
		for(it = p.begin(); it != p.end(); it++){
			k = it -> fi; k = -k;
//			cout << " k = " << k << endl;
//			cout << " p[k] = " << p[k] << endl;
			if(p[-k] && p[k]){ m += max(p[k], p[-k]); p[k] = p[-k] = 0; }
		}
		ans = ans * (ans - 1) + ans + m;
		cout << ans << endl;
	}
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<string>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#define fi first
#define se second
#define ll long long
#define PI acos(-1.0)
#define pb push_back
#define mp make_pair
#define PII pair<string, int>

const int maxx = 1e6 + 100;
const double eps = 1e-10;
const int mod = 10007;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;

using namespace std;

long long n, m, T, l, r;
long long ans;
char s[maxx + 5];
map<ll, ll> p;
map<ll, ll>::iterator it;

int main(){
	ll i, j, k, len;
	while( scanf("%lld", &n) == 1){
		m = ans = 0;
		for(i = 0; i < n; i++){
			scanf(" %s", s); len = strlen(s);
			queue<char> q;
			for(j = 0; j < len; j++){
				if(!q.empty()){
					if( q.front() == '(' && s[j] == ')' ) q.pop();
					else q.push(s[j]);
				}else q.push(s[j]);
			}
			if(q.empty()) ans++;
			else{
				l = q.size(); r = 0;
				while(!q.empty()){
					if(q.front() == '(') r++;
					q.pop();
				}
				if(r == 0) p[l]++;
				else if(r == l) p[-l]++;
			}
		}
/*			char now = '#'; bool ok = true; int cnt = 0;
			if(!q.empty()){ now = q.front(); q.pop(); cnt++; }
			else ans++;
			while(!q.empty()){
				if(q.front() == now){ cnt++; q.pop(); }
				else{ ok = false; break; }
			}
			if(ok){
				if(now == '('){ p[cnt] += 1; }
				else if(now == ')'){ p[-cnt] += 1; }
			} */
		m = 0;
		for(it = p.begin(); it != p.end(); it++){
			k = it -> fi;
			if(p.count(-k)) m += p[k] * p[-k];
		}
		ans = ans * ans + m / 2;
		cout << ans << endl;
	}
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<string>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#define fi first
#define se second
#define ll long long
#define PI acos(-1.0)
#define pb push_back
#define mp make_pair
#define PII pair<string, int>

const int maxx = 1e6 + 100;
const double eps = 1e-10;
const int mod = 10007;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;

using namespace std;

long long n, m, T;
ll l, r;
long long ans;
string s, t;
queue<char> q;
map<ll, ll> p;
map<ll, ll>::iterator it;

int main(){
	ll i, j, k, len;
	while( scanf("%lld", &n) == 1){
		getchar();
		p.clear(); m = ans = 0;
		for(i = 0; i < n; i++){
			cin >> s; len = s.size();
			while(!q.empty()) q.pop();
			for(j = 0; j < len; j++){
				if(q.empty()) q.push(s[j]);
				else{
					if( q.front() == '(' && s[j] == ')' ) q.pop();
					else q.push(s[j]);
				}
			}
			if(q.empty()) ans++;
			else{
				l = q.size(); r = 0;
				while(!q.empty()){
					if(q.front() == '(') r++;
					q.pop();
				}
				if(r == 0) p[l]++;
				else if(r == l) p[-l]++;
			}
		}
/*			char now = '#'; bool ok = true; ll cnt = 0;
			if(!q.empty()){ now = q.front(); q.pop(); cnt++; }
			else ans++;
			while(!q.empty()){
				if(q.front() == now){ cnt++; q.pop(); }
				else{ ok = false; break; }
			}
			if(ok){
				if(now == '('){ p[cnt] += 1; }
				else if(now == ')'){ p[-cnt] += 1; }
			}
		} */
		for(it = p.begin(); it != p.end(); it++){
			k = it -> fi;
			if(p.count(-k) ) m += (it -> se) * p[-k];
//			if(p[-k] && p[k]){ m += p[k] * p[-k]; p[k] = p[-k] = 0; }
		}
		ans = ans * (ans - 1) + ans + m / 2;
		cout << ans << endl;
	}
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<string>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#define fi first
#define se second
#define ll long long
#define PI acos(-1.0)
#define pb push_back
#define mp make_pair
#define PII pair<string, int>

const int maxx = 1e6 + 100;
const double eps = 1e-10;
const int mod = 10007;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;

using namespace std;

int n, T;
int l, r;
long long ans, m;
string s, t;
queue<char> q;
map<int, ll> p;
map<int, ll>::iterator it;

int main(){
	int i, j, k, len;
	while( scanf("%d", &n) == 1){
		getchar();
		p.clear(); m = ans = 0;
		for(i = 0; i < n; i++){
			cin >> s; len = s.size();
			while(!q.empty()) q.pop();
			for(j = 0; j < len; j++){
				if(q.size() == 0) q.push(s[j]);
				else{
					if( q.front() == '(' && s[j] == ')' ) q.pop();
					else q.push(s[j]);
				}
			}
			char now = '#'; bool ok = true; int cnt = 0;
			if(!q.empty()){ now = q.front(); q.pop(); cnt++; }
			else ans++;
			while(!q.empty()){
				if(q.front() == now){ cnt++; q.pop(); }
				else{ ok = false; break; }
			}
			if(ok){
				if(now == '('){ p[cnt] += 1; }
				else if(now == ')'){ p[-cnt] += 1; }
			}
		}
		m = 0;
		for(it = p.begin(); it != p.end(); it++){
			k = it -> fi; k = -k;
			if(p[k]) m += p[k] * p[-k];
		}
		ans = ans * (ans - 1) + ans + m / 2;
		cout << ans << endl;
	}
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<string>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#define fi first
#define se second
#define ll long long
#define PI acos(-1.0)
#define pb push_back
#define mp make_pair
#define PII pair<string, int>

const int maxx = 1e6 + 100;
const double eps = 1e-10;
const int mod = 10007;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;

using namespace std;

long long n, m, T;
ll l, r;
long long ans;
string s, t;
stack<char> q;
map<ll, ll> p;
map<ll, ll>::iterator it;

int main(){
	ll i, j, k, len;
	while( scanf("%lld", &n) == 1){
		getchar();
		p.clear(); m = ans = 0;
		for(i = 0; i < n; i++){
			cin >> s; len = s.size();
			while(!q.empty()) q.top();
			for(j = 0; j < len; j++){
				if(q.empty()) q.push(s[j]);
				else{
					if( q.top() == '(' && s[j] == ')' ) q.pop();
					else q.push(s[j]);
				}
			}
			if(q.empty()) ans++;
			else{
				l = q.size(); r = 0;
				while(!q.empty()){
					if(q.top() == '(') r++;
					q.pop();
				}
				if(r == 0) p[l]++;
				else if(r == l) p[-l]++;
			}
		}
/*			char now = '#'; bool ok = true; ll cnt = 0;
			if(!q.empty()){ now = q.front(); q.pop(); cnt++; }
			else ans++;
			while(!q.empty()){
				if(q.front() == now){ cnt++; q.pop(); }
				else{ ok = false; break; }
			}
			if(ok){
				if(now == '('){ p[cnt] += 1; }
				else if(now == ')'){ p[-cnt] += 1; }
			}
		} */
		for(it = p.begin(); it != p.end(); it++){
			k = it -> fi;
			if(p.count(-k) ) m += (it -> se) * p[-k];
//			if(p[-k] && p[k]){ m += p[k] * p[-k]; p[k] = p[-k] = 0; }
		}
		ans = ans * (ans - 1) + ans + m / 2;
		cout << ans << endl;
	}
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<string>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#define fi first
#define se second
#define ll long long
#define PI acos(-1.0)
#define pb push_back
#define mp make_pair
#define PII pair<string, int>

const int maxx = 1e6 + 100;
const double eps = 1e-10;
const int mod = 10007;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;

using namespace std;

long long n, m, T, l, r;
long long ans;
string s, t;
queue<char> q;
map<ll, ll> p;
map<ll, ll>::iterator it;

int main(){
	int i, j, k, len;
	while( scanf("%lld", &n) == 1){
		getchar();
		p.clear(); m = ans = 0;
		for(i = 0; i < n; i++){
			cin >> s; len = s.size();
			while(!q.empty()) q.pop();
			for(j = 0; j < len; j++){
				if(!q.empty()){
					if( q.front() == '(' && s[j] == ')' ) q.pop();
					else q.push(s[j]);
				}else q.push(s[j]);
			}
			if(q.empty()) ans++;
			else{
				l = q.size(); r = 0;
				while(!q.empty()){
					if(q.front() == '(') r++;
					q.pop();
				}
				if(r == 0) p[l]++;
				else if(r == l) p[-l]++;
			}
/*			char now = '#'; bool ok = true; int cnt = 0;
			if(!q.empty()){ now = q.front(); q.pop(); cnt++; }
			else ans++;
			while(!q.empty()){
				if(q.front() == now){ cnt++; q.pop(); }
				else{ ok = false; break; }
			}
			if(ok){
				if(now == '('){ p[cnt] += 1; }
				else if(now == ')'){ p[-cnt] += 1; }
			} */
		}
		m = 0;
		for(it = p.begin(); it != p.end(); it++){
			k = it -> fi; k = -k;
			if(p[k]) m += p[k] * p[-k];
		}
		ans = ans * (ans - 1) + ans + m / 2;
		cout << ans << endl;
	}
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<string>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#define fi first
#define se second
#define ll long long
#define PI acos(-1.0)
#define pb push_back
#define mp make_pair
#define PII pair<string, int>

const int maxx = 1e6 + 100;
const double eps = 1e-10;
const int mod = 10007;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;

using namespace std;

int n, T;
int l, r;
long long ans, m;
string s, t;
queue<char> q;
map<ll, ll> p;
map<ll, ll>::iterator it;

int main(){
	int i, j, k, len;
	while( scanf("%d", &n) == 1){
		getchar();
		p.clear(); m = ans = 0;
		for(i = 0; i < n; i++){
			cin >> s; len = s.size();
			while(!q.empty()) q.pop();
			for(j = 0; j < len; j++){
				if(q.size() == 0) q.push(s[j]);
				else{
					if( q.front() == '(' && s[j] == ')' ) q.pop();
					else q.push(s[j]);
				}
			}
			char now = '#'; bool ok = true; int cnt = 0;
			if(!q.empty()){ now = q.front(); q.pop(); cnt++; }
			else ans++;
			while(!q.empty()){
				if(q.front() == now){ cnt++; q.pop(); }
				else{ ok = false; break; }
			}
			if(ok){
				if(now == '('){ p[cnt] += 1; }
				else if(now == ')'){ p[-cnt] += 1; }
			}
		}
		m = 0;
		for(it = p.begin(); it != p.end(); it++){
			k = it -> fi; k = -k;
			if(p[k]) m += p[k] * p[-k];
		}
		ans = ans * (ans - 1) + ans + m;
		cout << ans << endl;
	}
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<string>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#define fi first
#define se second
#define ll long long
#define PI acos(-1.0)
#define pb push_back
#define mp make_pair
#define PII pair<string, int>

const int maxx = 1e6 + 100;
const double eps = 1e-10;
const int mod = 10007;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;

using namespace std;

int n, T;
int l, r;
long long ans, m;
string s, t;
queue<char> q;
map<int, ll> p;
map<int, ll>::iterator it;

int main(){
	int i, j, k, len;
	while( scanf("%d", &n) == 1){
		getchar();
		p.clear(); m = ans = 0;
		for(i = 0; i < n; i++){
			cin >> s; len = s.size();
			while(!q.empty()) q.pop();
			for(j = 0; j < len; j++){
				if(q.size() == 0) q.push(s[j]);
				else{
					if( q.front() == '(' && s[j] == ')' ) q.pop();
					else q.push(s[j]);
				}
			}
			if(q.empty()) ans++;
			else{
				l = q.size(); r = 0;
				while(!q.empty()){
					if(q.front() == '(') r++;
					q.pop();
				}
				if(r == 0) p[l]++;
				else if(r == l) p[-l]++;
			}
/*			char now = '#'; bool ok = true; int cnt = 0;
			if(!q.empty()){ now = q.front(); q.pop(); cnt++; }
			else ans++;
			while(!q.empty()){
				if(q.front() == now){ cnt++; q.pop(); }
				else{ ok = false; break; }
			}
			if(ok){
				if(now == '('){ p[cnt] += 1; }
				else if(now == ')'){ p[-cnt] += 1; }
			} */
		}
		m = 0;
		for(it = p.begin(); it != p.end(); it++){
			k = it -> fi; k = -k;
			if(p[k]) m += p[k] * p[-k];
		}
		ans = ans * (ans - 1) + ans + m / 2;
		cout << ans << endl;
	}
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<string>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#define fi first
#define se second
#define ll long long
#define PI acos(-1.0)
#define pb push_back
#define mp make_pair
#define PII pair<string, int>

const int maxx = 1e6 + 100;
const double eps = 1e-10;
const int mod = 10007;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;

using namespace std;

int n, m, T;
int l, r, ans;
string s, t;
queue<char> q;
map<int, int> p;
map<int, int>::iterator it;

int main(){
	int i, j, k, len;
	while( scanf("%d", &n) == 1){
		getchar();
		p.clear(); m = ans = 0;
		for(i = 0; i < n; i++){
			cin >> s; len = s.size();
			while(!q.empty()) q.pop();
			for(j = 0; j < len; j++){
				if(q.size() == 0) q.push(s[j]);
				else{
					if( q.front() == '(' && s[j] == ')' ) q.pop();
					else q.push(s[j]);
				}
			}
//			cout << q.size() << endl;
			char now = '#'; bool ok = true; int cnt = 0;
			if(!q.empty()){ now = q.front(); q.pop(); cnt++; }
			else{ m++; ans += m; }
//			cout << q.size() << " " << cnt << " " << now << endl;
			while(!q.empty()){
				if(q.front() == now){ cnt++; q.pop(); }
				else{ ok = false; break; }
			}
//			cout << cnt << endl;
			if(ok){
				if(now == '('){ p[cnt] += 1; }
				else if(now == ')'){ p[-cnt] += 1; }
			}
//			cout << ans << endl;
		}
		m = 0;
		for(it = p.begin(); it != p.end(); it++){
			k = it -> fi; k = -k;
//			cout << " k = " << k << endl;
//			cout << " p[k] = " << p[k] << endl;
			if(p[-k] && p[k]){ m += max(p[k], p[-k]); p[k] = p[-k] = 0; }
		}
		cout << ans  + m << endl;
	}
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<string>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#define fi first
#define se second
#define ll long long
#define PI acos(-1.0)
#define pb push_back
#define mp make_pair
#define PII pair<string, int>

const int maxx = 1e6 + 100;
const double eps = 1e-10;
const int mod = 10007;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;

using namespace std;

int n, T;
int l, r;
long long ans, m;
string s, t;
queue<char> q;
map<int, ll> p;
map<int, ll>::iterator it;

int main(){
	int i, j, k, len;
	while( scanf("%d", &n) == 1){
		getchar();
		p.clear(); m = ans = 0;
		for(i = 0; i < n; i++){
			cin >> s; len = s.size();
			while(!q.empty()) q.pop();
			for(j = 0; j < len; j++){
				if(!q.empty()){
					if( q.front() == '(' && s[j] == ')' ) q.pop();
					else q.push(s[j]);
				}else q.push(s[j]);
			}
			if(q.empty()) ans++;
			else{
				l = q.size(); r = 0;
				while(!q.empty()){
					if(q.front() == '(') r++;
					q.pop();
				}
				if(r == 0) p[l]++;
				else if(r == l) p[-l]++;
			}
/*			char now = '#'; bool ok = true; int cnt = 0;
			if(!q.empty()){ now = q.front(); q.pop(); cnt++; }
			else ans++;
			while(!q.empty()){
				if(q.front() == now){ cnt++; q.pop(); }
				else{ ok = false; break; }
			}
			if(ok){
				if(now == '('){ p[cnt] += 1; }
				else if(now == ')'){ p[-cnt] += 1; }
			} */
		}
		m = 0;
		for(it = p.begin(); it != p.end(); it++){
			k = it -> fi; k = -k;
			if(p[k]) m += p[k] * p[-k];
		}
		ans = ans * (ans - 1) + ans + m / 2;
		cout << ans << endl;
	}
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<string>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#define fi first
#define se second
#define ll long long
#define PI acos(-1.0)
#define pb push_back
#define mp make_pair
#define PII pair<string, int>

const int maxx = 1e6 + 100;
const double eps = 1e-10;
const int mod = 10007;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;

using namespace std;

long long n, m, T, l, r;
long long ans;
char s[maxx + 5];
map<ll, ll> p;
map<ll, ll>::iterator it;

int main(){
	int i, j, k, len;
	while( scanf("%lld", &n) == 1){
		m = ans = 0;
		for(i = 0; i < n; i++){
			scanf(" %s", s); len = strlen(s);
			queue<char> q;
			for(j = 0; j < len; j++){
				if(!q.empty()){
					if( q.front() == '(' && s[j] == ')' ) q.pop();
					else q.push(s[j]);
				}else q.push(s[j]);
			}
			if(q.empty()) ans++;
			else{
				l = q.size(); r = 0;
				while(!q.empty()){
					if(q.front() == '(') r++;
					q.pop();
				}
				if(r == 0) p[l]++;
				else if(r == l) p[-l]++;
			}
		}
/*			char now = '#'; bool ok = true; int cnt = 0;
			if(!q.empty()){ now = q.front(); q.pop(); cnt++; }
			else ans++;
			while(!q.empty()){
				if(q.front() == now){ cnt++; q.pop(); }
				else{ ok = false; break; }
			}
			if(ok){
				if(now == '('){ p[cnt] += 1; }
				else if(now == ')'){ p[-cnt] += 1; }
			} */
		m = 0;
		for(it = p.begin(); it != p.end(); it++){
			k = it -> fi; k = -k;
			if(p[k]) m += p[k] * p[-k];
		}
		ans = ans * ans + m / 2;
		cout << ans << endl;
	}
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<string>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#define fi first
#define se second
#define ll long long
#define PI acos(-1.0)
#define pb push_back
#define mp make_pair
#define PII pair<string, int>

const int maxx = 1e6 + 100;
const double eps = 1e-10;
const int mod = 10007;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;

using namespace std;

long long n, m, T;
ll l, r;
long long ans;
string s, t;
queue<char> q;
map<ll, ll> p;
map<ll, ll>::iterator it;

int main(){
	ll i, j, k, len;
	while( scanf("%lld", &n) == 1){
		getchar();
		p.clear(); m = ans = 0;
		for(i = 0; i < n; i++){
			cin >> s; len = s.size();
			while(!q.empty()) q.pop();
			for(j = 0; j < len; j++){
				if(q.empty()) q.push(s[j]);
				else{
					if( q.front() == '(' && s[j] == ')' ) q.pop();
					else q.push(s[j]);
				}
			}
			char now = '#'; bool ok = true; ll cnt = 0;
			if(!q.empty()){ now = q.front(); q.pop(); cnt++; }
			else ans++;
			while(!q.empty()){
				if(q.front() == now){ cnt++; q.pop(); }
				else{ ok = false; break; }
			}
			if(ok){
				if(now == '('){ p[cnt] += 1; }
				else if(now == ')'){ p[-cnt] += 1; }
			}
		}
		m = 0;
		for(it = p.begin(); it != p.end(); it++){
			k = it -> fi;
			if(p[-k] && p[k]){ m += p[k] * p[-k]; p[k] = p[-k] = 0; }
		}
		ans = ans * (ans - 1) + ans + m;
		cout << ans << endl;
	}
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<string>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#define fi first
#define se second
#define ll long long
#define PI acos(-1.0)
#define pb push_back
#define mp make_pair
#define PII pair<string, int>

const int maxx = 1e6 + 100;
const double eps = 1e-10;
const int mod = 10007;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;

using namespace std;

long long n, m, T;
ll l, r;
long long ans;
string s, t;
queue<char> q;
map<ll, ll> p;
map<ll, ll>::iterator it;

int main(){
	ll i, j, k, len;
	while( scanf("%lld", &n) == 1){
		getchar();
		p.clear(); m = ans = 0;
		for(i = 0; i < n; i++){
			cin >> s; len = s.size();
			while(!q.empty()) q.pop();
			for(j = 0; j < len; j++){
				if(q.empty()) q.push(s[j]);
				else{
					if( q.front() == '(' && s[j] == ')' ) q.pop();
					else q.push(s[j]);
				}
			}
			if(q.empty()) ans++;
			else{
				l = q.size(); r = 0;
				while(!q.empty()){
					if(q.front() == '(') r++;
					q.pop();
				}
				if(r == 0) p[l]++;
				else if(r == l) p[-l]++;
			}
		}
/*			char now = '#'; bool ok = true; ll cnt = 0;
			if(!q.empty()){ now = q.front(); q.pop(); cnt++; }
			else ans++;
			while(!q.empty()){
				if(q.front() == now){ cnt++; q.pop(); }
				else{ ok = false; break; }
			}
			if(ok){
				if(now == '('){ p[cnt] += 1; }
				else if(now == ')'){ p[-cnt] += 1; }
			}
		} */
		for(it = p.begin(); it != p.end(); it++){
			k = it -> fi;
			if(p.count(-k) ) m += p[k] * p[-k];
//			if(p[-k] && p[k]){ m += p[k] * p[-k]; p[k] = p[-k] = 0; }
		}
		ans = ans * (ans - 1) + ans + m / 2;
		cout << ans << endl;
	}
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<string>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#define fi first
#define se second
#define ll long long
#define PI acos(-1.0)
#define pb push_back
#define mp make_pair
#define PII pair<string, int>

const int maxx = 1e6 + 100;
const double eps = 1e-10;
const int mod = 10007;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;

using namespace std;

long long n, m, T, l, r;
long long ans;
char s[maxx + 5];
map<ll, ll> p;
map<ll, ll>::iterator it;

int main(){
	int i, j, k, len;
	while( scanf("%lld", &n) == 1){
		p.clear(); m = ans = 0;
		for(i = 0; i < n; i++){
			scanf(" %s", s); len = strlen(s);
			queue<char> q;
			for(j = 0; j < len; j++){
				if(!q.empty()){
					if( q.front() == '(' && s[j] == ')' ) q.pop();
					else q.push(s[j]);
				}else q.push(s[j]);
			}
			if(q.empty()) ans++;
			else{
				l = q.size(); r = 0;
				while(!q.empty()){
					if(q.front() == '(') r++;
					q.pop();
				}
				if(r == 0) p[l]++;
				else if(r == l) p[-l]++;
			}
/*			char now = '#'; bool ok = true; int cnt = 0;
			if(!q.empty()){ now = q.front(); q.pop(); cnt++; }
			else ans++;
			while(!q.empty()){
				if(q.front() == now){ cnt++; q.pop(); }
				else{ ok = false; break; }
			}
			if(ok){
				if(now == '('){ p[cnt] += 1; }
				else if(now == ')'){ p[-cnt] += 1; }
			} */
		}
		m = 0;
		for(it = p.begin(); it != p.end(); it++){
			k = it -> fi; k = -k;
			if(p[k]) m += p[k] * p[-k];
		}
		ans = ans * (ans - 1) + ans + m / 2;
		cout << ans << endl;
	}
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<string>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#define fi first
#define se second
#define ll long long
#define PI acos(-1.0)
#define pb push_back
#define mp make_pair
#define PII pair<string, int>

const int maxx = 1e6 + 100;
const double eps = 1e-10;
const int mod = 10007;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;

using namespace std;

int n, T;
int l, r;
long long ans, m;
string s, t;
queue<char> q;
map<int, ll> p;
map<int, ll>::iterator it;

int main(){
	int i, j, k, len;
	while( scanf("%d", &n) == 1){
		getchar();
		p.clear(); m = ans = 0;
		for(i = 0; i < n; i++){
			cin >> s; len = s.size();
			while(!q.empty()) q.pop();
			for(j = 0; j < len; j++){
				if(q.size() == 0) q.push(s[j]);
				else{
					if( q.front() == '(' && s[j] == ')' ) q.pop();
					else q.push(s[j]);
				}
			}
			char now = '#'; bool ok = true; int cnt = 0;
			if(!q.empty()){ now = q.front(); q.pop(); cnt++; }
			else ans++;
			while(!q.empty()){
				if(q.front() == now){ cnt++; q.pop(); }
				else{ ok = false; break; }
			}
			if(ok){
				if(now == '('){ p[cnt] += 1; }
				else if(now == ')'){ p[-cnt] += 1; }
			}
		}
		m = 0;
		for(it = p.begin(); it != p.end(); it++){
			k = it -> fi; k = -k;
			if(p[k]) m += p[k] * p[-k];
		}
		ans = ans * (ans - 1) + ans + m;
		cout << ans << endl;
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<map>
#include<stack>
#include<algorithm>
#define ll long long int
#define MAX 300010
using namespace std;
map<ll,ll>mp;
char str[MAX];
int main()
{
    ll i,n,len,m,k;
    ll counts,ans,sum;
    scanf("%lld",&n);
    getchar();
    m=0;
    ans=0;
    while(n--)
    {
        stack<char>s;
        scanf("%s",str);
        len=strlen(str);
        for(i=0; i<len; i++)
        {
            if(!s.empty())
            {
                if(s.top()=='('&&str[i]==')')
                {
                    s.pop();
                }
                else
                {
                    s.push(str[i]);
                }
            }
            else
            {
                s.push(str[i]);
            }
        }
        if(s.empty())///自身完全匹配
        {
            m++;
        }
        else
        {
            counts=s.size();
            sum=0;
            while(!s.empty())
            {
                if(s.top()=='(')
                {
                    sum++;///记录左括号个数
                }
                s.pop();
            }
            if(sum==0)///剩下的都是右括号
            {
                mp[-counts]++;///负数代表右括号
            }
            else if(sum==counts)///栈里剩下的都是左括号
            {
                mp[counts]++;///正数代表左括号
            }
        }
    }
    map<ll,ll>::iterator it;
    for(it=mp.begin(); it!=mp.end(); it++)
    {
        k=it->first;
        if(mp.count(-k))///只有存在左括号数等于右括号数的才存在完美匹配
        {
            ans+=(ll)(it->second)*mp[-k];
        }
    }
    printf("%lld\n",ans/2+m*m);
    return 0;
}


#include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<string>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#define fi first
#define se second
#define ll long long
#define PI acos(-1.0)
#define pb push_back
#define mp make_pair
#define PII pair<string, int>

const int maxx = 1e6 + 100;
const double eps = 1e-10;
const int mod = 10007;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;

using namespace std;

int n, T;
int l, r;
long long ans, m;
string s, t;
queue<char> q;
map<int, int> p;
map<int, int>::iterator it;

int main(){
	int i, j, k, len;
	while( scanf("%d", &n) == 1){
		getchar();
		p.clear(); m = ans = 0;
		for(i = 0; i < n; i++){
			cin >> s; len = s.size();
			while(!q.empty()) q.pop();
			for(j = 0; j < len; j++){
				if(q.size() == 0) q.push(s[j]);
				else{
					if( q.front() == '(' && s[j] == ')' ) q.pop();
					else q.push(s[j]);
				}
			}
			char now = '#'; bool ok = true; int cnt = 0;
			if(!q.empty()){ now = q.front(); q.pop(); cnt++; }
			else ans++;
			while(!q.empty()){
				if(q.front() == now){ cnt++; q.pop(); }
				else{ ok = false; break; }
			}
			if(ok){
				if(now == '('){ p[cnt] += 1; }
				else if(now == ')'){ p[-cnt] += 1; }
			}
		}
		m = 0;
		for(it = p.begin(); it != p.end(); it++){
			k = it -> fi; k = -k;
			if(p[-k] && p[k]){ m += p[k] * p[-k]; p[k] = p[-k] = 0; }
		}
		ans = ans * (ans - 1) + ans + m;
		cout << ans << endl;
	}
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<string>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#define fi first
#define se second
#define ll long long
#define PI acos(-1.0)
#define pb push_back
#define mp make_pair
#define PII pair<string, int>

const int maxx = 1e6 + 100;
const double eps = 1e-10;
const int mod = 10007;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;

using namespace std;

long long n, m, T, l, r;
long long ans;
char s[maxx + 5];
map<ll, ll> p;
map<ll, ll>::iterator it;

int main(){
	int i, j, k, len;
	while( scanf("%lld", &n) == 1){
		m = ans = 0;
		for(i = 0; i < n; i++){
			scanf(" %s", s); len = strlen(s);
			queue<char> q;
			for(j = 0; j < len; j++){
				if(!q.empty()){
					if( q.front() == '(' && s[j] == ')' ) q.pop();
					else q.push(s[j]);
				}else q.push(s[j]);
			}
			if(q.empty()) ans++;
			else{
				l = q.size(); r = 0;
				while(!q.empty()){
					if(q.front() == '(') r++;
					q.pop();
				}
				if(r == 0) p[l]++;
				else if(r == l) p[-l]++;
			}
/*			char now = '#'; bool ok = true; int cnt = 0;
			if(!q.empty()){ now = q.front(); q.pop(); cnt++; }
			else ans++;
			while(!q.empty()){
				if(q.front() == now){ cnt++; q.pop(); }
				else{ ok = false; break; }
			}
			if(ok){
				if(now == '('){ p[cnt] += 1; }
				else if(now == ')'){ p[-cnt] += 1; }
			} */
		}
		m = 0;
		for(it = p.begin(); it != p.end(); it++){
			k = it -> fi; k = -k;
			if(p[k]) m += p[k] * p[-k];
		}
		ans = ans * (ans - 1) + ans + m / 2;
		cout << ans << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define ll long long int
#define ld long double
#define vi vector<ll>
#define vii vector<pair<ll,ll> >
#define pll pair<ll,ll>
#define f(i,a,b) for(ll i=a;i<b;i++)
#define rev(i,a,b) for(ll i=a;i>=b;i--)
#define all(c) c.begin(),c.end()
#define sz(c) (ll)c.size()
#define lcm(a,b) (a*b)/(__gcd(a,b))
#define init(x,a) memset(x,a,sizeof(x))
#define lbd(a,val) lower_bound(all(a),val)
#define ubd(a,val) upper_bound(all(a),val)
#define pb push_back
#define mp1 make_pair
#define F first
#define S second
#define en '\n'
#define mod (ll)1000000007
#define M (ll)998244353
#define N 100005
#define PI 3.14159265358979323846

bool compare(string s1,string s2)
{
    ll i,j,k=0,l=0;
    ll m= s1.length();
    for(i=0;i<m;i++)
    {
        if(s1[i]=='1')
        {k++;}
        if(s2[i]=='1')
        {l++;}
    }
    return k>l;

}

int main()
{
    //IOS;
    ll t=1;
    //cin >> t;
    while(t--)
    {
        ll i,j,k=0,l=0;
        ll n,m=0;
        cin >> n;
        string s[n];
        ll a[n]={0};
        for(i=0;i<n;i++)
        {
            cin >> s[i];
            k=0;l=0;
            for(j=0;j<s[i].length();j++)
            {
                if(s[i][j]=='(')
                {
                    k++;
                }
                if(s[i][j]==')')
                {
                    k--;
                }
            }
            a[i]=k;
            if(k==0)
            {m++;}
        }
        sort(a,a+n);
        for(i=0;i<n;i++)
        {
            for(j=n-1;j>=i && a[j]+a[i]>=0;j--)
            {
                if(i!=j)
                {
                    if(a[j]+a[i] ==0 && a[i]!=0)
                    {
                        m++;
                    }
                    else if(a[i]==0 && a[j]==0)
                    {
                        m+=2;
                    }
                }
                // && abs(a[i])<=abs(a[j])
                if(!(a[i]*a[j]<=0))
                {
                    break;
                }
            }
        }
        cout << m << endl;
    }
}


#include <bits/stdc++.h>
using namespace std;

#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define ll long long int
#define ld long double
#define vi vector<ll>
#define vii vector<pair<ll,ll> >
#define pll pair<ll,ll>
#define f(i,a,b) for(ll i=a;i<b;i++)
#define rev(i,a,b) for(ll i=a;i>=b;i--)
#define all(c) c.begin(),c.end()
#define sz(c) (ll)c.size()
#define lcm(a,b) (a*b)/(__gcd(a,b))
#define init(x,a) memset(x,a,sizeof(x))
#define lbd(a,val) lower_bound(all(a),val)
#define ubd(a,val) upper_bound(all(a),val)
#define pb push_back
#define mp1 make_pair
#define F first
#define S second
#define en '\n'
#define mod (ll)1000000007
#define M (ll)998244353
#define N 100005
#define PI 3.14159265358979323846

int main()
{
    IOS;
    ll t=1;
    //cin >> t;
    while(t--)
    {
        ll i,j,k=0,l=0,z=0;
        ll n,m=0;
        cin >> n;
        string s[n];
        vector<int> a(0,0);
        for(i=0;i<n;i++)
        {
            cin >> s[i];
            vector<int> x;
            k=0,l=0;
            for(j=0;j<s[i].length();)
            {
                if(s[i][j]=='(')
                {
                    k++;
                }
                else if(s[i][j]==')')
                {
                    l++;
                }
                if(s[i][j]=='(')
                {
                    x.push_back(j);
                    j++;
                }
                else if(!x.empty() && s[i][j]==')')
                {
                    z=x.back();
                    x.pop_back();
                    s[i].erase(s[i].begin()+j);
                    s[i].erase(s[i].begin()+z);
                    k--;
                    l--;
                    j=j-1;
                }
                else
                {
                    j++;
                }
            }
            if(!s[i].empty())
            {
                //cout<<s[i]<<endl;
                if(k!=0 && l!=0)
                {
                    i--;
                    n--;
                }
                else
                {
                    //cout<<s[i]<<endl;
                    a.push_back(k-l);
                    //cout <<(k-l)<<endl;
                }

            }
            else
            {
                m++;
                i--;
                n--;
            }
        }
        m=m+m*(m-1);
        //sort(a.begin(),a.end());
        for(i=0;i<n;i++)
        {
            //cout <<a[i]<<endl;
            for(j=n-1;j>=i && j>=0;j--)
            {
                if(a[i]+a[j]==0 && a[i] * a[j] !=0 )
                {
                    m++;
                }
                if(a[i]+a[j]<0)
                {
                    //break;
                }
            }
        }
        cout << m << endl;
    }
}


#include <bits/stdc++.h>
using namespace std;

#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define ll long long int
#define ld long double
#define vi vector<ll>
#define vii vector<pair<ll,ll> >
#define pll pair<ll,ll>
#define f(i,a,b) for(ll i=a;i<b;i++)
#define rev(i,a,b) for(ll i=a;i>=b;i--)
#define all(c) c.begin(),c.end()
#define sz(c) (ll)c.size()
#define lcm(a,b) (a*b)/(__gcd(a,b))
#define init(x,a) memset(x,a,sizeof(x))
#define lbd(a,val) lower_bound(all(a),val)
#define ubd(a,val) upper_bound(all(a),val)
#define pb push_back
#define mp1 make_pair
#define F first
#define S second
#define en '\n'
#define mod (ll)1000000007
#define M (ll)998244353
#define N 100005
#define PI 3.14159265358979323846

int main()
{
    //IOS;
    ll t=1;
    //cin >> t;
    while(t--)
    {
        ll i,j,k=0,l=0,z=0;
        ll n,m=0;
        cin >> n;
        string s[n];
        vector<int> a(0,0);
        for(i=0;i<n;i++)
        {
            cin >> s[i];
            vector<int> x;
            k=0,l=0;
            for(j=0;j<s[i].length();)
            {
                if(s[i][j]=='(')
                {
                    k++;
                }
                else if(s[i][j]==')')
                {
                    l++;
                }
                if(s[i][j]=='(')
                {
                    x.push_back(j);
                    j++;
                }
                else if(!x.empty() && s[i][j]==')')
                {
                    z=x.back();
                    x.pop_back();
                    s[i].erase(s[i].begin()+j);
                    s[i].erase(s[i].begin()+z);
                    k--;
                    l--;
                    j=j-1;
                }
                else
                {
                    j++;
                }
            }
            if(!s[i].empty())
            {
                //cout<<s[i]<<endl;
                if(k!=0 && l!=0)
                {
                    i--;
                    n--;
                }
                else
                {
                    //cout<<s[i]<<endl;
                    a.push_back(k-l);
                    //cout <<(k-l)<<endl;
                }

            }
            else
            {
                m++;
                i--;
                n--;
            }
        }
        m=m+m*(m-1);
        sort(a.begin(),a.end());
        for(i=0;i<n;i++)
        {
            //cout <<a[i]<<endl;
            for(j=n-1;j>=i && j>=0;j--)
            {
                if(a[i]+a[j]==0 && a[i] * a[j] !=0 )
                {
                    m++;
                }
                if(a[i]+a[j]<0)
                {
                    break;
                }
            }
        }
        cout << m << endl;
    }
}


#include <bits/stdc++.h>
using namespace std;

#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define ll long long int
#define ld long double
#define vi vector<ll>
#define vii vector<pair<ll,ll> >
#define pll pair<ll,ll>
#define f(i,a,b) for(ll i=a;i<b;i++)
#define rev(i,a,b) for(ll i=a;i>=b;i--)
#define all(c) c.begin(),c.end()
#define sz(c) (ll)c.size()
#define lcm(a,b) (a*b)/(__gcd(a,b))
#define init(x,a) memset(x,a,sizeof(x))
#define lbd(a,val) lower_bound(all(a),val)
#define ubd(a,val) upper_bound(all(a),val)
#define pb push_back
#define mp1 make_pair
#define F first
#define S second
#define en '\n'
#define mod (ll)1000000007
#define M (ll)998244353
#define N 100005
#define PI 3.14159265358979323846

int main()
{
    //IOS;
    ll t=1;
    //cin >> t;
    while(t--)
    {
        ll i,j,k=0,l=0,p=0;
        ll n,m=0;
        cin >> n;
        string s[n];
        for(i=0;i<n;i++)
        {
            cin >> s[i];
            vector<int> x;
            for(j=0;j<s[i].length();)
            {
                if(s[i][j]=='(')
                {
                    x.push_back(j);
                    j++;
                }
                else if(!x.empty() && s[i][j]==')')
                {
                    k=x.back();
                    x.pop_back();
                    s[i].erase(s[i].begin()+j);
                    s[i].erase(s[i].begin()+k);
                    j=j-1;
                }
                else
                {
                    j++;
                }
            }
            if(!s[i].empty())
            {
            //cout<<s[i]<<endl;
            }
            else
            {
                m++;
                i--;
                n--;
            }
        }
        m=m+m*(m-1);
        //cout << m << endl;
        int a[n]={0};
        for(i=0;i<n;i++)
        {
            k=0,l=0;
            for(j=0;j<s[i].length();j++)
            {
                if(s[i][j]=='(')
                {
                    k++;
                }
                else if(s[i][j]==')')
                {
                    l++;
                }
            }
            a[i]=(k-l);
        }
        sort(a,a+n);
        for(i=0;i<n;i++)
        {
            for(j=n-1;j>i;j--)
            {
                if(a[i]+a[j]==0 && a[i]*a[j]!=0 )
                {
                    m++;
                }
                if(a[j]+a[i]<0)
                {
                    break;
                }
            }
        }
        cout << m << endl;
        /*
        ll a[n]={0};
        for(i=0;i<n;i++)
        {
            cin >> s[i];
            k=0;l=0,p=0;
            for(j=0;j<s[i].length();j++)
            {
                if(s[i][j]=='(')
                {
                    k++;
                    if(k>0)
                    {
                        p++;
                    }
                }
                if(s[i][j]==')')
                {
                    k--;
                    if(k<0)
                    {l++;}
                }
            }
            a[i]=k;
            if(k==0 && l==0)
            {m++;}
            else if(k>=0 && l!=0)
            {
                a[i]=mod;
            }
            else if(k<=0 && p!=0)
            {
               // a[i]=mod;
            }
        }
        sort(a,a+n);
        for(i=0;i<n;i++)
        {
            cout <<a[i]<<endl;
        }
        for(i=0;i<n;i++)
        {
            for(j=n-1;j>=i && a[j]+a[i]>=0;j--)
            {
                if(i!=j)
                {
                    if(a[j]+a[i] ==0 && a[i]!=0)
                    {
                        m++;
                        //cout <<"1 "<< i<<" "<<j <<endl;
                    }
                    else if(a[i]==0 && a[j]==0)
                    {
                        m+=2;
                        //cout << i<<" "<<j <<endl;
                        //cout <<
                    }
                }
                // && abs(a[i])<=abs(a[j])
                if(!(a[i]*a[j]<=0))
                {
                    break;
                }
            }
        }
        cout << m << endl;
        */
    }
}


#include <bits/stdc++.h>
using namespace std;

#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define ll long long int
#define ld long double
#define vi vector<ll>
#define vii vector<pair<ll,ll> >
#define pll pair<ll,ll>
#define f(i,a,b) for(ll i=a;i<b;i++)
#define rev(i,a,b) for(ll i=a;i>=b;i--)
#define all(c) c.begin(),c.end()
#define sz(c) (ll)c.size()
#define lcm(a,b) (a*b)/(__gcd(a,b))
#define init(x,a) memset(x,a,sizeof(x))
#define lbd(a,val) lower_bound(all(a),val)
#define ubd(a,val) upper_bound(all(a),val)
#define pb push_back
#define mp1 make_pair
#define F first
#define S second
#define en '\n'
#define mod (ll)1000000007
#define M (ll)998244353
#define N 100005
#define PI 3.14159265358979323846

bool compare(string s1,string s2)
{
    ll i,j,k=0,l=0;
    ll m= s1.length();
    for(i=0;i<m;i++)
    {
        if(s1[i]=='1')
        {k++;}
        if(s2[i]=='1')
        {l++;}
    }
    return k>l;

}

int main()
{
    //IOS;
    ll t=1;
    //cin >> t;
    while(t--)
    {
        ll i,j,k=0,l=0;
        ll n,m=0;
        cin >> n;
        string s[n];
        ll a[n]={0};
        for(i=0;i<n;i++)
        {
            cin >> s[i];
            k=0;l=0;
            for(j=0;j<s[i].length();j++)
            {
                if(s[i][j]=='(')
                {
                    k++;
                }
                if(s[i][j]==')')
                {
                    k--;
                }
            }
            a[i]=k;
            if(k==0)
            {m++;}
        }
        sort(a,a+n);
        for(i=0;i<n;i++)
        {
            for(j=n-1;j>=0 && a[j]+a[i]>=0;j--)
            {
                if(i!=j)
                {
                    if(a[j]+a[i] ==0 && a[i]!=0)
                    {
                        m++;
                    }
                    else if(a[i]==0 && a[j]==0)
                    {
                        m+=1;
                    }
                }
                // && abs(a[i])<=abs(a[j])
                if(!(a[i]*a[j]<=0))
                {
                    break;
                }
            }
        }
        cout << m << endl;
    }
}


#include <bits/stdc++.h>
using namespace std;

#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define ll long long int
#define ld long double
#define vi vector<ll>
#define vii vector<pair<ll,ll> >
#define pll pair<ll,ll>
#define f(i,a,b) for(ll i=a;i<b;i++)
#define rev(i,a,b) for(ll i=a;i>=b;i--)
#define all(c) c.begin(),c.end()
#define sz(c) (ll)c.size()
#define lcm(a,b) (a*b)/(__gcd(a,b))
#define init(x,a) memset(x,a,sizeof(x))
#define lbd(a,val) lower_bound(all(a),val)
#define ubd(a,val) upper_bound(all(a),val)
#define pb push_back
#define mp1 make_pair
#define F first
#define S second
#define en '\n'
#define mod (ll)1000000007
#define M (ll)998244353
#define N 100005
#define PI 3.14159265358979323846

bool compare(string s1,string s2)
{
    ll i,j,k=0,l=0;
    ll m= s1.length();
    for(i=0;i<m;i++)
    {
        if(s1[i]=='1')
        {k++;}
        if(s2[i]=='1')
        {l++;}
    }
    return k>l;

}

int main()
{
    //IOS;
    ll t=1;
    //cin >> t;
    while(t--)
    {
        ll i,j,k=0,l=0,p=0;
        ll n,m=0;
        cin >> n;
        string s[n];
        ll a[n]={0};
        for(i=0;i<n;i++)
        {
            cin >> s[i];
            k=0;l=0,p=0;
            for(j=0;j<s[i].length();j++)
            {
                if(s[i][j]=='(')
                {
                    k++;
                    if(k>0)
                    {
                        p++;
                    }
                }
                if(s[i][j]==')')
                {
                    k--;
                    if(k<0)
                    {l++;}
                }
            }
            a[i]=k;
            if(k==0 && l==0)
            {m++;}
            else if(k>=0 && l!=0)
            {
                a[i]=mod;
            }
            else if(k<=0 && p!=0)
            {
                a[i]=mod;
            }
        }
        sort(a,a+n);
        for(i=0;i<n;i++)
        {
           // cout <<a[i]<<endl;
        }
        for(i=0;i<n;i++)
        {
            for(j=n-1;j>=i && a[j]+a[i]>=0;j--)
            {
                if(i!=j)
                {
                    if(a[j]+a[i] ==0 && a[i]!=0)
                    {
                        m++;
                        //cout <<"1 "<< i<<" "<<j <<endl;
                    }
                    else if(a[i]==0 && a[j]==0)
                    {
                        m+=2;
                        //cout << i<<" "<<j <<endl;
                        //cout <<
                    }
                }
                // && abs(a[i])<=abs(a[j])
                if(!(a[i]*a[j]<=0))
                {
                    break;
                }
            }
        }
        cout << m << endl;
    }
}


#include <bits/stdc++.h>
using namespace std;

#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define ll long long int
#define ld long double
#define vi vector<ll>
#define vii vector<pair<ll,ll> >
#define pll pair<ll,ll>
#define f(i,a,b) for(ll i=a;i<b;i++)
#define rev(i,a,b) for(ll i=a;i>=b;i--)
#define all(c) c.begin(),c.end()
#define sz(c) (ll)c.size()
#define lcm(a,b) (a*b)/(__gcd(a,b))
#define init(x,a) memset(x,a,sizeof(x))
#define lbd(a,val) lower_bound(all(a),val)
#define ubd(a,val) upper_bound(all(a),val)
#define pb push_back
#define mp1 make_pair
#define F first
#define S second
#define en '\n'
#define mod (ll)1000000007
#define M (ll)998244353
#define N 100005
#define PI 3.14159265358979323846

bool compare(string s1,string s2)
{
    ll i,j,k=0,l=0;
    ll m= s1.length();
    for(i=0;i<m;i++)
    {
        if(s1[i]=='1')
        {k++;}
        if(s2[i]=='1')
        {l++;}
    }
    return k>l;

}

int main()
{
    //IOS;
    ll t=1;
    //cin >> t;
    while(t--)
    {
        ll i,j,k=0,l=0;
        ll n,m=0;
        cin >> n;
        string s[n];
        ll a[n]={0};
        for(i=0;i<n;i++)
        {
            cin >> s[i];
            k=0;l=0;
            for(j=0;j<s[i].length();j++)
            {
                if(s[i][j]=='(')
                {
                    k++;
                }
                if(s[i][j]==')')
                {
                    k--;
                    if(k<0)
                    {l++;}
                }
            }
            a[i]=k;
            if(k==0 && l==0)
            {m++;}
            else if(k==0 && l!=0)
            {
                a[i]=mod;
            }
        }
        sort(a,a+n);
        /*for(i=0;i<n;i++)
        {
            cout <<a[i]<<endl;
        }*/
        for(i=0;i<n;i++)
        {
            for(j=n-1;j>=i && a[j]+a[i]>=0;j--)
            {
                if(i!=j)
                {
                    if(a[j]+a[i] ==0 && a[i]!=0)
                    {
                        m++;
                    }
                    else if(a[i]==0 && a[j]==0)
                    {
                        m+=2;
                        //cout <<
                    }
                }
                // && abs(a[i])<=abs(a[j])
                if(!(a[i]*a[j]<=0))
                {
                    break;
                }
            }
        }
        cout << m << endl;
    }
}


#include <bits/stdc++.h>
using namespace std;

#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define ll long long int
#define ld long double
#define vi vector<ll>
#define vii vector<pair<ll,ll> >
#define pll pair<ll,ll>
#define f(i,a,b) for(ll i=a;i<b;i++)
#define rev(i,a,b) for(ll i=a;i>=b;i--)
#define all(c) c.begin(),c.end()
#define sz(c) (ll)c.size()
#define lcm(a,b) (a*b)/(__gcd(a,b))
#define init(x,a) memset(x,a,sizeof(x))
#define lbd(a,val) lower_bound(all(a),val)
#define ubd(a,val) upper_bound(all(a),val)
#define pb push_back
#define mp1 make_pair
#define F first
#define S second
#define en '\n'
#define mod (ll)1000000007
#define M (ll)998244353
#define N 100005
#define PI 3.14159265358979323846

int main()
{
    //IOS;
    ll t=1;
    //cin >> t;
    while(t--)
    {
        ll i,j,k=0,l=0;
        ll n,m=0;
        cin >> n;
        string s[n];
        for(i=0;i<n;i++)
        {
            cin >> s[i];
            vector<int> x;
            for(j=0;j<s[i].length();)
            {
                if(s[i][j]=='(')
                {
                    x.push_back(j);
                    j++;
                }
                else if(!x.empty() && s[i][j]==')')
                {
                    k=x.back();
                    x.pop_back();
                    s[i].erase(s[i].begin()+j);
                    s[i].erase(s[i].begin()+k);
                    j=j-1;
                }
                else
                {
                    j++;
                }
            }
            if(!s[i].empty())
            {
                k=0,l=0;
                for(j=0;j<s[i].length();j++)
                {
                    if(s[i][j]=='(')
                    {
                        k++;
                    }
                    else if(s[i][j]==')')
                    {
                        l++;
                    }
                }
                if(k!=0 && l!=0)
                {
                    i--;
                    n--;
                }
            //cout<<s[i]<<endl;
            }
            else
            {
                m++;
                i--;
                n--;
            }
        }
        m=m+m*(m-1);
        //cout << m << endl;
        int a[n]={0};
        pair<int,string> p[n];
        for(i=0;i<n;i++)
        {
            k=0,l=0;
            for(j=0;j<s[i].length();j++)
            {
                if(s[i][j]=='(')
                {
                    k++;
                }
                else if(s[i][j]==')')
                {
                    l++;
                }
            }
            p[i].F=(k-l);
            p[i].S=s[i];
        }
        sort(p,p+n);
        for(i=0;i<n;i++)
        {
            for(j=n-1;j>i;j--)
            {
                if(p[i].F+p[j].F==0 && p[i].F * p[j].F !=0 )
                {
                    //string y=p[j].S +p[i].S;
                    //cout << y<< endl;
                    m++;
                }
                if(p[i].F+p[j].F<0)
                {
                    break;
                }
            }
        }
        cout << m << endl;
        /*
        ll a[n]={0};
        for(i=0;i<n;i++)
        {
            cin >> s[i];
            k=0;l=0,p=0;
            for(j=0;j<s[i].length();j++)
            {
                if(s[i][j]=='(')
                {
                    k++;
                    if(k>0)
                    {
                        p++;
                    }
                }
                if(s[i][j]==')')
                {
                    k--;
                    if(k<0)
                    {l++;}
                }
            }
            a[i]=k;
            if(k==0 && l==0)
            {m++;}
            else if(k>=0 && l!=0)
            {
                a[i]=mod;
            }
            else if(k<=0 && p!=0)
            {
               // a[i]=mod;
            }
        }
        sort(a,a+n);
        for(i=0;i<n;i++)
        {
            cout <<a[i]<<endl;
        }
        for(i=0;i<n;i++)
        {
            for(j=n-1;j>=i && a[j]+a[i]>=0;j--)
            {
                if(i!=j)
                {
                    if(a[j]+a[i] ==0 && a[i]!=0)
                    {
                        m++;
                        //cout <<"1 "<< i<<" "<<j <<endl;
                    }
                    else if(a[i]==0 && a[j]==0)
                    {
                        m+=2;
                        //cout << i<<" "<<j <<endl;
                        //cout <<
                    }
                }
                // && abs(a[i])<=abs(a[j])
                if(!(a[i]*a[j]<=0))
                {
                    break;
                }
            }
        }
        cout << m << endl;
        */
    }
}