Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)
const INF = 0x7fffffff
func main() {
	S := 17
	seat := make([][]int, 27)
	for i :=range seat {
		seat[i] = make([]int, 27)
	}
	ans := make([]int, 105)
	id := 0
	Q := make([][]int, 0)
	for i := 0; i < S; i++ {
		seat[i][0] = -1
	}
	t := -1
	for ans[99] == 0 {
		t++
		if  t%5 == 0 && id <= 99 {
			if id % 5 == 1 {
				n := 5
				tmp := []int{id, n, t}
				Q = append(Q, tmp)
				id++
			} else {
				n := 2
				tmp := []int{id, n, t}
				Q = append(Q, tmp)
				id++
			}
		}
		for i := 0; i < 5; i++ {
			if seat[i][1] == 0 {
				continue
			}
			seat[i][1]--
			if seat[i][1] == 0 {
				seat[i][0] = -1
			}
		}
		f := len(Q)
		for f > 0 {
			i, n := Q[0][0], Q[0][1]
			f = 0
			for j := 0; j < S; j++ {
				g := 0
				for k := 0; k < n; k++ {
					if seat[j+k][0] >= 0 {
						g = 1
						break
					}
				}
				if g != 0 {
					continue
				}
				ans[i] = t-Q[0][2]
				Q = Q[1:]
				f = len(Q)
				for k := 0; k < n; k++ {
					seat[j+k][0] = i
					seat[j+k][1] = 17*(i&1) + 3*(i%3) + 19
				}
				break
			}
		}
	}
	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;
}