#include <bits/stdc++.h>
using namespace std;
long long MOD = 1e9 + 7;
vector<int> adj[100001];
int num1 = 0;
int num2 = 0;
int maxd = -1;
void dfs(int n, int par, int d) {
  if (d > maxd) {
    maxd = d;
    num1 = n;
  }
  for (auto x : adj[n]) {
    if (x != par) {
      dfs(x, n, d + 1);
    }
  }
}
void icchhipadey() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  stack<char> st;
  long long ans = 0;
  long long bal = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '(') {
      bal++;
    } else {
      bal--;
    }
  }
  if (bal) {
    cout << "-1";
    return;
  }
  bal = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '(') {
      bal++;
    } else
      bal--;
    if (bal < 0) {
      i++;
      ans++;
      while (bal < 0 && i < n) {
        if (s[i] == '(')
          bal++;
        else
          bal--;
        i++;
        ans++;
      }
      i--;
    }
  }
  cout << ans << endl;
}
int main() { icchhipadey(); }
