#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 10;
const long long int LINF = 1LL * INF * INF;
const int MAXN = 1e6 + 10;
const int MAXM = 5e3 + 10;
priority_queue<int> pq;
vector<vector<int> > graph;
queue<int> que;
int A[MAXN];
char S[MAXN];
int main() {
  int n, m, k, a, b, x, y, q;
  int sum = 0;
  int cnt = 0;
  int mx = 0;
  int mn = INF;
  int cur = 0, idx = -1;
  int tc;
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  cin >> S + 1;
  for (int i = 1; i <= n; i++) {
    if (S[i] == '(')
      A[i] = A[i - 1] + 1;
    else
      A[i] = A[i - 1] - 1;
  }
  if (A[n]) {
    cout << "-1\n";
    return 0;
  }
  int res = 0;
  for (int i = 1; i <= n; i++) {
    if (A[i] < 0) {
      ++cnt;
    } else if (!A[i]) {
      if (cnt) res += (cnt + 1);
      cnt = 0;
    }
  }
  cout << res << "\n";
  return 0;
}
