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>
#include<stack>
using namespace std;
int main(){
    int l; cin>>l;
    stack<int> s;
    int n,cat[101]={};
    for(int i=1;i<=l;++i){
	cin>>n;
	if(n>0){
	    s.push(n);
	    if(cat[n]==1){
		printf("%d\n",i);
		return 0;
	    }
	    cat[n]=1;
	}else{
	    if(s.empty()){
		printf("%d\n",i);
		return 0;
	    }
	    n=-n;
	    if(s.top()!=n){
		printf("%d\n",i);
		return 0;
	    }
	    cat[n]=0;
	    s.pop();
	}
    }

    printf("OK\n");
    return 0;
}

C++: #include<iostream>
#include<stdio.h>
#include<string>
#include <algorithm>
#include<cstring>
#include <math.h>
#include <iomanip>
using namespace std;

int main()
{
  int l;
  cin>>l;
  int c[1000];
  int x;
  for(int i=0;i<l;i++){
    cin>>x;
    if(i==0){
      if(x<0){
	cout<<1<<endl;
	return 0;
      }
      c[0]=x;
    }else{
      if(x<0){
	int ans;
	int k;
	for(k=i-1;k>=0;k--){
	  if(c[k]!=0)
	    ans=k;
	  if(c[k]!=0)
	    break;
	}
	if(-x==c[k]){
	  c[i]=0;
	  c[k]=0;
	}else if(x*(-1)!=c[k]){
	  cout<<i+1<<endl;
	  return 0;
	}
      }else{
	for(int j=0;j<i;j++){
	  if(c[j]==x){
	    cout<<i+1<<endl;
	    return 0;
	  }
	}
	c[i]=x;
      } 
    }
  }
  cout<<"OK"<<endl;
  return 0;
  
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
#include<utility>
#include<vector>
#include<queue>
#include<functional>
#include<map>
#include<set>
#include<cstring>
#include<string>
#include<stack>
#include<math.h>
#include<stdlib.h>
using namespace std;
#define int long long
typedef pair<int, int> pii;
typedef pair<int, pii > piii;
typedef pair<double, double> pdd;
typedef pair<int, char> pic;
typedef priority_queue<pii, vector<pii>, greater<pii> > RPQ;
#define mk make_pair
#define INF (1 << 30)
#define INFL (1ll << 60ll)
#define mod 1000000007

int N;
int a[10100];
stack<int> s;
bool in[10100];

signed main(){
    cin >> N;
    int flag = 0;
    for(int i = 0; i < N; i++){
        cin >> a[i];
        if(a[i] > 0){
            s.push(a[i]);
            if(in[a[i]]){
                flag = i + 1;
                break;
            }
            in[a[i]] = true;
        }
        else {
            a[i] *= -1;
            int top = 0;
            if(!s.empty()){
                top = s.top();
                s.pop();
            }
            if(!in[a[i]]){
                flag = i + 1;
                break;
            }
            in[a[i]] = false;
            if(top != a[i]){
                flag = i + 1;
                break;
            }
        }
    }
    if(flag == 0){
        cout << "OK" << endl;
    }
    else {
        cout << flag << endl;
    }
    

    return 0;
}

/*

*/

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

int main() {
	long long L, C;
	bool used[110] = {};
	stack<long long> ST;
	cin >> L;
	for (int i = 1; i <= L; i++) {
		cin >> C;
		if (C > 0) {
			if (used[C]) {
				cout << i << endl;
				return 0;
			}
			ST.push(C);
			used[C] = true;
		}
		else {
			if (ST.empty()) {
				cout << i << endl;
				return 0;
			}
			if (ST.top() != -C) {
				cout << i << endl;
				return 0;
			}
			ST.pop();
			used[-C] = false;
		}
	}
	cout << "OK" << endl;
}

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

int main(){
    int n;
    cin >> n;
    vector<int> ne(n);
    rep(i,n){
        cin >> ne[i];
    }
    int cnt = 1;
    bool t = true;
    vector<int> ima(n);
    stack<int> ana;
    ana.push(0);
    rep(j,n){
        if (ne[j] > 0){
            if (ima[ne[j]-1] == 0){
                ana.push(ne[j]);
                ima[ne[j]-1] = 1;
            }
            else{
                cnt += j;
                t = false;
                break;
            }
        }
        else{
            if (ana.top() + ne[j] == 0){
                ana.pop();
                ima[-ne[j]-1] = 0;
            }
            else{
                cnt += j;
                t = false;
                break;
            }
        }
    }
    if (t){
        cout << "OK" << endl;
    }
    else{
        cout << cnt << endl;
    }
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
long long inf = 1000000000000;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
int main() {
	int n;
	cin >> n;
	int a[1100];
	int i;
	for (i = 0; i < n; i++) {
		cin >> a[i];
	}
	stack<int> cave;
	bool cat[100] = {};
	for (i = 0; i < n; i++) {
		if (a[i] > 0) {
			if (!cat[a[i]]) {
				cave.push(a[i]);
				cat[a[i]]++;
			}
			else {
				cout << i + 1 << endl;
				return(0);
			}
		}
		else {
			a[i] *= -1;
			if (cat[a[i]] && a[i]==cave.top() && !cave.empty()) {
				cat[a[i]] = false;
				cave.pop();
			}
			else {
				cout << i + 1 << endl;
				return(0);
			}
		}
	}
	cout << "OK" << endl;
	return(0);
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<map>
#include<cstring>
#include<math.h>
#include<bitset>
#include<queue>
#include<set>
#include<iomanip>
#include<math.h>
#include<assert.h>
#include<string>
#include<cstdlib>
#include<cctype>
#include<unordered_map>
#include<sstream>
#include<numeric>
#include<stack>
// #include<bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int,int>;
constexpr ll INF = 1LL<<60;
const double INFD = 1e100;
const ll mod = 1000000007;
const double PI = 3.1415926535897932384626433832795028841971;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
};
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
};

// intやllをstringに変換
template<typename T> inline string toString(const T &a){
  ostringstream oss;
  oss << a;
  return oss.str();
};
// ----------------------------------------------------------------------------

int main(){
  int L;
  cin >> L;
  stack<int> s;
  set<int> se;
  for(int i=1; i<=L; i++){
    int q;
    cin >> q;
    if(q < 0){
      q = abs(q);
      if(se.size() == 0){
        cout << i<< endl;
        return 0;
      }
      if(s.top() == q){
        s.pop();
        se.erase(q);
        continue;
      }
      cout << i << endl;
      return 0;
    }else{
      if(se.count(q)){
        cout << i << endl;
        return 0;
      }
      se.insert(q);
      s.push(q);
    }
  }
  cout << "OK" << endl;
  return 0;
}