Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

type chair struct {
	group_id, rest_time int
}

type info struct {
	group_id, num, wait_sum, before_go int
	fixed                              bool
}

func main() {
	ans := make([]int, 100)
	chair := make([]chair, 17)
	for i := 0; i < 17; i++ {
		chair[i].group_id = -1
	}
	info := make([]info, 100)
	for i := 0; i <= 99; i++ {
		info[i].group_id = i
		info[i].before_go = 5 * i
		info[i].wait_sum = 0
		if i%5 == 1 {
			info[i].num = 5
		} else {
			info[i].num = 2
		}
		info[i].fixed = false
	}
	count := 0
	FLG := 0
	tmp_num := 0
	isOK := false
	waitFLG := false
	for {
		for i := 0; i < 17; i++ {
			if chair[i].group_id != -1 {
				chair[i].rest_time--
				if chair[i].rest_time <= 0 {
					chair[i].group_id = -1
				}
			}
		}
		waitFLG = false
		count = 0
		for i := 0; i < 100; i++ {
			if info[i].fixed {
				continue
			}
			if waitFLG == false && info[i].before_go == 0 {
				FLG = -1
				tmp_num = info[i].num
				for k := 0; k <= 17-tmp_num; k++ {
					isOK = true
					for p := 0; p < tmp_num; p++ {
						if chair[k+p].group_id != -1 {
							isOK = false
							break
						}
					}
					if isOK {
						FLG = k
						break
					}
				}
				if FLG != -1 {
					for k := FLG; k < FLG+tmp_num; k++ {
						chair[k].group_id = info[i].group_id
						chair[k].rest_time = 17*(i%2) + 3*(i%3) + 19
						info[i].fixed = true
					}
				} else {
					waitFLG = true
					info[i].wait_sum++
					count++
				}
			} else {
				if info[i].before_go == 0 {
					info[i].wait_sum++
				} else {
					info[i].before_go--
				}
				count++
			}
		}
		if count == 0 {
			break
		}
	}
	for i := 0; i <= 99; i++ {
		ans[i] = info[i].wait_sum
	}
	for scanner.Scan() {
		n, _ := strconv.Atoi(scanner.Text())
		fmt.Println(ans[n])
	}
}

Python: # -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0147

