/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author parsa bahrami
 */

#pragma GCC optimize("O2")

#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;
typedef pair<string, string> pss;

template<class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define sz(x)                       (ll) x.size()
#define jjoin(x)                    for (auto i : x) cout << i << endl;
#define all(x)                      (x).begin(),(x).end()
#define F                           first
#define S                           second
#define Mp                          make_pair
#define sep                         ' '
#define error(x)                    cerr << #x << " = " << x << endl
#define fast_io                     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define set_random                  mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#define kill(x)                     return cout << x << endl , 0;
#define Hkill(x)                    cout << x << endl; exit(0);
#define endl                        '\n'

ll poww(ll a, ll b, ll md) {
    return (!b ? 1 : (b & 1 ? a * poww(a * a % md, b / 2, md) % md : poww(a * a % md, b / 2, md) % md));
}

const ll MAXN = 2e5 + 10;
const ll MAXA = 101;
const ll INF = 8e18;
const ll MOD = 998244353; //1e9 + 7
const ld PI = 4 * atan((ld) 1);

int main() {
    fast_io;
    set_random;
    ll n, k;
    cin >> n >> k;
    if (k <= 1 || k >= 4 || (k == 2 && n <= 4) || n <= k)
        kill(-1);
    cout << n - 1 << endl;
    if (k == 2) {
        for (ll i = 1; i <= n - 1; i++)
            cout << i << sep << i + 1 << endl;
    } else {
        cout << 1 << sep << 2 << endl;
        cout << 2 << sep << 3 << endl;
        for (ll i = 4; i <= n; i++) cout << 1 << sep << i << endl;
    }
    return 0;
}


/* let's have some fun */
#include <bits/stdc++.h>
#define all(x) x.begin(),x.end()
#define pb(x) push_back(x)
#define pp pop_back()
#define f(x,y,i) for(int i=x;i<y;i++)
#define F(x,y,i) for(int i=x;i>y;i--)
#define mp make_pair
#define lb lower_bound
#define qu int q;cin>>q;while(q--)
#define GG ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;
using namespace std;
int main(){
	GG
	int n,k;
	cin>>n>>k;
	if(k==2 and n>4){
		cout<<n-1<<endl;
		f(1,n,i){
			cout<<i<<" "<<i+1<<endl;
		}
	}
	else if(k==3 and n>3){
		cout<<n-1<<endl;
		for(int i=1;i<=3;i++){
			cout<<i<<" "<<i+1<<endl;
		}
		for(int i=5;i<=n;i++){
			cout<<2<<" "<<i<<endl;
		}
	}
	else{
		cout<<-1<<endl;
	}

}
//GD 2020


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rngll(chrono::steady_clock::now().time_since_epoch().count());
template<typename T> T ctmax(T &x, const T &y){ return x = max(x, y); }
template<typename T> T ctmin(T &x, const T &y){ return x = min(x, y); }
template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

int main(){
	cin.tie(0)->sync_with_stdio(0);
	int n, k;
	cin >> n >> k;
	auto kill = [](){
		cout << -1;
		exit(0);
	};
	if(n == 4){
		if(k == 3){
			cout << "3\n1 2\n2 3\n3 4";
		}
		else{
			kill();
		}
	}
	else if(n <= 3){
		kill();
	}
	else{
		if(k >= 4){
			kill();
		}
		else if(k == 2){
			cout << n - 1 << "\n";
			for(int i = 0; i < n - 1; ++ i){
				cout << i + 1 << " " << i + 2 << "\n";
			}
		}
		else{
			int g1 = n - 2 >> 1, g2 = n - 2 - g1;
			vector<int> a1, a2;
			for(int i = 2; i < 2 + g1; ++ i){
				a1.push_back(i);
			}
			for(int i = 2 + g1; i < 2 + g1 + g2; ++ i){
				a2.push_back(i);
			}
			cout << g1 + g2 + g1 * g2<< "\n";
			for(auto u: a1){
				cout << "1 " << u << "\n";
			}
			for(auto u: a2){
				cout << u << " " << g1 + g2 + 2 << "\n";
			}
			for(auto u: a1){
				for(auto v: a2){
					cout << u << " " << v << "\n";
				}
			}
		}
	}
	return 0;
}

/*

*/

////////////////////////////////////////////////////////////////////////////////////////
//                                                                                //
//                                   Coded by Aeren                                   //
//                                                                                //
////////////////////////////////////////////////////////////////////////////////////////


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rngll(chrono::steady_clock::now().time_since_epoch().count());
template<typename T> T ctmax(T &x, const T &y){ return x = max(x, y); }
template<typename T> T ctmin(T &x, const T &y){ return x = min(x, y); }
template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

int main(){
	cin.tie(0)->sync_with_stdio(0);
	int n, k;
	cin >> n >> k;
	auto kill = [](){
		cout << -1;
		exit(0);
	};
	if(n == 4){
		if(k == 3){
			cout << "3\n1 2\n2 3\n3 4";
		}
		else{
			kill();
		}
	}
	else if(n <= 3){
		kill();
	}
	else{
		if(k >= 4 || k == 1){
			kill();
		}
		else if(k == 2){
			cout << n - 1 << "\n";
			for(int i = 0; i < n - 1; ++ i){
				cout << i + 1 << " " << i + 2 << "\n";
			}
		}
		else{
			int g1 = n - 2 >> 1, g2 = n - 2 - g1;
			vector<int> a1, a2;
			for(int i = 2; i < 2 + g1; ++ i){
				a1.push_back(i);
			}
			for(int i = 2 + g1; i < 2 + g1 + g2; ++ i){
				a2.push_back(i);
			}
			cout << g1 + g2 + g1 * g2<< "\n";
			for(auto u: a1){
				cout << "1 " << u << "\n";
			}
			for(auto u: a2){
				cout << u << " " << g1 + g2 + 2 << "\n";
			}
			for(auto u: a1){
				for(auto v: a2){
					cout << u << " " << v << "\n";
				}
			}
		}
	}
	return 0;
}

/*

*/

////////////////////////////////////////////////////////////////////////////////////////
//                                                                                //
//                                   Coded by Aeren                                   //
//                                                                                //
////////////////////////////////////////////////////////////////////////////////////////


#include <bits/stdc++.h>

using namespace std;

int n, k;

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cin >> n >> k;
	if(k == 1 or k > 3){
		cout << -1;
		return 0;
	}
	if((k == 2 and n < 5) or (k == 3 and n < 4)){
		cout << -1;
		return 0;
	}
	cout << n-1 << "\n";
	if(k == 2){
		for(int i = 1; i < n; ++i){
			cout << i << " " << i+1 << "\n";
		}
	} else {
		cout << "1 2\n2 3\n";
		for(int i = 4; i <= n; ++i){
			cout << i << " 3\n";
		}
	}
}


#include <bits/stdc++.h>

using namespace std;

int n, k;

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cin >> n >> k;
	if(k == 1 or k > 3){
		cout << -1;
		return 0;
	}
	if((k == 2 and n < 5) or (k == 3 and n < 4)){
		cout << -1;
		return 0;
	}
	cout << n-1 << "\n";
	if(k == 1){
		for(int i = 1; i < n; ++i){
			cout << i << " " << i+1 << "\n";
		}
	} else {
		cout << "1 2\n2 3\n";
		for(int i = 4; i <= n; ++i){
			cout << i << " 3\n";
		}
	}
}


#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);

void solv()
{
    int n, k;
    scanf("%d%d", &n, &k);
    if (k > 3 || k == 1)
    {
        printf("-1\n");
        return;
    }
    vector<pair<int, int> > ans;
    if (k == 2)
    {
        if (n <= 4)
        {
            printf("-1\n");
            return;
        }
        for (int i = 1; i < n; ++i)
            ans.push_back(m_p(i, i + 1));
    }
    else
    {
        if (n <= 3)
        {
            printf("-1\n");
            return;
        }
        ans.push_back(m_p(1, 2));
        ans.push_back(m_p(2, 3));
        ans.push_back(m_p(3, 4));
        for (int i = 5; i <= n; ++i)
        {
            ans.push_back(m_p(i, 3));
            ans.push_back(m_p(i, 4));
        }
    }
    printf("%d\n", ans.size());
    for (int i = 0; i < ans.size(); ++i)
    {
        printf("%d %d\n", ans[i].fi, ans[i].se);
    }
}

int main()
{
    #ifdef SOMETHING
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    #endif // SOMETHING
    //ios_base::sync_with_stdio(false);
    //cin.tie(0);
    solv();
    return 0;
}

//while ((double)clock() / CLOCKS_PER_SEC <= 0.9){}


#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);

void solv()
{
    int n, k;
    scanf("%d%d", &n, &k);
    if (k > 3 || k == 1)
    {
        printf("-1\n");
        return;
    }
    vector<pair<int, int> > ans;
    if (k == 2)
    {
        if (n <= 4)
        {
            printf("-1\n");
            return;
        }
        ans.push_back(m_p(1, 2));
        ans.push_back(m_p(1, 3));
        ans.push_back(m_p(2, 4));
        ans.push_back(m_p(3, 4));
        for (int i = 5; i <= n; ++i)
            ans.push_back(m_p(4, i));
    }
    else
    {
        if (n <= 3)
        {
            printf("-1\n");
            return;
        }
        ans.push_back(m_p(1, 2));
        ans.push_back(m_p(2, 3));
        ans.push_back(m_p(3, 4));
        for (int i = 5; i <= n; ++i)
        {
            ans.push_back(m_p(i, 3));
            ans.push_back(m_p(i, 4));
        }
    }
    printf("%d\n", ans.size());
    for (int i = 0; i < ans.size(); ++i)
    {
        printf("%d %d\n", ans[i].fi, ans[i].se);
    }
}

int main()
{
    #ifdef SOMETHING
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    #endif // SOMETHING
    //ios_base::sync_with_stdio(false);
    //cin.tie(0);
    solv();
    return 0;
}

//while ((double)clock() / CLOCKS_PER_SEC <= 0.9){}


#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;

#define all(x)                      (x).begin(),(x).end()
#define Sort(x)                     sort(all((x)))
#define X                           first
#define Y                           second
#define Mp                          make_pair
#define sep                         ' '
#define endl                        '\n'
#define debug(x)                    cerr << #x << " = " << x << endl
#define SZ(x)                       ll(x.size())
#define fast_io                     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define set_random                  mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

ll poww(ll a, ll b, ll md) {
    return (!b ? 1 : (b & 1 ? a * poww(a * a % md, b / 2, md) % md : poww(a * a % md, b / 2, md) % md));
}

const ll MAXN = 1e6 + 10;
const ll LOG = 22;
const ll INF = 8e18;
const ll MOD = 1e9 + 7; // 998244353; // 1e9 + 9;

ll n , k;

int main() {
    fast_io;

    cin >> n >> k;
    if(k == 2 && n > 4){
        cout << n - 1 << endl;
        for(ll i = 1 ; i < n ; i++) cout << i << sep << i + 1 << endl;
        return 0;
    }
    if(k == 3 && n >= 4){
        cout << 2 * n - 5 << endl;
        for(ll i = 3 ; i <= n ; i++)    cout << 1 << sep << i << endl;
        for(ll i = 4 ; i <= n ; i++)    cout << 2 << sep << i << endl;
        return 0;
    }
    cout << -1 << endl;

    return 0;
}
/*

*/


// oct code title CF/CF755E.cpp

#ifndef OCT_CODE_INSERT_PREx2dDOCUMENT  // oct code insert pre-document
#define OCT_CODE_INSERT_PREx2dDOCUMENT

// #define LUOGU
#if defined(ONLINE_JUDGE) && !defined(LUOGU)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#endif

#ifndef STL_BITS__2F_STDCx2Bx2Bx2FH
#define STL_BITS__2F_STDCx2Bx2Bx2FH
#include <bits/stdc++.h>
#endif

#define debug std::cerr << "Debug(" << __LINE__ << "): "
#ifdef ONLINE_JUDGE
#define cerr cout
#endif
#define pub push_back
#define pob pop_back
#define pup push
#define mkp make_pair
#define fir first
#define sec second
#define ite iterator
#define lob lower_bound
#define upb upper_bound
#define reg register

using uint8 = unsigned char;
using uint16 = unsigned short int;
using uint32 = unsigned int;
using uint64 = unsigned long long;
using int8 = signed char;
using int16 = short int;
using int32 = int;
using int64 = long long;
using ldb = long double;
using pi5 = std::pair<int32, int32>;
using pi6 = std::pair<int64, int64>;
using pi56 = std::pair<int32, int64>;
using pi65 = std::pair<int64, int32>;

