#include <bits/stdc++.h>
using namespace std;
int n, ans, sum, nowl, nowr;
bool ok = true;
string s;
int main() {
  cin >> n >> s;
  for (int i = 0; i < n; i++) {
    if (s[i] == '(') nowl++;
    if (s[i] == ')') nowr++;
    if (nowr > nowl) ok = false;
    sum++;
    if (nowl == nowr) {
      if (!ok) ans += sum;
      sum = 0;
      ok = true;
    }
  }
  if (nowl != nowr)
    puts("-1");
  else
    cout << ans + sum;
  return 0;
}
