import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; public class Main { long mo = 1_000_000_000 + 7; void run() { Scanner sc = new Scanner(System.in); long N = sc.nextLong(); String str = sc.next(); long ans = N * (N + 1) / 2; long sum = 0; long min = 0; long[][] a = new long[(int) N][2]; for (int i = 0; i < N; ++i) { sum += (str.charAt(i) == '(' ? 1 : -1); ans -= sum; min = Math.min(min, sum); a[i][0] = sum; a[i][1] = min; } Arrays.sort(a, new Comparator() { @Override public int compare(long[] o1, long[] o2) { return -Long.compare(o1[0], o2[0]); } }); sum = 0; min = 0; for (int i = 0; i < N; ++i) { min = Math.min(min, a[i][1] + sum); sum += a[i][0]; } System.out.println(ans + 2 * min); } public static void main(String[] args) { new Main().run(); } void tr(Object... objects) { System.out.println(Arrays.deepToString(objects)); } }