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<iostream>
using namespace std;

int main(){
	int L, num;
	cin >> L;
	int cat_list[101];
	for(int i=0; i<101; ++i){cat_list[i] = -1;}
	int  count=1;
	bool ok=true;
	
	int i = 0;
    while(i<L){
	    ++i;
		cin >> num;
		if(num>0){
			if(cat_list[num] != -1){ok=false; break;}
			else{cat_list[num] = count; ++count;}
		}
		else{
			num *= -1;
			if(cat_list[num] == -1 || cat_list[num] != count-1){ok=false; break;}
			else{cat_list[num]=-1; --count;}
		}
	}
	
	(i==L && ok) ? cout << "OK" << endl : cout << i << endl;

	return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;
#define Would
#define you
#define all(n)  n.begin(),n.end()
#define rall(n) n.rbegin(),n.rend()
typedef long long ll;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;
const double EPS = 1e-10;
const double pi = acos(-1);//3.1415926535897932384626433832795028...
const ll SIZE = 1 << 17;
int dx[] = { 1,0,-1,0 }, dy[] = { 0,1,0,-1 }, alp[30];
ll fac[200005], finv[200005], inv[200005];
vector<ll>dij;
struct edge { ll to, cost; };
vector<vector<edge> >G;

ll mod_pow(ll a, ll b) {
	ll res = 1, mul = a;
	for (int i = 0; i < 31; ++i) {
		if (b >> i & 1) {
			res *= mul;
			res %= MOD;
		}
		mul = (mul * mul) % MOD;
	}
	return res;
}

void addedge(int from, int to, int cost) {
	G[from].push_back({ to,cost });
	G[to].push_back({ from,cost });
}

template<typename T>
vector<T> make_v(size_t a) { return vector<T>(a); }

template<typename T, typename... Ts>
auto make_v(size_t a, Ts... ts) {
	return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

template<typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type
fill_v(T & t, const V & v) { t = v; }

template<typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type
fill_v(T & t, const V & v) {
	for (auto& e : t) fill_v(e, v);
}

template<typename T>
void outp(vector<T>v) {
	for (int i = 0; i < v.size(); ++i) {
		cout << v[i];
		if (i != v.size() - 1) { cout << " "; }
	}
}

double add(double a, double b) {
	if (abs(a + b) < EPS * (abs(a) + abs(b))) { return 0; }
	return a + b;
}

int n, k[1005];
bool isIn[1005];
int main() {
	cin >> n;
	stack<int>s;
	for (int i = 0; i < n; ++i) {
		cin >> k[i];
		if (k[i] > 0) {
			if (isIn[k[i]]) {
				cout << i + 1 << endl;
				return 0;
			}
			s.push(k[i]);
			isIn[k[i]] = 1;
		}
		else {
			k[i] *= -1;
			if ((!s.empty() && s.top() != k[i]) || s.empty()) {
				cout << i + 1 << endl;
				return 0;
			}
			s.pop();
			isIn[k[i]] = 0;
		}
	}

	cout << "OK" << endl;
}

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

int main(void){
  int n, stk[2000], sp;
  int flg[200]={0}, suc;
  
  cin >> n;
  sp = 0; suc = 0;
  for (int i=1; i<=n; i++){
    int t; cin >> t;
    if (t>0){ // in
      if (flg[t]){
        //cout << "deb: same cat went into twice" << endl;
        cout << i << endl;
        break;
      }
      flg[t]=1;
      stk[sp++] = t;
    } else { // out
      t = -t;
      if (!flg[t] || sp==0 || stk[sp-1]!=t){
        //if (!flg[t]) cout << "deb: the cat didnt went into" << endl;
        //if (sp==0) cout << "deb: no cat" << endl;
        //if (stk[sp-1]!=t) cout << "deb: the cat isnt the top" << endl;
        cout << i << endl;
        break;
      }
      sp--;
      flg[t]=0;
    }
    if (i==n) suc = 1;
  }
  if (suc) cout << "OK" << endl;
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
using namespace std;
typedef long long int ll;

int main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	int n; cin >> n;
	map<int,int> mp;
	stack<int> q;
	for(int i=1;i<=n;i++){
		int x; cin >> x;
		if(x>0){
			if(mp[x]>0){
				cout << i << endl;
				return 0;
			}
			q.push(x);
			mp[x]++;
		}
		else{
			if(mp[-x]==1&&q.top()==-x){
				q.pop();
				mp[-x]--;
			}
			else{
				cout << i << endl;
				return 0;
			}
		}
	}
	cout << "OK" << endl;
}

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

int main() {
	int L, c, i;
	cin >> L;
	stack<int> a;
	unordered_set<int> b;
	for (i = 1; i <= L; ++i) {
		cin >> c;
		if (0 < c) {
			if (b.count(c)) break;
			else {
				a.push(c);
				b.insert(a.top());
			}
		}
		else {
			if (a.empty()) break;
			else {
				if (a.top() + c) break;
				else {
					b.erase(a.top());
					a.pop();
				}
			}
		}
	}
	if (1 + L == i) cout << "OK";
	else cout << i;
	cout << endl;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define INF 1000000010
#define EPS 1e-9
#define fst first
#define scd second

#define debug(x) cout<<x<<endl;
#define repi(i,x,n) for(int i=x;i<n;i++)
#define rep(i,n) repi(i,0,n)
#define lp(i,n) repi(i,0,n)
#define repn(i,n) for(int i=n;i>=0;i--)
#define int long long
#define endl "\n"


signed main(){
  cin.tie(0);	
  ios::sync_with_stdio(false);
  int l;
  cin>>l;
  int a[1000];
  rep(i,l) cin>>a[i];
  stack<int> cat;
  int flag[101]={0};
  rep(i,l){
    if(a[i]>0){
      if(flag[a[i]]==1){cout<<i+1<<endl;return 0;}
      cat.push(a[i]);
      flag[a[i]]=1;
    }else{
      if(cat.empty()){cout<<i+1<<endl;return 0;}
      if(cat.top()!=abs(a[i])){cout<<i+1<<endl;return 0;}
      cat.pop();
      flag[abs(a[i])]=0;
    }
  }
  cout<<"OK"<<endl;
  return 0;
}

C++: #include "bits/stdc++.h"

#define REP(i,num) for(int i=0;i<(num);++i)
#define LOOP(i) while(i--)
#define ALL(c) c.begin(),c.end()
#define PRINTALL(c) for(auto pitr=c.begin();pitr!=c.end();++pitr){cout<<*pitr;if(next(pitr,1)!=c.end())cout<<' ';}cout<<endl;
#define PAIRCOMP(c,comp) [](const pair<ll,ll>& lhs,const pair<ll,ll>& rhs){return lhs.c comp rhs.c;}

using namespace std;
using ll = long long;

constexpr ll atcoder_mod = 1e9+7;

template<typename T=int>
T in(){T x; cin >> x; return (x);}
template<typename T=int,typename C=vector<T>>
C vecin(int N){C x(N);REP(i,N){x[i]=in<T>();}return move(x);}

void vout(){cout << endl;}
template<typename Head,typename... Tail>
void vout(Head&& h,Tail&&... t){cout << ' ' << h;vout(forward<Tail>(t)...);}
void out(){cout << endl;}
template<typename Head,typename... Tail>
void out(Head&& h,Tail&&... t){cout << h;vout(forward<Tail>(t)...);}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);

	int L=in();
	auto V=vecin(L);
	stack<int> S;
	set<int> H;
	REP(i,L){
		if(V[i]>0){
			if(H.find(V[i])!=H.end()){
				out(i+1);
				return 0;
			}
			S.push(V[i]);
			H.insert(V[i]);
		}
		else{
			if(S.empty()){
				out(i+1);
				return 0;
			}
			auto P=S.top();
			if(P!=abs(V[i])){
				out(i+1);
				return 0;
			}
			S.pop();
			H.erase(P);
		}
	}
	out("OK");
	return 0;
}