package round122;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class E2 {
	InputStream is;
	PrintWriter out;
	String INPUT = "9999999999999";

	void solve()
	{
		int M2 = 1<<13;
		int M5 = 5*5*5*5*5*5*5*5*5*5*5*5*5;

		long f = nl();
		int x2 = (int)(f % M2);
		int[] fa2 = new int[M2];
		int[] a2 = new int[1<<14];
		a2[0] = 0;
		a2[1] = 1;
		Arrays.fill(fa2, -1);
		fa2[0] = 0;
		fa2[1] = 1;
		for(int i = 2;i < 13000;i++){
			a2[i] = (a2[i-1] + a2[i-2]) % M2;
			if(fa2[a2[i]] == -1)fa2[a2[i]] = i;
		}
		if(fa2[x2] == -1){
			out.println(-1);
			return;
		}

		int f5 = (int)(f%5);
		List<Long> l = new ArrayList<Long>();
		{
			int[] a = new int[20];
			a[0] = 0;
			a[1] = 1;
			for(int i = 2;i < 4*5;i++){
				a[i] = (a[i-1] + a[i-2]) % 5;
			}
			for(int i = 0;i < 4*5;i++){
				if(a[i] == f5){
					l.add((long)i);
				}
			}
		}

		int[][] F = {
				{1, 1}, {1, 0}
		};
		for(long m = 25;m <= M5;m*=5){
			int fm = (int)(f % m);
			List<Long> nl = new ArrayList<Long>();
			for(long ll : l){
				for(int k = 0;k < 5;k++){
					long ne = ll+k*4*(m/5);
					long nf = 0;
					if(ne == 0){
						nf = 0;
					}else{
						nf = pow(F, new int[]{1, 0}, ne-1, (int)m)[0];
					}
					if(nf == fm){
						nl.add(ne);
					}
				}
			}
			l = nl;
		}

		long min = Long.MAX_VALUE;
		for(long ll : l){
			for(int u : a2){
				if(u == f % M2){
					long num = crtx(new long[]{M2*3/2, 4L*M5}, new long[]{u, ll}).longValue();
					if(num >= 0){
						min = Math.min(min, num);
					}
				}
			}
//			long num = crt(M2*3/2, fa2[x2], 4L*M5, ll);
		}
		if(min == Long.MAX_VALUE){
			out.println(-1);
		}else{
			out.println(min);
		}
	}

	public static long crt(long p, long m, long q, long n)
	{
		long[] apr = exGCD(p, q);
		if((n - m) % apr[0] != 0)return -1;
		long mod = p * q / apr[0];
		long a = (apr[1] * (n - m) / apr[0] * p + m) % mod;
		if(a < 0)a += mod;
		return a;
	}

	// int行列*ベクトル
	public static int[] mul(int[][] A, int[] v, int mod)
	{
		int m = A.length;
		int n = v.length;
		int[] w = new int[m];
		for(int i = 0;i < m;i++){
			long sum = 0;
			for(int k = 0;k < n;k++){
				sum += (long)A[i][k] * v[k];
				sum %= mod;
			}
			w[i] = (int)sum;
		}
		return w;
	}

	// int行列の2乗
	public static int[][] p2(int[][] A, int mod)
	{
		int n = A.length;
		int[][] C = new int[n][n];
		for(int i = 0;i < n;i++){
			for(int j = 0;j < n;j++){
				long sum = 0;
				for(int k = 0;k < n;k++){
					sum += (long)A[i][k] * A[k][j];
					sum %= mod;
				}
				C[i][j] = (int)sum;
			}
		}
		return C;
	}

	public static BigInteger[] exGCD(BigInteger a, BigInteger b)
	{
		BigInteger p = BigInteger.ONE, q = BigInteger.ZERO, r = BigInteger.ZERO, s = BigInteger.ONE;
		while(b.signum() > 0){
			BigInteger c = a.divide(b);
			BigInteger d;
			d = a; a = b; b = d.mod(b);
			d = p; p = q; q = d.subtract(c.multiply(q));
			d = r; r = s; s = d.subtract(c.multiply(s));
		}
		return new BigInteger[]{a, p, r};
	}

	public static BigInteger bi(long x){ return BigInteger.valueOf(x); }

	public static BigInteger crtx(long[] divs, long[] mods)
	{
		BigInteger div = bi(divs[0]), mod = bi(mods[0]);
		for(int i = 1;i < divs.length;i++){
			BigInteger[] apr = exGCD(div, bi(divs[i]));
			BigInteger mm = bi(mods[i]).subtract(mod);
			if(mm.mod(apr[0]).signum() != 0)return bi(-1);
			BigInteger da = div.divide(apr[0]);
			BigInteger ndiv = bi(divs[i]).multiply(da);
			BigInteger nmod = apr[1].multiply(mm).multiply(da).add(mod).mod(ndiv);
			if(nmod.signum() < 0)nmod = nmod.add(ndiv);
			div = ndiv;
			mod = nmod;
		}
		return mod;
	}

	// A^e*v
	public static int[] pow(int[][] A, int[] v, long e, int mod)
	{
		int[][] MUL = A;
		for(;e > 0;e>>>=1) {
			if((e&1)==1)v = mul(MUL, v, mod);
			MUL = p2(MUL, mod);
		}
		return v;
	}

	public static long[] exGCD(long a, long b)
	{
		long p = 1, q = 0, r = 0, s = 1;
		while(b > 0){
			long c = a / b;
			long d;
			d = a; a = b; b = d % b;
			d = p; p = q; q = d - c * q;
			d = r; r = s; s = d - c * s;
		}
		return new long[]{a, p, r};
	}

	void run() throws Exception
	{
		is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);

		long s = System.currentTimeMillis();
		solve();
		out.flush();
		tr(System.currentTimeMillis()-s+"ms");
	}

	public static void main(String[] args) throws Exception
	{
		new E2().run();
	}

	public int ni()
	{
		try {
			int num = 0;
			boolean minus = false;
			while((num = is.read()) != -1 && !((num >= '0' && num <= '9') || num == '-'));
			if(num == '-'){
				num = 0;
				minus = true;
			}else{
				num -= '0';
			}

			while(true){
				int b = is.read();
				if(b >= '0' && b <= '9'){
					num = num * 10 + (b - '0');
				}else{
					return minus ? -num : num;
				}
			}
		} catch (IOException e) {
		}
		return -1;
	}

	public long nl()
	{
		try {
			long num = 0;
			boolean minus = false;
			while((num = is.read()) != -1 && !((num >= '0' && num <= '9') || num == '-'));
			if(num == '-'){
				num = 0;
				minus = true;
			}else{
				num -= '0';
			}

			while(true){
				int b = is.read();
				if(b >= '0' && b <= '9'){
					num = num * 10 + (b - '0');
				}else{
					return minus ? -num : num;
				}
			}
		} catch (IOException e) {
		}
		return -1;
	}

	public String ns()
	{
		try{
			int b = 0;
			StringBuilder sb = new StringBuilder();
			while((b = is.read()) != -1 && (b == '\r' || b == '\n' || b == ' '));
			if(b == -1)return "";
			sb.append((char)b);
			while(true){
				b = is.read();
				if(b == -1)return sb.toString();
				if(b == '\r' || b == '\n' || b == ' ')return sb.toString();
				sb.append((char)b);
			}
		} catch (IOException e) {
		}
		return "";
	}

	public char[] ns(int n)
	{
		char[] buf = new char[n];
		try{
			int b = 0, p = 0;
			while((b = is.read()) != -1 && (b == ' ' || b == '\r' || b == '\n'));
			if(b == -1)return null;
			buf[p++] = (char)b;
			while(p < n){
				b = is.read();
				if(b == -1 || b == ' ' || b == '\r' || b == '\n')break;
				buf[p++] = (char)b;
			}
			return Arrays.copyOf(buf, p);
		} catch (IOException e) {
		}
		return null;
	}

	double nd() { return Double.parseDouble(ns()); }
	boolean oj = System.getProperty("ONLINE_JUDGE") != null;
	void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); }
}


