#include <bits/stdc++.h>
using namespace std;
const int mx = 1e5 + 5;
class cmp {
 public:
  cmp() {}
  bool operator()(const pair<int, int>& p1, const pair<int, int>& p2) const {
    if (p1.first == p2.first)
      return p1.second < p2.second;
    else
      return !(p1.first < p2.first);
  }
};
int sz, l, r, h;
int arr[mx];
int dp[2003][2005];
int solve(int i, int n) {
  if (i == sz) return 0;
  if (dp[i][n] != -1) return dp[i][n];
  int ch1 = 0;
  if ((((n + (arr[i] - 1)) % h) + h) % h >= l and
      (((n + (arr[i] - 1)) % h) + h) % h <= r)
    ch1 += solve(i + 1, (((n + (arr[i] - 1)) % h) + h) % h) + 1;
  else
    ch1 += solve(i + 1, (((n + (arr[i] - 1)) % h) + h) % h);
  int ch2 = 0;
  if ((n + arr[i]) % h >= l and (n + arr[i]) % h <= r)
    ch2 += solve(i + 1, (n + arr[i]) % h) + 1;
  else
    ch2 += solve(i + 1, (n + arr[i]) % h);
  return dp[i][n] = max(ch1, ch2);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int n;
  cin >> n;
  string s;
  cin >> s;
  int x = 0, ans = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '(')
      x++;
    else
      x--;
    if (x < 0 or (x == 0 and s[i] == '(')) ans++;
  }
  if (x) ans = -1;
  cout << ans << endl;
  return 0;
}
