OIG IV - pro

// https://szkopul.edu.pl/problemset/problem/jvPBZm53yfvDkYKuuxbQvjNe/site/?key=statement
//  OIG IV (1 etap)

#include <iostream>
#include <stack>

int main() {
    std::ios_base::sync_with_stdio(0);
    std::cin.tie(0);
    std::cout.tie(0);

    int n;
    std::cin >> n;

    int ans = 0;

    std::stack<char> s;

    for (int i = 0; i < n; i++) {
        char a;
        std::cin >> a;

        if (a == '{' || a == '[' || a == '(') {
            s.push(a);
            ans = std::max(ans, (int)s.size());
        } else {
            if (s.size() < 1) {
                std::cout << "NIE\n";
                return 0;
            }

            if (a == '}' && s.top() == '{') {
                s.pop();
            } else if (a == ']' && s.top() == '[') {
                s.pop();
            } else if (a == ')' && s.top() == '(') {
                s.pop();
            } else {
                std::cout << "NIE\n";
                return 0;
            }
        }
    }

    if (s.size() > 0) {
        std::cout << "NIE\n";
        return 0;
    }

    std::cout << ans << '\n';

    return 0;
}