Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func parseToArray2d(lines []string) [][]int64 {
	var result [][]int64
	for _, line := range lines {
		var parsedLine []int64
		for _, element := range strings.Split(line, " ") {
			n, err := strconv.ParseInt(element, 10, 64)
			if err != nil {
				fmt.Printf("Error: element = %v\n", element)
			}
			parsedLine = append(parsedLine, n)
		}
		result = append(result, parsedLine)
	}
	return result
}

func pop(stack []int64) ([]int64, int64) {
	lenStack := len(stack)
	return stack[0:(lenStack - 1)], stack[lenStack-1]
}

func contains(s []int64, e int64) bool {
	for _, a := range s {
		if a == e {
			return true
		}
	}
	return false
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	content := []string{}
	for scanner.Scan() {
		line := scanner.Text()
		content = append(content, line)
	}
	arr := parseToArray2d(content)
	input := arr[1]

	stack := []int64{}
	result := -1

	for i, x := range input {
		if x > 0 {
			if contains(stack, x) {
				result = i + 1
				break
			} else {
				stack = append(stack, x)
			}
		} else {
			var top int64
			if len(stack) == 0 {
				result = i + 1
				break
			}
			stack, top = pop(stack)
			if top != -x {
				result = i + 1
			}
		}
	}

	if result < 0 {
		fmt.Println("OK")
	} else {
		fmt.Println(result)
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using VI = vector<int>;
using VL = vector<ll>;
using VS = vector<string>;
template<class T> using PQ = priority_queue<T, vector<T>, greater<T>>;
#define FOR(i,a,n) for(int (i)=(a);(i)<(n);++(i))
#define eFOR(i,a,n) for(int (i)=(a);(i)<=(n);++(i))
#define rFOR(i,a,n) for(int (i)=(n)-1;(i)>=(a);--(i))
#define erFOR(i,a,n) for(int (i)=(n);(i)>=(a);--(i))
#define each(i, a) for(auto &i : a)
#define SORT(i) sort((i).begin(),(i).end())
#define rSORT(i,a) sort((i).begin(),(i).end(),(a))
#define all(i) (i).begin(),(i).end()
#define out(y,x) ((y) < 0 || h <= (y) || (x) < 0 || w <= (x))
constexpr ll INF = 1000000000;
constexpr ll LLINF = 1LL << 60;
constexpr ll mod = 1000000007;
constexpr ll MOD = 998244353;
constexpr ld eps = 1e-10;
constexpr ld pi = 3.1415926535897932;
template<class T>inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; }return false; }
template<class T>inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; }return false; }
inline void init() { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); }
template<class T>inline istream& operator>>(istream& is, vector<T>& v) { for (auto& elemnt : v)is >> elemnt; return is; }
template<class T>inline istream& operator>>(istream& is, deque<T>& v) { for (auto& elemnt : v)is >> elemnt; return is; }
template<class T, class U>inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template<class T>inline vector<T> vec(size_t a) { return vector<T>(a); }
template<class T>inline vector<T> defvec(T def, size_t a) { return vector<T>(a, def); }
template<class T, class... Ts>inline auto vec(size_t a, Ts... ts) { return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...)); }
template<class T, class... Ts>inline auto defvec(T def, size_t a, Ts... ts) { return vector<decltype(defvec<T>(def, ts...))>(a, defvec<T>(def, ts...)); }

int main() {
    init();

    int n; cin >> n;
    vector<bool> in(101);
    stack<int> s;
    FOR(i, 0, n) {
        int a; cin >> a;
        if (0 < a) {
            if (in[a]) {
                cout << i + 1 << "\n";
                return 0;
            }
            s.push(a);
            in[a] = true;
        }
        else {
            if (!in[-a]) {
                cout << i + 1 << "\n";
                return 0;
            }
            if (s.empty() || s.top() != -a) {
                cout << i + 1 << "\n";
                return 0;
            }
            s.pop();
            in[-a] = false;
        }
    }
    cout << "OK\n";
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); ++i)
using namespace std;

const int M = 111;

void fail(int n) {
    cout << n << '\n';
    exit(0);
}

int main() {
    int N; cin >> N;
    vector<int> cats(M);
    stack<int> st;
    auto push = [&](int c) -> bool {
        if(cats[c]) return false;
        st.push(c);
        cats[c] = 1;
        return true;
    };
    auto pop = [&](int c) -> bool {
        if(st.empty()) return false;
        if(st.top() != c) return false;
        st.pop();
        cats[c] = 0;
        return true;
    };
    rep(i, N) {
        int c; cin >> c;
        if(c > 0) {
            if(!push(c)) fail(i + 1);
        } else {
            if(!pop(-c)) fail(i + 1);
        }
    }
    cout << "OK" << '\n';
    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <functional>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
using namespace std;
template<class T, class Compare = less<T> >
using MaxHeap = priority_queue<T, vector<T>, Compare>;
template<class T, class Compare = greater<T> >
using MinHeap = priority_queue<T, vector<T>, Compare>;
using llong = long long;

stack<llong> st;
llong in_stack[1005];
llong l;
llong c;

int main() {
    cin >> l;

    for (int i = 0; i < l; i++) {
        cin >> c;

        if (c > 0) {
            if (in_stack[c] == 1) {
                cout << i + 1 << endl;
                return 0;
            }
            
            st.push(c);
            in_stack[c] = 1;
        }
        else {
            if (st.empty() || st.top() != -c) {
                cout << i + 1 << endl;
                return 0;
            }
            
            st.pop();
            in_stack[-c] = 0;
        }
    }

    cout << "OK" << endl;

    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

int main(){
    int L;
    cin >> L;
    vector<int> C(L);
    for(int i = 0; i < L; i++) cin >> C[i];
    vector<int> in(L+1,0);
    stack<int> s;

    int ans = 1e9;
    for(int i = 0; i< L; i++){
        if(C[i] > 0){
            if(in[C[i]]) ans = min(ans, i+1);
            else in[C[i]] = 1, s.push(C[i]);
        } 
        if(C[i] < 0){
            if(s.size() == 0) ans = min(ans, i+1);
            else if(in[-C[i]] && s.top() != -C[i]) ans = min(ans, i+1);
            else if(!in[-C[i]]) ans = min(ans, i+1);
            else in[-C[i]] = 0, s.pop();
        }
    }

    if(ans == 1e9) cout << "OK" << endl;
    else cout << ans << endl;
}

C++: #include <iostream>
#include <stack>
#include <set>
using namespace std;

int main(void) {
    int L;cin>>L;
    set<int> cats; // 横穴に入った猫を保存
    stack<int> caves; // 横穴を表す
    for (int i = 1; i <= L; i++) {
        int c;cin>>c;
        if (c > 0) {
            // すでに入っているのに入ろうとしたらアウト
            if (cats.find(c) != cats.end()) {
                cout << i << endl;
                return 0;
            }
            cats.insert(c);
            caves.push(c);
        } else {
            int c_real = (-1) * c;//猫の番号

            if (cats.find(c_real) == cats.end()) {
                // そのような猫が横穴に入っていないのであればアウト
                cout << i << endl;
                return 0;
            }
            int last_cat = caves.top();
            // 出ようとした猫が横穴の出口にいなかったらアウト
            if (c_real != last_cat) {
                cout << i << endl;
                return 0;
            }
            set<int>::iterator it = cats.find(c_real);
            cats.erase(it);
            caves.pop();
        }
    }
    cout << "OK" << endl;

    return 0;
}