#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const int MOD2 = 998244353;
const long long int MAX = 9187201950435737471;
const int N = 100005;
const int INF = 1e9 + 7;
const long double PI = acos(-1);
void solve(int T) {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int m = 0;
  if (n & 1) {
    cout << "-1\n";
    return;
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == ')') {
      m++;
    }
  }
  if (m != n / 2) {
    cout << "-1\n";
    return;
  }
  int ans = 0;
  stack<int> a;
  stack<int> b;
  for (int i = 0; i < n; i++) {
    if (s[i] == ')') {
      if (b.empty()) {
        a.push(i);
      } else {
        b.pop();
      }
    } else if (s[i] == '(') {
      if (a.empty()) {
        b.push(i);
      } else {
        if (a.size() == 1) {
          ans += (i - a.top() + 1);
          a.pop();
        } else
          a.pop();
      }
    }
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int T = 1;
  for (int i = 1; i <= T; i++) {
    solve(i);
  }
  return 0;
}
