#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
const int dr[] = {+1, -1, +0, +0, +1, -1, +1, -1};
const int dc[] = {+0, +0, +1, -1, +1, -1, -1, +1};
const int ms[] = {+31, +29, +31, 30, +31, +30, +31, +31, +30, +31, +30, +31};
const int N = 2e4 + 5;
int dp[N];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  string w;
  cin >> w;
  stack<char> chk;
  int cost = 0;
  for (int i = 0; i < (int)n; i++) {
    if (chk.empty() || w[i] == chk.top()) {
      chk.push(w[i]);
    } else {
      chk.pop();
      if (w[i] == '(') cost += 2;
    }
  }
  if ((int)chk.size() == 0)
    cout << cost << '\n';
  else
    cout << -1 << '\n';
  return 0;
}