//package round122;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class E2 {
	InputStream is;
	PrintWriter out;
	String INPUT = "";

	void solve()
	{
		int M2 = 1<<13;
		int M5 = 5*5*5*5*5*5*5*5*5*5*5*5*5;

		long f = nl();
		int x2 = (int)(f % M2);
		int[] fa2 = new int[M2];
		int[] a2 = new int[1<<14];
		a2[0] = 0;
		a2[1] = 1;
		Arrays.fill(fa2, -1);
		fa2[0] = 0;
		fa2[1] = 1;
		for(int i = 2;i < 13000;i++){
			a2[i] = (a2[i-1] + a2[i-2]) % M2;
			if(fa2[a2[i]] == -1)fa2[a2[i]] = i;
		}
		if(fa2[x2] == -1){
			out.println(-1);
			return;
		}

		int f5 = (int)(f%5);
		List<Long> l = new ArrayList<Long>();
		{
			int[] a = new int[20];
			a[0] = 0;
			a[1] = 1;
			for(int i = 2;i < 4*5;i++){
				a[i] = (a[i-1] + a[i-2]) % 5;
			}
			for(int i = 0;i < 4*5;i++){
				if(a[i] == f5){
					l.add((long)i);
				}
			}
		}

		int[][] F = {
				{1, 1}, {1, 0}
		};
		for(long m = 25;m <= M5;m*=5){
			int fm = (int)(f % m);
			List<Long> nl = new ArrayList<Long>();
			for(long ll : l){
				for(int k = 0;k < 5;k++){
					long ne = ll+k*4*(m/5);
					long nf = 0;
					if(ne == 0){
						nf = 0;
					}else{
						nf = pow(F, new int[]{1, 0}, ne-1, (int)m)[0];
					}
					if(nf == fm){
						nl.add(ne);
					}
				}
			}
			l = nl;
		}

		long min = Long.MAX_VALUE;
		for(long ll : l){
			for(int i = 0;i < a2.length;i++){
				if(a2[i] == f % M2){
					long num = crtx(new long[]{M2*3/2, 4L*M5}, new long[]{i, ll}).longValue();
					if(num >= 0){
						min = Math.min(min, num);
					}
				}
			}
//			long num = crt(M2*3/2, fa2[x2], 4L*M5, ll);
		}
		if(min == Long.MAX_VALUE){
			out.println(-1);
		}else{
			out.println(min);
		}
	}

	public static long crt(long p, long m, long q, long n)
	{
		long[] apr = exGCD(p, q);
		if((n - m) % apr[0] != 0)return -1;
		long mod = p * q / apr[0];
		long a = (apr[1] * (n - m) / apr[0] * p + m) % mod;
		if(a < 0)a += mod;
		return a;
	}

	// int行列*ベクトル
	public static int[] mul(int[][] A, int[] v, int mod)
	{
		int m = A.length;
		int n = v.length;
		int[] w = new int[m];
		for(int i = 0;i < m;i++){
			long sum = 0;
			for(int k = 0;k < n;k++){
				sum += (long)A[i][k] * v[k];
				sum %= mod;
			}
			w[i] = (int)sum;
		}
		return w;
	}

	// int行列の2乗
	public static int[][] p2(int[][] A, int mod)
	{
		int n = A.length;
		int[][] C = new int[n][n];
		for(int i = 0;i < n;i++){
			for(int j = 0;j < n;j++){
				long sum = 0;
				for(int k = 0;k < n;k++){
					sum += (long)A[i][k] * A[k][j];
					sum %= mod;
				}
				C[i][j] = (int)sum;
			}
		}
		return C;
	}

	public static BigInteger[] exGCD(BigInteger a, BigInteger b)
	{
		BigInteger p = BigInteger.ONE, q = BigInteger.ZERO, r = BigInteger.ZERO, s = BigInteger.ONE;
		while(b.signum() > 0){
			BigInteger c = a.divide(b);
			BigInteger d;
			d = a; a = b; b = d.mod(b);
			d = p; p = q; q = d.subtract(c.multiply(q));
			d = r; r = s; s = d.subtract(c.multiply(s));
		}
		return new BigInteger[]{a, p, r};
	}

	public static BigInteger bi(long x){ return BigInteger.valueOf(x); }

	public static BigInteger crtx(long[] divs, long[] mods)
	{
		BigInteger div = bi(divs[0]), mod = bi(mods[0]);
		for(int i = 1;i < divs.length;i++){
			BigInteger[] apr = exGCD(div, bi(divs[i]));
			BigInteger mm = bi(mods[i]).subtract(mod);
			if(mm.mod(apr[0]).signum() != 0)return bi(-1);
			BigInteger da = div.divide(apr[0]);
			BigInteger ndiv = bi(divs[i]).multiply(da);
			BigInteger nmod = apr[1].multiply(mm).multiply(da).add(mod).mod(ndiv);
			if(nmod.signum() < 0)nmod = nmod.add(ndiv);
			div = ndiv;
			mod = nmod;
		}
		return mod;
	}

	// A^e*v
	public static int[] pow(int[][] A, int[] v, long e, int mod)
	{
		int[][] MUL = A;
		for(;e > 0;e>>>=1) {
			if((e&1)==1)v = mul(MUL, v, mod);
			MUL = p2(MUL, mod);
		}
		return v;
	}

	public static long[] exGCD(long a, long b)
	{
		long p = 1, q = 0, r = 0, s = 1;
		while(b > 0){
			long c = a / b;
			long d;
			d = a; a = b; b = d % b;
			d = p; p = q; q = d - c * q;
			d = r; r = s; s = d - c * s;
		}
		return new long[]{a, p, r};
	}

	void run() throws Exception
	{
		is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);

		long s = System.currentTimeMillis();
		solve();
		out.flush();
		tr(System.currentTimeMillis()-s+"ms");
	}

	public static void main(String[] args) throws Exception
	{
		new E2().run();
	}

	public int ni()
	{
		try {
			int num = 0;
			boolean minus = false;
			while((num = is.read()) != -1 && !((num >= '0' && num <= '9') || num == '-'));
			if(num == '-'){
				num = 0;
				minus = true;
			}else{
				num -= '0';
			}

			while(true){
				int b = is.read();
				if(b >= '0' && b <= '9'){
					num = num * 10 + (b - '0');
				}else{
					return minus ? -num : num;
				}
			}
		} catch (IOException e) {
		}
		return -1;
	}

	public long nl()
	{
		try {
			long num = 0;
			boolean minus = false;
			while((num = is.read()) != -1 && !((num >= '0' && num <= '9') || num == '-'));
			if(num == '-'){
				num = 0;
				minus = true;
			}else{
				num -= '0';
			}

			while(true){
				int b = is.read();
				if(b >= '0' && b <= '9'){
					num = num * 10 + (b - '0');
				}else{
					return minus ? -num : num;
				}
			}
		} catch (IOException e) {
		}
		return -1;
	}

	public String ns()
	{
		try{
			int b = 0;
			StringBuilder sb = new StringBuilder();
			while((b = is.read()) != -1 && (b == '\r' || b == '\n' || b == ' '));
			if(b == -1)return "";
			sb.append((char)b);
			while(true){
				b = is.read();
				if(b == -1)return sb.toString();
				if(b == '\r' || b == '\n' || b == ' ')return sb.toString();
				sb.append((char)b);
			}
		} catch (IOException e) {
		}
		return "";
	}

	public char[] ns(int n)
	{
		char[] buf = new char[n];
		try{
			int b = 0, p = 0;
			while((b = is.read()) != -1 && (b == ' ' || b == '\r' || b == '\n'));
			if(b == -1)return null;
			buf[p++] = (char)b;
			while(p < n){
				b = is.read();
				if(b == -1 || b == ' ' || b == '\r' || b == '\n')break;
				buf[p++] = (char)b;
			}
			return Arrays.copyOf(buf, p);
		} catch (IOException e) {
		}
		return null;
	}

	double nd() { return Double.parseDouble(ns()); }
	boolean oj = System.getProperty("ONLINE_JUDGE") != null;
	void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); }
}


import java.util.List;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.math.BigInteger;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskE solver = new TaskE();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskE {
    static final long MODULO = (long) 1e13;

    private long[][] mul(long[][] a, long[][] b, long modulo) {
        long[][] c = new long[2][2];
        for (int i = 0; i < 2; ++i)
            for (int j = 0; j < 2; ++j) {
                BigInteger sum = BigInteger.ZERO;
                for (int k = 0; k < 2; ++k)
                    sum = sum.add(BigInteger.valueOf(a[i][k]).multiply(BigInteger.valueOf(b[k][j])));
                c[i][j] = sum.mod(BigInteger.valueOf(modulo)).longValue();
            }
        return c;
    }

    static class Candidate {
        long offset;
        long[][] trans;

        Candidate(long offset, long[][] trans) {
            this.offset = offset;
            this.trans = trans;
        }
    }

    public void solve(int testNumber, InputReader in, PrintWriter out) {
        long f = in.nextLong();
        long mod = 1;
        long period = 1;
        long[][] mPeriod = new long[][]{new long[]{0, 1}, new long[]{1, 1}};
        List<Candidate> offsets = new ArrayList<Candidate>();
        offsets.add(new Candidate(0, new long[][]{new long[]{1, 0}, new long[]{0, 1}}));
        for (int i = 0; i < 26; ++i) {
            if (i < 13)
                mod *= 2;
            else
                mod *= 5;
            long nperiod = period;
            long[][] nmPeriod = mPeriod;
            while (nmPeriod[0][1] % mod != 0 || nmPeriod[1][1] % mod != 1) {
                nmPeriod = mul(nmPeriod, mPeriod, MODULO);
                nperiod += period;
            }
            List<Candidate> newOffsets = new ArrayList<Candidate>();
            for (Candidate old : offsets) {
                long[][] trans = old.trans;
                for (long noffset = old.offset; noffset < nperiod; noffset += period) {
                    if ((trans[0][1] % mod == f % mod)) {
                        newOffsets.add(new Candidate(noffset, trans));
                    }
                    trans = mul(trans, mPeriod, MODULO);
                }
            }
            offsets = newOffsets;
            period = nperiod;
            mPeriod = nmPeriod;
        }
        long min = Long.MAX_VALUE;
        for (Candidate c : offsets)
            min = Math.min(min, c.offset);
        if (min == Long.MAX_VALUE) out.println(-1); else out.println(min);
	}
}

class InputReader {
    private BufferedReader reader;
    private StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream));
        tokenizer = null;
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }

    public long nextLong() {
        return Long.parseLong(next());
    }

    }


import java.io.*;
import java.util.*;
import java.math.*;

public class Main {
	public static void main(String[] args) {
		new Main().run();
	}

	final static int PERIOD = 60;
	final static int POWER = 13;

	long fib(long n, long mod) {
		long[][] mat = new long[2][2];
		mat[0][1] = mat[1][0] = mat[1][1] = 1;
		Matrix v = new Matrix(mat);
		v = v.power(n, mod);
		return v.mat[0][1];
	}

	void run() {
		InputReader in = new InputReader(System.in);
		PrintWriter out = new PrintWriter(System.out);

		long num = in.nextLong(), mod = 10, per = PERIOD;
		ArrayList<Long> valid = new ArrayList<Long>();
		for (int i = 0; i < PERIOD; ++i)
			if (fib(i, mod) == num % mod)
				valid.add((long)i);

		for (int i = 1; i < POWER; ++i) {
			mod *= 10;
			long rem = num % mod;
			ArrayList<Long> nv = new ArrayList<Long>();
			for (int j = 0; j < 10; ++j)
				for (int k = 0; k < valid.size(); ++k) {
					if (fib(j * per + valid.get(k), mod) == rem)
						nv.add(j * per + valid.get(k));
				}
			valid = nv;
			per *= 10;
		}

		if (valid.size() == 0)
			out.println(-1);
		else
			out.println(valid.get(0));
		out.close();
	}
}

class Matrix {
	long[][] mat;
	final static Matrix ONE = new Matrix(new long[][] {{1, 0}, {0, 1}});
	Matrix(long[][] mat) {
		this.mat = mat;
	}
	long add(long a, long b, long mod) {
		if (a + b >= mod)
			return a + b - mod;
		return a + b;
	}
	long multiply(long a, long b, long mod) {
		long ret = 0;
		while (a > 0) {
			if ((a & 1) == 1)
				ret = add(ret, b, mod);
			b = add(b, b, mod);
			a >>= 1;
		}
		return ret;
	}
	Matrix multiply(Matrix o, long mod) {
		long[][] ret = new long[2][2];
		for (int i = 0; i < 2; ++i)
			for (int j = 0; j < 2; ++j)
				for (int k = 0; k < 2; ++k)
					ret[i][j] = add(ret[i][j], multiply(mat[i][k], o.mat[k][j], mod), mod);
		return new Matrix(ret);
	}
	Matrix power(long p, long mod) {
		if (p == 0) {
			return Matrix.ONE;
		}
		Matrix ret = power(p / 2, mod);
		ret = ret.multiply(ret, mod);
		if ((p & 1) == 1)
			ret = ret.multiply(this, mod);
		return ret;
	}
}

class InputReader {
	BufferedReader buff;
	StringTokenizer tokenizer;

