#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int open = 0;
  int close = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '(') {
      open++;
    } else {
      close++;
    }
  }
  if (open != close) {
    cout << -1 << endl;
  } else {
    open = 0;
    close = 0;
    long long int ans = 0;
    bool flag = true;
    for (int i = 0; i < n; i++) {
      if (s[i] == '(') {
        open++;
      } else {
        close++;
      }
      if (open == close) {
        if (!flag) {
          ans += (2 * open);
          open = 0;
          close = 0;
          flag = true;
        } else {
          open = 0;
          close = 0;
          continue;
        }
      }
      if (close > open) {
        flag = false;
      } else {
        flag = true;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
