#include <bits/stdc++.h>
using namespace std;
bool areParanthesisBalanced(string expr) {
  stack<char> s;
  char x;
  for (int i = 0; i < expr.length(); i++) {
    if (expr[i] == '(' || expr[i] == '[' || expr[i] == '{') {
      s.push(expr[i]);
      continue;
    }
    if (s.empty()) return false;
    switch (expr[i]) {
      case ')':
        x = s.top();
        s.pop();
        if (x == '{' || x == '[') return false;
        break;
      case '}':
        x = s.top();
        s.pop();
        if (x == '(' || x == '[') return false;
        break;
      case ']':
        x = s.top();
        s.pop();
        if (x == '(' || x == '{') return false;
        break;
    }
  }
  return (s.empty());
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  cin.ignore();
  string s;
  cin >> s;
  int op = 0;
  int ans = 0;
  int st = -1, en = -1;
  for (int i = 0; i < n; ++i) {
    if (st == -1) {
      st = i;
    }
    if (s[i] == '(') {
      op++;
    } else {
      op--;
    }
    if (op == 0) {
      if (areParanthesisBalanced(s.substr(st, i - st + 1))) {
      } else {
        ans += i - st + 1;
      }
      st = -1;
    }
  }
  if (op == 0) {
    cout << ans << '\n';
  } else {
    cout << "-1\n";
  }
  return 0;
}