	InputReader(InputStream stream) {
		buff = new BufferedReader(new InputStreamReader(stream));
		tokenizer = null;
	}
	boolean hasNext() {
		while (tokenizer == null || !tokenizer.hasMoreTokens())
			try {
				tokenizer = new StringTokenizer(buff.readLine());
			}
			catch (Exception e) {
				return false;
			}
		return true;
	}
	String next() {
		if (!hasNext())
			throw new RuntimeException();
		return tokenizer.nextToken();
	}
	int nextInt() { return Integer.parseInt(next()); }
	long nextLong() { return Long.parseLong(next()); }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.OutputStream;
import java.util.Arrays;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedOutputStream;
import java.util.HashSet;
import java.util.StringTokenizer;
import java.io.Writer;
import java.io.BufferedReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author An Almost Retired Guy
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        MogohuReaIdol solver = new MogohuReaIdol();
        solver.solve(1, in, out);
        out.close();
    }

    static class MogohuReaIdol {
        public void solve(int testNumber, InputReader in, OutputWriter out) {
            Point[] sum = minkowskiSum(minkowskiSum(readPolygon(in), readPolygon(in)), readPolygon(in));
            Point[] query = readPolygon(in);
            for (int i = 0; i < query.length; i++) query[i] = query[i].mul(3);
            HashSet<EzIntIntPair> Sum = new HashSet<>(), Query = new HashSet<>(), set = new HashSet<>();
            for (Point P : sum) Sum.add(P.toPair());
            for (Point P : query) Query.add(P.toPair());
            Point[] join = new Point[sum.length + query.length];
            for (int i = 0; i < sum.length; i++) join[i] = sum[i];
            for (int i = 0; i < query.length; i++) join[sum.length + i] = query[i];
            ConvexHull.Point[] hull = ConvexHull.convexHull(join);
            for (ConvexHull.Point P : hull) {
                EzIntIntPair pair = new EzIntIntPair(P.x, P.y);
                if (Query.contains(pair) && !Sum.contains(pair)) set.add(pair);
            }
            for (Point P : query) out.println(set.contains(P.toPair()) ? "NO" : "YES");
        }

        Point[] minkowskiSum(Point[] A, Point[] B) {
            int idxA = 0, idxB = 0;
            for (int i = 1; i < A.length; i++) if (A[i].compareTo(A[idxA]) < 0) idxA = i;
            for (int i = 1; i < B.length; i++) if (B[i].compareTo(B[idxB]) < 0) idxB = i;
            Point[] ans = new Point[A.length + B.length];
            Point P = A[idxA].add(B[idxB]);
            for (int i = 0, j = 0; i < A.length || j < B.length; ) {
                ans[i + j] = P;
                int I = idxA + i, J = idxB + j;
                Point U = new Point(A[I % A.length], A[(I + 1) % A.length]), V = new Point(B[J % B.length], B[(J + 1) % B.length]);
                if (j == B.length || (i < A.length && U.cross(V) >= 0)) {
                    P = P.add(U);
                    i++;
                } else {
                    P = P.add(V);
                    j++;
                }
            }
            return ans;
        }

        Point[] readPolygon(InputReader in) {
            int n = in.nextInt();
            Point[] ans = new Point[n];
            for (int i = 0; i < n; i++) ans[i] = new Point(in.nextInt(), in.nextInt());
            return ans;
        }

        class Point extends ConvexHull.Point implements Comparable<Point> {
            Point(int x, int y) {
                super(x, y);
            }

            Point(Point A, Point B) {
                this(B.x - A.x, B.y - A.y);
            }

            public int compareTo(Point that) {
                int cmp = Integer.compare(this.x, that.x);
                if (cmp != 0) return cmp;
                return Integer.compare(this.y, that.y);
            }

            long cross(Point that) {
                return (long) this.x * that.y - (long) this.y * that.x;
            }

            Point add(Point that) {
                return new Point(this.x + that.x, this.y + that.y);
            }

            Point mul(int factor) {
                return new Point(x * factor, y * factor);
            }

            EzIntIntPair toPair() {
                return new EzIntIntPair(x, y);
            }

        }

    }

    static class OutputWriter extends PrintWriter {
        public OutputWriter(OutputStream outputStream) {
            super(new BufferedOutputStream(outputStream));
        }

        public OutputWriter(Writer writer) {
            super(writer);
        }

        public void close() {
            super.close();
        }

    }

    static class EzIntIntPair implements Comparable<EzIntIntPair> {
        private static final int HASHCODE_INITIAL_VALUE = 0x811c9dc5;
        private static final int HASHCODE_MULTIPLIER = 0x01000193;
        public final int first;
        public final int second;

        public EzIntIntPair(int first, int second) {
            this.first = first;
            this.second = second;
        }

        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            EzIntIntPair that = (EzIntIntPair) o;

            return first == that.first && second == that.second;
        }

        public int hashCode() {
            int hash = HASHCODE_INITIAL_VALUE;
            hash = (hash ^ PrimitiveHashCalculator.getHash(first)) * HASHCODE_MULTIPLIER;
            hash = (hash ^ PrimitiveHashCalculator.getHash(second)) * HASHCODE_MULTIPLIER;
            return hash;
        }

        public int compareTo(EzIntIntPair other) {
            int cmpU = Integer.compare(first, other.first);
            return cmpU != 0 ? cmpU : Integer.compare(second, other.second);
        }

        public String toString() {
            return "(" + first + ", " + second + ")";
        }

    }

    static class ConvexHull {
        public static ConvexHull.Point[] convexHull(ConvexHull.Point[] p) {
            int n = p.length;
            if (n <= 1)
                return p;
            Arrays.sort(p, (a, b) -> Integer.compare(a.x, b.x) != 0 ? Integer.compare(a.x, b.x) : Integer.compare(a.y, b.y));
            ConvexHull.Point[] h = new ConvexHull.Point[n * 2];
            int cnt = 0;
            for (int i = 0; i < n; h[cnt++] = p[i++])
                while (cnt > 1 && cross(h[cnt - 2], h[cnt - 1], p[i]) >= 0)
                    --cnt;
            for (int i = n - 2, t = cnt; i >= 0; h[cnt++] = p[i--])
                while (cnt > t && cross(h[cnt - 2], h[cnt - 1], p[i]) >= 0)
                    --cnt;
            return Arrays.copyOf(h, cnt - 1 - (h[0].x == h[1].x && h[0].y == h[1].y ? 1 : 0));
        }

        static long cross(ConvexHull.Point a, ConvexHull.Point b, ConvexHull.Point c) {
            return (long) (b.x - a.x) * (c.y - a.y) - (long) (b.y - a.y) * (c.x - a.x);
        }

        public static class Point {
            public final int x;
            public final int y;

            public Point(int x, int y) {
                this.x = x;
                this.y = y;
            }

        }

    }

    static final class PrimitiveHashCalculator {
        private PrimitiveHashCalculator() {
        }

        public static int getHash(int x) {
            return x;
        }

    }

    static class InputReader {
        BufferedReader br;
        StringTokenizer st;

        public InputReader(InputStream inputStream) {
            br = new BufferedReader(new InputStreamReader(inputStream));
        }

        public String next() {
            while (st == null || !st.hasMoreElements()) {
                st = new StringTokenizer(nextLine());
            }
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public String nextLine() {
            try {
                return br.readLine();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.TreeSet;
import java.io.BufferedOutputStream;
import java.util.StringTokenizer;
import java.io.Writer;
import java.io.BufferedReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author An Almost Retired Guy
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        MogohuReaIdol solver = new MogohuReaIdol();
        solver.solve(1, in, out);
        out.close();
    }

    static class MogohuReaIdol {
        void add(TreeSet<Point> set, Point point) {
            Point l = set.lower(point);
            if (l == null) set.add(point);
            else {
                Point r = set.higher(point);
                if (r == null || l.ccw(point, r)) set.add(point);
                else return;
            }
            l = set.lower(point);
            if (l != null) while (true) {
                Point ll = set.lower(l);
                if (ll == null || ll.ccw(l, point)) break;
                set.remove(l);
                l = ll;
            }
            Point r = set.higher(point);
            if (r == null) return;
            while (true) {
                Point rr = set.higher(r);
                if (rr == null || point.ccw(r, rr)) break;
                set.remove(r);
                r = rr;
            }
        }

        boolean test(TreeSet<Point> set, Point point) {
            if (set.contains(point)) return true;
            Point l = set.lower(point);
            if (l == null) return false;
            Point r = set.higher(point);
            if (r == null) return false;
            return !l.ccw(point, r);
        }

        public void solve(int testNumber, InputReader in, OutputWriter out) {
            Point[] sum = minkowskiSum(minkowskiSum(readPolygon(in), readPolygon(in)), readPolygon(in));
            Point[] query = readPolygon(in);
            for (int i = 0; i < query.length; i++) query[i] = query[i].mul(3);
            TreeSet<Point> lower = new TreeSet<>(), upper = new TreeSet<>((i, j) -> j.compareTo(i));
            for (Point P : sum) {
                add(lower, P);
                add(upper, P);
            }
            for (Point P : query) {
                boolean ans = test(lower, P) && test(upper, P);
                out.println(ans ? "YES" : "NO");
            }
        }

        Point[] minkowskiSum(Point[] A, Point[] B) {
            int idxA = 0, idxB = 0;
            for (int i = 1; i < A.length; i++) if (A[i].compareTo(A[idxA]) < 0) idxA = i;
            for (int i = 1; i < B.length; i++) if (B[i].compareTo(B[idxB]) < 0) idxB = i;
            Point[] ans = new Point[A.length + B.length];
            Point P = A[idxA].add(B[idxB]);
            for (int i = 0, j = 0; i < A.length || j < B.length; ) {
                ans[i + j] = P;
                int I = idxA + i, J = idxB + j;
                Point U = new Point(A[I % A.length], A[(I + 1) % A.length]), V = new Point(B[J % B.length], B[(J + 1) % B.length]);
                if (j == B.length || (i < A.length && U.cross(V) >= 0)) {
                    P = P.add(U);
                    i++;
                } else {
                    P = P.add(V);
                    j++;
                }
            }
            return ans;
        }

        Point[] readPolygon(InputReader in) {
            int n = in.nextInt();
            Point[] ans = new Point[n];
            for (int i = 0; i < n; i++) ans[i] = new Point(in.nextInt(), in.nextInt());
            return ans;
        }

        class Point extends ConvexHull.Point implements Comparable<Point> {
            Point(int x, int y) {
                super(x, y);
            }

            Point(Point A, Point B) {
                this(B.x - A.x, B.y - A.y);
            }

            public int compareTo(Point that) {
                int cmp = Integer.compare(this.x, that.x);
                if (cmp != 0) return cmp;
                return Integer.compare(this.y, that.y);
            }

            long cross(Point that) {
                return (long) this.x * that.y - (long) this.y * that.x;
            }

            Point add(Point that) {
                return new Point(this.x + that.x, this.y + that.y);
            }

            Point mul(int factor) {
                return new Point(x * factor, y * factor);
            }

            boolean ccw(Point A, Point B) {
                Point U = new Point(A.x - x, A.y - y), V = new Point(B.x - x, B.y - y);
                long cross = (long) U.x * V.y - (long) U.y * V.x;
                return cross > 0;
            }

        }

    }

    static class ConvexHull {
        public static class Point {
            public final int x;
            public final int y;

            public Point(int x, int y) {
                this.x = x;
                this.y = y;
            }

        }

    }

    static class OutputWriter extends PrintWriter {
        public OutputWriter(OutputStream outputStream) {
            super(new BufferedOutputStream(outputStream));
        }

        public OutputWriter(Writer writer) {
            super(writer);
        }

        public void close() {
            super.close();
        }

    }

    static class InputReader {
        BufferedReader br;
        StringTokenizer st;

        public InputReader(InputStream inputStream) {
            br = new BufferedReader(new InputStreamReader(inputStream));
        }

        public String next() {
            while (st == null || !st.hasMoreElements()) {
                st = new StringTokenizer(nextLine());
            }
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public String nextLine() {
            try {
                return br.readLine();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class Idol implements Runnable {
    static class Point {
        final long x;
        final long y;

        Point(long x, long y) {
            this.x = x;
            this.y = y;
        }
    }

    class Polygon {
        List<Point> vertices = new ArrayList<Point>();
        List<Point> leftBorder = new ArrayList<Point>();
        List<Point> rightBorder = new ArrayList<Point>();

        void read() throws IOException {
            int n = nextInt();
            for (int i = 0; i < n; ++i) {
                int x = nextInt();
                int y = nextInt();
                vertices.add(new Point(x, y));
            }
        }

        public void prepareBorders() {
            long maxy = Long.MIN_VALUE;
            long miny = Long.MAX_VALUE;
            for (Point p : vertices) {
                miny = Math.min(miny, p.y);
                maxy = Math.max(maxy, p.y);
            }
            {
                int rightStart;
                for (rightStart = 0; rightStart < vertices.size(); ++rightStart) {
                    if (vertices.get(rightStart).y == miny && vertices.get((rightStart + 1) % vertices.size()).y > miny) {
                        break;
                    }
                }
                for (int i = rightStart;; i = (i + 1) % vertices.size()) {
                    rightBorder.add(vertices.get(i));
                    if (vertices.get(i).y == maxy)
                        break;
                }
            }
            {
                int leftStart;
                for (leftStart = 0; leftStart < vertices.size(); ++leftStart) {
                    if (vertices.get(leftStart).y == maxy && vertices.get((leftStart + 1) % vertices.size()).y < maxy) {
                        break;
                    }
                }
                for (int i = leftStart;; i = (i + 1) % vertices.size()) {
                    leftBorder.add(vertices.get(i));
                    if (vertices.get(i).y == miny)
                        break;
                }
            }
        }

        public boolean inside(Point point) {
            int rightPos;
            int leftPos;
            {
                int left = -1;
                int right = rightBorder.size();
                while (right - left > 1) {
                    int middle = (left + right) / 2;
                    if (rightBorder.get(middle).y <= point.y)
                        left = middle;
                    else
                        right = middle;
                }
                if (left < 0) return false;
                if (left == rightBorder.size() - 1) --left;
                rightPos = left;
            }
            {
                int left = -1;
                int right = leftBorder.size();
                while (right - left > 1) {
                    int middle = (left + right) / 2;
                    if (leftBorder.get(middle).y >= point.y)
                        left = middle;
                    else
                        right = middle;
                }
                if (left < 0) return false;
                if (left == leftBorder.size() - 1) --left;
                leftPos = left;
            }
            int z = 1;
            z *= signum(rightBorder.get(rightPos), point, rightBorder.get(rightPos + 1));
            z *= signum(leftBorder.get(leftPos), leftBorder.get(leftPos + 1), point);
            return z <= 0;
        }
    }

    private static int signum(Point a, Point b, Point c) {
        long dxb = b.x - a.x;
        long dyb = b.y - a.y;
        long dxc = c.x - a.x;
        long dyc = c.y - a.y;
        long z = dxb * dyc - dxc * dyb;
        if (z < 0) return -1; else if (z > 0) return 1; else return 0;
    }

    public Polygon add(Polygon a, Polygon b) {
        Polygon c = new Polygon();
        int sa = 0;
        {
            Point sap = a.vertices.get(0);
            for (int i = 1; i < a.vertices.size(); ++i) {
                Point p = a.vertices.get(i);
                if (p.y < sap.y || p.y == sap.y && p.x < sap.x) {
                    sap = p;
                    sa = i;
                }
            }
        }
        int sb = 0;
        {
            Point sbp = b.vertices.get(0);
            for (int i = 1; i < b.vertices.size(); ++i) {
                Point p = b.vertices.get(i);
                if (p.y < sbp.y || p.y == sbp.y && p.x < sbp.x) {
                    sbp = p;
                    sb = i;
                }
            }
        }
        int ra = sa;
        int rb = sb;
        while (true) {
            Point ca = a.vertices.get(sa);
            Point na = a.vertices.get((sa + 1) % a.vertices.size());
            Point cb = b.vertices.get(sb);
            Point nb = b.vertices.get((sb + 1) % b.vertices.size());
            c.vertices.add(addPoints(ca, cb));
            long dxa = na.x - ca.x;
            long dya = na.y - ca.y;
            long dxb = nb.x - cb.x;
            long dyb = nb.y - cb.y;
            long z = (dxa * dyb - dya * dxb);
            if (z > 0) {
                sa = (sa + 1) % a.vertices.size();
            } else if (z < 0) {
                sb = (sb + 1) % b.vertices.size();
            } else {
                sa = (sa + 1) % a.vertices.size();
                sb = (sb + 1) % b.vertices.size();
            }
            if (sa == ra && sb == rb)
                break;
        }
        return c;
    }

    private Point addPoints(Point ca, Point cb) {
        return new Point(ca.x + cb.x, ca.y + cb.y);
    }

    private void solve() throws IOException {
        Polygon[] p = new Polygon[3];
        for (int i = 0; i < p.length; ++i) {
            p[i] = new Polygon();
            p[i].read();
        }
        Polygon sum = p[0];
        for (int i = 1; i < p.length; ++i) {
            sum = add(sum, p[i]);
        }
        sum.prepareBorders();
        int m = nextInt();
        for (int i = 0; i < m; ++i) {
            int x = nextInt() * 3;
            int y = nextInt() * 3;
            Point pp = new Point(x, y);
            if (sum.inside(pp)) {
                writer.println("YES");
            } else {
                writer.println("NO");
            }
        }
    }

    public static void main(String[] args) {
        new Idol().run();
    }

    BufferedReader reader;
    StringTokenizer tokenizer;
    PrintWriter writer;

    public void run() {
        try {
            reader = new BufferedReader(new InputStreamReader(System.in));
            tokenizer = null;
            writer = new PrintWriter(System.out);
            solve();
            reader.close();
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }

    long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }

    double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }

    String nextToken() throws IOException {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            tokenizer = new StringTokenizer(reader.readLine());
        }
        return tokenizer.nextToken();
    }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.TreeSet;
/*
5
4 1 1 1 3 3 3 3 1
4 4 3 6 2 6 0 4 0
6 -5 3 -4 4 -3 4 -2 3 -3 2 -4 2
3 0 -1 1 -3 -1 -3
4 1 -4 1 -6 -1 -6 -1 -4
5
8 1 1 1 2 1 3 2 3 3 3 3 2 3 1 2 1
4 4 3 6 2 6 0 4 0
6 -5 3 -4 4 -3 4 -2 3 -3 2 -4 2
3 0 -1 1 -3 -1 -3
4 1 -4 1 -6 -1 -6 -1 -4
7
4
1 0
1 1
2 1
2 0
4
4 -1
4 0
5 0
5 -1
4
7 0
7 1
8 1
8 0
4
10 -1
10 0
11 0
11 -1
4
-1 0
-1 -1
-2 -1
-2 0
4
-4 1
-4 0
-5 0
-5 1
4
-7 0
-7 -1
-8 -1
-8 0
 */
public class Geom {

	static int N, totalSz;
	static Poly ar[];
	static boolean RADIAL_SORT,ID_SORT;
	static Vec radialPnts[];
	static TreeSet<Poly> ts;
	static boolean inTreeSet[];
	static final boolean debug = false;
	static Vec ORIGIN = new Vec(0,0);
	static final double eps = 1e-6;

	public static void main(String args[]) {
		FS in = new FS();
		N = in.nextInt();
		ar = new Poly[N];
		//Read in the input and make the polygons
		for(int i = 0; i < N; i++) {
			int sz = in.nextInt();
			Vec pts[] = new Vec[sz];
			for(int j = 0; j < sz; j++) {
				double x = in.nextInt();
				double y = in.nextInt();
				int id = sz-j-1; //make counter-clockwise
				pts[id] = new Vec(x,y,id);
			}
			ar[i] = new Poly(pts, i);
			totalSz += ar[i].sz;
		}
		radialPnts = new Vec[totalSz];
		int idd = 0;
		for(Poly p : ar)
			for(Vec v : p.vs)
				radialPnts[idd++] = v;
		setRadialSort();
		Arrays.sort(radialPnts);
		if(debug) System.out.println(Arrays.toString(radialPnts));
		initSweep();
		sweep();
		double res = 0;
		for(Poly p : ar)
			res += p.area();
		System.out.println(res);
	}

	static void sweep() {
		for(Vec v : radialPnts) {
			if(debug) {
				System.out.println("V= "+v);
				System.out.print("TS:");
				for(Poly p : ts) System.out.print(p.id+" ");
				System.out.println();
			}
			if(v.isLast) {
				//Removing this polygon
				Poly first = ts.first();
				ts.remove(v.poly);
				inTreeSet[v.poly.id] = false;
				if(first.id == v.poly.id && ts.size() > 0 ) { // If this polygon was first, add the lines to the next guy
					Poly next = ts.first();
					Seg s = new Seg(ORIGIN, v);
					next.intersectF(s, true);
					next.intersectR(s, true);
					next.isSeen = true; // This guy is seen
				}
				v.poly.killSegs();
			}
			else {
				v.poly.updateSegs(v);
				if(!inTreeSet[v.poly.id]) {
					Poly curFirst = ts.size() == 0 ? null : ts.first();
					ts.add(v.poly);
					inTreeSet[v.poly.id] = true;
					if(ts.first().id == v.poly.id) {
						v.poly.isSeen = true;
						if(curFirst != null) {
							Seg s = new Seg(ORIGIN, v);
							curFirst.intersectF(s, false);
							curFirst.intersectR(s, false);
						}
					}
				}
			}
		}
	}
	static void initSweep() {
		//Set up the fSegs and rSegs (but NOT isSeen / adding the lines/points);
		inTreeSet = new boolean[N];
		ts = new TreeSet<Poly>();
		for(Vec v : radialPnts) {
			if(debug) System.out.println("V= "+v);
			if(v.isLast) { //Remove this polygon
				ts.remove(v.poly);
				inTreeSet[v.poly.id] = false;
				v.poly.killSegs();
			}
			else {
				v.poly.updateSegs(v);
				if(!inTreeSet[v.poly.id]) ts.add(v.poly);
				inTreeSet[v.poly.id] = true;
			}
			if(debug) {
				System.out.print("TS:");
				for(Poly p : ts) System.out.print(p.id+" ");
				System.out.println();
			}
		}

		//Sweep is done, set the poly in the first of the treeset to be seen if there is one
		if(ts.size() > 0) {
			Poly first = ts.first();
			first.isSeen = true;
		}
		if(debug) System.out.println("Done with init\n\n\n");
	}

	static void setRadialSort() {
		RADIAL_SORT = true; ID_SORT = false;
	}
	static void setIDSort() {
		RADIAL_SORT = false; ID_SORT = true;
	}

	static class Vec implements Comparable<Vec>{
		double x,y;
		int id;
		double sId;
		Vec prev,next;
		Poly poly;
		boolean isLast;
		int polyRemoveId;

		public Vec(double xx, double yy) {x=xx;y=yy;}
		public Vec(double xx, double yy, int ii) {x=xx;y=yy;id=ii;sId=ii;}
		public double cross(Vec o) { return x*o.y - y*o.x; }
		public Vec add(Vec o) { return new Vec(x+o.x, y+o.y); }
		public Vec sub(Vec o) { return new Vec(x-o.x, y-o.y); }
		public double mag(Vec o) { return x*o.x + y*o.y; }
		public Vec scale(double d) { return new Vec(x*d,y*d);}

		public boolean inFrontOf(Seg o) {
			long c1 = (long)Math.round(o.a.cross(this));
			long c2 = (long)Math.round(this.cross(o.b));
			if(c1 >= 0 && c2 >= 0) {
				long c3 = (long)Math.round(o.dir.cross(this.sub(o.a)));
				return c3 >= 0;
			}
			return false;
		}

		public static int compare(Vec a, Vec b) {
			int res = 0;
			if(a.x < 0) res += a.y >= 0 ? 1 : 2;
			else if(a.y < 0) res += 3;
			if(b.x < 0) res -= b.y >= 0 ? 1 : 2;
			else if(b.y < 0) res -= 3;
			if(res == 0) {
				long cross = (long)Math.round(a.cross(b));
				//Further points come first if same angle
				if(cross == 0) return Long.compare((long)Math.round(b.mag(b)), (long)Math.round(a.mag(a)));
				if(cross > 0) return -1;
				else return 1;
			}
			return res;
		}

		@Override
		public int compareTo(Vec o) {
			if(RADIAL_SORT) {
				return Vec.compare(this, o);
			}
			else if(ID_SORT) {
				return Double.compare(sId, o.sId);
			}
			throw null;
		}
		@Override
		public String toString() {
			return "("+x+","+y+") id="+id+" p="+(poly == null ? -1 : poly.id)+" sId= "+sId;
		}
	}

	static class Seg{
		Vec a,b,dir;
		public Seg(Vec aa, Vec bb) {
			a=aa;
			b=bb;
			dir = b.sub(a);
		}

		public boolean inFrontOf(Seg o) {
			if(a.inFrontOf(o)) return true;
			if(b.inFrontOf(o)) return true;
			return false;
		}

		public Vec lineIntersect(Seg o) {
			double det = o.dir.x*dir.y-dir.x*o.dir.y;
			if(eq(det, 0)) return null;
			double dist = (o.dir.x * (o.a.y-a.y)-
					o.dir.y*(o.a.x-a.x))/det;
			return a.add(dir.scale(dist));
		}
	}
	static class Poly implements Comparable<Poly>{
		int sz, id;
		Vec vs[];
		Seg fSeg, rSeg;
		double fSegAId,rSegAId;
		boolean isSeen;
		ArrayList<Vec> interVecs;

		public Poly(Vec[] vss, int ii) {
			ArrayList<Vec> vs2 = new ArrayList<Vec>();
			for(int i = 0; i < vss.length; i++) {
				Vec v = vss[i];
				Vec prev = vss[(i-1+vss.length)%vss.length];
				Vec next = vss[(i+1)%vss.length];
				if((long)Math.round((v.sub(prev)).cross((next.sub(v)))) != 0) vs2.add(v);
			}
			vs = new Vec[vs2.size()];
			for(int i = 0; i < vs2.size(); i++) {
				Vec v = vs2.get(i);
				v.id = i; v.sId = i;
				vs[i] = v;
			}
			if(debug) {
				System.out.println("Poly: ");
				for(Vec v : vs) System.out.println("  v="+v);
			}

//			vs=vss;
			sz = vs.length;
			isSeen = false;
			id = ii;
			for(int i = 0; i < sz; i++) {
				Vec prev = vs[(i-1+sz)%sz];
				Vec next = vs[(i+1)%sz];
				vs[i].prev = prev;
				vs[i].next = next;
				vs[i].poly = this;
				long c1 = (long)Math.round(prev.cross(vs[i]));
				long c2 = (long)Math.round(next.cross(vs[i]));
				if(c1 >= 0 && c2 > 0) vs[i].isLast = true;
			}
			interVecs = new ArrayList<Vec>();
		}

		public void killSegs() { fSeg = null; rSeg = null; }
		public void updateSegs(Vec v) {
			if(fSeg == null || rSeg == null) {
				fSeg = new Seg(v, v.prev);
				fSegAId = v.id;
				rSeg = new Seg(v, v.next);
				rSegAId = v.id;
			}
			else {
				if(fSeg.b.id == v.id) {
					fSeg = new Seg(v, v.prev);
					fSegAId = v.id;
				}
				if(rSeg.b.id == v.id) {
					rSeg = new Seg(v, v.next);
					rSegAId = v.id;
				}
			}
		}
		public void intersectF(Seg s, boolean polyRemoved) {
			if(debug) System.out.println(" inter from "+s.b.poly.id +" to "+this.id);
			Vec v = s.lineIntersect(fSeg);
			//get the id for where to put this
			if(fSeg.b.id == sz-1 && eq(fSegAId, 0)) { //can't do inbetween because this is going from (sz-1) -> 0
				double d = fSegAId + (double)sz - eps;
				v.sId = d;
			}
			else {
				double d = fSegAId - eps;
				v.sId = d;
			}
			v.polyRemoveId = (polyRemoved ? 1 : -1);
			interVecs.add(v);
			fSegAId = v.sId;
		}
		public void intersectR(Seg s, boolean polyRemoved) {
			Vec v = s.lineIntersect(rSeg);
			//get hte id for where to put this
			if(rSeg.a.id == sz-1) {
				double d = rSegAId + eps;
				v.sId = d;
			}
			else {
				double d = rSegAId + eps;
				v.sId = d;
			}
			v.polyRemoveId = (polyRemoved ? 1 : -1);
			interVecs.add(v);
			rSegAId = v.sId;
		}

		public double area() {
			if(debug) System.out.println("PID: "+id+" seen="+isSeen);
			if(!isSeen) return 0;
			else {
				double res = 0;
				setIDSort();

				if(interVecs.size() == 0) return getArea(vs);

				Vec ps[] = new Vec[vs.length + interVecs.size()];
				int idd = 0;
				for(Vec v : vs) ps[idd++] = v;
				for(Vec v : interVecs) ps[idd++] = v;
				Arrays.sort(ps);

				if(debug) {
					System.out.println("Poly: "+id);
					for(Vec v : ps) System.out.println("  "+v);
					System.out.println();
				}

				int curL = ps.length-1, curR = 0;
				TreeSet<Vec> pts = new TreeSet<Vec>();
				pts.add(ps[curL]); pts.add(ps[curR]);

				boolean addArea = false;
				boolean started = false;
				while(true) {
					while(ps[curL].polyRemoveId == 0) {
						if(curL == curR) break;
						curL = (curL - 1 + ps.length)%ps.length;
						pts.add(ps[curL]);
					}
					while(ps[curR].polyRemoveId == 0) {
						if(curL == curR) break;
						curR = (curR + 1)%ps.length;
						pts.add(ps[curR]);
					}
					if(debug) System.out.println(" PTS: "+pts.toString());
					if(!started) {
						addArea = ps[curL].polyRemoveId == -1;
						started = true;
					}
					if(addArea) res += getArea(tsToArray(pts));
					pts.clear();
					if(curR == curL) break;
					pts.add(ps[curL]);
					pts.add(ps[curR]);
					curL = (curL - 1 + ps.length)%ps.length;
					curR = (curR + 1)%ps.length;
					pts.add(ps[curL]);
					pts.add(ps[curR]);
					addArea = !addArea;
				}

				return res;
			}
		}

		@Override
		public int compareTo(Poly o) {
			if(id == o.id) return 0;
			if(fSeg.inFrontOf(o.fSeg)) return -1;
			return 1;
		}

	}
	static Vec[] tsToArray(TreeSet<Vec> tss){
		Vec res[] = new Vec[tss.size()];
		int idd = 0;
		for(Vec v : tss) res[idd++] = v;
		return res;
	}
	static double getArea(Vec[] vs) {
		if(debug) {
			System.out.println("AREA:");
			for(Vec v : vs) System.out.println(" "+v);
		}
		double res = 0;
		for(int i = 0; i < vs.length; i++) {
			Vec a = vs[i];
			Vec b = vs[(i+1)%vs.length];
			res += (b.x - a.x) * ((b.y + a.y)/2.0);
		}
		if(debug)System.out.println("  a="+Math.abs(res));
		return Math.abs(res);
	}

	static boolean eq(double a, double b) {
		return Math.abs(a-b) <= eps;
	}

	static class FS{
		BufferedReader br;
		StringTokenizer st;
		public FS() { br = new BufferedReader(new InputStreamReader(System.in));}
		String next() {
			while(st == null || !st.hasMoreElements()) {
				try { st = new StringTokenizer(br.readLine());}
				catch(Exception e) { throw null;}
			}
			return st.nextToken();
		}
		int nextInt() { return Integer.parseInt(next());}
	}
}


import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.NoSuchElementException;

public class C {

  private static final int NUM = 60;

  private int[] garbage = new int[NUM];
  private int garbageSize = 0;

  private boolean check(int[] living, int livingSize) {
    if (garbageSize > livingSize) {
      return false;
    }

    int extraGarbage = 0;
    for (int bit = 0; bit < NUM; bit++) {
      if (living[bit] < garbage[bit]) {
        extraGarbage += garbage[bit] - living[bit];
      } else {
        extraGarbage = Math.max(extraGarbage - (living[bit] - garbage[bit]), 0);
      }
    }

    return extraGarbage == 0;
  }

  private void garbageAdd(long g) {
    for (int i = 0; i < NUM; i++) {
      if (g <= (1L << i) * 2) {
        garbage[i]++;
        break;
      }
    }
    garbageSize++;
  }

  private void solve(FastScanner in, PrintWriter out) {
    int N = in.nextInt();
    long[] seq = in.nextLongArray(N);

    int[] count = new int[NUM];
    int target = 0;
    for (long s : seq) {
      if (s == (1 << target)) {
        count[target]++;
      } else if (s == (1 << (target + 1))) {
        target++;
        count[target]++;
      } else {
        garbageAdd(s);
      }
    }

    int[] living = new int[NUM];
    for (int i = 0; i < NUM; i++) {
      if (i == 0) {
        living[i] += count[i];
        continue;
      }

      int evol = Math.min(living[i - 1], count[i]);
      living[i - 1] -= evol;
      living[i] += evol;
      for (int j = 0; j < count[i] - evol; j++) {
        garbageAdd(1L << i);
      }
    }

    int livingSize = 0;
    for (int l : living) {
      livingSize += l;
    }

    ArrayList<Integer> ans = new ArrayList<>();
    while (check(living, livingSize)) {
      ans.add(livingSize);
      livingSize--;
      if (livingSize == 0) {
        break;
      }
      for (int i = 0; i < NUM; i++) {
        if (living[i] == 0) {
          garbageAdd(1L << i);
        } else {
          garbageAdd(1L << i);
          living[i]--;
          break;
        }
      }
    }

    if (ans.isEmpty()) {
      out.println(-1);
    } else {
      Collections.sort(ans);
      for (int i = 0; i < ans.size(); i++) {
        if (i > 0) {
          out.print(" ");
        }
        out.print(ans.get(i));
      }
      out.println();
    }

  }

  public static void main(String[] args) {
    PrintWriter out = new PrintWriter(System.out);
    new C().solve(new FastScanner(), out);
    out.close();
  }

  private static class FastScanner {

    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int bufferLength = 0;

    private boolean hasNextByte() {
      if (ptr < bufferLength) {
        return true;
      } else {
        ptr = 0;
        try {
          bufferLength = in.read(buffer);
        } catch (IOException e) {
          e.printStackTrace();
        }
        if (bufferLength <= 0) {
          return false;
        }
      }
      return true;
    }

    private int readByte() {
      if (hasNextByte()) {
        return buffer[ptr++];
      } else {
        return -1;
      }
    }

    private static boolean isPrintableChar(int c) {
      return 33 <= c && c <= 126;
    }

    private void skipUnprintable() {
      while (hasNextByte() && !isPrintableChar(buffer[ptr])) {
        ptr++;
      }
    }

    boolean hasNext() {
      skipUnprintable();
      return hasNextByte();
    }

    public String next() {
      if (!hasNext()) {
        throw new NoSuchElementException();
      }
      StringBuilder sb = new StringBuilder();
      int b = readByte();
      while (isPrintableChar(b)) {
        sb.appendCodePoint(b);
        b = readByte();
      }
      return sb.toString();
    }

    long nextLong() {
      if (!hasNext()) {
        throw new NoSuchElementException();
      }
      long n = 0;
      boolean minus = false;
      int b = readByte();
      if (b == '-') {
        minus = true;
        b = readByte();
      }
      if (b < '0' || '9' < b) {
        throw new NumberFormatException();
      }
      while (true) {
        if ('0' <= b && b <= '9') {
          n *= 10;
          n += b - '0';
        } else if (b == -1 || !isPrintableChar(b)) {
          return minus ? -n : n;
        } else {
          throw new NumberFormatException();
        }
        b = readByte();
      }
    }

    double nextDouble() {
      return Double.parseDouble(next());
    }

    double[] nextDoubleArray(int n) {
      double[] array = new double[n];
      for (int i = 0; i < n; i++) {
        array[i] = nextDouble();
      }
      return array;
    }

    double[][] nextDoubleMap(int n, int m) {
      double[][] map = new double[n][];
      for (int i = 0; i < n; i++) {
        map[i] = nextDoubleArray(m);
      }
      return map;
    }

    public int nextInt() {
      return (int) nextLong();
    }

    public int[] nextIntArray(int n) {
      int[] array = new int[n];
      for (int i = 0; i < n; i++) {
        array[i] = nextInt();
      }
      return array;
    }

    public long[] nextLongArray(int n) {
      long[] array = new long[n];
      for (int i = 0; i < n; i++) {
        array[i] = nextLong();
      }
      return array;
    }

    public String[] nextStringArray(int n) {
      String[] array = new String[n];
      for (int i = 0; i < n; i++) {
        array[i] = next();
      }
      return array;
    }

    public char[][] nextCharMap(int n) {
      char[][] array = new char[n][];
      for (int i = 0; i < n; i++) {
        array[i] = next().toCharArray();
      }
      return array;
    }

    public int[][] nextIntMap(int n, int m) {
      int[][] map = new int[n][];
      for (int i = 0; i < n; i++) {
        map[i] = nextIntArray(m);
      }
      return map;
    }
  }
}


import java.util.*;
import java.io.*;

public class C {
  FastScanner in;
  PrintWriter out;

  public void solve() throws IOException {
    int[] powCnt = new int[63];
    int[] other = new int[63];
    long[] pows = new long[63];
    pows[0] = 1L;
    for (int i = 1; i < 63; i++) {
      pows[i] = pows[i - 1] * 2;
    }

    int n = in.nextInt();
    long[] a = new long[n];

    for (int i = 0; i < n; i++) {
      a[i] = in.nextLong();
      boolean isPow = false;
      for (int j = 0; j < 63; j++) {
        if (a[i] == pows[j]) {
          powCnt[j]++;
          isPow = true;
        }
        if (!isPow && j != 0 && a[i] > pows[j - 1] && a[i] < pows[j]) {
          other[j]++;
        }
      }
    }

    int min = Arrays.stream(other).sum();
    int max = powCnt[0];
    boolean exists = false;
    for (int len = min; len <= max; len++) {
      int tails = 0;
      int cur = len;
      for (int i = 0; i < 62; i++) {
        tails += other[i + 1];
        int oldCur = cur;
        if (powCnt[i] < cur) {
          cur = powCnt[i];
        }
        int ended = oldCur - cur;
        if (tails < ended) {
          tails = 0;
        } else {
          tails -= ended;
        }
        tails += powCnt[i] - cur;
      }
      if (tails == 0) {
        out.print(len + " ");
        exists = true;
      }
    }
    if (!exists) {
      out.println(-1);
    } else {
      out.println();
    }
  }

  public void run() {
    try {
      in = new FastScanner(System.in);
      out = new PrintWriter(System.out);
      solve();

      out.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  class FastScanner {
    BufferedReader br;
    StringTokenizer st;

    FastScanner(InputStream is) {
      br = new BufferedReader(new InputStreamReader(is));
    }

    String next() {
      while (st == null || !st.hasMoreTokens()) {
        try {
          st = new StringTokenizer(br.readLine());
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      return st.nextToken();
    }

    int nextInt() {
      return Integer.parseInt(next());
    }

    long nextLong() {
      return Long.parseLong(next());
    }
  }

  public static void main(String[] arg) {
    new C().run();
  }
}


import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.NoSuchElementException;

public class C {

  private static final int NUM = 60;

  private int[] garbage = new int[NUM];
  private int garbageSize = 0;

  private boolean check(int[] living, int livingSize) {
    if (garbageSize > livingSize) {
      return false;
    }

    int livingCount = 0;
    int extraGarbage = 0;
    int garbageCount = garbageSize;
    for (int bit = 0; bit < NUM; bit++) {
      livingCount += living[bit];
      garbageSize -= garbage[bit];
      if (living[bit] < garbage[bit]) {
        extraGarbage += garbage[bit] - living[bit];
      } else {
        extraGarbage = Math.max(extraGarbage - (living[bit] - garbage[bit]), 0);
      }

      if (livingCount == livingSize) {
        return extraGarbage == 0 && garbageCount == 0;
      }
    }

    throw new IllegalStateException();
  }

  private void garbageAdd(long g) {
    for (int i = 0; i < NUM; i++) {
      if (g <= (1L << i) * 2) {
        garbage[i]++;
        break;
      }
    }
    garbageSize++;
  }

  private void solve(FastScanner in, PrintWriter out) {
    int N = in.nextInt();
    long[] seq = in.nextLongArray(N);

    int[] count = new int[NUM];
    int target = 0;
    for (long s : seq) {
      if (s == (1 << target)) {
        count[target]++;
      } else if (s == (1 << (target + 1))) {
        target++;
        count[target]++;
      } else {
        garbageAdd(s);
      }
    }

    int[] living = new int[NUM];
    for (int i = 0; i < NUM; i++) {
      if (i == 0) {
        living[i] += count[i];
        continue;
      }

      int evol = Math.min(living[i - 1], count[i]);
      living[i - 1] -= evol;
      living[i] += evol;
      for (int j = 0; j < count[i] - evol; j++) {
        garbageAdd(1L << i);
      }
    }

    int livingSize = 0;
    for (int l : living) {
      livingSize += l;
    }

    ArrayList<Integer> ans = new ArrayList<>();
    while (check(living, livingSize)) {
      ans.add(livingSize);
      livingSize--;
      if (livingSize == 0) {
        break;
      }
      for (int i = 0; i < NUM; i++) {
        if (living[i] == 0) {
          garbageAdd(1L << i);
        } else {
          garbageAdd(1L << i);
          living[i]--;
          break;
        }
      }
    }

    if (ans.isEmpty()) {
      out.println(-1);
    } else {
      Collections.sort(ans);
      for (int i = 0; i < ans.size(); i++) {
        if (i > 0) {
          out.print(" ");
        }
        out.print(ans.get(i));
      }
      out.println();
    }

  }

  public static void main(String[] args) {
    PrintWriter out = new PrintWriter(System.out);
    new C().solve(new FastScanner(), out);
    out.close();
  }

  private static class FastScanner {

    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int bufferLength = 0;

    private boolean hasNextByte() {
      if (ptr < bufferLength) {
        return true;
      } else {
        ptr = 0;
        try {
          bufferLength = in.read(buffer);
        } catch (IOException e) {
          e.printStackTrace();
        }
        if (bufferLength <= 0) {
          return false;
        }
      }
      return true;
    }

    private int readByte() {
      if (hasNextByte()) {
        return buffer[ptr++];
      } else {
        return -1;
      }
    }

    private static boolean isPrintableChar(int c) {
      return 33 <= c && c <= 126;
    }

    private void skipUnprintable() {
      while (hasNextByte() && !isPrintableChar(buffer[ptr])) {
        ptr++;
      }
    }

    boolean hasNext() {
      skipUnprintable();
      return hasNextByte();
    }

    public String next() {
      if (!hasNext()) {
        throw new NoSuchElementException();
      }
      StringBuilder sb = new StringBuilder();
      int b = readByte();
      while (isPrintableChar(b)) {
        sb.appendCodePoint(b);
        b = readByte();
      }
      return sb.toString();
    }

    long nextLong() {
      if (!hasNext()) {
        throw new NoSuchElementException();
      }
      long n = 0;
      boolean minus = false;
      int b = readByte();
      if (b == '-') {
        minus = true;
        b = readByte();
      }
      if (b < '0' || '9' < b) {
        throw new NumberFormatException();
      }
      while (true) {
        if ('0' <= b && b <= '9') {
          n *= 10;
          n += b - '0';
        } else if (b == -1 || !isPrintableChar(b)) {
          return minus ? -n : n;
        } else {
          throw new NumberFormatException();
        }
        b = readByte();
      }
    }

    double nextDouble() {
      return Double.parseDouble(next());
    }

    double[] nextDoubleArray(int n) {
      double[] array = new double[n];
      for (int i = 0; i < n; i++) {
        array[i] = nextDouble();
      }
      return array;
    }

    double[][] nextDoubleMap(int n, int m) {
      double[][] map = new double[n][];
      for (int i = 0; i < n; i++) {
        map[i] = nextDoubleArray(m);
      }
      return map;
    }

    public int nextInt() {
      return (int) nextLong();
    }

    public int[] nextIntArray(int n) {
      int[] array = new int[n];
      for (int i = 0; i < n; i++) {
        array[i] = nextInt();
      }
      return array;
    }

    public long[] nextLongArray(int n) {
      long[] array = new long[n];
      for (int i = 0; i < n; i++) {
        array[i] = nextLong();
      }
      return array;
    }

    public String[] nextStringArray(int n) {
      String[] array = new String[n];
      for (int i = 0; i < n; i++) {
        array[i] = next();
      }
      return array;
    }

    public char[][] nextCharMap(int n) {
      char[][] array = new char[n][];
      for (int i = 0; i < n; i++) {
        array[i] = next().toCharArray();
      }
      return array;
    }

    public int[][] nextIntMap(int n, int m) {
      int[][] map = new int[n][];
      for (int i = 0; i < n; i++) {
        map[i] = nextIntArray(m);
      }
      return map;
    }
  }
}


import java.util.*;
import java.io.*;

public class C {
  FastScanner in;
  PrintWriter out;

  public void solve() throws IOException {
    int[] powCnt = new int[63];
    int[] other = new int[63];
    long[] pows = new long[63];
    pows[0] = 1L;
    for (int i = 1; i < 63; i++) {
      pows[i] = pows[i - 1] * 2;
    }

    int n = in.nextInt();
    long[] a = new long[n];

    for (int i = 0; i < n; i++) {
      a[i] = in.nextLong();
      boolean isPow = false;
      for (int j = 0; j < 63; j++) {
        if (a[i] == pows[j]) {
          powCnt[j]++;
          isPow = true;
        }
        if (!isPow && j != 0 && a[i] > pows[j - 1] && a[i] < pows[j]) {
          other[j]++;
        }
      }
    }

    int min = Arrays.stream(other).sum();
    int max = powCnt[0];
    boolean exists = false;
    for (int len = min; len <= max; len++) {
      int tails = 0;
      int cur = len;
      for (int i = 0; i < 63; i++) {
        tails += other[i + 1];
        int oldCur = cur;
        if (powCnt[i] < cur) {
          cur = powCnt[i];
        }
        int ended = oldCur - cur;
        if (tails < ended) {
          tails = 0;
        } else {
          tails -= ended;
        }
        tails += powCnt[i] - cur;
      }
      if (tails == 0) {
        out.print(len + " ");
        exists = true;
      }
    }
    if (!exists) {
      out.println(-1);
    } else {
      out.println();
    }
  }

  public void run() {
    try {
      in = new FastScanner(System.in);
      out = new PrintWriter(System.out);
      solve();

      out.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  class FastScanner {
    BufferedReader br;
    StringTokenizer st;

    FastScanner(InputStream is) {
      br = new BufferedReader(new InputStreamReader(is));
    }

    String next() {
      while (st == null || !st.hasMoreTokens()) {
        try {
          st = new StringTokenizer(br.readLine());
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      return st.nextToken();
    }

    int nextInt() {
      return Integer.parseInt(next());
    }

    long nextLong() {
      return Long.parseLong(next());
    }
  }

  public static void main(String[] arg) {
    new C().run();
  }
}


import java.util.Scanner;

public class E {
  static Scanner sc = new Scanner(System.in);
  static final double logE = Math.log(Math.E);

  public static void main(String[] args) {
    int V = sc.nextInt();
    double[] factLog = new double[3001];
    for (int i = 1; i < factLog.length; i++) {
      factLog[i] = Math.log(i) + factLog[i - 1];
    }
    for (int i = 0; i < V; i++) {
      int[] K = new int[250];
      long sum = 0;
      for (int j = 0; j < K.length; j++) {
        K[j] = sc.nextInt();
        sum += K[j];
      }
      double P = sum * 1.0 / K.length;
      double uniform = -Math.log(2 * P + 1) * K.length;
      double poisson = 0;
      for (int j = 0; j < K.length; j++) {
        poisson += K[j] * Math.log(P) - P * logE - factLog[K[j]];
      }
//      System.err.println(uniform + " " + poisson);
      int bestI = 0;
      if (uniform < poisson) {
        double bestLikelyhood = -Double.MAX_VALUE;
        for (int j = Math.max(10, (int) P - 10); j <= (int) P + 10; j++) {
          double likelyhood = 0;
          for (int k = 0; k < K.length; k++) {
            likelyhood += K[k] * Math.log(j) - j * logE;
          }
          if (likelyhood > bestLikelyhood) {
            bestLikelyhood = likelyhood;
            bestI = j;
          }
        }
      } else {
        int max = 0;
        for (int j = 0; j < K.length; j++) {
          max = Math.max(max, K[j]);
        }
        bestI = max / 2 + 1;
      }
      System.out.println(bestI);
    }
  }

}


import java.util.Scanner;

public class E {
  static Scanner sc = new Scanner(System.in);
  static final double logE = Math.log(Math.E);

  public static void main(String[] args) {
    int V = sc.nextInt();
    double[] factLog = new double[3001];
    for (int i = 1; i < factLog.length; i++) {
      factLog[i] = Math.log(i) + factLog[i - 1];
    }
    for (int i = 0; i < V; i++) {
      int[] K = new int[250];
      long sum = 0;
      for (int j = 0; j < K.length; j++) {
        K[j] = sc.nextInt();
        sum += K[j];
      }
      double P = sum * 1.0 / K.length;
      double uniform = -Math.log(2 * P + 1) * K.length;
      double poisson = 0;
      for (int j = 0; j < K.length; j++) {
        poisson += K[j] * Math.log(P) - P * logE - factLog[K[j]];
      }
//      System.err.println(uniform + " " + poisson);
      int bestI = 0;
      if (uniform < poisson) {
        double bestLikelyhood = -Double.MAX_VALUE;
        for (int j = Math.max(10, (int) P - 10); j <= (int) P + 10; j++) {
          double likelyhood = 0;
          for (int k = 0; k < K.length; k++) {
            likelyhood += K[k] * Math.log(j) - j * logE;
          }
          if (likelyhood > bestLikelyhood) {
            bestLikelyhood = likelyhood;
            bestI = j;
          }
        }
      } else {
        long sum2 = 0;
        for (int j = 0; j < K.length; j++) {
          sum2 += K[j] * K[j];
        }
        bestI = (int)(Math.sqrt(12.0 * (1.0 * sum2 / K.length - Math.pow(1.0 * sum / K.length, 2))) + 0.5);
      }
      System.out.println(bestI);
    }
  }

}


import java.util.Scanner;

public class E {
  static Scanner sc = new Scanner(System.in);
  static final double logE = Math.log(Math.E);

  public static void main(String[] args) {
    int V = sc.nextInt();
    double[] factLog = new double[3001];
    for (int i = 1; i < factLog.length; i++) {
      factLog[i] = Math.log(i) + factLog[i - 1];
    }
    for (int i = 0; i < V; i++) {
      int[] K = new int[10];
      long sum = 0;
      for (int j = 0; j < K.length; j++) {
        K[j] = sc.nextInt();
        sum += K[j];
      }
      double P = sum * 1.0 / K.length;
      double uniform = -Math.log(2 * P + 1) * K.length;
      double poisson = 0;
      for (int j = 0; j < K.length; j++) {
        poisson += K[j] * Math.log(P) - P * logE - factLog[K[j]];
      }
//      System.err.println(uniform + " " + poisson);
      double bestLikelyhood = -Double.MAX_VALUE;
      int bestI = 0;
      if (uniform < poisson) {
        for (int j = Math.max(10, (int) P - 10); j <= (int) P + 10; j++) {
          double likelyhood = 0;
          for (int k = 0; k < K.length; k++) {
            likelyhood += K[k] * Math.log(j) - j * logE - factLog[K[k]];
          }
          if (likelyhood > bestLikelyhood) {
            bestLikelyhood = likelyhood;
            bestI = j;
          }
        }
      } else {
        bestI = (int)(P + 0.5);
      }
      System.out.println(bestI);
    }
  }

}


import java.util.Scanner;

public class D {
  static Scanner sc = new Scanner(System.in);

  public static void main(String[] args) {
    int V = sc.nextInt();
    double[] factLog = new double[3001];
    for (int i = 1; i < factLog.length; i++) {
      factLog[i] = Math.log(i) + factLog[i - 1];
    }
    for (int i = 0; i < V; i++) {
      int[] K = new int[250];
      long sum = 0;
      for (int j = 0; j < K.length; j++) {
        K[j] = sc.nextInt();
        sum += K[j];
      }
      double P = sum * 1.0 / K.length;
      System.out.println((int)P);
//      double uniform = -Math.log(2 * P + 1) * K.length;
//      double poisson = 0;
//      for (int j = 0; j < K.length; j++) {
//        poisson += K[j] * Math.log(P) - P * Math.log(Math.E) - factLog[K[j]];
//      }
////      System.err.println(uniform + " " + poisson);
//      System.out.println(uniform < poisson ? "poisson" : "uniform");
    }
  }

}


import java.util.*;
import java.math.*;
import java.io.*;
public class E{
  public static void main(String args[]) throws Exception{
    int magic=51;
    BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
    int n=Integer.parseInt(in.readLine());
    String s[]=in.readLine().split(" ");
    int[]a=new int[n];
    a[0]=1;
    for(int i=1;i<n;i++)a[i]=Integer.parseInt(s[i-1]);
    s=in.readLine().split(" ");
    int[]b=new int[n];
    for(int i=0;i<n;i++)b[i]=Integer.parseInt(s[i]);
    String sm=in.readLine();
    BigInteger M=new BigInteger(sm);
    int[]c=new int[n];
    for(int i=1;i<n;i++)if(a[i]!=1){
      BigInteger[]qr=M.divideAndRemainder(new BigInteger(a[i]+""));
      c[i]=(int)qr[1].longValue();
      M=qr[0];
    }
    long mod=(long)(1e9+7);
    int mx=(int)(3e5+10);
    long [][]f=new long[2][magic*mx];
    int rw=0;
    f[0][0]=1;
    int sbi=0;
    int bef=0;
    for(int i=0;i<n;i++){
      int nxt=(int)( (1l*a[i]*b[i]-c[i] + bef)/a[i] );
      for(int x=0;x<=bef;x++){
        if(x-a[i]>=0){
          f[rw][x]+=f[rw][x-a[i]];;
          if(f[rw][x]>=mod)f[rw][x]-=mod;
        }
      }
      rw^=1;
      for(int x=0;x<=nxt;x++){
        f[rw][x]=0;
        long from=1l*a[i]*x+c[i];
        long at=from;
        long m=0;
        if(from>bef){
          m=(from-bef+a[i]-1)/a[i];
          at=from-m*a[i];
        }
        if(m<=b[i] && at>=0){
          long inc=f[rw^1][(int)at];
          long old=at;
          at=from-1l*a[i]*(b[i]+1);
          if(at>=0)inc-=f[rw^1][(int)at];
          inc=(inc%mod+mod)%mod;
          f[rw][x]=inc;
        }
      }
      bef=nxt;
    }

    if(M.compareTo(new BigInteger(""+bef))>0)System.out.println("0");
    else System.out.println(f[rw][(int)M.longValue()]);
  }
}


import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class E {

	BufferedReader br;
	PrintWriter out;
	StringTokenizer st;
	boolean eof;

	static final int P = 1_000_000_007;

	int[] update(int[] dp, int cnt) {
		int[] ret = new int[dp.length + cnt];
		int sum = 0;
		for (int i = 0; i < ret.length; i++) {
			if (i < dp.length) {
				sum += dp[i];
				if (sum >= P) {
					sum -= P;
				}
			}
			ret[i] = sum;
			if (i >= cnt) {
				sum -= dp[i - cnt];
				if (sum < 0) {
					sum += P;
				}
			}
		}
		return ret;
	}

	void solve() throws IOException {
		int n = nextInt();
		int[] a = new int[n - 1];
		for (int i = 0; i < n - 1; i++) {
			a[i] = nextInt();
		}

		int[] b = new int[n];
		for (int i = 0; i < n; i++) {
			b[i] = nextInt();
		}

		int[] dp = { 1 };

		BigInteger big = new BigInteger(nextToken());

		for (int i = 0; i < n; i++) {
			dp = update(dp, b[i]);

			if (i == n - 1) {
				break;
			}

			if (a[i] == 1) {
				continue;
			}

			BigInteger[] tmp = big.divideAndRemainder(BigInteger.valueOf(a[i]));

			int rem = tmp[1].intValue();

			if (rem >= dp.length) {
				out.println(0);
				return;
			}

			int newLen = (dp.length - 1 - rem) / a[i] + 1;
			int[] newDp = new int[newLen];

			for (int ii = rem, j = 0; j < newLen; ii += a[i], j++) {
				newDp[j] = dp[ii];
			}

			big = tmp[0];
			dp = newDp;
		}

		if (big.compareTo(BigInteger.valueOf(dp.length)) < 0) {
			out.println(dp[big.intValue()]);
		} else {
			out.println(0);
		}
	}

	E() throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		out = new PrintWriter(System.out);
		solve();
		out.close();
	}

	public static void main(String[] args) throws IOException {
		new E();
	}

	String nextToken() {
		while (st == null || !st.hasMoreTokens()) {
			try {
				st = new StringTokenizer(br.readLine());
			} catch (Exception e) {
				eof = true;
				return null;
			}
		}
		return st.nextToken();
	}

	String nextString() {
		try {
			return br.readLine();
		} catch (IOException e) {
			eof = true;
			return null;
		}
	}

	int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}
}


import java.util.*;
import java.math.*;
import java.io.*;
public class E{
  public static void main(String args[]) throws Exception{
    int magic=51;
    BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
    int n=Integer.parseInt(in.readLine());
    String s[]=in.readLine().split(" ");
    int[]a=new int[n];
    a[0]=1;
    for(int i=1;i<n;i++)a[i]=Integer.parseInt(s[i-1]);
    s=in.readLine().split(" ");
    int[]b=new int[n];
    for(int i=0;i<n;i++)b[i]=Integer.parseInt(s[i]);
    String sm=in.readLine();
    BigInteger M=new BigInteger(sm);
    int[]c=new int[n];
    for(int i=1;i<n;i++)if(a[i]!=1){
      BigInteger[]qr=M.divideAndRemainder(new BigInteger(a[i]+""));
      c[i]=(int)qr[1].longValue();
      M=qr[0];
    }
    long mod=(long)(1e9+7);
    int mx=(int)(3e5+10);
    long [][]f=new long[2][magic*mx];
    int rw=0;
    f[0][0]=1;
    int sbi=0;
    int bef=0;
    for(int i=0;i<n;i++){
      int nxt=bef+(int)( (1l*a[i]*b[i]-c[i])/a[i] );
      for(int x=0;x<=bef;x++){
        if(x-a[i]>=0){
          f[rw][x]+=f[rw][x-a[i]];;
          if(f[rw][x]>=mod)f[rw][x]-=mod;
        }
      }
      rw^=1;
      for(int x=0;x<=nxt;x++){
        f[rw][x]=0;
        long from=1l*a[i]*x+c[i];
        long at=from;
        long m=0;
        if(from>bef){
          m=(from-bef+a[i]-1)/a[i];
          at=from-m*a[i];
        }
        if(m<=b[i] && at>=0){
          long inc=f[rw^1][(int)at];
          long old=at;
          at=from-1l*a[i]*(b[i]+1);
          if(at>=0)inc-=f[rw^1][(int)at];
          inc=(inc%mod+mod)%mod;
          f[rw][x]=inc;
        }
      }
      bef=nxt;
    }

    if(M.compareTo(new BigInteger(""+bef))>0)System.out.println("0");
    else System.out.println(f[rw][(int)M.longValue()]);
  }
}


import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class E {

	BufferedReader br;
	PrintWriter out;
	StringTokenizer st;
	boolean eof;

	static final int P = 1_000_000_007;

	int[] update(int[] dp, int cnt) {
		int[] ret = new int[dp.length + cnt];
		int sum = 0;
		for (int i = 0; i < ret.length; i++) {
			if (i < dp.length) {
				sum += dp[i];
				if (sum >= P) {
					sum -= P;
				}
			}
			if (i >= cnt) {
				sum -= dp[i - cnt];
				if (sum < 0) {
					sum += P;
				}
			}
			ret[i] = sum;
		}
		return ret;
	}

	void solve() throws IOException {
		int n = nextInt();
		int[] a = new int[n - 1];
		for (int i = 0; i < n - 1; i++) {
			a[i] = nextInt();
		}

		int[] b = new int[n];
		for (int i = 0; i < n; i++) {
			b[i] = nextInt();
		}

		int[] dp = { 1 };

		BigInteger big = new BigInteger(nextToken());

		for (int i = 0; i < n; i++) {
			dp = update(dp, b[i]);

			if (i == n - 1) {
				break;
			}

			if (a[i] == 1) {
				continue;
			}

			BigInteger[] tmp = big.divideAndRemainder(BigInteger.valueOf(a[i]));

			int rem = tmp[1].intValue();

			if (rem >= dp.length) {
				out.println(0);
				return;
			}

			int newLen = (dp.length - 1 - rem) / a[i] + 1;
			int[] newDp = new int[newLen];

			for (int ii = rem, j = 0; j < newLen; ii += a[i], j++) {
				newDp[j] = dp[ii];
			}

			big = tmp[0];
			dp = newDp;
		}

		if (big.compareTo(BigInteger.valueOf(dp.length)) < 0) {
			out.println(dp[big.intValue()]);
		} else {
			out.println(0);
		}
	}

	E() throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		out = new PrintWriter(System.out);
		solve();
		out.close();
	}

	public static void main(String[] args) throws IOException {
		new E();
	}

	String nextToken() {
		while (st == null || !st.hasMoreTokens()) {
			try {
				st = new StringTokenizer(br.readLine());
			} catch (Exception e) {
				eof = true;
				return null;
			}
		}
		return st.nextToken();
	}

	String nextString() {
		try {
			return br.readLine();
		} catch (IOException e) {
			eof = true;
			return null;
		}
	}

	int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class E {

    static int find(fenwik_tree t, int y, int lo, int hi) {
        while(lo<=hi) {
            int mid=(lo+hi)>>1;
            long cnt=t.query(0, mid);

            if(cnt<y) {
                lo=mid+1;
            } else {
                hi=mid-1;
            }
        }
        return hi+1;
    }

    static void run_stream(InputStream ins) throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String[] nm=br.readLine().split(" ");
        int n=Integer.parseInt(nm[0]);
        int m=Integer.parseInt(nm[1]);

        int[] i2c=new int[n+m+1];
        fenwik_tree t=new fenwik_tree(n+m+1);
        for (int i=1; i<=n; i++) {
            t.update(m+i, 1);
            i2c[m+i]=i;
        }

        int[] out=new int[n+1];
        for (int i=0; i<m; i++) {
            String[] xy=br.readLine().split(" ");
            int x=Integer.parseInt(xy[0]);
            int y=Integer.parseInt(xy[1]);

            int j=find(t, y, m-i, n+m), k=i2c[j];
            if(out[k]>0&&out[k]!=x) {
                System.out.println("-1");
                return;
            } else {
                out[k]=x;
            }
            t.update(j,-1);  i2c[j]=-1;
            t.update(m-i,1); i2c[m-i]=k;
        }

        boolean[] b=new boolean[n+1]; int j=1;
        for (int i=1; i<=n; i++) {
            if(out[i]!=0) {
                if(b[out[i]]) {
                    System.out.println("-1");
                    return;
                }
                b[out[i]]=true;
            }
        }

        for (int i=1; i<=n; i++) {
            if(out[i]==0) {
                while(b[j]) {
                    j++;
                }
                out[i]=j;
            }
            b[out[i]]=true;
        }

        StringBuilder sb=new StringBuilder();
        for (int i=1; i<=n; i++) {
            sb.append(out[i]+" ");
        }
        System.out.println(sb.toString());
    }

    public static void main(String[] args) throws IOException {
        run_stream(System.in);
    }

    static class fenwik_tree {
        int n;
        long[] fadd;

        fenwik_tree(int n) {
            this.n=n; fadd=new long[n];
        }

        void update(int i, long add) {
            while (i<n) {
                fadd[i]+=add;
                i|=(i+1);
            }
        }

        long query(int i) {
            long ret=0;
            while (i>=0) {
                ret+=fadd[i];
                i=(i&(i+1))-1;
            }
            return ret;
        }

        long query(int l, int r) {
            return query(r)-query(l-1);
        }
    }

}


//Created by Aminul on 11/17/2020.

import java.io.*;
import java.util.*;

import static java.lang.Math.*;

public class CF1447F1 {
    public static void main(String[] args) throws Exception {
        Scanner in = new Scanner(System.in);
        PrintWriter pw = new PrintWriter(System.out);
        int n = in.nextInt();
        int[] arr = new int[n + 1];
        int[] freq = new int[101];
        int mostFreq = 0;
        for (int i = 1; i <= n; i++) {
            arr[i] = in.nextInt();
            freq[arr[i]]++;
            if (freq[arr[i]] > freq[mostFreq]) {
                mostFreq = arr[i];
            }
        }

        int res = 0;
        for (int i = 1; i <= 100; i++) {
            if (i != mostFreq) {
                int tmp = maxSumArrayWithZeroSum(n, mostFreq, i, arr);
                res = max(res, tmp);
            }
        }

        pw.println(res);
        pw.close();
    }

    static int maxSumArrayWithZeroSum(int n, int x, int y, int[] arr) {
        int offset = n + 5;
        int[] idx = new int[offset + offset + 10];
        int invalid = -n - n - n;
        Arrays.fill(idx, invalid);
        int sum = 0, res = 0;
        idx[offset + sum] = 0;
        for (int i = 1; i <= n; i++) {
            if (arr[i] == x) sum++;
            else if (arr[i] == y) sum--;
            if (idx[offset + sum] != invalid) res = max(res, i - idx[offset + sum]);
            else idx[offset + sum] = i;
        }
        return res;
    }

    static void debug(Object... obj) {
        System.err.println(Arrays.deepToString(obj));
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.io.PrintWriter;
import java.util.TreeMap;
public class E1113
{
    static class QN{
        int index;
        long sum;
        QN(int in,long an){
            index=in;
            sum=an;
        }
    }
    static class SegmentTree{
        long sum,pmin,lazy;
        int st,en;
        boolean rem;
        SegmentTree left,right;
        SegmentTree(int _st,int _en,long _v){
            st=_st;
            en=_en;
            lazy=_v;
            rem=true;
            sum=(en-st+1)*lazy;
            pmin=Math.min(lazy,sum);
        }
        long pmin(){
            return rem?(Math.min(lazy,(en-st+1)*lazy)):(pmin);
        }
        long sum(){
            return rem?((en-st+1)*lazy):(sum);
        }
        void update(int l,int r,long v){
            //completely outside
            if(l>en||r<st)
                return;
            //completely inside
            if(l<=st&&r>=en){
                //update lazy;
                lazy=v;
                rem=true;
                return;
            }
            //partial cover
            lazyPropagate();
            left.update(l, r, v);
            right.update(l, r, v);
            sum=left.sum()+right.sum();
            pmin=Math.min(left.pmin(),left.sum()+right.pmin());
            return;
        }
        void lazyPropagate(){
            if(left==null){
                int mid=(st+en)>>1;
                left=new SegmentTree(st,mid,lazy);
                right=new SegmentTree(mid+1,en,lazy);
            }else if(rem){
                left.lazy=lazy;
                right.lazy=lazy;
                left.rem=true;
                right.rem=true;
            }
            if(rem){
                sum=(en-st+1)*lazy;
                pmin=Math.min(lazy,sum);
            }
            rem=false;
        }
        /**returns least index which has value <= given value**/
        QN query(int l,int r,long v,long su){
            //no cover
            if(st>r||en<l)
                return new QN(-1,0);
            //full cover
            if(l<=st&&en<=r){
                if(su+pmin()<=v)
                    return query2(v,su);
                return new QN(-1,su+sum());
            }
            lazyPropagate();

            QN q1=left.query(l,r,v,su);
            if(q1.index!=-1)
                return q1;
            q1=right.query(l,r,v,q1.sum);
            return q1;
        }
        QN query2(long v,long su){
            if(st==en)
                return new QN(st,su+sum());
            lazyPropagate();
            if(left.pmin()+su<=v)
                return left.query2(v,su);
            else
                return right.query2(v,su+left.sum());

        }

    }
    static class Scanner
    {
        BufferedReader br;
        StringTokenizer tk=new StringTokenizer("");
        public Scanner(InputStream is)
        {
            br=new BufferedReader(new InputStreamReader(is));
        }
        public int nextInt() throws IOException
        {
            if(tk.hasMoreTokens())
                return Integer.parseInt(tk.nextToken());
            tk=new StringTokenizer(br.readLine());
            return nextInt();
        }
        public long nextLong() throws IOException
        {
            if(tk.hasMoreTokens())
                return Long.parseLong(tk.nextToken());
            tk=new StringTokenizer(br.readLine());
            return nextLong();
        }
        public String next() throws IOException
        {
            if(tk.hasMoreTokens())
                return (tk.nextToken());
            tk=new StringTokenizer(br.readLine());
            return next();
        }
        public String nextLine() throws IOException
        {
            tk=new StringTokenizer("");
            return br.readLine();
        }
        public double nextDouble() throws IOException
        {
            if(tk.hasMoreTokens())
                return Double.parseDouble(tk.nextToken());
            tk=new StringTokenizer(br.readLine());
            return nextDouble();
        }
        public char nextChar() throws IOException
        {
            if(tk.hasMoreTokens())
                return (tk.nextToken().charAt(0));
            tk=new StringTokenizer(br.readLine());
            return nextChar();
        }
        public int[] nextIntArray(int n) throws IOException
        {
            int a[]=new int[n];
            for(int i=0;i<n;i++)
                a[i]=nextInt();
            return a;
        }
        public long[] nextLongArray(int n) throws IOException
        {
            long a[]=new long[n];
            for(int i=0;i<n;i++)
                a[i]=nextLong();
            return a;
        }
        public int[] nextIntArrayOneBased(int n) throws IOException
        {
            int a[]=new int[n+1];
            for(int i=1;i<=n;i++)
                a[i]=nextInt();
            return a;
        }
        public long[] nextLongArrayOneBased(int n) throws IOException
        {
            long a[]=new long[n+1];
            for(int i=1;i<=n;i++)
                a[i]=nextLong();
            return a;
        }

    }

    public static void main(String args[]) throws IOException
    {
        Scanner in=new Scanner(System.in);
        PrintWriter out=new PrintWriter(System.out);
        SegmentTree st=new SegmentTree(1,1000000000,0);
        int q=in.nextInt();
        TreeMap<Integer,Integer> mp=new TreeMap<>();
        mp.put(0,0);
        mp.put(1000000001,0);
        while(q--!=0){
            int tp=in.nextInt();
            switch(tp){
                case 1:
                    int tm=in.nextInt();
                    int vl=in.nextInt();
                    int nxt=mp.higherKey(tm)-1;
                    st.update(tm, nxt, vl);
                    mp.put(tm,vl);
                    break;
                case 2:
                    int tc=in.nextInt();
                    int vp=mp.lowerEntry(tc).getValue();
                    int net=mp.higherKey(tc)-1;
                    st.update(tc, net, vp);
                    mp.remove(tc);
                    break;
                case 3:
                    int l=in.nextInt();
                    int lc=l;
                    l=mp.ceilingKey(l);
                    int r=in.nextInt();
                    int v=in.nextInt();
                    if(v==0){
                        out.println(lc);
                        continue;
                    }
                    if(l>=r){
                        out.println(-1);
                        continue;
                    }
                    QN aa=st.query(l, r-1, -v, 0);
                    if(aa.index==-1)
                        out.println(-1);
                    else{
                        int spd=mp.floorEntry(aa.index).getValue();
                        double remv=-(v+(aa.sum-spd));
                        double anss=aa.index;
                        anss+=remv/spd;
                        out.println(anss);

                    }

            }

        }
        out.close();
    }

}