namespace oct {

template <typename _Tp>
class priority_queue
    : public std::priority_queue<_Tp, std::vector<_Tp>, std::greater<_Tp> > {};

/* Array tn4 is the 4-direction changes in coordinate system.
 * The directions in order is {right, up, left, down}.
 */
const int tn4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int tn8[8][2] = {{1, 0},  {1, 1},   {0, 1},  {-1, 1},
                       {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
const double exp = 1e-8;

void sync(int pre = 8);
template <typename _Tp>
_Tp &mad(_Tp &x, _Tp y);
template <typename _Tp>
_Tp &mid(_Tp &x, _Tp y);
template <typename _Tp>
bool in(_Tp x, _Tp l, _Tp r);
template <typename _Tp>
bool in(_Tp x, _Tp y, _Tp l, _Tp r);
template <typename _Tp>
_Tp sqr(_Tp x);
template <typename _Tp>
_Tp power(_Tp x, int64 m);
template <typename _Tp>
void sort(_Tp &x, _Tp &y);
template <typename _Tp1, typename _Tp2>
std::pair<_Tp1, _Tp2> operator+(std::pair<_Tp1, _Tp2> x,
                                std::pair<_Tp1, _Tp2> y);
template <typename _Tp>
_Tp &operator+=(_Tp &x, _Tp y);
template <typename _Tp>
_Tp gcd(_Tp &x, _Tp &y);

inline void sync(int pre) {
    std::ios::sync_with_stdio(0);
    std::cin.tie(0), std::cout.tie(0);
    std::cout.flags(std::ios::fixed);
    std::cout.precision(pre);
    std::cout.setf(std::ios::showpoint);
}
template <typename _Tp>
inline _Tp &mad(_Tp &x, _Tp y) {
    return x = std::max(x, y);
}
template <typename _Tp>
inline _Tp &mid(_Tp &x, _Tp y) {
    return x = std::min(x, y);
}
template <typename _Tp>
inline bool in(_Tp x, _Tp l, _Tp r) {
    return l <= x && x <= r;
}
template <typename _Tp>
inline bool in(_Tp x, _Tp y, _Tp l, _Tp r) {
    return l <= x && y <= r;
}
template <typename _Tp>
inline _Tp sqr(_Tp x) {
    return x * x;
}
template <typename _Tp>
inline _Tp power(_Tp x, int64 m) {
    return m == 1 ? x : (m & 1 ? power(x * x, m / 2) * x : power(x * x, m / 2));
}
template <typename _Tp>
inline void sort(_Tp &x, _Tp &y) {
    if (x > y) std::swap(x, y);
}
template <typename _Tp1, typename _Tp2>
inline std::pair<_Tp1, _Tp2> operator+(std::pair<_Tp1, _Tp2> x,
                                       std::pair<_Tp1, _Tp2> y) {
    return x.fir += y.fir, x.sec += y.sec, x;
}
template <typename _Tp>
inline _Tp &operator+=(_Tp &x, _Tp y) {
    return x = x + y;
}
template <typename _Tp>
inline _Tp gcd(_Tp &x, _Tp &y) {
    return std::__gcd(x, y);
}

}  // namespace oct

#endif  // oct code end pre-document

int n, k;

int main() {
    std::cin >> n >> k;
    if (n <= 3 || k > 3 || k == 1) return std::cout << -1 << std::endl, 0;
    if (n == 4 && k == 3) return std::cout << -1 << std::endl, 0;
    std::cout << n - 1 << std::endl;
    if (k == 2) {
        for (int i = 1; i < n; ++i) std::cout << i << ' ' << i + 1 << std::endl;
        return 0;
    }
    for (int i = 2; i < n - 1; ++i) std::cout << 1 << ' ' << i << std::endl;
    std::cout << n - 1 << ' ' << n << std::endl;
    return 0;
}


// oct code title CF/CF755E.cpp

#ifndef OCT_CODE_INSERT_PREx2dDOCUMENT  // oct code insert pre-document
#define OCT_CODE_INSERT_PREx2dDOCUMENT

// #define LUOGU
#if defined(ONLINE_JUDGE) && !defined(LUOGU)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#endif

#ifndef STL_BITS__2F_STDCx2Bx2Bx2FH
#define STL_BITS__2F_STDCx2Bx2Bx2FH
#include <bits/stdc++.h>
#endif

#define debug std::cerr << "Debug(" << __LINE__ << "): "
#ifdef ONLINE_JUDGE
#define cerr cout
#endif
#define pub push_back
#define pob pop_back
#define pup push
#define mkp make_pair
#define fir first
#define sec second
#define ite iterator
#define lob lower_bound
#define upb upper_bound
#define reg register

using uint8 = unsigned char;
using uint16 = unsigned short int;
using uint32 = unsigned int;
using uint64 = unsigned long long;
using int8 = signed char;
using int16 = short int;
using int32 = int;
using int64 = long long;
using ldb = long double;
using pi5 = std::pair<int32, int32>;
using pi6 = std::pair<int64, int64>;
using pi56 = std::pair<int32, int64>;
using pi65 = std::pair<int64, int32>;

namespace oct {

template <typename _Tp>
class priority_queue
    : public std::priority_queue<_Tp, std::vector<_Tp>, std::greater<_Tp> > {};

/* Array tn4 is the 4-direction changes in coordinate system.
 * The directions in order is {right, up, left, down}.
 */
const int tn4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int tn8[8][2] = {{1, 0},  {1, 1},   {0, 1},  {-1, 1},
                       {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
const double exp = 1e-8;

void sync(int pre = 8);
template <typename _Tp>
_Tp &mad(_Tp &x, _Tp y);
template <typename _Tp>
_Tp &mid(_Tp &x, _Tp y);
template <typename _Tp>
bool in(_Tp x, _Tp l, _Tp r);
template <typename _Tp>
bool in(_Tp x, _Tp y, _Tp l, _Tp r);
template <typename _Tp>
_Tp sqr(_Tp x);
template <typename _Tp>
_Tp power(_Tp x, int64 m);
template <typename _Tp>
void sort(_Tp &x, _Tp &y);
template <typename _Tp1, typename _Tp2>
std::pair<_Tp1, _Tp2> operator+(std::pair<_Tp1, _Tp2> x,
                                std::pair<_Tp1, _Tp2> y);
template <typename _Tp>
_Tp &operator+=(_Tp &x, _Tp y);
template <typename _Tp>
_Tp gcd(_Tp &x, _Tp &y);

inline void sync(int pre) {
    std::ios::sync_with_stdio(0);
    std::cin.tie(0), std::cout.tie(0);
    std::cout.flags(std::ios::fixed);
    std::cout.precision(pre);
    std::cout.setf(std::ios::showpoint);
}
template <typename _Tp>
inline _Tp &mad(_Tp &x, _Tp y) {
    return x = std::max(x, y);
}
template <typename _Tp>
inline _Tp &mid(_Tp &x, _Tp y) {
    return x = std::min(x, y);
}
template <typename _Tp>
inline bool in(_Tp x, _Tp l, _Tp r) {
    return l <= x && x <= r;
}
template <typename _Tp>
inline bool in(_Tp x, _Tp y, _Tp l, _Tp r) {
    return l <= x && y <= r;
}
template <typename _Tp>
inline _Tp sqr(_Tp x) {
    return x * x;
}
template <typename _Tp>
inline _Tp power(_Tp x, int64 m) {
    return m == 1 ? x : (m & 1 ? power(x * x, m / 2) * x : power(x * x, m / 2));
}
template <typename _Tp>
inline void sort(_Tp &x, _Tp &y) {
    if (x > y) std::swap(x, y);
}
template <typename _Tp1, typename _Tp2>
inline std::pair<_Tp1, _Tp2> operator+(std::pair<_Tp1, _Tp2> x,
                                       std::pair<_Tp1, _Tp2> y) {
    return x.fir += y.fir, x.sec += y.sec, x;
}
template <typename _Tp>
inline _Tp &operator+=(_Tp &x, _Tp y) {
    return x = x + y;
}
template <typename _Tp>
inline _Tp gcd(_Tp &x, _Tp &y) {
    return std::__gcd(x, y);
}

}  // namespace oct

#endif  // oct code end pre-document

int n, k;

int main() {
    std::cin >> n >> k;
    if (n <= 3 || k > 3 || k == 1) return std::cout << -1 << std::endl, 0;
    if (n == 4 && k == 3) return std::cout << -1 << std::endl, 0;
    std::cout << n - 1 << std::endl;
    if (k == 2) {
        for (int i = 1; i < n; ++i) std::cout << i << ' ' << i + 1 << std::endl;
        return 0;
    }
    for (int i = 2; i < n; ++i) std::cout << 1 << ' ' << i << std::endl;
    std::cout << n - 1 << ' ' << n << std::endl;
    return 0;
}


// oct code title CF/CF755E.cpp

#ifndef OCT_CODE_INSERT_PREx2dDOCUMENT  // oct code insert pre-document
#define OCT_CODE_INSERT_PREx2dDOCUMENT

// #define LUOGU
#if defined(ONLINE_JUDGE) && !defined(LUOGU)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#endif

#ifndef STL_BITS__2F_STDCx2Bx2Bx2FH
#define STL_BITS__2F_STDCx2Bx2Bx2FH
#include <bits/stdc++.h>
#endif

#define debug std::cerr << "Debug(" << __LINE__ << "): "
#ifdef ONLINE_JUDGE
#define cerr cout
#endif
#define pub push_back
#define pob pop_back
#define pup push
#define mkp make_pair
#define fir first
#define sec second
#define ite iterator
#define lob lower_bound
#define upb upper_bound
#define reg register

using uint8 = unsigned char;
using uint16 = unsigned short int;
using uint32 = unsigned int;
using uint64 = unsigned long long;
using int8 = signed char;
using int16 = short int;
using int32 = int;
using int64 = long long;
using ldb = long double;
using pi5 = std::pair<int32, int32>;
using pi6 = std::pair<int64, int64>;
using pi56 = std::pair<int32, int64>;
using pi65 = std::pair<int64, int32>;

namespace oct {

template <typename _Tp>
class priority_queue
    : public std::priority_queue<_Tp, std::vector<_Tp>, std::greater<_Tp> > {};

/* Array tn4 is the 4-direction changes in coordinate system.
 * The directions in order is {right, up, left, down}.
 */
const int tn4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int tn8[8][2] = {{1, 0},  {1, 1},   {0, 1},  {-1, 1},
                       {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
const double exp = 1e-8;

void sync(int pre = 8);
template <typename _Tp>
_Tp &mad(_Tp &x, _Tp y);
template <typename _Tp>
_Tp &mid(_Tp &x, _Tp y);
template <typename _Tp>
bool in(_Tp x, _Tp l, _Tp r);
template <typename _Tp>
bool in(_Tp x, _Tp y, _Tp l, _Tp r);
template <typename _Tp>
_Tp sqr(_Tp x);
template <typename _Tp>
_Tp power(_Tp x, int64 m);
template <typename _Tp>
void sort(_Tp &x, _Tp &y);
template <typename _Tp1, typename _Tp2>
std::pair<_Tp1, _Tp2> operator+(std::pair<_Tp1, _Tp2> x,
                                std::pair<_Tp1, _Tp2> y);
template <typename _Tp>
_Tp &operator+=(_Tp &x, _Tp y);
template <typename _Tp>
_Tp gcd(_Tp &x, _Tp &y);

inline void sync(int pre) {
    std::ios::sync_with_stdio(0);
    std::cin.tie(0), std::cout.tie(0);
    std::cout.flags(std::ios::fixed);
    std::cout.precision(pre);
    std::cout.setf(std::ios::showpoint);
}
template <typename _Tp>
inline _Tp &mad(_Tp &x, _Tp y) {
    return x = std::max(x, y);
}
template <typename _Tp>
inline _Tp &mid(_Tp &x, _Tp y) {
    return x = std::min(x, y);
}
template <typename _Tp>
inline bool in(_Tp x, _Tp l, _Tp r) {
    return l <= x && x <= r;
}
template <typename _Tp>
inline bool in(_Tp x, _Tp y, _Tp l, _Tp r) {
    return l <= x && y <= r;
}
template <typename _Tp>
inline _Tp sqr(_Tp x) {
    return x * x;
}
template <typename _Tp>
inline _Tp power(_Tp x, int64 m) {
    return m == 1 ? x : (m & 1 ? power(x * x, m / 2) * x : power(x * x, m / 2));
}
template <typename _Tp>
inline void sort(_Tp &x, _Tp &y) {
    if (x > y) std::swap(x, y);
}
template <typename _Tp1, typename _Tp2>
inline std::pair<_Tp1, _Tp2> operator+(std::pair<_Tp1, _Tp2> x,
                                       std::pair<_Tp1, _Tp2> y) {
    return x.fir += y.fir, x.sec += y.sec, x;
}
template <typename _Tp>
inline _Tp &operator+=(_Tp &x, _Tp y) {
    return x = x + y;
}
template <typename _Tp>
inline _Tp gcd(_Tp &x, _Tp &y) {
    return std::__gcd(x, y);
}

}  // namespace oct

#endif  // oct code end pre-document

int n, k;

int main() {
    std::cin >> n >> k;
    if (n <= 3 || k > 3 || k == 1) return std::cout << -1 << std::endl, 0;
    if (n == 4 && k == 2) return std::cout << -1 << std::endl, 0;
    std::cout << n - 1 << std::endl;
    if (k == 2) {
        for (int i = 1; i < n; ++i) std::cout << i << ' ' << i + 1 << std::endl;
        return 0;
    }
    for (int i = 2; i < n; ++i) std::cout << 1 << ' ' << i << std::endl;
    std::cout << n - 1 << ' ' << n << std::endl;
    return 0;
}


// oct code title CF/CF755E.cpp

#ifndef OCT_CODE_INSERT_PREx2dDOCUMENT  // oct code insert pre-document
#define OCT_CODE_INSERT_PREx2dDOCUMENT

// #define LUOGU
#if defined(ONLINE_JUDGE) && !defined(LUOGU)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#endif

#ifndef STL_BITS__2F_STDCx2Bx2Bx2FH
#define STL_BITS__2F_STDCx2Bx2Bx2FH
#include <bits/stdc++.h>
#endif

#define debug std::cerr << "Debug(" << __LINE__ << "): "
#ifdef ONLINE_JUDGE
#define cerr cout
#endif
#define pub push_back
#define pob pop_back
#define pup push
#define mkp make_pair
#define fir first
#define sec second
#define ite iterator
#define lob lower_bound
#define upb upper_bound
#define reg register

using uint8 = unsigned char;
using uint16 = unsigned short int;
using uint32 = unsigned int;
using uint64 = unsigned long long;
using int8 = signed char;
using int16 = short int;
using int32 = int;
using int64 = long long;
using ldb = long double;
using pi5 = std::pair<int32, int32>;
using pi6 = std::pair<int64, int64>;
using pi56 = std::pair<int32, int64>;
using pi65 = std::pair<int64, int32>;

namespace oct {

template <typename _Tp>
class priority_queue
    : public std::priority_queue<_Tp, std::vector<_Tp>, std::greater<_Tp> > {};

/* Array tn4 is the 4-direction changes in coordinate system.
 * The directions in order is {right, up, left, down}.
 */
const int tn4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int tn8[8][2] = {{1, 0},  {1, 1},   {0, 1},  {-1, 1},
                       {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
const double exp = 1e-8;

void sync(int pre = 8);
template <typename _Tp>
_Tp &mad(_Tp &x, _Tp y);
template <typename _Tp>
_Tp &mid(_Tp &x, _Tp y);
template <typename _Tp>
bool in(_Tp x, _Tp l, _Tp r);
template <typename _Tp>
bool in(_Tp x, _Tp y, _Tp l, _Tp r);
template <typename _Tp>
_Tp sqr(_Tp x);
template <typename _Tp>
_Tp power(_Tp x, int64 m);
template <typename _Tp>
void sort(_Tp &x, _Tp &y);
template <typename _Tp1, typename _Tp2>
std::pair<_Tp1, _Tp2> operator+(std::pair<_Tp1, _Tp2> x,
                                std::pair<_Tp1, _Tp2> y);
template <typename _Tp>
_Tp &operator+=(_Tp &x, _Tp y);
template <typename _Tp>
_Tp gcd(_Tp &x, _Tp &y);

inline void sync(int pre) {
    std::ios::sync_with_stdio(0);
    std::cin.tie(0), std::cout.tie(0);
    std::cout.flags(std::ios::fixed);
    std::cout.precision(pre);
    std::cout.setf(std::ios::showpoint);
}
template <typename _Tp>
inline _Tp &mad(_Tp &x, _Tp y) {
    return x = std::max(x, y);
}
template <typename _Tp>
inline _Tp &mid(_Tp &x, _Tp y) {
    return x = std::min(x, y);
}
template <typename _Tp>
inline bool in(_Tp x, _Tp l, _Tp r) {
    return l <= x && x <= r;
}
template <typename _Tp>
inline bool in(_Tp x, _Tp y, _Tp l, _Tp r) {
    return l <= x && y <= r;
}
template <typename _Tp>
inline _Tp sqr(_Tp x) {
    return x * x;
}
template <typename _Tp>
inline _Tp power(_Tp x, int64 m) {
    return m == 1 ? x : (m & 1 ? power(x * x, m / 2) * x : power(x * x, m / 2));
}
template <typename _Tp>
inline void sort(_Tp &x, _Tp &y) {
    if (x > y) std::swap(x, y);
}
template <typename _Tp1, typename _Tp2>
inline std::pair<_Tp1, _Tp2> operator+(std::pair<_Tp1, _Tp2> x,
                                       std::pair<_Tp1, _Tp2> y) {
    return x.fir += y.fir, x.sec += y.sec, x;
}
template <typename _Tp>
inline _Tp &operator+=(_Tp &x, _Tp y) {
    return x = x + y;
}
template <typename _Tp>
inline _Tp gcd(_Tp &x, _Tp &y) {
    return std::__gcd(x, y);
}

}  // namespace oct

#endif  // oct code end pre-document

int n, k;

int main() {
    std::cin >> n >> k;
    if (n <= 3 || k > 3 || k == 1) return std::cout << -1 << std::endl, 0;
    if (n == 4 && k == 3) return std::cout << -1 << std::endl, 0;
    std::cout << n - 1 << std::endl;
    if (k == 2) {
        for (int i = 1; i < n; ++i) std::cout << i << ' ' << i + 1 << std::endl;
        return 0;
    }
    for (int i = 1; i < n - 2; ++i) std::cout << 1 << ' ' << i << std::endl;
    std::cout << n - 1 << ' ' << n << std::endl;
    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/priority_queue.hpp>
//#pragma GCC optimize("O3")
//#pragma GCC target("sse4")
//#pragma GCC target("avx2")

#define deb(x) cout << #x << " = " << x << "\n"
#define all(x) (x).begin(), (x).end()
#define len(x) (int) x.size()
#define lsb(x) (x) & -(x)
#define l(x) (x << 1) + 1
#define r(x) (x << 1) + 2

#define xx first
#define yy second
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound

using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <ld, ld> pld;
typedef pair <ll, ll> pll;

template <class T1, class T2 = less <T1>> using pb_heap = __gnu_pbds::priority_queue <T1, T2, binary_heap_tag>;
template <class T1, class T2 = null_type, class T3 = less <T1>> using pb_map = tree <T1, T2, T3, rb_tree_tag, tree_order_statistics_node_update>;
template <class T1, class T2 = null_type, class T3 = hash <T1>> using pb_umap = gp_hash_table <T1, T2, T3>;
template <class T1, class T2, class T3 = hash <T1>> using umap = unordered_map <T1, T2, T3>;
template <class T> using uset = unordered_set <T>;
template <class T> using vec = vector <T>;

const ll infll = numeric_limits <ll>::max() >> 1;
const int inf = numeric_limits <int>::max() >> 1;
const int N = 1001;
int n, k;

void input() {
    cin >> n >> k;
}

void solve() {
    if(n <= 3 || k >= 4 || k == 1) {
        cout << "-1\n";
        return;
    }
    if(n == 4 && k == 2) {
        cout << "-1\n";
        return;
    }
    if(k == 2) {
        cout << n - 1 << "\n";
        for(int i = 1; i < n; ++i) {
            cout << i << " " << i + 1 << "\n";
        }
        return;
    } else {
        cout << 2 * n - 5 << "\n";
        for(int i = 1; i < n; ++i) {
            cout << i << " " << i + 1 << "\n";
        }
        for(int i = 3; i < n - 1; ++i) {
            cout << 1 << " " << i << "\n";
        }
        return;
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    input();
    solve();
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/priority_queue.hpp>
//#pragma GCC optimize("O3")
//#pragma GCC target("sse4")
//#pragma GCC target("avx2")

#define deb(x) cout << #x << " = " << x << "\n"
#define all(x) (x).begin(), (x).end()
#define len(x) (int) x.size()
#define lsb(x) (x) & -(x)
#define l(x) (x << 1) + 1
#define r(x) (x << 1) + 2

#define xx first
#define yy second
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound

using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <ld, ld> pld;
typedef pair <ll, ll> pll;

template <class T1, class T2 = less <T1>> using pb_heap = __gnu_pbds::priority_queue <T1, T2, binary_heap_tag>;
template <class T1, class T2 = null_type, class T3 = less <T1>> using pb_map = tree <T1, T2, T3, rb_tree_tag, tree_order_statistics_node_update>;
template <class T1, class T2 = null_type, class T3 = hash <T1>> using pb_umap = gp_hash_table <T1, T2, T3>;
template <class T1, class T2, class T3 = hash <T1>> using umap = unordered_map <T1, T2, T3>;
template <class T> using uset = unordered_set <T>;
template <class T> using vec = vector <T>;

const ll infll = numeric_limits <ll>::max() >> 1;
const int inf = numeric_limits <int>::max() >> 1;
const int N = 1001;
int n, k;

void input() {
    cin >> n >> k;
}

void solve() {
    if(n <= 3 || k >= 4 || k == 1) {
        cout << "-1\n";
        return;
    }
    if(n == 4 && k == 2) {
        cout << "-1\n";
        return;
    }
    if(k == 2 || n == 4) {
        cout << n - 1 << "\n";
        for(int i = 1; i < n; ++i) {
            cout << i << " " << i + 1 << "\n";
        }
        return;
    } else {
        cout << 2 * n - 4 << "\n";
        for(int i = 1; i < n; ++i) {
            cout << i << " " << i + 1 << "\n";
        }
        for(int i = 3; i < n; ++i) {
            cout << 1 << " " << i << "\n";
        }
        return;
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    input();
    solve();
}


# include <bits/stdc++.h>

using namespace std;

typedef pair <int, int> pii;

# define endl '\n'
# define sep ' '
# define pb push_back
# define F first
# define S  second

int main(){
	int n, k;
	cin >> n >> k;

	if (k < 2 || k > 3 || n < 4 ){
		cout << -1 << endl;
		return 0;
	}
	if (k == 2){
		if (n == 4){
			cout << -1 << endl;
			return 0;
		}
		cout << n-1 << endl;
		for (int i=1; i<n; i++) cout << i << sep << i+1 << endl;
	}

	else{
		cout << 2*n-5  << endl;
		for (int i=2; i<n-1; i++) cout << 1 << sep << i << endl;
		for (int i=3; i<n-1; i++) cout << 2 << sep << i << endl;
		cout << 2 << sep << n-1 << endl;
		cout << n-1 << sep << n << endl;
	}
}


# include <bits/stdc++.h>

using namespace std;

typedef pair <int, int> pii;

# define endl '\n'
# define sep ' '
# define pb push_back
# define F first
# define S  second

int main(){
	int n, k;
	cin >> n >> k;

	if (k < 2 || k > 3 || n < 4 ){
		cout << -1 << endl;
		return 0;
	}
	if (k == 2){
		cout << n-1 << endl;
		for (int i=1; i<n; i++) cout << i << sep << i+1 << endl;
	}

	else{
		cout << 2*n-5  << endl;
		for (int i=2; i<n-1; i++) cout << 1 << sep << i << endl;
		for (int i=2; i<n-2; i++) cout << i << sep << i+1 << endl;
		cout << 2 << sep << n-1 << endl;
		cout << n-1 << sep << n << endl;
	}
}


# include <bits/stdc++.h>

using namespace std;

typedef pair <int, int> pii;

# define endl '\n'
# define sep ' '
# define pb push_back
# define F first
# define S  second

int main(){
	int n, k;
	cin >> n >> k;

	if (k < 2 || k > 3 || n < 4 ){
		cout << -1 << endl;
		return 0;
	}
	if (k == 2){
		cout << n-1 << endl;
		for (int i=1; i<n; i++) cout << i << sep << i+1 << endl;
	}

	else{
		cout << 2*n-5  << endl;
		for (int i=2; i<n-1; i++) cout << 1 << sep << i << endl;
		for (int i=3; i<n-1; i++) cout << 2 << sep << i << endl;
		cout << 2 << sep << n-1 << endl;
		cout << n-1 << sep << n << endl;
	}
}


// In the name of Allah.
// We're nothing and you're everything.
// Ya Ali!

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 5e5 + 14;
int n, k;
int main(){
    ios::sync_with_stdio(0), cin.tie(0);
    cin >> n >> k;
    if(k == 1 || k > 3 || k >= n || n == 3 || n == 4 && k == 2)
        cout << "-1\n";
    else if(k == 2){
        cout << n - 1 << '\n';
        for(int i = 2; i <= n; i++)
            cout << i - 1 << ' ' << i << '\n';
    }
    else{
        cout << 3 + 2 * (n - 4) << '\n';
        cout << "1 2 \n1 3\n2 4\n";
        for(int i = 5; i <= n; i++)
            cout << "1 " << i << "\n2 " << i << '\n';
    }
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define so sort(a , a+n)
#define ll long long
#define migmig ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int maxn=2*1e5+50;
int a[maxn];
int main()
{
	migmig
	int n, k;
	cin>>n>>k;
	if(k==1||k>3)
		cout<<-1<<endl;
	else if(n<=4&&k==2)
		cout<<-1<<endl;
	else if(k==2)
	{
		cout<<n-1<<endl;
		for(int i=2;i<=n;i++)
			cout<<i<<" "<<i-1<<endl;
	}
	else if(k==3&&n>3)
	{
		cout<<n-1<<endl;
		for(int i=2;i<n;i++)
		{
			cout<<1<<" "<<i<<endl;
		}
		cout<<2<<" "<<n<<endl;
	}
	else
		cout<<-1;
}


#include<bits/stdc++.h>

using namespace std;

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int n,k;
	cin >> n >> k;
	if(k>3 || n<(k+1)){
		cout << -1 << "\n";
		return 0;
	}

	vector<pair<int,int>> res;
	if(k==3){
		for(int i=0;i<n;i++){
			if(i!=0 && i!=2) res.push_back(make_pair(0,i));
			if(i!=0 && i!=1 && i!=3) res.push_back(make_pair(3,i));
		}
		//G  : 1 0 3 2
		//G' : 0 2 1 3
	}
	if(k==2){
		if(n==3 || n==4){
			cout << -1 << "\n";
			return 0;
		}
		for(int i=1;i<n;i++){
			res.push_back(make_pair(i-1,i));
		}
	}
	if(k==1){
		cout << -1 << "\n";
		return 0;
	}

	cout << res.size() << "\n";
	for(int i=0;i<res.size();i++){
		cout << (res[i].first+1) << " " << (res[i].second+1) << "\n";
	}
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int n,k;
	cin >> n >> k;
	if(k>3 || n<(k+1)){
		cout << -1 << "\n";
		return 0;
	}

	vector<pair<int,int>> res;
	if(k==3){
		for(int i=0;i<n;i++){
			if(i!=0 && i!=2) res.push_back(make_pair(0,i));
			if(i!=0 && i!=1 && i!=3) res.push_back(make_pair(3,i));
		}
		//G  : 1 0 3 2
		//G' : 0 2 1 3
	}
	if(k==2){
		if(n==3){
			cout << -1 << "\n";
			return 0;
		}
		for(int i=1;i<n;i++){
			res.push_back(make_pair(i-1,i));
		}
	}
	if(k==1){
		cout << -1 << "\n";
		return 0;
	}

	cout << res.size() << "\n";
	for(int i=0;i<res.size();i++){
		cout << (res[i].first+1) << " " << (res[i].second+1) << "\n";
	}
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int n,k;
	cin >> n >> k;
	if(k>3 || n<(k+1)){
		cout << -1 << "\n";
		return 0;
	}

	vector<pair<int,int>> res;
	if(k==3){
		for(int i=0;i<n;i++){
			if(i!=0 && i!=2) res.push_back(make_pair(0,i));
			if(i!=0 && i!=1 && i!=3) res.push_back(make_pair(3,i));
		}
		//G  : 1 0 3 2
		//G' : 0 2 1 3
	}
	if(k==2){
		if(n==3){
			cout << -1 << "\n";
			return 0;
		}
		for(int i=0;i<n;i++){
			if(i!=0 && i!=1) res.push_back(make_pair(0,i));
			if(i!=0 && i!=1 && i!=2) res.push_back(make_pair(1,i));
			if(i!=0 && i!=1 && i!=2) res.push_back(make_pair(2,i));
		}
		//G  : 0 3 1
		//G' : 0 1 2
	}
	if(k==1){
		cout << -1 << "\n";
		return 0;
	}

	cout << res.size() << "\n";
	for(int i=0;i<res.size();i++){
		cout << (res[i].first+1) << " " << (res[i].second+1) << "\n";
	}
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int n,k;
	cin >> n >> k;
	if(k>3 || n<(k+1)){
		cout << -1 << "\n";
		return 0;
	}

	vector<pair<int,int>> res;
	if(k==3){
		for(int i=0;i<n;i++){
			if(i!=0 && i!=2) res.push_back(make_pair(0,i));
			if(i!=0 && i!=1 && i!=3) res.push_back(make_pair(3,i));
		}
		//G  : 1 0 3 2
		//G' : 0 2 1 3
	}
	if(k==2){
		if(n==3){
			cout << -1 << "\n";
			return 0;
		}
		for(int i=0;i<n;i++){
			if(i!=0 && i!=1) res.push_back(make_pair(0,i));
			if(i!=0 && i!=1 && i!=2) res.push_back(make_pair(2,i));
		}
		res.push_back(make_pair(1,3));
		//G  : 0 3 1
		//G' : 0 1 2
	}
	if(k==1){
		cout << -1 << "\n";
		return 0;
	}

	cout << res.size() << "\n";
	for(int i=0;i<res.size();i++){
		cout << (res[i].first+1) << " " << (res[i].second+1) << "\n";
	}
	return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#define pb push_back
#define ob pop_back
#define xx first
#define m_p make_pair
#define yy second
#define dob double
#define pii pair < int , int >
#define pii_i pair < pair < int , int > , int >
#define gcd __gcd
#define lb(x) ((x)&(-x))
#define int long long int
#define ss size()
using namespace std;
const long long M = 1e9 + 7 , maxn = 2e3 + 50;
int ans , n , m , x , y , q , w , k ;

int32_t main(){

        cin >> n >> k;

        if(k == 2){
                if(n < 5)
                        return cout << -1 , 0;

                cout << n-1 << "\n";

                for(int i = 1 ; i < n ; i++){
                        cout << i << " " << i+1 << "\n";
                }

                return 0;
        }
        if(k == 3){
                if(n < 4)
                        return cout << -1 , 0;

                cout << n - 1 << "\n" << "1 2\n" << "2 3\n";

                for(int i = 4 ; i <= n ; i++){
                        cout << 3 << " " << i << "\n";
                }
        }

        cout << -1 << "\n";

        return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#define pb push_back
#define ob pop_back
#define xx first
#define m_p make_pair
#define yy second
#define dob double
#define pii pair < int , int >
#define pii_i pair < pair < int , int > , int >
#define gcd __gcd
#define lb(x) ((x)&(-x))
#define int long long int
#define ss size()
using namespace std;
const long long M = 1e9 + 7 , maxn = 2e3 + 50;
int ans , n , m , x , y , q , w , k ;

int32_t main(){

        cin >> n >> k;

        if(k == 2){
                if(n < 5)
                        return cout << -1 , 0;

                cout << n-1 << "\n";

                for(int i = 1 ; i < n ; i++){
                        cout << i << " " << i+1 << "\n";
                }

                return 0;
        }
        if(k == 3){
                if(n < 4)
                        return cout << -1 , 0;

                cout << n - 3 + n - 2 << "\n";

                for(int i = 3 ; i <= n ; i++){
                        cout << 1 << " " << i << "\n";
                }
                for(int i = 4 ; i <= n ; i++){
                        cout << 2 << " " << i << "\n";
                }
        }

        cout << -1 << "\n";

        return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#define pb push_back
#define ob pop_back
#define xx first
#define m_p make_pair
#define yy second
#define dob double
#define pii pair < int , int >
#define pii_i pair < pair < int , int > , int >
#define gcd __gcd
#define lb(x) ((x)&(-x))
#define int long long int
#define ss size()
using namespace std;
const long long M = 1e9 + 7 , maxn = 2e3 + 50;
int ans , n , m , x , y , q , w , k ;

int32_t main(){

        cin >> n >> k;

        if(k == 2){
                if(n < 5)
                        return cout << -1 , 0;

                cout << n-1 << "\n";

                for(int i = 1 ; i < n ; i++){
                        cout << i << " " << i+1 << "\n";
                }

                return 0;
        }
        if(k == 3){
                if(n < 4)
                        return cout << -1 , 0;

                cout << n - 3 + n - 2 << "\n";

                for(int i = 3 ; i <= n ; i++){
                        cout << 1 << " " << i << "\n";
                }
                for(int i = 4 ; i <= n ; i++){
                        cout << 2 << " " << i << "\n";
                }
                return 0;
        }

        cout << -1 << "\n";

        return 0;
}


//#define __USE_MINGW_ANSI_STDIO 0
#include <iostream>
#include <bits/stdc++.h>
using namespace std;

#define debug(x) cout<<#x<<" :: "<<x<<endl;
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<endl;
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<endl;

#define boost ios::sync_with_stdio(0); cin.tie(0)

#define fi first
#define se second
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)

typedef long long ll;
typedef double ld;
typedef pair<int, int> pii;

const int N = 1e6 + 5;

/***************************************************************************/

int main() {

	boost;
	int n, k; cin>>n>>k;
	if(k != 2 and k != 3) {
		cout<<"-1\n";
		exit(0);
	}
	if(k == 2) {
		if(n <= 4) {
			cout<<"-1\n";
			exit(0);
		}
		cout<<n-1<<"\n";
		for(int i=1; i<n; i++) {
			cout<<i<<" "<<i+1<<"\n";
		}
	}
	else {
		if(n <= 3) {
			cout<<"-1\n";
			exit(0);
		}
		cout<<n+n-5<<"\n";
		for(int i=1; i<n; i++) {
			cout<<i<<" "<<i+1<<"\n";
		}
		for(int i=5; i<=n; i++) {
			cout<<"3 "<<i<<"\n";
		}
	}
	return 0;
}


// be name khoda \\

// #define stream_enable
// #define bloat_enable
#define long_enable
#define debug_enable

#ifdef bloat_enable
#include <bits/stdc++.h>
#else
#include <iostream>
#include <map>
#include <iomanip>
#include <vector>
#include <set>
#include <cmath>
#include <utility>
#include <algorithm>
#include <sstream>
#include <queue>
#include <stack>
#include <limits.h>
#include <fstream>
#include <cstring>
#endif

using namespace std;

#ifdef stream_enable
#define cin sss
#endif

#ifdef long_enable
typedef long long int ll;
#else
typedef int ll;
#endif

typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef pair<ll, pii> pip;
typedef vector<vi> vii;
typedef vector<vii> viii;
typedef vector<bool> vb;
typedef vector<vb> vbb;
typedef vector<pii> vpii;
typedef vector<pip> vpip;
typedef priority_queue<ll> maxHeap;
typedef priority_queue<ll, vi, greater<ll>> minHeap;

const ll MOD = 1000000007;

const long long BIG = 1446803456761533460LL;
const int Big = 336860180;

#ifdef long_enable
const ll INF = LONG_LONG_MAX;
#else
const ll INF = INT_MAX;
#endif

const vii adj4({{0, 1}, {0, -1}, {1, 0}, {-1, 0}});
const vii adj8({{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {-1, -1}, {1, -1}, {-1, 1}, {1, 1}});

#define mp make_pair
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define pb push_back

#define print(x) cout << (x) << endl
#define printv(x) fori (j12345, (x).size()) { cout << (x)[j12345] << ' '; } cout << endl
#define printvv(x) fori (i123456, (x).size()) { fori (j123456, (x)[i123456].size()) { cout << (x)[i123456][j123456] << ' '; } cout << endl; }
#define printp(x) cout << "(" << x.ff << ", " << x.ss << ")" << endl
#define printvp(x) fori (i1234567, (x).size()) { cout << "(" << x[i1234567].ff << ", " << x[i1234567].ss << ")" << endl; }
#define printa(x, n) fori (ja12345, n) { cout << (x)[ja12345] << ' '; } cout << endl
#define printap(x, n) fori (ia1234567, n) { cout << "(" << x[ia1234567].ff << ", " << x[ia1234567].ss << ")" << endl; }
#define printaa(x, n, m) fori (iaa123456, n) { fori (jaa123456, m) { cout << (x)[iaa123456][jaa123456] << ' '; } cout << endl; }

#ifdef debug_enable
#define debug(x) cout << #x << " -> "; print(x)
#define debugv(x) cout << #x << " -> "; printv(x)
#define debugvv(x) cout << #x << " ->\n"; printvv(x)
#define debugp(x) cout << #x << " -> "; printp(x)
#define debugvp(x) cout << #x << " ->\n"; printvp(x)
#define debuga(x, n) cout << #x << " -> "; printa(x, n)
#define debugap(x, n) cout << #x << " ->\n"; printap(x, n)
#define debugaa(x, n, m) cout << #x << " ->\n"; printaa(x, n, m)
#else
#define debug(x)
#define debugv(x)
#define debugvv(x)
#define debugp(x)
#define debugvp(x)
#define debuga(x, n)
#define debugap(x, n)
#define debugaa(x, n, m)
#endif

#define fori(i, n) for(ll i = 0; i < (n); ++i)
#define forir(i, n) for(ll i = (n) - 1; i >= 0; --i)
#define forifrom(i, f, n) for(ll i = (f); i < (n); ++i)
#define forirto(i, n, f) for(ll i = (n); i >= (f); --i)

#define smin(a, b) a = min(a, (b))
#define smax(a, b) a = max(a, (b))

#define inp(x) ll x; cin >> x
#define inp2(x, y) ll x, y; cin >> x >> y
#define inp3(x, y, z) ll x, y, z; cin >> x >> y >> z
#define inp4(x, y, z, zz) ll x, y, z, zz; cin >> x >> y >> z >> zz
#define inpvi(x, size) vi x(size); fori(i123458, size) cin >> x[i123458]

ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a % b);
}

bool isprime(ll n) {
    if (n == 2 || n == 3) return true;
    if (n < 2 || n % 2 == 0 || n % 3 == 0) return false;
    for (ll i = 6; (i - 1) * (i - 1) <= n; i += 6) {
        if (n % (i - 1) == 0 || n % (i + 1) == 0) {
            return false;
        }
    }
    return true;
}

vb sprime;
void genPrime(ll sz) {
    sprime = vb(sz, true);
    sprime[0] = false;
    sprime[1] = false;
    forifrom (i, 2, sz) {
        if (sprime[i]) {
            for (ll j = i * i; j < sz; j += i) {
                sprime[j] = false;
            }
        }
    }
}

ll powMod(ll a, ll b, ll mod) {
    ll n = 1;
    ll p = a;
    while (b > 0) {
        if (b % 2 == 1) {
            n *= p;
            n %= mod;
        }
        p *= p;
        p %= mod;
        b /= 2;
    }
    return n;
}

ll modularInverse(ll a, ll mod) {
    return powMod(a, mod - 2, mod);
}

ll binarysearch(ll l, ll r, bool (*bsfunction) (ll)) {
    while (r - l > 1) {
        ll mid = (l + r) / 2;
        bool val = bsfunction(mid);
        if (val) {
            r = mid;
        } else {
            l = mid;
        }
    }
    return l;
}

stringstream sss;

// -----------------------------------------------------------------------

const ll maxn = 1010;

void MAIN() {

    inp2(n, k);
    if (k == 2 && n >= 5) {
        print(n - 1);
        forifrom (i, 1, n) {
            cout << i << ' ' << i + 1 << endl;
        }
    } else if (k == 3 && n >= 4) {
        print((n - 4) * 2 + 3);
        cout << "1 2\n2 3\n3 4\n";
        forifrom (i, 5, n + 1) {
            cout << i << " 1\n" << i << " 3\n";
        }
    } else {
        print(-1);
    }
}

// -----------------------------------------------------------------------

#ifdef stream_enable
const ll TESTCASEN = 1;
#else
const ll TESTCASEN = 1;
#endif

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(10);
    sss << R"(
5 2
5 2
    )";
    fori (i, TESTCASEN) {
        MAIN();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n, k;
int main() {
	cin >> n >> k;
	if (k >= 3 || k == 1) return !printf("-1\n");
	if (k == 2) {
		if (n <= 3) return !printf("-1\n");
		printf("%d\n", n - 1);
		for (int i = 1; i < n; i ++) {
			printf("%d %d\n", i, i + 1);
		}
	}

}


#include <bits/stdc++.h>
using namespace std;
int n, k;
int main() {
	cin >> n >> k;
	if (k >= 4 || k == 1) return !printf("-1\n");
	if (k == 2) {
		if (n <= 4) return !printf("-1\n");
		printf("%d\n", n - 1);
		for (int i = 1; i < n; i ++) {
			printf("%d %d\n", i, i + 1);
		}
	}
	if (k == 3) {
		if (n <= 3) return !printf("-1\n");
		printf("%d\n", n - 1);
		printf("1 2\n2 3\n");
		for (int i = 4; i <= n; i ++) {
			printf("%d %d\n", 3,i);
		}
	}

}


#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  if (n < 4 || k != 2) {
    cout << -1 << '\n';
    return 0;
  }
  cout << n << '\n';
  for (int i = 1; i < n; i++) {
    cout << i << ' ' << i + 1 << '\n';
  }
  cout << 1 << ' ' << n << '\n';
  return 0;
}


#include<stdio.h>
int main(void)
{

    int n,d,f=0,th=0,tw=0,o=0,tw1=0,tw2=0;
    int r1,r2,total=0;
    scanf("%d", &n);
    for(int i=0;i<n;i++)
    {
        scanf("%d", &d);
        if(d==4)
            f=f+1;
        else if(d==3)
            th=th+1;
        else if(d==2)
            tw=tw+1;
        else
            o=o+1;
    }
    if(th>o) {r1=o;th=th-o;o=0;}
    else if(th<o) {r1=th;o=o-th;th=0;}
    else {r1=th;th=0,o=0;}
    if(tw%2==0) tw=tw/2;
    else {tw=tw/2;o=o+2;};
    if(o&&o%4) r2=o/4+1;
    else r2=o/4;
    printf("%d", f+r2+r1+th+tw);

}


#include<stdio.h>
int main(void)
{

    int n,d,f=0,th=0,tw=0,o=0;
    int r1,r2,total=0;
    scanf("%d", &n);
    for(int i=0;i<n;i++)
    {
        scanf("%d", &d);
        if(d==4)
            f=f+1;
        else if(d==3)
            th=th+1;
        else if(d==2)
            tw=tw+1;
        else
            o=o+1;
    }
    if((th>=0&&o>0)&&(o>=0&&th>0))
    {
    if(th>=o)
        r1=th-o;
    else
        r1=o-th;
    }
    else
        r1=o;
    tw=(2*tw)+r1;
    if(tw%4==0)
        r2=tw/8;
    else
    {
        if(tw%4>=2)
            r2=(tw/8)+2;
        else
            r2=(tw/8)+1;
    }
    total=total+f+th+r2;
    printf("%d",total);

}


#include<stdio.h>
int main(void)
{

    int n,d,f=0,th=0,tw=0,o=0;
    int r1,r2,total=0;
    scanf("%d", &n);
    for(int i=0;i<n;i++)
    {
        scanf("%d", &d);
        if(d==4)
            f=f+1;
        else if(d==3)
            th=th+1;
        else if(d==2)
            tw=tw+1;
        else
            o=o+1;
    }
    if(th>=o)
        r1=th-o;
    else
        r1=o-th;
    tw=tw+r1;
    if(tw%4==0)
        r2=tw/4;
    else
        r2=(tw/4)+1;
    total=total+f+th+r2;
    printf("%d",total);

}


#include<stdio.h>
int main(void)
{

    int n,d,f=0,th=0,tw=0,o=0;
    int r1,r2,total=0;
    scanf("%d", &n);
    for(int i=0;i<n;i++)
    {
        scanf("%d", &d);
        if(d==4)
            f=f+1;
        else if(d==3)
            th=th+1;
        else if(d==2)
            tw=tw+1;
        else
            o=o+1;
    }
    if(th>0&&o>0)
    {
    if(th>=o)
        r1=th-o;
    else
        r1=o-th;
    }
    else
        r1=o;
    tw=tw+r1;
    if(tw%4==0)
        r2=tw/4;
    else
        r2=(tw/4)+1;
    total=total+f+th+r2;
    printf("%d",total);

}


#include<stdio.h>
int main(void)
{

    int n,d,f=0,th=0,tw=0,o=0;
    int r1,r2,r,q1,q2,q,total=0;
    scanf("%d", &n);
    for(int i=0;i<n;i++)
    {
        scanf("%d", &d);
        total=total+d;
    }
    r=total/4;
    if(total%4==0)
    {
        printf("%d", r);
    }
    else
    {
        printf("%d",r+1);
    }

}


#include <stdio.h>
int main()
{
	long long int n, a[1000], i, j, sum = 0, k;
	scanf("%lld", &n);
	for (i = 0; i < n; i++)
	{
		scanf("%lld", &a[i]);
	}
	for (i = 0; i < n; i++)
	{
		for (j = i + 1; j < n; j++)
		{
			if (a[i] < a[j])
			{
				int tem = a[i];
				a[i] = a[j];
				a[j] = tem;
			}
		}
	}
	for (i = 0; i < n; i++)
	{
		sum = sum + a[i];
	}
	k = sum / 4;
	if (sum % 4 == 0)
		printf("%lld\n", k);
	else
		printf("%lld\n", k + 1);
}


#include <stdio.h>
#include <stdlib.h>
#include<math.h>

int main()
{
 int  i=0 , sum = 0 , num = 0;
 int n , j = 0 ;

    scanf(" %d",&n);
  int group[n] ;
  int*s = group ;

     while(n--)
     {
         scanf(" %d",s);

       if(*s == 4)
       {
         ++num;
         j++ ;
       }
    if(*s != 4)
       {
            sum += *s ;
            i++;
       }
    if (sum == 8 )
       {

        if(i%2 == 0)
        {
            num += (sum/i) ;
        }
        else
        {
         num += (sum/i)+1;

       }

       sum = 0 ;
       i = 0 ;
       }
    else if (sum > 8)
       {
           num += 3;
           sum=0 ;
           i=0;
       }
    else if (sum == 4 )
       {
           num++;
           sum = 0 ;
           i=0;
       }
    else if (sum < 4 && n==0 && sum!=0)
       {
           num++;
       }
    else if (sum < 8 && n==0 && sum!=0)
       {
           num+=2 ;
           i=0;
       }

       ++s;
     }

     printf("%d", num) ;

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
  int n , i=0 , sum = 0 , num = 0;

    scanf(" %d",&n);
  int group[n] ;
  int*s = group ;

     while(n--)
     {
         scanf(" %d",s);

       if(*s == 4)
       {
         ++num;
         continue ;
       }
        sum += *s ;
       if (sum == 8 )
       {
           num+=2;
           sum = 0 ;
       }
       else if (sum > 8)
       {
           num += 3;
           sum=0 ;
       }
       else if (sum == 4 || (sum < 4 && n==0) )
       {
           num++;
           sum = 0 ;
       }
       else if (sum <= 4 && n ==0)
       {
           num++;
       }
       ++s;
     }
     printf("%d", num) ;

    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include<math.h>

int main()
{
 float  i=0 , sum = 0 , num = 0;
 int n ;

    scanf(" %d",&n);
  float group[n] ;
  float*s = group ;

     while(n--)
     {
         scanf(" %f",s);

       if(*s == 4)
       {
         ++num;

       }
       if(*s != 4)
       {
            sum += *s ;
            i++;
       }
       if (sum == 8 )
       {
          num = ceil(sum/i);
           sum = 0 ;
       }
       else if (sum > 8)
       {
           num += 3;
           sum=0 ;
       }
       else if (sum == 4 )
       {
           num++;
           sum = 0 ;
       }
      else if (sum < 4 && n==0 && sum!=0)
       {
           num++;
       }
       else if (sum < 8 && n==0 && sum!=0)
       {
           num+=2 ;
       }

       ++s;
     }
     ;
     printf("%.0f", num) ;

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
  int n , i=0 , sum = 0 , num = 0;

    scanf(" %d",&n);
  int group[n] ;
  int*s = group ;

     while(n--)
     {
         scanf(" %d",s);

       if(*s == 4)
       {
         ++num;
         continue ;
       }
        sum += *s ;
       if (sum == 8)
       {
           num+=2;
           sum = 0 ;
       }
       else if (sum > 8)
       {
           num += 3;
           sum=0 ;
       }
       else if (sum == 4 )
       {
           num++;
           sum = 0 ;
       }
       else if (sum <= 4 && n ==1)
       {
           num++;
       }
       ++s;
     }
     printf("%d", num) ;

    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include<math.h>

int main()
{
 int sum1_2=0 , num = 0 , count[5] = {0}, n , a ;

    scanf(" %d",&n);

     while(n--)
     {
         scanf(" %d",&a);
          count[a] ++ ;
     }
     num = count[4]+count[3] ;

     if(count[1]>count[3])
     {
         count[1] -= count[3] ;
     }
     else
     {
         count[1] = 0 ;
     }
     sum1_2 = (count[1] + count[2]*2) ;

     if(sum1_2 % 4 == 0)
     {
         num += sum1_2/4 ;
     }
     else
        num += ((sum1_2/4) + 1);

     printf("%d", num) ;

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
  int n , i=0 , sum = 0 , num = 0;

    scanf(" %d",&n);
  int group[n] ;
  int*s = group ;

     while(n--)
     {
         scanf(" %d",s);

       if(*s == 4)
       {
         ++num;
         continue ;
       }
        sum += *s ;
       if (sum == 8)
       {
           num+=2;
           sum = 0 ;
       }
       else if (sum > 8)
       {
           num += 3;
           sum=0 ;
       }
       else if (sum == 4 )
       {
           num++;
           sum = 0 ;
       }
       else if (sum > 4 || n ==1)
       {
           num++;
       }
       ++s;
     }
     printf("%d", num) ;

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
  int n , i=0 , sum = 0 , num = 0;

    scanf(" %d",&n);
  int group[n] ;
  int*s = group ;

     while(n--)
     {
         scanf(" %d",s);

       if(*s == 4)
       {
         ++num;
         continue ;
       }
        sum += *s ;
       if (sum == 8)
       {
           num+=2;
           sum = 0 ;
       }
       else if (sum > 8)
       {
           num += 3;
           sum=0 ;
       }
       else if (sum == 4 )
       {
           num++;
           sum = 0 ;
       }
       ++s;
     }
     printf("%d", num) ;

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
  int n , i=0 , sum = 0 , num = 0;

    scanf(" %d",&n);
  int group[n] ;
  int*s = group ;

     while(n--)
     {
         scanf(" %d",s);

       if(*s == 4)
       {
         ++num;

       }
       if(*s != 4)
       {
            sum += *s ;
       }
       if (sum == 8 )
       {
           num+=2;
           sum = 0 ;
       }
       else if (sum > 8)
       {
           num += 3;
           sum=0 ;
       }
       else if (sum == 4 )
       {
           num++;
           sum = 0 ;
       }
      else if (sum < 4 && n==0)
       {
           num++;
       }
       else if (sum < 8 && n==0)
       {
           num+=2 ;
       }

       ++s;
     }
     ;
     printf("%d", num) ;

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
  int n , i=0 , sum = 0 , num = 0;

    scanf(" %d",&n);
  int group[n] ;
  int*s = group ;

     while(n--)
     {
         scanf(" %d",s);

       if(*s == 4)
       {
         ++num;
         continue ;
       }
        sum += *s ;
       if (sum == 8)
       {
           num+=2;
           sum = 0 ;
       }
       else if (sum > 8)
       {
           num += 3;
           sum=0 ;
       }
       else if (sum == 4 )
       {
           num++;
           sum = 0 ;
       }
       else if (sum < 4 && n ==1)
       {
           num++;
       }
       ++s;
     }
     printf("%d", num) ;

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
  int n , i=0 , sum = 0 , num = 0;

    scanf(" %d",&n);
  int group[n] ;
  int*s = group ;

     while(n--)
     {
         scanf(" %d",s);

       if(*s == 4)
       {
         ++num;
         continue ;
       }
        sum += *s ;
       if (sum == 8 )
       {
           num+=2;
           sum = 0 ;
       }
       else if (sum > 8)
       {
           num += 3;
           sum=0 ;
       }
       else if (sum == 4 || (sum < 4 && n==0) )
       {
           num++;
           sum = 0 ;
       }
       else if (sum <= 4 )
       {
           num++;
       }
       else if (sum < 8)
       {
           num+=2 ;
       }
       ++s;
     }
     printf("%d", num) ;

    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include<math.h>

int main()
{
 int  i=0 , sum = 0 , num = 0;
 int n ;

    scanf(" %d",&n);
  int group[n] ;
  int*s = group ;

     while(n--)
     {
         scanf(" %d",s);

       if(*s == 4)
       {
         ++num;

       }
       if(*s != 4)
       {
            sum += *s ;
            i++;
       }
       if (sum == 8 )
       {
        if(i%2 == 0)
        {
            num = sum/i ;
        }
        else
        {
         num = (sum/i)+1;
       }
       sum = 0 ;
       i = 0 ;
       }
       else if (sum > 8)
       {
           num += 3;
           sum=0 ;
           i=0;
       }
       else if (sum == 4 )
       {
           num++;
           sum = 0 ;
           i=0;
       }
      if (sum < 4 && n==0 && sum!=0)
       {
           num++;
       }
       else if (sum < 8 && n==0 && sum!=0)
       {
           num+=2 ;
           i=0;
       }

       ++s;
     }

     printf("%d", num) ;

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
  int n , i=0 , sum = 0 , num = 0;

    scanf(" %d",&n);
  int group[n] ;
  int*s = group ;

     while(n--)
     {
         scanf(" %d",s);

       if(*s == 4)
       {
         ++num;
         continue ;
       }
        sum += *s ;
       if (sum == 8)
       {
           num+=2;
           sum = 0 ;
       }
       else if (sum > 8)
       {
           num += 3;
           sum=0 ;
       }
       else if (sum == 4 )
       {
           num++;
           sum = 0 ;
       }
       else if (sum <= 4 && n ==0)
       {
           num++;
       }
       ++s;
     }
     printf("%d", num) ;

    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include<math.h>

int main()
{
 int  i=0 , sum = 0 , num = 0;
 int n , j = 0 ;

    scanf(" %d",&n);
  int group[n] ;
  int*s = group ;

     while(n--)
     {
         scanf(" %d",s);

       if(*s == 4)
       {
         ++num;
         j++ ;
       }
    if(*s != 4)
       {
            sum += *s ;
            i++;
       }
    if (sum == 8 )
       {

        if(i%2 == 0)
        {
            num += (sum/i)+j ;
        }
        else
        {
         num += (sum/i)+1;

       }

       sum = 0 ;
       i = 0 ;
       }
    else if (sum > 8)
       {
           num += 3;
           sum=0 ;
           i=0;
       }
    else if (sum == 4 )
       {
           num++;
           sum = 0 ;
           i=0;
       }
    else if (sum < 4 && n==0 && sum!=0)
       {
           num++;
       }
    else if (sum < 8 && n==0 && sum!=0)
       {
           num+=2 ;
           i=0;
       }

       ++s;
     }

     printf("%d", num) ;

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
  int n , i=0 , sum = 0 , num = 0;

    scanf(" %d",&n);
  int group[n] ;
  int*s = group ;

     while(n--)
     {
         scanf(" %d",s);

       if(*s == 4)
       {
         ++num;

       }
       if(*s != 4)
       {
            sum += *s ;
       }
       if (sum == 8 )
       {
           num+=2;
           sum = 0 ;
       }
       else if (sum > 8)
       {
           num += 3;
           sum=0 ;
       }
       else if (sum == 4 )
       {
           num++;
           sum = 0 ;
       }
      else if (sum < 4 && n==0 && sum!=0)
       {
           num++;
       }
       else if (sum < 8 && n==0 && sum!=0)
       {
           num+=2 ;
       }

       ++s;
     }
     ;
     printf("%d", num) ;

    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include<math.h>

int main()
{
 int  i=0 , sum = 0 , num = 0;
 int n ;

    scanf(" %d",&n);
  int group[n] ;
  int*s = group ;

     while(n--)
     {
         scanf(" %d",s);

       if(*s == 4)
       {
         ++num;

       }
       if(*s != 4)
       {
            sum += *s ;
            i++;
       }
       if (sum == 8 )
       {
          if(i==3)
          {
              num+=3;
          }
          else
          {
            num +=2;
       }
           sum = 0 ;
       }
       else if (sum > 8)
       {
           num += 3;
           sum=0 ;
       }
       else if (sum == 4 )
       {
           num++;
           sum = 0 ;
       }
      if (sum < 4 && n==0 && sum!=0)
       {
           num++;
       }
       else if (sum < 8 && n==0 && sum!=0)
       {
           num+=2 ;
       }

       ++s;
     }

     printf("%d", num) ;

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
  int n , i=0 , sum = 0 , num = 0;

    scanf(" %d",&n);
  int group[n] ;
  int*s = group ;

     while(n--)
     {
         scanf(" %d",s);

       if(*s == 4)
       {
         ++num;
         continue ;
       }
        sum += *s ;
       if (sum == 8)
       {
           num+=2;
           sum = 0 ;
       }
       else if (sum > 8)
       {
           num += 3;
           sum=0 ;
       }
       else if (sum == 4 )
       {
           num++;
           sum = 0 ;
       }
       else if (sum < 4 || n ==1)
       {
           num++;
       }
       ++s;
     }
     printf("%d", num) ;

    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include<math.h>

int main()
{
 int  i=0 , sum = 0 , num = 0;
 int n ;

    scanf(" %d",&n);
  int group[n] ;
  int*s = group ;

     while(n--)
     {
         scanf(" %d",s);

       if(*s == 4)
       {
         ++num;
       }
    if(*s != 4)
       {
            sum += *s ;
            i++;
       }
    if (sum == 8 )
       {
        if(i%2 == 0)
        {
            num += (sum/i) ;
        }
        else
        {
         num += ((sum/i)+1);
       }

       sum = 0 ;
       i = 0 ;
       }
    else if (sum > 8)
       {
           num += 3;
           sum=0 ;
           i=0;
       }
    else if (sum == 4 )
       {
           num++;
           sum = 0 ;
           i=0;
       }
    else if (sum < 4 && n==0 && sum!=0)
       {
           num++;
       }
    else if (sum < 8 && n==0 && sum!=0)
       {
           num+=2 ;
           i=0;
       }

       ++s;
     }

     printf("%d", num) ;

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
  int n , i=0 , sum = 0 , num = 0;

    scanf(" %d",&n);
  int group[n] ;
  int*s = group ;

     while(n--)
     {
         scanf(" %d",s);

       if(*s == 4)
       {
         ++num;
         continue ;
       }
        sum += *s ;
       if (sum == 8 || (sum < 8 && n==0))
       {
           num+=2;
           sum = 0 ;
       }
       else if (sum > 8)
       {
           num += 3;
           sum=0 ;
       }
       else if (sum == 4 )
       {
           num++;
           sum = 0 ;
       }
       else if (sum <= 4 && n ==0)
       {
           num++;
       }
       ++s;
     }
     printf("%d", num) ;

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
  int n , i=0 , sum = 0 , num = 0;

    scanf(" %d",&n);
  int group[n] ;
  int*s = group ;

     while(n--)
     {
         scanf(" %d",s);

       if(*s == 4)
       {
         ++num;
         continue ;
       }
        sum += *s ;
       if (sum == 8)
       {
           num+=2;
           sum = 0 ;
       }
       else if (sum > 8)
       {
           num += 3;
           sum=0 ;
       }
       else if (sum <= 4 )
       {
           num++;
           sum = 0 ;
       }
       ++s;
     }
     printf("%d", num) ;

    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include<math.h>

int main()
{
 int  i=0 , sum = 0 , num = 0;
 int n ;

    scanf(" %d",&n);
  int group[n] ;
  int*s = group ;

     while(n--)
     {
         scanf(" %d",s);

       if(*s == 4)
       {
         ++num;

       }
       if(*s != 4)
       {
            sum += *s ;
            i++;
       }
       if (sum == 8 )
       {
          if(i==3)
          {
              num+=3;

          }
          else
          {
            num +=2;
       }
           sum = 0 ;
           i=0 ;
       }
       else if (sum > 8)
       {
           num += 3;
           sum=0 ;
           i=0;
       }
       else if (sum == 4 )
       {
           num++;
           sum = 0 ;
           i=0;
       }
      if (sum < 4 && n==0 && sum!=0)
       {
           num++;
       }
       else if (sum < 8 && n==0 && sum!=0)
       {
           num+=2 ;
           i=0;
       }

       ++s;
     }

     printf("%d", num) ;

    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include<math.h>

int main()
{
 int  i=0 , sum = 0 , num = 0;
 int n , j = 0 ;

    scanf(" %d",&n);
  int group[n] ;
  int*s = group ;

     while(n--)
     {
         scanf(" %d",s);

       if(*s == 4)
       {
         ++num;
         j++ ;
       }
    if(*s != 4)
       {
            sum += *s ;
            i++;
       }
    if (sum == 8 )
       {
        if(i%2 == 0)
        {
            num = (sum/i)+j ;
        }
        else
        {
         num = ((sum/i)+1)+j;
       }

       sum = 0 ;
       i = 0 ;
       }
    else if (sum > 8)
       {
           num += 3;
           sum=0 ;
           i=0;
       }
    else if (sum == 4 )
       {
           num++;
           sum = 0 ;
           i=0;
       }
    else if (sum < 4 && n==0 && sum!=0)
       {
           num++;
       }
    else if (sum < 8 && n==0 && sum!=0)
       {
           num+=2 ;
           i=0;
       }

       ++s;
     }

     printf("%d", num) ;

    return 0;
}


#include<stdio.h>
#include<stdlib.h>

int cmpfunc(const void*a, const void*b)
{
    return (*(int*)b- *(int*)a);
}

int main()
{
    int n, i, j, f=0, count=0;
    scanf("%d", &n);
    int a[n];
    for(i=0;i<n;i++)
    {
        scanf("%d", &a[i]);
    }
    qsort(a,n,sizeof(int),cmpfunc);
    for(i=0;i<n;i++)
    {
        if(a[i]==0)
            break;
        if(a[i]==4)
            count++;
        else if(a[i]<4)
        {
            j=n-1;
            while(i<j)
            {
                f=0;
                while(a[j]==0 && i<j)
                {
                    j--;
                }
                int sum=0;
                sum+=a[i]+a[j];
                if(sum<4)
                {
                    a[j]=0;
                    while(sum<=4)
                    {
                        j--;
                        sum+=a[j];
                        a[j]=0;
                        if(sum==4)
                            break;
                    }
                    f=1;
                    count++;
                    break;

                }
                else if(sum>4)
                    break;
                else if(sum==4)
                {
                    f=1;
                    count++;
                    a[j]=0;
                    break;
                }

                j--;
            }
            if(f==0)
                count++;

        }
    }
    printf("%d\n", count);
}


#include<stdio.h>
#include<stdlib.h>

int cmpfunc(const void * a, const void * b)
{
    return (*(int*)b - *(int*)a);
}

int main()
{
    int n, i, j, pos, count=0, k, f=0, sum=0, found=0, c=0;
    scanf("%d", &n);
    int s[n];
    for(i=0;i<n;i++)
    {
        scanf("%d", &s[i]);
    }
    qsort(s,n,sizeof(int),cmpfunc);

    for(i=0;i<n;i++)
    {
        sum=s[i];
        f=0;
        c=0;
        if(s[i]==4)
            count++;
        else
        {
            for(j=i+1;j<n;j++)
            {
                if(sum+s[j]==4)
                {
                    sum=0;
                    f=1;
                    count++;
                    pos=j;
                    while(c+1>0)
                    {
                    for(k=pos;k<n;k++)
                     {
                         s[k]=s[k+1];
                     }
                     n=n-1;
                     pos--;
                     c--;
                     printf("%d %d\n", s[i], c);
                    }
                     break;
                }
                else if(s[i]+s[j]<4)
                {
                    found=2;
                    c++;
                    sum+=s[j];
                }
           }
           if(f==0 && found==2){
            count++;
            break;
           }
           else if(f==1 && found==2)
            break;

           else if(f==0)
            count++;
        }
        if(f==0 && found==2 && i==n-1){
            break;
           }

    }
   printf("%d\n", count);

    return 0;
}


#include<stdio.h>
#include<stdlib.h>

int cmpfunc(const void*a, const void*b)
{
    return (*(int*)b- *(int*)a);
}

int main()
{
    int n, i, j, f=0, count=0;
    scanf("%d", &n);
    int a[n];
    for(i=0;i<n;i++)
    {
        scanf("%d", &a[i]);
    }
    qsort(a,n,sizeof(int),cmpfunc);
    for(i=0;i<n;i++)
    {
        if(a[i]==0)
            break;
        if(a[i]==4)
            count++;
        else if(a[i]<4)
        {
            j=n-1;
            while(i<j)
            {
                f=0;
                while(a[j]==0 && i<j)
                {
                    j--;
                }
                int sum=0;
                sum+=a[i]+a[j];
                if(sum<4)
                {
                    a[j]=0;
                    while(sum<=4)
                    {
                        j--;
                        sum+=a[j];
                        if(sum<=4){
                                a[j]=0;
                        }
                        if(sum==4)
                            break;
                    }
                    f=1;
                    count++;
                    break;

                }
                else if(sum>4)
                    break;
                else if(sum==4)
                {
                    f=1;
                    count++;
                    a[j]=0;
                    break;
                }

                j--;
            }
            if(f==0)
                count++;

        }
    }
    printf("%d\n", count);
}


#include<stdio.h>
#include<stdlib.h>

int cmpfunc(const void * a, const void * b)
{
    return (*(int*)b - *(int*)a);
}

int main()
{
    int n, i, j, pos, count=0, k, f=0, sum=0, found=0, c=0;
    scanf("%d", &n);
    int s[n];
    for(i=0;i<n;i++)
    {
        scanf("%d", &s[i]);
    }
    qsort(s,n,sizeof(int),cmpfunc);

    for(i=0;i<n;i++)
    {
        sum=s[i];
        f=0;
        c=0;
        if(s[i]==4)
            count++;
        else
        {
            for(j=i+1;j<n;j++)
            {
                if(sum+s[j]==4)
                {
                    sum=0;
                    f=1;
                    count++;
                    pos=j;
                    while(c+1>0)
                    {
                    for(k=pos;k<n;k++)
                     {
                         s[k]=s[k+1];
                     }
                     n=n-1;
                     pos--;
                     c--;
                    }
                     break;
                }
                else if(s[i]+s[j]<4)
                {
                    found=2;
                    c++;
                    sum+=s[j];
                }
           }
           if(f==0 && found==2){
            count++;
            break;
           }
           else if(f==1 && found==2)
            break;

           else if(f==0)
            count++;
        }
        if(f==0 && found==2 && i==n-1){
            break;
           }

    }
   printf("%d\n", count);

    return 0;
}


#include<stdio.h>
#include<stdlib.h>

int cmpfunc(const void * a, const void * b)
{
    return (*(int*)b - *(int*)a);
}

int main()
{
    int n, i, j, pos, count=0, k, f=0, sum=0, found=0, c=0;
    scanf("%d", &n);
    int s[n];
    for(i=0;i<n;i++)
    {
        scanf("%d", &s[i]);
    }
    qsort(s,n,sizeof(int),cmpfunc);

    for(i=0;i<n;i++)
    {
        sum=s[i];
        f=0;
        c=0;
        if(s[i]==4)
            count++;
        else
        {
            for(j=i+1;j<n;j++)
            {
                if(sum+s[j]==4)
                {
                    sum=0;
                    f=1;
                    count++;
                    if(found==2)
                    {
                        pos=j-c;
                    }
                    else
                        pos=j;
                    for(k=pos;k<=j;k++)
                     {
                         s[k]=s[k+1];
                         n=n-1;
                     }

                     break;
                }
                else if(s[i]+s[j]<4)
                {
                    found=2;
                    c++;
                    sum+=s[j];
                }
           }
           if(f==0 && found==2 && i==n-1){
            break;
           }

           else if(f==0)
            count++;
        }
        if(f==0 && found==2 && i==n-1){
            break;
           }

    }
   printf("%d\n", count);

    return 0;
}


#include<stdio.h>
#include<stdlib.h>

int cmpfunc(const void * a, const void * b)
{
    return (*(int*)b - *(int*)a);
}

int main()
{
    int n, i, j, pos, count=0, k, f=0;
    scanf("%d", &n);
    int s[n];
    for(i=0;i<n;i++)
    {
        scanf("%d", &s[i]);
    }
    qsort(s,n,sizeof(int),cmpfunc);

    for(i=0;i<n;i++)
    {
        f=0;
        if(s[i]==4)
            count++;
        else
        {
            for(j=n-1;j>=i;j--)
            {
                if(s[i]+s[j]==4)
                {
                    f=1;
                    count++;
                    pos=j;
                    k=n-1;
                    while(pos<=k)
                     {
                        s[k]=s[k+1];
                        k--;
                     }
                     n--;
                     break;
                }
           }
           if(f==0)
            count++;
        }
    }
   printf("%d\n", count);

    return 0;
}


#include<stdio.h>
#include<stdlib.h>

int cmpfunc(const void * a, const void * b)
{
    return (*(int*)b - *(int*)a);
}

int main()
{
    int n, i, j, pos, count=0, k, f=0, sum=0;
    scanf("%d", &n);
    int s[n];
    for(i=0;i<n;i++)
    {
        scanf("%d", &s[i]);
    }
    qsort(s,n,sizeof(int),cmpfunc);

    for(i=0;i<n;i++)
    {
        sum=s[i];
        f=0;
        if(s[i]==4)
            count++;
        else
        {
            for(j=i+1;j<n;j++)
            {
                if(sum+s[j]==4)
                {
                    sum=0;
                    f=1;
                    count++;
                    pos=j;
                    for(k=pos;k<n;k++)
                     {
                        s[k]=s[k+1];
                        n--;

                     }
                     break;
                }
                else if(s[i]+s[j]<4)
                {
                    f=2;
                    sum+=s[j];
                }

           }
           if(f==0 && f==2 && i==n-1){
            count++;
            break;
           }
           else if(f==0)
            count++;
        }
    }
   printf("%d\n", count);

    return 0;
}


#include<stdio.h>
#include<stdlib.h>

int cmpfunc(const void*a, const void*b)
{
    return (*(int*)b- *(int*)a);
}

int main()
{
    int n, i, j, f=0, count=0;
    scanf("%d", &n);
    int a[n];
    for(i=0;i<n;i++)
    {
        scanf("%d", &a[i]);
    }
    qsort(a,n,sizeof(int),cmpfunc);
    for(i=0;i<n;i++)
    {
        if(a[i]==0)
            break;
        if(a[i]==4)
            count++;
        else if(a[i]<4)
        {
            j=n-1;
            while(i<j)
            {
                f=0;
                while(a[j]==0)
                {
                    j--;
                }
                int sum=0;
                sum+=a[i]+a[j];
                if(sum<4)
                {
                    a[j]=0;
                    while(sum<=4)
                    {
                        j--;
                        sum+=a[j];
                        a[j]=0;
                    }
                    f=1;
                    count++;

                }
                else if(sum>4)
                    break;
                else if(sum==4)
                {
                    f=1;
                    count++;
                    a[j]=0;
                    break;
                }

                j--;
            }
            if(f==0)
                count++;

        }
    }
    printf("%d\n", count);
}


#include<stdio.h>
#include<stdlib.h>

int cmpfunc(const void * a, const void * b)
{
    return (*(int*)b - *(int*)a);
}

int main()
{
    int n, i, j, pos, count=0, k, f=0, sum=0;
    scanf("%d", &n);
    int s[n];
    for(i=0;i<n;i++)
    {
        scanf("%d", &s[i]);
    }
    qsort(s,n,sizeof(int),cmpfunc);

    for(i=0;i<n;i++)
    {
        sum=s[i];
        f=0;
        if(s[i]==4)
            count++;
        else
        {
            for(j=n-1;j>=i;j--)
            {
                if(sum+s[j]==4)
                {
                    sum=0;
                    f=1;
                    count++;
                    pos=j;
                    k=n-1;
                    while(pos<=k)
                     {
                        s[k]=s[k+1];
                        k--;
                        n--;
                     }

                     break;
                }
                if(s[i]+s[j]<4)
                {
                    sum+=s[j];
                }
           }
           if(f==0)
            count++;
        }
    }
   printf("%d\n", count);

    return 0;
}


#include<stdio.h>
#include<stdlib.h>

int cmpfunc(const void * a, const void * b)
{
    return (*(int*)b - *(int*)a);
}

int main()
{
    int n, i, j, pos, count=0, k;
    scanf("%d", &n);
    int s[n];
    for(i=0;i<n;i++)
    {
        scanf("%d", &s[i]);
    }
    qsort(s,n,sizeof(int),cmpfunc);

    for(i=0;i<n;i++)
    {
        if(s[i]==4)
            count++;
        else
        {
            for(j=i+1;j<n;j++)
            {
                if(s[i]+s[j]==4)
                {
                    count++;
                    pos=j;
                    k=n-1;
                    while(pos<=k)
                     {
                        s[k-1]=s[k];
                        k--;
                     }
                     n--;
                }
            }
        }
    }
    printf("%d\n", count+1);

    return 0;
}


#include<stdio.h>
#include<stdlib.h>

int cmpfunc(const void*a, const void*b)
{
    return (*(int*)b- *(int*)a);
}

int main()
{
    int n, i, j, f=0, count=0;
    scanf("%d", &n);
    int a[n];
    for(i=0;i<n;i++)
    {
        scanf("%d", &a[i]);
    }
    qsort(a,n,sizeof(int),cmpfunc);
    for(i=0;i<n;i++)
    {
        if(a[i]==0)
            break;
        if(a[i]==4)
            count++;
        else if(a[i]<4)
        {
            j=n-1;
            while(i<j)
            {
                f=0;
                while(a[j]==0)
                {
                    j--;
                }
                if(a[i]+a[j]>4)
                    break;
                else if(a[i]+a[j]==4)
                {
                    f=1;
                    count++;
                    a[j]=0;
                    break;
                }
                else if(a[i]+a[j]<4)
                {
                    a[j]=0;

                }
                j--;
            }
            if(f==0)
                count++;

        }
    }
    printf("%d\n", count);
}


#include<stdio.h>
#include<stdlib.h>

int cmpfunc(const void * a, const void * b)
{
    return (*(int*)b - *(int*)a);
}

int main()
{
    int n, i, j, pos, count=0, k, f=0, sum=0;
    scanf("%d", &n);
    int s[n];
    for(i=0;i<n;i++)
    {
        scanf("%d", &s[i]);
    }
    qsort(s,n,sizeof(int),cmpfunc);

    for(i=0;i<n;i++)
    {
        sum=s[i];
        f=0;
        if(s[i]==4)
            count++;
        else
        {
            for(j=n-1;j>i;j--)
            {
                if(sum+s[j]==4)
                {
                    sum=0;
                    f=1;
                    count++;
                    pos=j;
                    k=n-1;
                    while(pos<=k)
                     {
                        s[k]=s[k+1];
                        k--;
                        n--;
                     }

                     break;
                }
                if(s[i]+s[j]<4)
                {
                    f=2;
                    sum+=s[j];
                }

           }
           if(f==0 && f==2 && i==n-1){
            count++;
            break;
           }
           else if(f==0)
            count++;
        }
    }
   printf("%d\n", count);

    return 0;
}


#include<stdio.h>

int main()
{
    int i,j,group,n=0;
    scanf("%d",&group);
    int member[group];
    for(i=0;i<group;i++){
        scanf("%d",&member[i]);

    }
    int taxi=0;

    for(i=0;i<group;i++){
        if(member[i]==4){
            member[i]=0;
            taxi++;

        }
        if(member[i]==3){
            for(j=0;j<group;j++){
                if(member[j]==1){
                    taxi++;
                    member[i]=0;
                    member[j]=0;
                    break;
                }

            }
            if(member[i]==3){
                taxi++;
                member[i]=0;
            }

        }

        }

    for(i=0;i<group;i++){
            if(member[i]==2){
                for(j=0;j<group;j++){
                    if(i!=j && member[j]==2){
                        taxi++;
                        member[i]=0;
                        member[j]=0;
                        break;
                    }

                }

            }

    }

    for(i=0;i<group;i++){
        n=n+member[i];
        if(n>3){
            taxi++;
            n=0;
        }
    }

    if(n>0 && n<4){
        taxi++;
    }

    printf("%d\n",taxi);
    return 0;

}


#include<stdio.h>

int main()
{
    int taxi=0,i,group,oneh=0,oneh2=0,twoh=0,threeh=0;
    scanf("%d",&group);
    int member[group];

    for(i=0;i<group;i++){
        scanf("%d",&member[i]);
        if(member[i]==4) taxi++;
        if(member[i]==3) threeh++;
        if(member[i]==2) twoh++;
        if(member[i]==1) oneh++;
    }

    if(threeh==oneh && oneh!=0){
        taxi+=threeh;
        threeh=0;  oneh=0;
    }

    if(threeh>oneh && oneh!=0){
        taxi+=oneh;
        threeh-=oneh;  oneh=0;
    }

    if(threeh<oneh && oneh!=0){
        taxi+=threeh;
        threeh=0;  oneh-=threeh;
    }

    taxi+=threeh;

    int a=0;
    a=twoh%2;
    taxi+=(twoh/2);
    twoh=a;

    if(oneh<3 && oneh!=0){
        taxi++;
        twoh=0;
        oneh=0;

    }

    if(oneh<5 && oneh!=0){
        taxi++;
    }
    else{
        while(oneh>0){
            taxi++;
            oneh-=4;
        }
    }

    printf("%d\n",taxi);

    return 0;
}


#include<stdio.h>

int main()
{
    int taxi=0,i,group,oneh=0,oneh2=0,twoh=0,threeh=0;
    scanf("%d",&group);
    int member[group];

    for(i=0;i<group;i++){
        scanf("%d",&member[i]);
        if(member[i]==4) taxi++;
        if(member[i]==3) threeh++;
        if(member[i]==2) twoh++;
        if(member[i]==1) oneh++;
    }

    if(threeh==oneh && oneh!=0){
        taxi+=threeh;
        threeh=0;  oneh=0;
    }

    if(threeh>oneh && oneh!=0){
        taxi+=oneh;
        threeh-=oneh;  oneh=0;
    }

    if(threeh<oneh && oneh!=0){
        taxi+=threeh;
        threeh=0;  oneh-=threeh;
    }

    taxi+=threeh;

    oneh2=oneh%2;
    oneh=oneh-oneh2;

    if(twoh==(oneh*2)  && oneh!=0){
        taxi+=twoh;
        twoh=0;  oneh=0;
    }

    if(twoh>(oneh*2)  && oneh!=0){
        taxi+=oneh;
        twoh-=oneh;  oneh=0;
    }

    if(twoh<(oneh*2) && oneh!=0){
        taxi+=twoh;
        twoh=0;  oneh=oneh-(twoh*2)+oneh2; oneh2=0;
    }

    if(oneh2==1){
        taxi++;  twoh=0; oneh=0;
    }

    int a=0;
    a=twoh%2;
    taxi+=(twoh/2);
    taxi+=a;

    if(oneh<5 && oneh!=0){
        taxi++;
    }
    else{
        while(oneh>0){
            taxi++;
            oneh-=4;
        }
    }

    printf("%d\n",taxi);

    return 0;
}


#include<stdio.h>

int main()
{
    int taxi=0,i,group,oneh=0,oneh2=0,twoh=0,threeh=0;
    scanf("%d",&group);
    int member[group];

    for(i=0;i<group;i++){
        scanf("%d",&member[i]);
        if(member[i]==4) taxi++;
        if(member[i]==3) threeh++;
        if(member[i]==2) twoh++;
        if(member[i]==1) oneh++;
    }

    if(threeh==oneh && oneh!=0){
        taxi+=threeh;
        threeh=0;  oneh=0;
    }

    if(threeh>oneh && oneh!=0){
        taxi+=oneh;
        threeh-=oneh;  oneh=0;
    }

    if(threeh<oneh && oneh!=0){
        taxi+=threeh;
        oneh-=threeh;   threeh=0;
    }

    taxi+=threeh;

    int a=0;
    a=twoh%2;
    taxi+=(twoh/2);
    twoh=a;

    if(twoh==1 && oneh<3 && oneh!=0){
        taxi++;
        twoh=0;
        oneh=0;

    }
    taxi+=twoh;

    if(oneh<5 && oneh!=0){
        taxi++;
        oneh=0;
    }
    else{
        while(oneh>0){
            taxi++;
            oneh-=4;
        }
    }

    printf("%d\n",taxi);

    return 0;
}


#include<stdio.h>

int main()
{
    int groups,i,taxi=0;
    scanf("%d",&groups);

    int members[groups];

    for(i=0;i<groups;i++){
        scanf("%d",&members[i]);
    }

    int vagfol,vagsesh,vagsesh1=0;
    for(i=0;i<groups;i++){
        vagfol=0,vagsesh=0;
        vagfol=members[i]/4;
        taxi+=vagfol;
        vagsesh=members[i]%4;
        vagsesh1+=vagsesh;
        if(vagsesh1==4){
            taxi++;
            vagsesh1=0;
        }
        if(vagsesh1>4){
            vagsesh1-=4;
            taxi++;
        }

    }

    if(vagsesh1>0 && vagsesh1<4){
        taxi++;
    }

    printf("%d\n",taxi);

    return 0;
}


#include<stdio.h>

int main()
{
    int groups,i,taxi=0;
    scanf("%d",&groups);

    int members[groups];

    for(i=0;i<groups;i++){
        scanf("%d",&members[i]);
    }

    int count=0;
    for(i=0;i<groups;i++){
            if(members[i]==4){
                taxi++;
                members[i]=0;
            }

            else{
                count+=members[i];
                if(count==4){
                    taxi++;
                    count=0;
                }
                if(count<4){
                    members[i]=0;
                }
            }

            if(members[i]==3){
                taxi++;
                members[i]=0;
            }

            else{
                count+=members[i];
                if(count==3){
                    taxi++;
                    count=0;
                }
                if(count<4){
                    members[i]=0;
                }
            }

            if(members[i]==2){
                taxi++;
                members[i]=0;
            }

            else{
                count+=members[i];
                if(count==2){
                    taxi++;
                    count=0;
                }
                if(count<4){
                    members[i]=0;
                }
            }

            if(members[i]==1){
                taxi++;
                members[i]=0;
            }

            else{
                count+=members[i];
                if(count==1){
                    taxi++;
                    count=0;
                }
                if(count<4){
                    members[i]=0;
                }
            }

        }

    printf("%d\n",taxi);

    return 0;
}


#include<stdio.h>

int main()
{
    int taxi=0,group,i,n=0,j;
    scanf("%d",&group);
    int member[group];

    for(i=0;i<group;i++){
        scanf("%d",&member[i]);
    }

    for(i=0;i<group;i++){

        switch(member[i]){

            case 4 :  taxi++;
                        member[i]=0;
                        break;

            case 3 :  for(j=0;j<group;j++){
                            if(member[j]==1){
                                taxi++;
                                member[i]=0;
                                member[j]=0;
                                break;
                            }

                            }
                            if(member[i]==3){
                                taxi++;
                                member[i]=0;
                            }
                            break;

            case 2 :  for(j=0;j<group;j++){
                           if(i!=j && member[j]==2){
                                 taxi++;
                                 member[i]=0;
                                 member[j]=0;
                                 break;
                           }

                           }
                           break;

            default  :  break;

        }

    }

                            for(j=0;j<group;j++){
                            n=n+member[j];
                            member[j]=0;
                            if(n>3){
                            taxi++;
                            n=0;

                            }
                            }

    if(n>0 && n<4){
        taxi++;
    }

    printf("%d\n",taxi);

    return 0;
}


#include<stdio.h>

int main()
{
    int taxi=0,i,group,oneh=0,oneh2=0,twoh=0,threeh=0;
    scanf("%d",&group);
    int member[group];

    for(i=0;i<group;i++){
        scanf("%d",&member[i]);
        if(member[i]==4) taxi++;
        if(member[i]==3) threeh++;
        if(member[i]==2) twoh++;
        if(member[i]==1) oneh++;
    }

    if(threeh==oneh && oneh!=0){
        taxi+=threeh;
        threeh=0;  oneh=0;
    }

    if(threeh>oneh && oneh!=0){
        taxi+=oneh;
        threeh-=oneh;  oneh=0;
    }

    if(threeh<oneh && oneh!=0){
        taxi+=threeh;
        oneh-=threeh;   threeh=0;
    }

    taxi+=threeh;

    int a=0;
    a=twoh%2;
    taxi+=(twoh/2);
    twoh=a;

    if(twoh==1 && oneh!=0){
        taxi++;
        twoh=0;
        oneh=oneh-2;

    }
    taxi+=twoh;

    if(oneh<5 && oneh>0){
        taxi++;
        oneh=0;
    }
    else{
        while(oneh>0){
            taxi++;
            oneh-=4;
        }
    }

    printf("%d\n",taxi);

    return 0;
}


#include<stdio.h>

int main()
{
    int taxi=0,i,group,oneh=0,oneh2=0,twoh=0,threeh=0;
    scanf("%d",&group);
    int member[group];

    for(i=0;i<group;i++){
        scanf("%d",&member[i]);
        if(member[i]==4) taxi++;
        if(member[i]==3) threeh++;
        if(member[i]==2) twoh++;
        if(member[i]==1) oneh++;
    }

    if(threeh==oneh && oneh!=0){
        taxi+=threeh;
        threeh=0;  oneh=0;
    }

    if(threeh>oneh && oneh!=0){
        taxi+=oneh;
        threeh-=oneh;  oneh=0;
    }

    if(threeh<oneh && oneh!=0){
        taxi+=threeh;
        threeh=0;  oneh-=threeh;
    }

    taxi+=threeh;

    int a=0;
    a=twoh%2;
    taxi+=(twoh/2);
    twoh=a;

    if(oneh<3 && oneh!=0){
        taxi++;
        twoh=0;
        oneh=0;

    }
    taxi+=twoh;

    if(oneh<5 && oneh!=0){
        taxi++;
    }
    else{
        while(oneh>0){
            taxi++;
            oneh-=4;
        }
    }

    printf("%d\n",taxi);

    return 0;
}


#include<stdio.h>

int main()
{
    int i,j,group,n=0;
    scanf("%d",&group);
    int member[group];
    for(i=0;i<group;i++){
        scanf("%d",&member[i]);

    }
    int taxi=0;

    for(i=0;i<group;i++){
        if(member[i]==4){
            member[i]=0;
            taxi++;

        }
        if(member[i]==3){
            for(j=0;j<group;j++){
                if(member[j]==1){
                    taxi++;
                    member[i]=0;
                    member[j]=0;
                    break;
                }

            }
            if(member[i]==3){
                taxi++;
                member[i]=0;
            }

        }

        }

    for(i=0;i<group;i++){
            if(member[i]==2){
                for(j=0;j<group;j++){
                    if(i!=j && member[j]==2){
                        taxi++;
                        member[i]=0;
                        member[j]=0;
                    }

                }

            }

    }

    for(i=0;i<group;i++){
        n=n+member[i];
        if(n>3){
            taxi++;
            n=0;
        }
    }

    if(n>0 && n<4){
        taxi++;
    }

    printf("%d\n",taxi);
    return 0;

}


#include<stdio.h>

int main()
{
    int taxi=0,group,i,n=0,j;
    scanf("%d",&group);
    int member[group];

    for(i=0;i<group;i++){
        scanf("%d",&member[i]);
    }

    for(i=0;i<group;i++){

        switch(member[i]){

            case 4 :  taxi++;
                        member[i]=0;
                        break;

            case 3 :  for(j=0;j<group;j++){
                            if(member[j]==1){
                                taxi++;
                                member[i]=0;
                                member[j]=0;
                                break;
                            }

                            }
                            if(member[i]==3){
                                taxi++;
                                member[i]=0;
                            }
                            break;

            case 2 :  for(j=0;j<group;j++){
                           if(i!=j && member[j]==2){
                                 taxi++;
                                 member[i]=0;
                                 member[j]=0;
                                 break;
                           }

                           }
                           break;

            case 1 :  for(j=0;j<group;j++){
                            n=n+member[j];
                            member[j]=0;
                            if(n>3){
                            taxi++;
                            n=0;

                            }
                            }
                            break;

            default  :  break;

        }

    }

    if(n>0 && n<4){
        taxi++;
    }

    printf("%d\n",taxi);

    return 0;
}


#include<stdio.h>

int main()
{
    int taxi=0,i,group,oneh=0,twoh=0,threeh=0;
    scanf("%d",&group);
    int member[group];

    for(i=0;i<group;i++){
        scanf("%d",&member[i]);
        if(member[i]==4) taxi++;
        if(member[i]==3) threeh++;
        if(member[i]==2) twoh++;
        if(member[i]==1) oneh++;
    }

    if(threeh==oneh && oneh!=0){
        taxi+=threeh;
        threeh=0;  oneh=0;
    }

    if(threeh>oneh && oneh!=0){
        taxi+=oneh;
        threeh-=oneh;  oneh=0;
    }

    if(threeh<oneh && oneh!=0){
        taxi+=threeh;
        threeh=0;  oneh-=threeh;
    }

    taxi+=threeh;

    if(twoh==(oneh*2)  && oneh!=0){
        taxi+=twoh;
        twoh=0;  oneh=0;
    }

    if(twoh>(oneh*2)  && oneh!=0){
        taxi+=oneh;
        twoh-=oneh;  oneh=0;
    }

    if(twoh<(oneh*2) && oneh!=0){
        taxi+=twoh;
        twoh=0;  oneh-=(twoh*2);
    }

    int a=0;
    a=twoh%2;
    taxi+=(twoh/2);
    taxi+=a;

    if(oneh<5 && oneh!=0){
        taxi++;
    }
    else{
        while(oneh>0){
            taxi++;
            oneh-=4;
        }
    }

    printf("%d\n",taxi);

    return 0;
}