"""
import sys
from sys import stdin
from heapq import heappop, heappush
from collections import deque
input = stdin.readline


class Seat():
    # ??????????????¶???????????????????????????
    # '_'?????????????????????'o'???????????§??£??????????????????
    def __init__(self, n):
        self.seat = '_' * n

    def get(self, num):
        # num????????£?¶?????????§???????????????????????°?????§???????????????????????????????????????????????????????????????????????°-1????????????
        i = self.seat.find('_'*num)
        if i != -1:
            self.seat = self.seat[0:i] + 'o'*num + self.seat[i+num:]
            return i
        return -1

    def release(self, i, num):
        # ??§??£???????????????????????????????????????????????????????????????????????????????????´??§???????????????
        self.seat = self.seat[0:i] + '_'*num + self.seat[i+num:]


def solve():
    waiting_time = [-1] * 100   #  ?????°??????????????¨???????????????
    NUM_OF_SEAT = 17
    seat = Seat(NUM_OF_SEAT)    #  ???????????¶????????????????????????????????????
    LEAVE = 0                   #  ??\???????£???????????????????????????????????????\
    COME = 1                    #  LEAVE??¨COME??????????????????????????´????????????????????\?????§LEAVE????????????????????????????????????????????´???????????????
    in_out = []                 #  ??\?????????????????????????????????
    Q = deque()                      #  ??§??????????????????
    # 100???????????\????????????????????????
    for group_id in range(100):
        if group_id % 5 == 1:
            num = 5
        else:
            num = 2
        heappush(in_out, (group_id * 5, COME, NUM_OF_SEAT+1, group_id, num)) #  ????????????????????????????????????????¨?????????????????????´????????????????????????????????°????????????????????\????????°

    # ???????????\??????????????????????????????????????????????????????
    while in_out:
        time, event, start_seat, group_id, num = heappop(in_out)
        if event == COME:
            Q.append((time, group_id, num)) #  ??\????????´???????????¢????????£?????????????????§??????????????§??????????????§?????????
        else:
            seat.release(start_seat, num) #  ?£???????????????????????????????
        while Q:                #  ?????£???????????°??????????????§?????????????????§????????????
            res = seat.get(Q[0][2]) #  ?????????????????????????????§????????????
            if res != -1:
                arrive, group_id, num = Q.popleft()
                waiting_time[group_id] = time - arrive #  ????????°?????????????????\?????????????????§???????????§????????????????¨????
                eating_time = 17 * (group_id % 2) + 3*(group_id % 3) + 19 #  ?£??????????
                heappush(in_out, (time + eating_time, LEAVE, res, group_id, num)) #  ?????????????????????????????¨???????????????????????????
            else:
                break           #  ??????????????????????????°??????????????§???????????£????????§????¬??????°?????????????¢??????????
    return waiting_time         #  ??¨??°??????????????????????????????


def main(args):
    waiting_time = solve()

    for line in sys.stdin:
        print(waiting_time[int(line)])


if __name__ == '__main__':
    main(sys.argv[1:])

C++: #define _USE_MATH_DEFINES
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <cassert>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>
#include <iomanip>
#include <iterator>
 
using namespace std;
 
typedef long long ll;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;
 
#define FOR(i,n) for(int i = 0; i < (n); i++)
#define sz(c) ((int)c.size())

typedef unsigned long long ull;
const int INF = (int)1e8;


int main(){
	int m = 17;
	queue<int> q;
	vector<int> v(m,-1);
	set<Pii> st;
	int t = 0;
	vector<int> ans(100);

	while(t < 1000 || !q.empty()){
		bool change = false;
		if((t % 5 == 0) && (t / 5 < 100)){
			int i = t/5;
			q.push(i);
			st.insert(Pii(17*(i%2)+3*(i%3)+19 + t,i));
			ans[i] = t;
			change = true;
		}

		while(!st.empty() && (*(st.begin())).first == t){
			int c = (*(st.begin())).second;
			st.erase(st.begin());
			FOR(i,m) if(v[i] == c) v[i] = -1;
			change = true;
		}

		if(change) while(!q.empty()){
			int c = q.front();
			int l = (c%5 == 1) ? 5 : 2;
			int cnt = 0;
			int st = -1;
			FOR(i,m){
				if(v[i] == -1) cnt++;
				else cnt = 0;
				if(cnt == l){
					st = i - l + 1;
					break;
				}
			}
			if(st != -1){
				q.pop();
				ans[c] = t - ans[c];
				FOR(i,l) v[i+st] = c;
			} else {
				break;
			}
		}

		t++;
	}

	int n;
	while(cin>>n){
		cout << ans[n] << endl;
	}
	return 0;
}

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

main(){
  int data[100][2], ans[101];
  int seki[17], q=0;
  for(int i=0;i<100;i++){
    if(i%5==1){
      data[i][0]=5;
    }
    else{
      data[i][0]=2;
    }
    data[i][1]=17*(i%2)+3*(i%3)+19;
  }
  for(int i=0;i<17;i++){
    seki[i]=0;
  }
  
  for(int i=0;i<50;i++){
    // printf("!%d! ", i);
    for(int j=0;j<17;j++){
      //printf("%2d ", seki[j]);
    }
    //printf("\n");
    for(int j=0;j<17;j++){
      if(seki[j]>0){
	seki[j]--;
      }
    }

    while(q<=i/5){
      int tmp=q;
      if(data[q][0]==2){
	for(int j=0;j<16;j++){
	  if(seki[j]==0 && seki[j+1]==0){
	    seki[j]=seki[j+1]=data[q][1];
	    ans[q]=i-q*5;
	    q++;
	    break;
	  }
	}
      }
      else{
	for(int j=0;j<13;j++){
	  if(seki[j]==0 && seki[j+1]==0 && seki[j+2]==0 && seki[j+3]==0 && seki[j+4]==0){
	    seki[j]=seki[j+1]=seki[j+2]=seki[j+3]=seki[j+4]=data[q][1];
	    ans[q]=i-q*5;
	    q++;
	    break;
	  }
	}
      }
      if(tmp==q) break;
    }
  }
  
  int n;
  while(cin >> n){
    cout << ans[n] << endl;
  }
  
  return 0;
}