#include <bits/stdc++.h>
using namespace std;
char a[1000005];
int main() {
  int n;
  int left = 0;
  int right = 0;
  int wrong = 0;
  int num = 0;
  int True = 0;
  cin >> n;
  cin >> a;
  if (n % 2 != 0) {
    cout << "-1" << endl;
    return 0;
  }
  for (int i = 0; i < n; i++) {
    if (a[i] == '(') {
      if (wrong != 0) {
        wrong--;
        left++;
      } else {
        True++;
        left++;
      }
    }
    if (a[i] == ')') {
      if (True != 0) {
        True--;
        right++;
      } else {
        wrong++;
        right++;
        num++;
      }
    }
  }
  if (right != left) {
    cout << "-1" << endl;
  } else {
    cout << num * 2 << endl;
  }
  return 0;
}
